如果需要访问产生回退触发器的原因,可以使用@feignClient中的fallbackFactory属性:
pom:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
</dependencies>
@FeignClient(value = "report-query",fallbackFactory =ReportQueryServiceHystrix.class )
public interface ReportQueryService {
@RequestMapping(value = "/reportquery/index", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
String index();
}
@Component
public class ReportQueryServiceHystrix implements FallbackFactory<ReportQueryService>{
private static final Logger log= LoggerFactory.getLogger(ReportQueryServiceHystrix.class);
@Override
public ReportQueryService create(Throwable arg0) {
return new ReportQueryService() {
@Override
public String index() {
ReportQueryServiceHystrix.log.info("fallback; reason was: {}", arg0.getMessage());
return arg0.getMessage();
}
};
}
}
yml: 开启熔断
feign:
hystrix:
enabled: true
启动类:
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableFeignClients
@EnableHystrix
public class WebserviceApplication {
public static void main(String[] args) {
SpringApplication.run(WebserviceApplication.class, args);
}
}
访问结果:
com.netflix.client.ClientException: Load balancer does not have available server for client: report-query