Spring feignClient 定义拦截器

我们在使用Http请求时,或多或少都会使用到拦截器,那么在 FeignClient 定义拦截器怎么定义呢?两种方式

1、继承 RequestInterceptor 类

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;

@Component
public class TestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        //拦截器的处理逻辑
    }
}

这种方式声明的拦截器是全局的,也就是所有的 FeignClient 发出的请求都会走这个拦截器。

在某一些情况下,我们只需要拦截部分特定的url,也就是为每一个FeignClient单独设置拦截器,那么你可以使用第二种方式

2、通过 FeignClient 中的 configuration 属性,设置单独的配置类(里边可以设置很多相关的配置)

TestFeignClient

指定的配置类是 TestConfig

@FeignClient(configuration = TestConfig.class)
public class TestFeignClient {
}

TestConfig的定义如下,

public class TestConfig {
    @Bean
    public TestInterceptor testInterceptor(){
        return new TestInterceptor();
    }
}
注意,这里不能对 TestConfig 添加 @Configuration,咱们看下官方的说明:

 从上边可以看到 TestConfig 类被 @Configuration 标记,那么他将变成默认的配置,intercaptor,有变成全局的,

TestInterceptor 

public class TestInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate template) {
        //拦截器的处理逻辑
    }
}

 

3、配置类定义组件的范围

上文中提到,@Configuration标记的配置类,可以定义一些组件信息,不过具体可以定义哪些组件,官方文档中有提到:

 

 可以看到,文中说的,openfegin组件提供的默认 configuration 的类是 :FeignClientsConfiguration,如果用户自定义了 配置类,那么用户自定义的组件优先级更高,会覆盖  FeignClientsConfiguration 中定义的组件,具体包含哪些组件参考如下:

package org.springframework.cloud.openfeign;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.cloud.openfeign.support.SpringMvcContract;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.core.convert.ConversionService;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.format.support.FormattingConversionService;

import com.netflix.hystrix.HystrixCommand;

import feign.Contract;
import feign.Feign;
import feign.Logger;
import feign.Retryer;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.hystrix.HystrixFeign;
import feign.optionals.OptionalDecoder;

/**
 * @author Dave Syer
 * @author Venil Noronha
 */
@Configuration
public class FeignClientsConfiguration {

	@Autowired
	private ObjectFactory<HttpMessageConverters> messageConverters;

	@Autowired(required = false)
	private List<AnnotatedParameterProcessor> parameterProcessors = new ArrayList<>();

	@Autowired(required = false)
	private List<FeignFormatterRegistrar> feignFormatterRegistrars = new ArrayList<>();

	@Autowired(required = false)
	private Logger logger;

	@Bean
	@ConditionalOnMissingBean
	public Decoder feignDecoder() {
		return new OptionalDecoder(new ResponseEntityDecoder(new SpringDecoder(this.messageConverters)));
	}

	@Bean
	@ConditionalOnMissingBean
	public Encoder feignEncoder() {
		return new SpringEncoder(this.messageConverters);
	}

	@Bean
	@ConditionalOnMissingBean
	public Contract feignContract(ConversionService feignConversionService) {
		return new SpringMvcContract(this.parameterProcessors, feignConversionService);
	}

	@Bean
	public FormattingConversionService feignConversionService() {
		FormattingConversionService conversionService = new DefaultFormattingConversionService();
		for (FeignFormatterRegistrar feignFormatterRegistrar : feignFormatterRegistrars) {
			feignFormatterRegistrar.registerFormatters(conversionService);
		}
		return conversionService;
	}

	@Configuration
	@ConditionalOnClass({ HystrixCommand.class, HystrixFeign.class })
	protected static class HystrixFeignConfiguration {
		@Bean
		@Scope("prototype")
		@ConditionalOnMissingBean
		@ConditionalOnProperty(name = "feign.hystrix.enabled")
		public Feign.Builder feignHystrixBuilder() {
			return HystrixFeign.builder();
		}
	}

	@Bean
	@ConditionalOnMissingBean
	public Retryer feignRetryer() {
		return Retryer.NEVER_RETRY;
	}

	@Bean
	@Scope("prototype")
	@ConditionalOnMissingBean
	public Feign.Builder feignBuilder(Retryer retryer) {
		return Feign.builder().retryer(retryer);
	}

	@Bean
	@ConditionalOnMissingBean(FeignLoggerFactory.class)
	public FeignLoggerFactory feignLoggerFactory() {
		return new DefaultFeignLoggerFactory(logger);
	}

}

 

官方说明:https://cloud.spring.io/spring-cloud-netflix/multi/multi_spring-cloud-feign.html

posted @ 2020-06-22 19:34  fight-ing  阅读(7625)  评论(1编辑  收藏  举报
Fork me on GitHub