spring cloud hystrix 簡易配置

通常和feign一塊處理

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

啓用hystrix

# 啓用hystrix
feign.hystrix.enabled=true

Service a 調用 service b

package com.itheima.hystrix.servicea.agent;

import feign.hystrix.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;

@FeignClient(name = "feign-hystrix-service-b",fallbackFactory = ServiceBAgentHystrix.class)
public interface ServiceBAgent {
    @GetMapping("/service-b/service")
    String service();
}

@Component
class ServiceBAgentHystrix implements FallbackFactory<ServiceBAgent>{

    @Override
    public ServiceBAgent create(Throwable cause) {
        return new ServiceBAgent() {
            @Override
            public String service() {
                return "service-b熔斷...";
            }
        };
    }
}
相關文章
相關標籤/搜索