最近用了Spring feign,项目中后端请求三方的接口,需要用到token, 以前么有用过。 在网上搜了一下,

大概有三种方法:

1. 用@RequestHeader注解添加参数到请求头中去, 这样每个方法都需要添加

@FeignClient(name = “taskClient”, url = “${feign.taskask.url}”)
public interface TaskInfoClient {
/**
* 创建任务
* @return
*/
@PostMapping(“/test/task” )
String createTaskInfo(@RequestBody JSONObject req,@RequestHeader(name = “token”,required = true) String token);
}
2. 用的比较多的一种方法,拦截器:

@Configuration //如果写个配置对所有的feign 都会拦截
public class FeignConfiguration implements RequestInterceptor {

@Override
public void apply(RequestTemplate requestTemplate) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
requestTemplate.header(“token”, request.getHeader(“token”));
}
}
如果不配置 @Configuration , 可以在需要拦截的feign client上面加上: configuration = FeignConfiguration.class

@FeignClient(name = “taskClient”, url = “${feign.taskask.url}” ,configuration = FeignConfiguration.class)
public interface TaskInfoClient {
/**
* 创建任务
* @return
*/
@PostMapping(“/test/task” )
String createTaskInfo(@RequestBody JSONObject req,@RequestHeader(name = “token”,required = true) String token);
}
3 大神的方法了,和第二种方法的区别就是不需要添加配置文件,但是FeignConfiguration这个还是要的,不需要加配置文件就加一个自定义策略

package com.hiynn.provider.configuration;

import com.netflix.hystrix.HystrixThreadPoolKey;
import com.netflix.hystrix.HystrixThreadPoolProperties;
import com.netflix.hystrix.strategy.HystrixPlugins;
import com.netflix.hystrix.strategy.concurrency.HystrixConcurrencyStrategy;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariable;
import com.netflix.hystrix.strategy.concurrency.HystrixRequestVariableLifecycle;
import com.netflix.hystrix.strategy.eventnotifier.HystrixEventNotifier;
import com.netflix.hystrix.strategy.executionhook.HystrixCommandExecutionHook;
import com.netflix.hystrix.strategy.metrics.HystrixMetricsPublisher;
import com.netflix.hystrix.strategy.properties.HystrixPropertiesStrategy;
import com.netflix.hystrix.strategy.properties.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Callable;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
@Configuration
public class FeignHystrixConcurrencyStrategy extends HystrixConcurrencyStrategy {
private HystrixConcurrencyStrategy delegate;

public FeignHystrixConcurrencyStrategy() {
try {
this.delegate = HystrixPlugins.getInstance().getConcurrencyStrategy();
if (this.delegate instanceof FeignHystrixConcurrencyStrategy) {
// Welcome to singleton hell…
return;
}
HystrixCommandExecutionHook commandExecutionHook =
HystrixPlugins.getInstance().getCommandExecutionHook();
HystrixEventNotifier eventNotifier = HystrixPlugins.getInstance().getEventNotifier();
HystrixMetricsPublisher metricsPublisher = HystrixPlugins.getInstance().getMetricsPublisher();
HystrixPropertiesStrategy propertiesStrategy =
HystrixPlugins.getInstance().getPropertiesStrategy();
this.logCurrentStateOfHystrixPlugins(eventNotifier, metricsPublisher, propertiesStrategy);
HystrixPlugins.reset();
HystrixPlugins.getInstance().registerConcurrencyStrategy(this);
HystrixPlugins.getInstance().registerCommandExecutionHook(commandExecutionHook);
HystrixPlugins.getInstance().registerEventNotifier(eventNotifier);
HystrixPlugins.getInstance().registerMetricsPublisher(metricsPublisher);
HystrixPlugins.getInstance().registerPropertiesStrategy(propertiesStrategy);
} catch (Exception e) {
log.error(“Failed to register Sleuth Hystrix Concurrency Strategy”, e);
}
}

private void logCurrentStateOfHystrixPlugins(HystrixEventNotifier eventNotifier,
HystrixMetricsPublisher metricsPublisher, HystrixPropertiesStrategy propertiesStrategy) {
if (log.isDebugEnabled()) {
log.debug(“Current Hystrix plugins configuration is [” + “concurrencyStrategy [“
+ this.delegate + “],” + “eventNotifier [” + eventNotifier + “],” + “metricPublisher [“
+ metricsPublisher + “],” + “propertiesStrategy [” + propertiesStrategy + “],” + “]”);
log.debug(“Registering Sleuth Hystrix Concurrency Strategy.”);
}
}

@Override
public <T> Callable<T> wrapCallable(Callable<T> callable) {
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
return new WrappedCallable<>(callable, requestAttributes);
}

@Override
public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey,
HystrixProperty<Integer> corePoolSize, HystrixProperty<Integer> maximumPoolSize,
HystrixProperty<Integer> keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue) {
return this.delegate.getThreadPool(threadPoolKey, corePoolSize, maximumPoolSize, keepAliveTime,
unit, workQueue);
}

@Override
public ThreadPoolExecutor getThreadPool(HystrixThreadPoolKey threadPoolKey,
HystrixThreadPoolProperties threadPoolProperties) {
return this.delegate.getThreadPool(threadPoolKey, threadPoolProperties);
}

@Override
public BlockingQueue<Runnable> getBlockingQueue(int maxQueueSize) {
return this.delegate.getBlockingQueue(maxQueueSize);
}

@Override
public <T> HystrixRequestVariable<T> getRequestVariable(HystrixRequestVariableLifecycle<T> rv) {
return this.delegate.getRequestVariable(rv);
}

static class WrappedCallable<T> implements Callable<T> {
private final Callable<T> target;
private final RequestAttributes requestAttributes;

public WrappedCallable(Callable<T> target, RequestAttributes requestAttributes) {
this.target = target;
this.requestAttributes = requestAttributes;
}

@Override
public T call() throws Exception {
try {
RequestContextHolder.setRequestAttributes(requestAttributes);
return target.call();
} finally {
RequestContextHolder.resetRequestAttributes();
}
}
}

}
我遇到的问题比较扯淡,我调用第三方的服务,总是说我传的token不对。 用抓包工具看看,果然传的不对,

前端页面传给我的也叫“token”

但是我明明用了拦截器把header中的token 重写了啊, 但是还是没有变化,传到三方接口的一直是前端的token,奇怪,目前没有查到办法: 我把自己的接口token名字改掉就好了,

应该是 : 上一个服务或者是前端传递过来的header参数传递到下一个服务中去 , 但是不知道怎么能覆盖header中的token

参考: https://blog.csdn.net/lidai352710967/article/details/88680173

————————————————
版权声明:本文为CSDN博主「LZHH_2008」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/lzhh_2008/article/details/105438478