1.Feign
为服务利用中,ribbon和hystrix同时呈现,feign整合两者,并提供了申明式消费者客户端.
feign是一种集成工具性能有:近程调用,ribbon-负载平衡和重试,hystrix-降级 熔断
1.1Feign近程调用
Fegin提供了申明式客户端,只须要定义一个接口,就能够通过接口做近程调用.具体调用代码通过动静代理增加.
// 调用商品服务的近程调用接口
// 通过注解配置3件事:调用哪个服务,调用什么门路,向这个门路提交什么参数
@FeignClient(name="item-service")
public interface ItemFeignClient {
@GetMapping("/{orderId}")
JsonResult<List<Item>> getItems(@PathVariable String orderId);
}
1.1.1增加申明式客户端
1.OpenFegin依赖
2.启动类增加@EnableFeignClient
3.定义申明式客户端接口 例如:ItemFeignClient UserFeignClient OrderFeignClient
4.增加一个测试用的控制器,应用三个申明式客户端接口调用近程服务.
1.2Feign集成Ribbon
负载平衡 重试
Feign默认启用了Ribbon的负载平衡和重试 0配置.
默认重试参数:MaxAutoRetries=0 MaxAutoRetriesNextServer=1 ReadTimeout=1000
重试参数配置
# 对所有服务都无效
ribbon:
MaxAutoRetries: 1
# 对 item-service 独自配置,对其余服务有效
item-service:
ribbon:
MaxAutoRetries: 0
1.3Feign集成Hystrix
默认不启用hystrix,Feign不举荐启用hystrix
1.3.1如果有非凡需要要启用hystrix,首先做根底配置
1.增加hystrix残缺依赖
2.增加@EnableCircuitBreaker
3.yml配置feign.hystrix.enable=true
1.3.2降级
在申明式客户端接口的注解中,制订一个降级类(之前的是降级办法)
@FeignClient(name="item-service", fallback=降级类.class)
public interface ItemFeignClient {
....
}
降级类要作为申明式客户端接口的子类来定义.
1.3.3hystrix监控
用actuator裸露hystrix.stream监控端点
1.actuator依赖
2.裸露监控端点
m.e.w.e.i=hystrix.stream
3.http://localhost:3001/actuator/hystrix.stream
4.通过调用后盾服务,产生监控数据.
1.4熔断
10秒内20次申请,50%失败执行了降级代码.
发表回复