spring cloud(二)-服务消费(Feign 断路器)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/lh87270202/article/details/84030425

前言

断路器:
在分布式架构中,当某个服务单元发生故障之后,通过断路器的故障监控,向调用方返回一个错误响应,而不是长时间的等待。这样就不会使得线程因调用故障服务被长时间占用不释放,避免了故障在分布式系统中的蔓延。

1、 Feign使用Hystrix

不需要在Feigh工程中引入Hystix,Feign中已经依赖了Hystrix。
使用@FeignClient注解中的fallback属性指定回调类

//定义的FeignClient
@FeignClient(value = "compute-service", fallback = ClientCallBackHystrix.class)
public interface AddClient {
    @RequestMapping(method = RequestMethod.GET, value = "/add")
    Integer add(@RequestBody Test input);
}
//定义AddClient 回调方法,定义一个class类实现AddClient 
@Component
public class ClientCallBackHystrix  implements AddClient {
    @Override
    public Integer add(@RequestBody Test input) {
        return -1;
    }
}

猜你喜欢

转载自blog.csdn.net/lh87270202/article/details/84030425