Spring Cloud Feign(第六篇) 之自定义配置

本文详细探讨了Spring Cloud Feign的自定义配置,包括application.properties(.yml)配置文件和Java Config的优先级。讲解了三种配置情况:无配置、default-to-properties=true和default-to-properties=false时的配置覆盖规则。还介绍了全局和局部配置,以及如何注册自定义类型转换FeignFormatterRegistrar和方法参数注解AnnotatedParameterProcessor。同时解决了@RequestMapping被SpringMVC加载的问题,并给出了自定义配置的总结和样例代码链接。
摘要由CSDN通过智能技术生成

这里写图片描述

Spring Cloud Feign 之自定义配置

环境信息: java 1.8、Spring boot 1.5.10.RELEASE、spring cloud-Edgware.SR3、maven 3.3+

使用Feign默认配置可能不能满足需求,这时就需要我们实现自己的Feign配置,如下几种配置

application.properties(.yml)全局和局部(针对单个Feign接口),包含以下配置

spring java config全局配置和局部(针对单个Feign接口)

application.properties(.yml)配置文件和java config的优先级

下面代码就是处理配置使之生效,FeignClientFactoryBean#configureFeign :

protected void configureFeign(FeignContext context, Feign.Builder builder) {
  //配置文件,以feign.client开头
   FeignClientProperties properties = applicationContext.getBean(FeignClientProperties.class);
   if (properties != null) {
      if (properties.isDefaultToProperties()) {
          //使用java config 配置
         configureUsingConfiguration(context, builder);
//
          configureUsingProperties(properties.getConfig().get(properties.getDefaultConfig()), builder);
         configureUsingProperties(properties.getConfig().get(this.name), builder);
      } else {
         configureUsingProperties(properties.getConfig().get(properties.getDefaultConfig()), builder);
         configureUsingProperties(properties.getConfig().get(this.name), builder);
         configureUsingConfiguration(context, builder);
      }
   } else {
      configureUsingConfiguration(context, builder);
   }
}

所有配置都是单个属性覆盖,如果对 Spring boot配置优先级有所了解

第一种:配置文件无配置

使用 java config 配置,优先级有低到高进行单个配置覆盖

1、FeignClientsConfiguration Spring Cloud Feign 全局默认配置。

2、@EnableFeignClients#defaultConfiguration 自定义全局默认配置。

3、FeignClient#configuration 单个Feign接口局部配置。

第二种:feign.client.default-to-properties=true(默认true)

java configapplication.properties(.yml)配置,优先级有低到高进行单个配置覆盖

1、FeignClientsConfiguration Spring Cloud Feign 全局默认配置。

2、@EnableFeignClients#defaultConfiguration 自定义全局默认配置。

3、FeignClient#configuration 单个Feign接口局部配置。

4、application.properties(.yml)配置文件全局默认配置,配置属性feign.client.default-config指定默认值(defult),如何使用,在application.properties(.yml)配置文件应用小节讲解

5、application.properties(.yml)配置文件局部配置,指定@FeignClient#name局部配置。

第三种:feign.client.default-to-properties=false(默认true)

java configapplication.properties(.yml)配置,优先级有低到高进行单个配置覆盖

1、application.properties(.yml)配置文件全局默认配置,配置属性feign.client.default-config指定默认值(defult),如何使用,在application.properties(.yml)配置文件应用小节讲解

2、application.properties(.yml)配置文件局部配置,指定@FeignClient#name局部配置。

3、FeignClientsConfiguration Spring Cloud Feign 全局默认配置。

4、@EnableFeignClients#defaultConfiguration 自定义全局默认配置。

5、FeignClient#configuration 单个Feign接口局部配置。

application.properties(.yml)配置文件应用

支持以下配置项:

private Logger.Level loggerLevel;//日志级别

private Integer connectTimeout;//连接超时时间 java.net.HttpURLConnection#getConnectTimeout(),如果使用Hystrix,该配置无效

private Integer readTimeout;//读取超时时间  java.net.HttpURLConnection#getReadTimeout(),如果使用Hystrix,该配置无效

private Class<Retryer> retryer;//重试接口实现类,默认实现 feign.Retryer.Default

private Class<ErrorDecoder> errorDecoder;//错误编码
@FeignClient 是 Spring Cloud 中用于声明式的 HTTP 客户端的注解。它可以方便地定义和使用 RESTful 服务的接口。 要配置 @FeignClient 的重试,可以通过以下步骤进行: 1. 首先,在项目的配置文件中,添加以下配置项: ``` feign: client: config: default: retryer: com.example.CustomRetryer ``` 这里的 `com.example.CustomRetryer` 是自定义的重试器类,用于控制重试的逻辑。 2. 然后,创建一个实现了 `Retryer` 接口的自定义重试器类。该接口定义了重试的方法和逻辑。例如: ```java import feign.RetryableException; import feign.Retryer; public class CustomRetryer implements Retryer { private final int maxAttempts; private final long backoff; private int attempt; public CustomRetryer() { this(3, 1000); } public CustomRetryer(int maxAttempts, long backoff) { this.maxAttempts = maxAttempts; this.backoff = backoff; this.attempt = 1; } @Override public void continueOrPropagate(RetryableException e) { if (attempt++ >= maxAttempts) { throw e; } try { Thread.sleep(backoff); } catch (InterruptedException ignored) { Thread.currentThread().interrupt(); } } @Override public Retryer clone() { return new CustomRetryer(maxAttempts, backoff); } } ``` 在上面的例子中,我们定义了最大重试次数为 3 次,每次重试间隔为 1000 毫秒。 3. 最后,在使用 @FeignClient 注解的接口上,添加 `@Retryable` 注解,并指定自定义的重试器类。例如: ```java @FeignClient(name = "example-service", configuration = ExampleServiceClientConfig.class) @Retryable(retryer = CustomRetryer.class) public interface ExampleServiceClient { // 接口方法定义 } ``` 这样,当调用 @FeignClient 注解的接口方法时,如果发生异常,将会按照自定义的重试逻辑进行重试。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值