记录一次Feign调用时往header中加参数遇到的问题

Feign中往header加参数的方式

Feign调用时在header中添加参数_huahuo147的博客-CSDN博客

场景:服务调用是前端调A服务,A服务调B/C服务。我维护的是B/C服务,虽然我这边生效了,但A服务调B/C服务也要判断前端有没有往header里塞东西。在demo服务中自测没问题,但是A服务调B/C服务死活带不上参数

问题1:

A调B服务是用Feign调的,我在FeignInterceptor中添加了配置,本地自测生效,部署到测试环境就失效。

原因排查:A是parent服务,其中有很多子服务,每个子服务都有可能调B服务,因此A服务中直接给所有调B服务的Feign套了个注解,注解如下:
 

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@FeignClient(name = "xxx", url = "${xxx.domain:}",
        configuration = SearchFeignClientsConfiguration.class)
public @interface SearchCenterFeignClient {
  /**
   * A custom <code>@Configuration</code> for the feign client. Can contain
   * override <code>@Bean</code> definition for the pieces that make up the
   * client, for instance {@link feign.codec.Decoder},
   * {@link feign.codec.Encoder}, {@link feign.Contract}.
   *
   * @see FeignClientsConfiguration for the defaults
   */
  Class<?>[] configuration() default {};

  /**
   * Fallback class for the specified Feign client interface. The fallback
   * class must implement the interface annotated by this annotation and be a
   * valid spring bean.
   */
  Class<?> fallback() default void.class;

  Class<?> fallbackFactory() default void.class;
  /**
   * Path prefix to be used by all method-level mappings. Can be used with or
   * without <code>@RibbonClient</code>.
   */
  String path() default "";
}

其他服务调用时不再加FeignClient注解,而是直接加自定义注解,如:
 

@SearchCenterFeignClient(path = "xx-api", fallbackFactory = SearchCenterClientFallBackFactory.class)
public interface SearchCenterClient {

  @GetMapping(value = {"xx-config/{xxId}"})
  List<Category4QueryDto> searchConfig(
      @PathVariable(value = "xxId", required = false) Long xxId,
      @RequestParam(value = "type", required = false, defaultValue = "false") String type,
      @RequestParam("suggestionUrlPrefix") String suggestionUrlPrefix);

  @GetMapping("search/xxx")
  String xxx(
      @RequestParam Map parameterPairs);

由于SearchCenterFeignClient 接口中有这行代码:Class<?>[] configuration() default {};
导致我指定的Configuration在测试服务器中没有生效

解决方案:干掉这行代码

问题2:多线程下传递threadLocal

我用拦截器拦截header中的参数并且放到threadLocal中,但是多线程失效

解决方案:

1.用这个threadLocal,可以传递父子线程:InheritableThreadLocal。

2.借助ali的方案

解决线程切换导致ThreadLocal值丢失 - 简书

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值