|
@@ -0,0 +1,90 @@
|
|
|
+/*
|
|
|
+ * @(#)FeignConfig.java 2020年6月18日下午5:16:16
|
|
|
+ * Copyright All rights reserved.
|
|
|
+ */
|
|
|
+package com.flyer.foster.config.feign;
|
|
|
+import cn.dev33.satoken.stp.StpUtil;
|
|
|
+import com.flyer.foster.pojo.CheckUtil;
|
|
|
+import feign.Feign;
|
|
|
+import feign.Logger;
|
|
|
+import feign.RequestInterceptor;
|
|
|
+import feign.codec.Encoder;
|
|
|
+import feign.form.spring.SpringFormEncoder;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.ObjectFactory;
|
|
|
+import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
|
|
+import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
|
|
|
+import org.springframework.cloud.openfeign.FeignAutoConfiguration;
|
|
|
+import org.springframework.cloud.openfeign.support.SpringEncoder;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.context.annotation.Primary;
|
|
|
+import org.springframework.context.annotation.Scope;
|
|
|
+import org.springframework.web.context.request.RequestContextHolder;
|
|
|
+import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
+
|
|
|
+import javax.servlet.http.HttpServletRequest;
|
|
|
+
|
|
|
+/**
|
|
|
+ *
|
|
|
+ * Feign自定义配置
|
|
|
+ * @author: wangyf
|
|
|
+ * @version: 1.0 Date: 2020年6月18日 下午5:16:16
|
|
|
+ */
|
|
|
+@Configuration
|
|
|
+@ConditionalOnClass(Feign.class)
|
|
|
+@AutoConfigureBefore(FeignAutoConfiguration.class)
|
|
|
+@Slf4j
|
|
|
+public class FeignConfig {
|
|
|
+
|
|
|
+ @Bean
|
|
|
+ public RequestInterceptor requestInterceptor() {
|
|
|
+ return requestTemplate -> {
|
|
|
+ ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
|
|
|
+ if (null != attributes) {
|
|
|
+ HttpServletRequest request = attributes.getRequest();
|
|
|
+ log.info("Feign request: {}", request.getRequestURI());
|
|
|
+ // 将token信息放入header中
|
|
|
+ requestTemplate.header(StpUtil.getTokenName(), request.getHeader(StpUtil.getTokenName()));
|
|
|
+ requestTemplate.header("appId", request.getHeader("appId"));
|
|
|
+ String key = request.getParameter("key");
|
|
|
+ if(StringUtils.isNotBlank(key)){
|
|
|
+ requestTemplate.query("key",key);
|
|
|
+ }
|
|
|
+ requestTemplate.query("key", CheckUtil.key);
|
|
|
+ }else{
|
|
|
+ requestTemplate.query("key", CheckUtil.key);
|
|
|
+ }
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Feign 客户端的日志记录,默认级别为NONE
|
|
|
+ * Logger.Level 的具体级别如下:
|
|
|
+ * NONE:不记录任何信息
|
|
|
+ * BASIC:仅记录请求方法、URL以及响应状态码和执行时间
|
|
|
+ * HEADERS:除了记录 BASIC级别的信息外,还会记录请求和响应的头信息
|
|
|
+ * FULL:记录所有请求与响应的明细,包括头信息、请求体、元数据
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ Logger.Level feignLoggerLevel() {
|
|
|
+ return Logger.Level.FULL;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Feign支持文件上传
|
|
|
+ * @param messageConverters
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ @Primary
|
|
|
+ @Scope("prototype")
|
|
|
+ public Encoder multipartFormEncoder(ObjectFactory<HttpMessageConverters> messageConverters) {
|
|
|
+ return new SpringFormEncoder(new SpringEncoder(messageConverters));
|
|
|
+ }
|
|
|
+}
|
|
|
+
|