好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

使用Feign动态设置header和原理分析

Feign动态设置header和原理

项目中用到了Feign做远程调用, 有部分场景需要动态配置header

开始的做法是通过 @RequestHeader 设置参数来实现动态的header配置

例如

?

1

2

@GetMapping (value = "/test" , consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})    

String access( @RequestHeader ( "Auth" ) String auth, @RequestBody Expression expression);

这种方式虽然可以达到header的动态配置, 但是当参数过多时会降低接口可用性, 所以想通过传递bean的方式来设置header

先说解决办法

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

public class HeaderInterceptor implements RequestInterceptor {

    @Override

    public void apply(RequestTemplate requestTemplate) {

        byte [] bytes = requestTemplate.requestBody().asBytes();

        Identity identity = JSONObject.parseObject(bytes, Identity. class );

        requestTemplate.header( "Auth" , identity.getSecret());

    }

/**

  * configuration指定Interceptor

**/

@FeignClient (name = "test" , url = "127.0.0.1:8300" , configuration = HeaderInterceptor. class )

public interface GolangTestHandle2 {

    @GetMapping (value = "/handler" , consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE})

    String handle(Identity identity);

}

自定义Interceptor实现RequestInterceptor接口, 回调方法apply提供了RequestTemplate对象, 对象内部封装了request的所有信息, 最后通过configuration指定接口, 之后就随便你怎么玩了(例如通过body获取接口参数并动态设置header)

值得注意的一点是HeaderInterceptor如果注入到Springboot容器的话会全局生效, 就是说及时没有指定configuration也会对全局feign接口生效, 为什么呢? 这里简单说明一下

首先Feign为每个feign class创建springcontext上下文

spring通过调用getObject获取feign工厂实例

?

1

2

3

4

    @Override

    public Object getObject() throws Exception {

        return getTarget();

    }

    内部调用FeignClientFatoryBean.getTarget()方法

?

1

2

3

4

5

6

7

<T> T getTarget() {

        //获取feign上下文

        FeignContext context = this .applicationContext.getBean(FeignContext. class );

        //构建feign Builder

        Feign.Builder builder = feign(context);

        ...

    }

根据feign(FeignContext context)构建Builder

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

protected Feign.Builder feign(FeignContext context) {

        FeignLoggerFactory loggerFactory = get(context, FeignLoggerFactory. class );

        Logger logger = loggerFactory.create( this .type);

        // @formatter:off

        Feign.Builder builder = get(context, Feign.Builder. class )

                // required values

                .logger(logger)

                //默认springEncoder

                .encoder(get(context, Encoder. class ))

                //默认OptionalDecoder

                .decoder(get(context, Decoder. class ))

                //默认SpringMvcContrat

                .contract(get(context, Contract. class ));

        // @formatter:on

        //配置该feign的context

        configureFeign(context, builder);

        return builder;

    }

    在构建过程中通过FeignClientFactoryBean.configureUsingConfiguration为feign class注册基本的配置项, 其中也包括了Interceptor的注册

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

    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);

        }

        //从feign context获取interceptors

        Map<String, RequestInterceptor> requestInterceptors = context

                .getInstances( this .contextId, RequestInterceptor. class );

        if (requestInterceptors != null ) {

            builder.requestInterceptors(requestInterceptors.values());

        }

        if ( this .decode404) {

            builder.decode404();

        }

    }

contextId为具体的feign class id, RequestInterceptor为具体的接口, 即是说通过context.getInstances获取所有RequestInterceptor实例并注册到builder中.

?

1

2

3

4

5

6

7

8

9

    public <T> Map<String, T> getInstances(String name, Class<T> type) {

        AnnotationConfigApplicationContext context = getContext(name);

        //使用beanNamesForTypeIncludingAncestors

        if (BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context,

                type).length > 0 ) {

            return BeanFactoryUtils.beansOfTypeIncludingAncestors(context, type);

        }

        return null ;

    }

获取工厂中的实例使用的是beanNamesForTypeIncludingAncestors方法, 该方法不仅会从feign的factory中查找, 也会通过父级别spring工厂查找相应实例(类似于springmvc的工厂)

也是因为该方法, 即使你没有在FeignClient中配置configuration, 但是你的Interceptor通过@Component等方法注入容器的话也会全局生效的, 所以如果指向让你的Interceptor部分生效不让它注入到Spring容器就好

设置Feign的header信息(两种形式)

在使用微服务SpringCloud全家桶组件Fegin的时候,我们在进行远程服务之间调用的同时,为了防止客户端劫持信息,我们需要将一些敏感信息添加到我们的Fegin头部(Header)当中,今天朋友问起,总结一下:那么工作中常见的方式有两种

1.在方法参数前面添加@RequestHeader注解

?

1

2

@PostMapping (value = "/getPersonDetail" ) 

public ServerResponse getPersonDetail( @RequestBody Map map, @RequestHeader (name = "id" ) String id);

使用@RequestHeader(name = "id")可以传递动态header属性

2.实现RequestInterceptor接口

设置Header(所有的Fegin请求)

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

import org.springframework.context.annotation.Configuration; 

import org.springframework.web.context.request.RequestContextHolder; 

import org.springframework.web.context.request.ServletRequestAttributes; 

import feign.RequestInterceptor; 

import feign.RequestTemplate; 

@Configuration

public class FeignConfiguration implements RequestInterceptor {    

        @Override    

        public void apply(RequestTemplate template) {      

                ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();       

                HttpServletRequest request = attributes.getRequest();        

                Enumeration<String> headerNames = request.getHeaderNames();        

                if (headerNames != null ) {            

                        while (headerNames.hasMoreElements()) {             

                                String name = headerNames.nextElement();              

                                String values = request.getHeader(name);             

                                template.header(name, values);            

                        }            

                }    

        } 

 

@Component

@FeignClient (value = "abc" ,fallback = abcServiceHystric. class ,configuration = FeignConfiguration. class ) public interface AbcService { }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://segmentfault测试数据/a/1190000021015214

查看更多关于使用Feign动态设置header和原理分析的详细内容...

  阅读:25次