controller层调用的service接口,用了@FeignClient注解,FeignClient是服务调用,把客户端与服务器端注入eureka,来调用我们的服务器端的接口,fallback的是继承HystrixService 类为其设计的fallback处理逻辑,避免代码耦合
调用接口异常返回:
逻辑方法返回单独处理
yml(消费者):
server:
port: 80
eureka:
client:
register-with-eureka: false #表示是否将自己注册进Eurekaserver默认为true。
fetch-registry: true #是否M从Eurekaserver抓取已有的注册信息,默认为true。单节点无所谓,集群必须设置为true才能配合ribbon使用负载均衡
service-url:
defaultZone: http://localhost:7001/eureka
feign:
hystrix:
enabled: true
service接口(openfegin):
@FeignClient(value = "CLOUD-PROVEIDER-USER", fallbackFactory = UserServiceFallbackFactory.class)
@Component
public interface UserService {
@GetMapping("/selectId/{id}")
String selectId(@PathVariable("id") Long id);
}
service实现类(openfegin):
@Component
public class UserServiceFallbackFactory implements FallbackFactory<UserService> {
@Override
public UserService create(Throwable throwable) {
return (id) -> "hystrx 断路成功";
}
}
总结:
- @FeignClient(value = “CLOUD-PROVEIDER-USER”, fallbackFactory = UserServiceFallbackFactory.class)
- @Component**
综合总结:
- 控制类HystirxController上DefaultProperties注解默认fallback
- 控制层执行方法SingleHystirxController上HystrixCommand注解单独设置的fallback
- 调用service方法@FeignClient(fallback = FallbackService .class)
fallback优先显示:
运行时异常 < 超时 < 宕机