Feign和Ribbon学习

原文链接:

https://blog.csdn.net/weixin_44296862/article/details/96938992

https://blog.csdn.net/qq_43140314/article/details/104175783

Feign是一个声明式的web service客户端,它使得编写web service客户端更为容易。

创建接口,为接口添加注解,即可使用Feign。Feign可以使用Feign注解或者JAX-RS注解,还支持热插拔的编码器和解码器。

Spring Cloud为Feign添加了Spring MVC的注解支持,并整合了Ribbon和Eureka来为使用Feign时提供负载均衡。

示例:

  • name为服务名称
  • fallbackFactory为服务降级处理方法

Feign配置说明

        Feign是一款Java语言编写的HttpClient绑定器,在Spring Cloud微服务中用于实现微服务之间的声明式调用,Feign自身可以支持多种HttpClient工具包,例如OkHttp及Apache HttpClient,针对不同的HttpClient其默认常见配置如下:

feign:
  hystrix:
    enabled: true
  client:
    config:
      #JDK默认HttpURLConnection 实现的 Http Client配置
      default:
        #连接超时时间
        connectTimeout: 5000
        #读取超时时间
        readTimeout: 5000
        #错误解码器
        errorDecoder: com.wudimanong.client.common.FeignClientErrorDecoder
        #解码器
        encoder: com.wudimanong.client.common.FeignClientEncoder
        #编码器
        decoder: com.wudimanong.client.common.FeignClientDecoder

配置详解:

是否开启hystrix
feign.hystrix.enabled=true
连接超时时间
feign.client.config.default.connectTimeout=5000
读取超时时间
feign.client.config.default.readTimeout=5000

Ribbon配置说明

        Ribbon在Spring Cloud中对于支持微服之间的通信发挥着非常关键的作用,其主要功能包括客户端负载均衡器及用于中间层通信的客户端。

        在基于Feign的微服务通信中无论是否开启Hystrix,Ribbon都是必不可少的,Ribbon的配置参数主要如下:

ribbon:
  #说明:同一台实例的最大自动重试次数,默认为1次,不包括首次
  MaxAutoRetries: 1
  #说明:要重试的下一个实例的最大数量,默认为1,不包括第一次被调用的实例
  MaxAutoRetriesNextServer: 1
  #说明:是否所有的操作都重试,默认为true
  OkToRetryOnAllOperations: true
  #说明:从注册中心刷新服务器列表信息的时间间隔,默认为2000毫秒,即2秒
  ServerListRefreshInterval: 2000
  #说明:使用Apache HttpClient连接超时时间,单位为毫秒
  ConnectTimeout: 3000
  #说明:使用Apache HttpClient读取的超时时间,单位为毫秒
  ReadTimeout: 3000
  #说明:初始服务器列表,不需要手工配置,在运行时动态根据注册中心更新
  listOfServers: www.microsoft.com:80,www.yahoo.com:80,www.google.com:80

或者:

ribbon:
  #说明:同一台实例的最大自动重试次数,默认为1次,不包括首次
  MaxAutoRetries: 1
  #说明:要重试的下一个实例的最大数量,默认为1,不包括第一次被调用的实例
  MaxAutoRetriesNextServer: 1
  #说明:是否所有的操作都重试,默认为true
  OkToRetryOnAllOperations: true
  #说明:从注册中心刷新服务器列表信息的时间间隔,默认为2000毫秒,即2秒
  ServerListRefreshInterval: 2000
  #说明:使用Apache HttpClient连接超时时间,单位为毫秒
  ConnectTimeout: 3000
  #说明:使用Apache HttpClient读取的超时时间,单位为毫秒
  ReadTimeout: 3000
  #说明:初始服务器列表,不需要手工配置,在运行时动态根据注册中心更新
  listOfServers: www.microsoft.com:80,www.yahoo.com:80,www.google.com:80

以上配置方式将对所有的微服务调用有效,如果想针对单独的微服务进行配置,使用“微服务名.ribbon”这样的配置方式即可,例如:

bike:
  ribbon:
    ReadTimeout: 30000
operation:
  ribbon:
    ReadTimeout: 30000


Feign配置说明


Feign是一款Java语言编写的HttpClient绑定器,在Spring Cloud微服务中用于实现微服务之间的声明式调用,Feign自身可以支持多种HttpClient工具包,例如OkHttp及Apache HttpClient,针对不同的HttpClient其默认常见配置如下:

feign:
  hystrix:
    enabled: true
  client:
    config:
      #JDK默认HttpURLConnection 实现的 Http Client配置
      default:
        #连接超时时间
        connectTimeout: 5000
        #读取超时时间
        readTimeout: 5000
        #错误解码器
        errorDecoder: com.wudimanong.client.common.FeignClientErrorDecoder
        #解码器
        encoder: com.wudimanong.client.common.FeignClientEncoder
        #编码器
        decoder: com.wudimanong.client.common.FeignClientDecoder