您好,登錄后才能下訂單哦!
小編給大家分享一下微服務feign調用添加token的問題,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
具體的怎么調用就不說了 如下配置,就可以在請求頭中添加需要的請求頭信息。
package localdate; import feign.RequestInterceptor; import feign.RequestTemplate; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Configuration; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest; import java.util.Collection; import java.util.Enumeration; import java.util.Iterator; import java.util.Map; /** * feign調用服務時,會丟失請求頭信息。需要在這里把認證信息收到添加上去 * @author TRON * @since 2019-11-23 * * */ @Configuration @Slf4j public class FeignTokenInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { log.info("======上下文中獲取原請求信息======"); String token = "without token"; HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); Enumeration<String> headerNames = request.getHeaderNames(); while (headerNames.hasMoreElements()) { String headerName = headerNames.nextElement(); String HeadValue = request.getHeader(headerName); log.info("===原請求頭信息=== headName: {}, headValue: {}", headerName, HeadValue); if (headerName.equals("X-Authorization-access_token")||headerName.equals("x-authorization-access_token")) { token = HeadValue; } } log.info("=======Feign添加頭部信息start======"); // requestTemplate.header("X-Authorization-access_token", token); requestTemplate.header("X-Authorization-access_token", "tron123456"); log.info("=======Feign添加頭部信息end======"); } }
package localdate; import com.netflix.hystrix.HystrixThreadPoolKey; 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 org.slf4j.Logger; import org.slf4j.LoggerFactory; 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; /** * 自定義并發策略 * 將現有的并發策略作為新并發策略的成員變量 * 在新并發策略中,返回現有并發策略的線程池、Queue * * hystrix.command.default.execution.isolation.strategy=THREAD * Hystrix的默認隔離策略(官方推薦,當使用該隔離策略時,是沒辦法拿到 ThreadLocal 中的值的,但是RequestContextHolder 源碼中,使用了兩個ThreadLocal) * hystrix.command.default.execution.isolation.strategy=SEMAPHORE (將隔離策略改為SEMAPHORE 也可以解決這個問題,但是官方并不推薦這個策略,因為這個策略對網絡資源消耗比較大) * * 主要是解決當 Hystrix的默認隔離策略是THREAD時,不能通過RequestContextHolder獲取到request對象的問題 * */ //@Configuration public class FeignConfig extends HystrixConcurrencyStrategy { private static final Logger log = LoggerFactory.getLogger(FeignConfig.class); private HystrixConcurrencyStrategy delegate; public FeignConfig() { try { this.delegate = HystrixPlugins.getInstance().getConcurrencyStrategy(); if (this.delegate instanceof FeignConfig) { // 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 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(); } } } }
feign: client: config: default: connectTimeout: 5000 #連接超時3秒,連接失敗時直接調用降級方法 readTimeout: 100000 #連接成功,處理數據的時間限制10秒 100000 讀取時間過短會拋異常java.net.SocketTimeoutException: Read timed out loggerLevel: full #日志輸出等級 hystrix: enabled: true hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 5000 #服務連接成功,但是時間過長,降級方法調用時間 60000 5000
我只是記錄服務提供方、消費方的代碼編寫,配置什么的大家在網上搜,一大堆。
啟動類上加上注解@EnableFeignClients,然后正常的寫controller、service等業務邏輯
1.首先啟動類上加上注解@EnableFeignClients
2.編寫服務調用接口
3.編寫接口熔斷處理方法
4.本人遇到的問題是需要用到調用方的請求頭里面的信息,但是在提供方取不到,這時可以通過在調用方增加配置來解決
import feign.RequestInterceptor; import feign.RequestTemplate; import java.util.Enumeration; import javax.servlet.http.HttpServletRequest; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; /** * @author ydf * @date 2021/5/13 * @description: **/ public class FeignBasicAuthRequestInterceptor implements RequestInterceptor { @Override public void apply(RequestTemplate requestTemplate) { 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); requestTemplate.header(name, values); } } } }
import com.jingling.netsign.applet.interceptor.FeignBasicAuthRequestInterceptor; import feign.RequestInterceptor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; /** * @author ydf * @date 2021/5/13 * @description: **/ @Configuration public class FeignSupportConfig { /** * feign請求攔截器 * * @return */ @Bean public RequestInterceptor requestInterceptor(){ return new FeignBasicAuthRequestInterceptor(); } }
看完了這篇文章,相信你對“微服務feign調用添加token的問題”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。