关于java:SpringCloud梳理Feign

11次阅读

共计 1207 个字符,预计需要花费 4 分钟才能阅读完成。

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% 失败执行了降级代码.
正文完
 0