最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

使用Feign調(diào)用時(shí)添加驗(yàn)證信息token到請求頭方式

 更新時(shí)間:2022年03月04日 15:54:55   作者:木子人弋山  
這篇文章主要介紹了使用Feign調(diào)用時(shí)添加驗(yàn)證信息token到請求頭方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

Feign調(diào)用添加驗(yàn)證信息token到請求頭

1、這是最簡單的一個方法

但是需要對每個調(diào)用都一一添加,就是使用@RequestHeader注解添加參數(shù)到請求頭中去

@FeignClient(name = "capability-register", fallback = ApiServiceClientFallBack.class )
public interface ApiServiceClient {?
? ? @GetMapping("/apiDebug/")
? ? Result debug(@RequestParam("url") String path,
? ? ? ? ? ? ? ? ?@RequestParam("param") String param,
? ? ? ? ? ? ? ? ?@RequestParam("method") String method,
? ? ? ? ? ? ? ? ?@RequestParam("appKey") String appKey,
? ? ? ? ? ? ? ? ?@RequestHeader(name = "Token",required = true) String Token);
}

這里需要注意,其他幾個參數(shù)都是調(diào)用debug接口需要的參數(shù),使用此接口時(shí),直接獲取驗(yàn)證信息放進(jìn)token中,就可以了

2、這個方法是網(wǎng)上大多數(shù)人的用法

但是我看到一個大神的博客,說是這種方法有點(diǎn)不好,然后大神自定義了一個Hystrix的策略,這個第三種方法再講

這種方法是對所有的feign調(diào)用統(tǒng)一設(shè)置請求頭

package com.hiynn.provider.configuration;?
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;?
import javax.servlet.http.HttpServletRequest;
?
/**
?* @author lidai
?* @date 2019/2/27 16:14
?* <p>
?* Feign調(diào)用的時(shí)候添加請求頭Token
?*/
@Configuration
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添加configuration屬性

@FeignClient(name = "capability-register", fallback = ApiServiceClientFallBack.class ,configuration = FeignConfiguration.class)
public interface ApiServiceClient {
?
? ? @GetMapping("/apiDebug/")
? ? Result debug(@RequestParam("url") String path,
? ? ? ? ? ? ? ? ?@RequestParam("param") String param,
? ? ? ? ? ? ? ? ?@RequestParam("method") String method,
? ? ? ? ? ? ? ? ?@RequestParam("appKey") String appKey);
}

到這里的時(shí)候,如果你debug的話,會發(fā)現(xiàn)FeignConfiguration中的attributes獲取不到,需要再配置文件中添加如下配置就可以了

hystrix:
? command:
? ? default:
? ? ? execution:
? ? ? ? isolation:
? ? ? ? ? strategy: SEMAPHORE

3、第三種方法就是大神的方法了

和第二種方法的區(qū)別就是不需要添加配置文件,但是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;
?
/**
?* @author lidai
?* @date 2019/3/18 10:09
?*/
@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();
? ? ? ? ? ? }
? ? ? ? }
? ? }??
}

加上這個就可以了,親測三種方法均可行。 

Feign中增加請求頭

最近遇到項(xiàng)目在調(diào)用

