使用Feign調用時添加驗證信息token到請求頭方式

Feign調用添加驗證信息token到請求頭

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

但是需要對每個調用都一一添加,就是使用@RequestHeader註解添加參數到請求頭中去

@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);
}

這裡需要註意,其他幾個參數都是調用debug接口需要的參數,使用此接口時,直接獲取驗證信息放進token中,就可以瞭

2、這個方法是網上大多數人的用法

但是我看到一個大神的博客,說是這種方法有點不好,然後大神自定義瞭一個Hystrix的策略,這個第三種方法再講

這種方法是對所有的feign調用統一設置請求頭

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調用的時候添加請求頭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);
}

到這裡的時候,如果你debug的話,會發現FeignConfiguration中的attributes獲取不到,需要再配置文件中添加如下配置就可以瞭

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

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;
 
/**
 * @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中增加請求頭

最近遇到項目在調用

實現RequestInterceptor 接口,然後可以根據feignClient中的值加到請求頭中,使用時候在feignClient中配置,這樣就可以把參數傳入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);    
}

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。

推薦閱讀: