<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
feign.hystrix.enabled=true
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 熔断...";}
};
}
}