實(shí)現(xiàn)RequestInterceptor 接口,然后可以根據(jù)feignClient中的值加到請求頭中,使用時(shí)候在feignClient中配置,這樣就可以把參數(shù)傳入feign中

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.apache.commons.collections4.CollectionUtils;?
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
??
public class FeignConfiguration implements RequestInterceptor {??
? ? @Override
? ? public void apply(RequestTemplate template) {
?
? ? ? ? List<String> list = new ArrayList<>();
? ? ? ? if ("get".equalsIgnoreCase(template.method())){
? ? ? ? ? ? Map<String, Collection<String>> queries = template.queries();
? ? ? ? ? ? list = (List<String>) queries.get("key");
?
? ? ? ? } else if ("post".equalsIgnoreCase(template.method())){
? ? ? ? ? ? byte[] body = template.body();
? ? ? ? ? ? Map<String, Object> map = JSONObject.parseObject(body, Map.class);
? ? ? ? ? ? Object key = map.get("key");
? ? ? ? ? ? if (key != null){
? ? ? ? ? ? ? ? list.add(key.toString());
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? if (CollectionUtils.isNotEmpty(list)) {
? ? ? ? ? ? template.header("head", list);
? ? ? ? }
? ? }?
}
import com.hbasesoft.vcc.sgp.integral.goods.config.FeignConfiguration;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;?
?
@FeignClient(name = "test", path = "/test", configuration = FeignConfiguration.class)
public interface ErpGoodsCouponsExternalClient {
?
? ? @GetMapping("/url")
? ? Object getData(@RequestParam("key") String key);? ??
}

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • JavaWeb登陸功能實(shí)現(xiàn)代碼

    JavaWeb登陸功能實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了JavaWeb登陸功能實(shí)現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • Spring Boot整合Swagger2的完整步驟詳解

    Spring Boot整合Swagger2的完整步驟詳解

    這篇文章主要給大家介紹了關(guān)于Spring Boot整合Swagger2的完整步驟,文中通過示例代碼將整合的步驟一步步介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2018-07-07
  • 深入解析MybatisPlus多表連接查詢

    深入解析MybatisPlus多表連接查詢

    在一些復(fù)雜的業(yè)務(wù)場景中,我們經(jīng)常會遇到多表連接查詢的需求,本文主要介紹了深入解析MybatisPlus多表連接查詢,具有一定的參考價(jià)值,感興趣的可以了解一下
    2024-06-06
  • 解決MyBatis報(bào)錯:There is no getter for property named'Xxx'in'class xxx.xxx.Xxx'

    解決MyBatis報(bào)錯:There is no getter for 

    這篇文章主要介紹了解決MyBatis報(bào)錯:There is no getter for property named'Xxx'in'class xxx.xxx.Xxx'問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 面試題:Java 實(shí)現(xiàn)查找旋轉(zhuǎn)數(shù)組的最小數(shù)字

    面試題:Java 實(shí)現(xiàn)查找旋轉(zhuǎn)數(shù)組的最小數(shù)字

    這篇文章主要介紹了Java 實(shí)現(xiàn)查找旋轉(zhuǎn)數(shù)組的最小數(shù)字,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-07-07
  • Java秒殺系統(tǒng):web層詳解

    Java秒殺系統(tǒng):web層詳解

    本文主要介紹了如何設(shè)計(jì)一個秒殺系統(tǒng)的web層相關(guān)知識。具有很好的參考價(jià)值。下面跟著小編一起來看下吧,希望能夠給你帶來幫助
    2021-10-10
  • Java logback日志的簡單使用

    Java logback日志的簡單使用

    這篇文章主要介紹了Java logback日志的使用詳解,幫助大家更好的理解和學(xué)習(xí)使用Java,感興趣的朋友可以了解下
    2021-03-03
  • Java實(shí)現(xiàn)調(diào)用ElasticSearch?API的示例詳解

    Java實(shí)現(xiàn)調(diào)用ElasticSearch?API的示例詳解

    這篇文章主要為大家詳細(xì)介紹了Java調(diào)用ElasticSearch?API的效果資料,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-03-03
  • Maven工程打包jar的多種方式

    Maven工程打包jar的多種方式

    Maven打包一般可以生成兩種包一種是可以直接運(yùn)行的包,一種是依賴包(只是編譯包),這篇文章主要介紹了Maven工程打包jar的多種方式步驟詳解,需要的朋友可以參考下
    2023-04-04
  • java lambda 表達(dá)式中的雙冒號的用法說明 ::

    java lambda 表達(dá)式中的雙冒號的用法說明 ::

    這篇文章主要介紹了java lambda 表達(dá)式中的雙冒號的用法說明 ::具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09

最新評論

瓦房店市| 蒙城县| 新干县| 盘山县| 贵南县| 阜阳市| 长岛县| 遂平县| 珠海市| 射洪县| 青川县| 马山县| 海阳市| 抚顺市| 桦川县| 阳高县| 西和县| 广水市| 高邮市| 漠河县| 泽州县| 罗甸县| 洛阳市| 英吉沙县| 皋兰县| 两当县| 土默特右旗| 香河县| 海南省| 石柱| 阳东县| 新巴尔虎左旗| 右玉县| 公主岭市| 饶平县| 乃东县| 腾冲县| 军事| 武夷山市| 枣阳市| 廉江市|