微服务之间调用时时常遇到需要将服务A的token传递给服务B的情况,如果每个服务调用单独写就太僵硬了,之前试过自己写AOP去处理这个问题,功能上虽然没问题,但是总感觉比较low,最近在解决另外一个问题时,意外学到了一个解决方法。
这里贴一下核心代码
@Slf4j @Service public class FeignServiceImpl implements FeignService { @Autowired private ConfigurableEnvironment environment; @Autowired private FeignClientRequestInterceptor requestInterceptor; private final Feign.Builder nameBuilder; @Autowired public FeignServiceImpl(Decoder decoder, Encoder encoder, Client client, Contract contract) { // nameBuilder直接使用client,它会使用负载均衡 nameBuilder = Feign.builder() .client(client) .encoder(encoder) .decoder(decoder) .contract(contract); } @Override public <T> T newInstanceByTenantName(Class<T> apiType, String shortName) { String bsServiceName = environment.getProperty("feign.name"); Str