cn.ishow.starter.rpc.RpcAutoConfiguration Maven / Gradle / Ivy
package cn.ishow.starter.rpc;
import cn.ishow.starter.rpc.aop.RpcClientAdvice;
import cn.ishow.starter.rpc.aop.RpcClientAdvisor;
import cn.ishow.starter.rpc.basic.CustomDecoder;
import cn.ishow.starter.rpc.basic.HeaderInterceptor;
import cn.ishow.starter.rpc.context.RpcContext;
import cn.ishow.starter.rpc.processor.ClientBeanPostProcessor;
import cn.ishow.starter.rpc.processor.DecoderBeanPostProcessor;
import cn.ishow.starter.rpc.properties.GlobalHttpProperties;
import feign.Client;
import feign.Retryer;
import feign.codec.Decoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
/**
* @author chongyin
* @create 2022/5/10 下午7:43
* @description
*/
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties({GlobalHttpProperties.class})
public class RpcAutoConfiguration {
@Bean
@ConditionalOnMissingBean(value = Decoder.class)
public CustomDecoder decoder(ObjectFactory messageConverters, RpcContext rpcContext) {
return new CustomDecoder(messageConverters, rpcContext);
}
@Bean
@ConditionalOnMissingClass(value = {"feign.httpclient.ApacheHttpClient", "feign.okhttp.OkHttpClient"})
@ConditionalOnMissingBean(value = Client.class)
public Client defaultClient() {
return new Client.Default(null, null);
}
@Bean
@ConditionalOnMissingBean(value = Retryer.class)
public Retryer feignRetryer() {
return Retryer.NEVER_RETRY;
}
@Bean
public DecoderBeanPostProcessor decoderBeanPostProcessor() {
return new DecoderBeanPostProcessor();
}
@Bean
public ClientBeanPostProcessor clientBeanPostProcessor() {
return new ClientBeanPostProcessor();
}
/**
* 注入feign的相关拦截器
*
* @param advice
* @return
*/
@Bean
public RpcClientAdvisor clientProxyAdvisor(RpcClientAdvice advice) {
RpcClientAdvisor clientProxyAdvisor = new RpcClientAdvisor();
clientProxyAdvisor.setAdvice(advice);
return clientProxyAdvisor;
}
/**
* 对feign请求执行前增加相关参数
*
* @return
*/
@Bean
public RpcClientAdvice feignClientAdvice(RpcContext rpcContext, Environment environment) {
return new RpcClientAdvice(rpcContext, environment);
}
@Bean
public RpcContext rpcContext() {
return new RpcContext();
}
@Bean
public HeaderInterceptor requestHeaderInterceptor() {
return new HeaderInterceptor();
}
}