从零搭建springcloud微服务(六)----Hystrix(熔断器)
一、整合feign和Hystrix
1.修改配置文件
cloud-consumer模块的配置文件中增加feign.hystrix.enabled=true
2.在user-open-api模块下增加package包名:com.plkd.usercenter.client.hystrix
新增实现类UserRemoteClientFallback实现UserRemoteClient接口
@Component
public class UserRemoteClientFallback implements UserRemoteClient {
@Override
public UseDto findUserByUserName(String userName) {
System.out.println("系统异常,请稍后再试!");
return null;
}
}
注意这里要加上@Component,否则会以下异常:
Caused by: java.lang.IllegalStateException: No fallback instance of type class com.plkd.usercenter.client.hystrix.UserRemoteClientFallback found for feign client cloud-provider
3.将user-open-api模块下UserRemoteClient类
@FeignClient("cloud-provider")修改成@FeignClient(value = "cloud-provider", fallback = UserRemoteClientFallback.class)
4.修改cloud-consumer模块下CloudConsumerApplication类
增加@ComponentScan(value = "com.plkd")
二.启动cloud-consumer,启动cloud-provider
浏览器访问http://localhost:8080/order/getUserByOrderId/1
关闭cloud-provider服务,浏览器访问http://localhost:8080/order/getUserByOrderId/1
console输出