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的方案