提交 3792f463 编写于 作者: JEECG低代码平台's avatar JEECG低代码平台

FeignConfig 重复注册问题处理

上级 f3523660
package org.jeecg.starter.cloud.config; //package org.jeecg.starter.cloud.config;
//
import feign.Feign; //import feign.Feign;
import feign.Logger; //import feign.Logger;
import feign.RequestInterceptor; //import feign.RequestInterceptor;
import feign.codec.Encoder; //import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder; //import feign.form.spring.SpringFormEncoder;
import lombok.extern.slf4j.Slf4j; //import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.constant.CommonConstant; //import org.jeecg.common.constant.CommonConstant;
import org.springframework.beans.factory.ObjectFactory; //import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.AutoConfigureBefore; //import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; //import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters; //import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.FeignAutoConfiguration; //import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.cloud.openfeign.support.SpringEncoder; //import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean; //import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; //import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary; //import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope; //import org.springframework.context.annotation.Scope;
import org.springframework.web.context.request.RequestContextHolder; //import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; //import org.springframework.web.context.request.ServletRequestAttributes;
//
import javax.servlet.http.HttpServletRequest; //import javax.servlet.http.HttpServletRequest;
//
//
@ConditionalOnClass(Feign.class) //@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class) //@AutoConfigureBefore(FeignAutoConfiguration.class)
@Slf4j //@Slf4j
@Configuration //@Configuration
public class FeignClientConfig { //public class FeignClientConfig {
//
@Bean // @Bean
public RequestInterceptor requestInterceptor() { // public RequestInterceptor requestInterceptor() {
return requestTemplate -> { // return requestTemplate -> {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); // ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
if (null != attributes) { // if (null != attributes) {
HttpServletRequest request = attributes.getRequest(); // HttpServletRequest request = attributes.getRequest();
log.info("Feign request: {}", request.getRequestURI()); // log.info("Feign request: {}", request.getRequestURI());
// 将token信息放入header中 // // 将token信息放入header中
String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN); // String token = request.getHeader(CommonConstant.X_ACCESS_TOKEN);
if(token==null){ // if(token==null){
token = request.getParameter("token"); // token = request.getParameter("token");
} // }
log.info("Feign request token: {}", token); // log.info("Feign request token: {}", token);
requestTemplate.header(CommonConstant.X_ACCESS_TOKEN, token); // requestTemplate.header(CommonConstant.X_ACCESS_TOKEN, token);
} // }
}; // };
} // }
//
//
//
/** // /**
* Feign 客户端的日志记录,默认级别为NONE // * Feign 客户端的日志记录,默认级别为NONE
* Logger.Level 的具体级别如下: // * Logger.Level 的具体级别如下:
* NONE:不记录任何信息 // * NONE:不记录任何信息
* BASIC:仅记录请求方法、URL以及响应状态码和执行时间 // * BASIC:仅记录请求方法、URL以及响应状态码和执行时间
* HEADERS:除了记录 BASIC级别的信息外,还会记录请求和响应的头信息 // * HEADERS:除了记录 BASIC级别的信息外,还会记录请求和响应的头信息
* FULL:记录所有请求与响应的明细,包括头信息、请求体、元数据 // * FULL:记录所有请求与响应的明细,包括头信息、请求体、元数据
*/ // */
@Bean // @Bean
Logger.Level feignLoggerLevel() { // Logger.Level feignLoggerLevel() {
return Logger.Level.FULL; // return Logger.Level.FULL;
} // }
//
/** // /**
* Feign支持文件上传 // * Feign支持文件上传
* @param messageConverters // * @param messageConverters
* @return // * @return
*/ // */
@Bean // @Bean
@Primary // @Primary
@Scope("prototype") // @Scope("prototype")
public Encoder multipartFormEncoder(ObjectFactory<HttpMessageConverters> messageConverters) { // public Encoder multipartFormEncoder(ObjectFactory<HttpMessageConverters> messageConverters) {
return new SpringFormEncoder(new SpringEncoder(messageConverters)); // return new SpringFormEncoder(new SpringEncoder(messageConverters));
} // }
} //}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册