Feign中如何配置RequestInterceptor

可以通过配置类和配置文件两种方式注册RequestInterceptor。

  • 通过配置类配置时,通过FeignContext获取 RequestInterceptor bean;
  • 通过配置文件注册时,通过 ApplicationContext 获取 RequestInterceptor bean。

1. 通过配置类进行配置

设置配置类有两种方式。

1.1 通过@EnableFeignClients的defaultConfiguration属性配置

@EnableFeignClients 注解的 defaultConfiguration 属性值为 @Configuration 注解的配置类,配置类内定义的 bean 会注册为所有 @FeignClient 的默认配置。

配置信息会保存为 FeignClientSpecification 对象。

1.2 通过@FeignClient的configuration属性配置

@FeignClient 注解的 configuration 属性值为 @Configuration 注解的配置类,其定义的 bean 为每个FeignClient 的专有 bean。

FeignContext 为每一个 @FeignClient 注解的接口提供独立的AnnotationConfigApplicationContext,其中包含 FeignClient 的私有bean 和 EnableFeignClient 的默认 bean。

org.springframework.cloud.netflix.feign.FeignClientFactoryBean#configureFeign

protected void configureFeign(FeignContext context, Feign.Builder builder) {
	FeignClientProperties properties = applicationContext.getBean(FeignClientProperties.class);
	if (properties != null) {
		if (properties.isDefaultToProperties()) {
			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);
	}
}

org.springframework.cloud.netflix.feign.FeignClientFactoryBean#configureUsingConfiguration

protected void configureUsingConfiguration(FeignContext context, Feign.Builder builder) {
	Logger.Level level = getOptional(context, Logger.Level.class);
	if (level != null) {
		builder.logLevel(level);
	}
	Retryer retryer = getOptional(context, Retryer.class);
	if (retryer != null) {
		builder.retryer(retryer);
	}
	ErrorDecoder errorDecoder = getOptional(context, ErrorDecoder.class);
	if (errorDecoder != null) {
		builder.errorDecoder(errorDecoder);
	}
	Request.Options options = getOptional(context, Request.Options.class);
	if (options != null) {
		builder.options(options);
	}
	Map<String, RequestInterceptor> requestInterceptors = context.getInstances(
			this.name, RequestInterceptor.class);
	if (requestInterceptors != null) {
		builder.requestInterceptors(requestInterceptors.values());
	}

	if (decode404) {
		builder.decode404();
	}
}

2. 通过配置文件配置

具体参数可以参考 org.springframework.cloud.netflix.feign.FeignClientProperties 类。

org.springframework.cloud.netflix.feign.FeignClientFactoryBean#configureUsingProperties

protected void configureUsingProperties(FeignClientProperties.FeignClientConfiguration config, Feign.Builder builder) {
	if (config == null) {
		return;
	}

	if (config.getLoggerLevel() != null) {
		builder.logLevel(config.getLoggerLevel());
	}

	if (config.getConnectTimeout() != null && config.getReadTimeout() != null) {
		builder.options(new Request.Options(config.getConnectTimeout(), config.getReadTimeout()));
	}

	if (config.getRetryer() != null) {
		Retryer retryer = getOrInstantiate(config.getRetryer());
		builder.retryer(retryer);
	}

	if (config.getErrorDecoder() != null) {
		ErrorDecoder errorDecoder = getOrInstantiate(config.getErrorDecoder());
		builder.errorDecoder(errorDecoder);
	}

	if (config.getRequestInterceptors() != null && !config.getRequestInterceptors().isEmpty()) {
		// this will add request interceptor to builder, not replace existing
		for (Class<RequestInterceptor> bean : config.getRequestInterceptors()) {
			RequestInterceptor interceptor = getOrInstantiate(bean);
			builder.requestInterceptor(interceptor);
		}
	}

	if (config.getDecode404() != null) {
		if (config.getDecode404()) {
			builder.decode404();
		}
	}
}

参考

Feign的请求拦截器

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要给特定的 Feign Client 添加 RequestInterceptor,需要在定义该 Feign Client 的接口,通过 @FeignClient 注解的 configuration 属性指定一个配置类(实现了 FeignClientConfigurer 接口),然后在该配置添加 RequestInterceptor。 具体步骤如下: 1. 定义 Feign Client 接口并添加 @FeignClient 注解,在注解指定该 Feign Client 的名称和配置类。 ```java @FeignClient(name = "example", configuration = ExampleClientConfig.class) public interface ExampleClient { // ... } ``` 2. 定义配置类 ExampleClientConfig,并实现 FeignClientConfigurer 接口,在 configure 方法添加 RequestInterceptor。 ```java @Configuration public class ExampleClientConfig implements FeignClientConfigurer { @Override public void configure(FeignClientFactoryBean factoryBean, FeignBuilder feignBuilder) { // 添加特定的 RequestInterceptor feignBuilder.requestInterceptor(new ExampleRequestInterceptor()); } // ... } ``` 3. 编写 ExampleRequestInterceptor,实现 RequestInterceptor 接口。 ```java public class ExampleRequestInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { // 添加特定的请求头或参数 requestTemplate.header("X-Example-Header", "value"); } } ``` 通过这样的方式,就可以给特定的 Feign Client 添加 RequestInterceptor 了。需要注意的是,如果要给所有 Feign Client 都添加同一个 RequestInterceptor,可以在配置直接添加,不需要在每个接口指定。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值