Commit 2d90978c by 刘宇峰

框架完善

1 parent 18ccf57f
......@@ -5,7 +5,7 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.11</version>
<version>2.6.13</version>
</parent>
<groupId>com.glsx</groupId>
......@@ -24,8 +24,8 @@
<maven.compiler.target>8</maven.compiler.target>
<!-- 版本说明:https://github.com/alibaba/spring-cloud-alibaba/wiki/%E7%89%88%E6%9C%AC%E8%AF%B4%E6%98%8E-->
<spring-cloud.version>2021.0.4</spring-cloud.version>
<spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version>
<spring-cloud.version>2021.0.5</spring-cloud.version>
<spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
<revision>2.0.0-SNAPSHOT</revision>
<!-- 本地发布服务器 -->
......
......@@ -13,7 +13,6 @@
<properties>
<curator.version>2.10.0</curator.version>
<guava.version>18.0</guava.version>
</properties>
<dependencies>
......
......@@ -11,48 +11,27 @@
<artifactId>rest-admin-feign</artifactId>
<properties>
<feign.form.version>3.8.0</feign.form.version>
</properties>
<dependencies>
<!-- feign -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
<exclusions>
<exclusion>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.glsx</groupId>
<artifactId>rest-admin-sentinel</artifactId>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId>
</dependency>
<dependency>
<groupId>io.github.openfeign</groupId>
<artifactId>feign-gson</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<groupId>com.glsx</groupId>
<artifactId>rest-admin-sentinel</artifactId>
</dependency>
<!--json-->
<dependency>
<groupId>com.alibaba</groupId>
......
package com.alibaba.cloud.sentinel.feign;
import com.glsx.plat.feign.fallback.FunFallbackFactory;
import feign.Contract;
import feign.Feign;
import feign.InvocationHandlerFactory;
import feign.Target;
import lombok.SneakyThrows;
import org.springframework.beans.BeansException;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.cloud.openfeign.FeignContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.util.ReflectionUtils;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Map;
public class FunSentinelFeign {
public static Builder builder() {
return new Builder();
}
public static final class Builder extends Feign.Builder implements ApplicationContextAware {
private Contract contract = new Contract.Default();
private ApplicationContext applicationContext;
private FeignContext feignContext;
@Override
public Feign.Builder invocationHandlerFactory(
InvocationHandlerFactory invocationHandlerFactory) {
throw new UnsupportedOperationException();
}
@Override
public Builder contract(Contract contract) {
this.contract = contract;
return this;
}
@Override
public Feign build() {
super.invocationHandlerFactory(new InvocationHandlerFactory() {
@SneakyThrows
@Override
public InvocationHandler create(Target target, Map<Method, MethodHandler> dispatch) {
Object feignClientFactoryBean = Builder.this.applicationContext
.getBean("&" + target.type().getName());
Class fallback = (Class) getFieldValue(feignClientFactoryBean,
"fallback");
Class fallbackFactory = (Class) getFieldValue(feignClientFactoryBean,
"fallbackFactory");
String name = (String) getFieldValue(feignClientFactoryBean, "name");
Object fallbackInstance;
FallbackFactory fallbackFactoryInstance;
// check fallback and fallbackFactory properties
// //获取指定的构造方法
// Constructor<SentinelInvocationHandler> constructor = SentinelInvocationHandler.class.
// getDeclaredConstructor(Target.class, Map.class, FallbackFactory.class);
// constructor.setAccessible(true);
// if (void.class != fallback) {
// fallbackInstance = getFromContext(name, "fallback", fallback,
// target.type());
// return constructor.newInstance(target, dispatch, new FallbackFactory.Default(fallbackInstance));
// }
// if (void.class != fallbackFactory) {
// fallbackFactoryInstance = (FallbackFactory) getFromContext(name,
// "fallbackFactory", fallbackFactory,
// FallbackFactory.class);
// return constructor.newInstance(target, dispatch, fallbackFactoryInstance);
// }
// // 默认的 fallbackFactory
// FunFallbackFactory funFallbackFactory = new FunFallbackFactory(target);
// return constructor.newInstance(target, dispatch, funFallbackFactory);
if (void.class != fallback) {
fallbackInstance = getFromContext(name, "fallback", fallback,
target.type());
return new SentinelInvocationHandler(target, dispatch,
new FallbackFactory.Default(fallbackInstance));
}
if (void.class != fallbackFactory) {
fallbackFactoryInstance = (FallbackFactory) getFromContext(name,
"fallbackFactory", fallbackFactory,
FallbackFactory.class);
return new SentinelInvocationHandler(target, dispatch,
fallbackFactoryInstance);
}
// 默认的 fallbackFactory
FunFallbackFactory funFallbackFactory = new FunFallbackFactory(target);
return new SentinelInvocationHandler(target, dispatch, funFallbackFactory);
}
private Object getFromContext(String name, String type, Class fallbackType, Class targetType) {
Object fallbackInstance = feignContext.getInstance(name, fallbackType);
if (fallbackInstance == null) {
throw new IllegalStateException(String.format(
"No %s instance of type %s found for feign client %s",
type, fallbackType, name));
}
if (!targetType.isAssignableFrom(fallbackType)) {
throw new IllegalStateException(String.format(
"Incompatible %s instance. Fallback/fallbackFactory of type %s is not assignable to %s for feign client %s",
type, fallbackType, targetType, name));
}
return fallbackInstance;
}
});
super.contract(new SentinelContractHolder(contract));
return super.build();
}
private Object getFieldValue(Object instance, String fieldName) {
Field field = ReflectionUtils.findField(instance.getClass(), fieldName);
field.setAccessible(true);
try {
return field.get(instance);
} catch (IllegalAccessException e) {
// ignore
}
return null;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
feignContext = this.applicationContext.getBean(FeignContext.class);
}
}
}
\ No newline at end of file
......@@ -16,6 +16,7 @@ import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.loadbalancer.config.LoadBalancerAutoConfiguration;
import org.springframework.cloud.openfeign.FeignAutoConfiguration;
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
import org.springframework.cloud.openfeign.support.SpringDecoder;
......@@ -33,7 +34,7 @@ import java.util.List;
* @author payu
*/
@Configuration
@Import({FeignAutoConfiguration.class, OkHttpConfig.class})
@Import({FeignAutoConfiguration.class, LoadBalancerAutoConfiguration.class})
public class FeignConfig {
@Autowired
......@@ -68,13 +69,12 @@ public class FeignConfig {
@Bean
public Encoder feignEncoder() {
return new SpringEncoder(feignHttpMessageConverter());
return new SpringEncoder(messageConverters);
}
@Bean
public Decoder feignDecoder() {
// return new SpringDecoder(feignHttpMessageConverter());
return new OptionalDecoder(new ResponseEntityDecoder(new ResultDecoder(new SpringDecoder(feignHttpMessageConverter()))));
return new OptionalDecoder(new ResponseEntityDecoder(new FeignResultDecoder(new SpringDecoder(messageConverters))));
}
private ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() {
......
......@@ -2,10 +2,7 @@ package com.glsx.plat.feign.config;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.glsx.plat.exception.BusinessException;
import com.glsx.plat.exception.ExceptionInfo;
import com.glsx.plat.exception.ServiceException;
import com.glsx.plat.exception.SystemMessage;
import com.glsx.plat.exception.*;
import feign.FeignException;
import feign.Response;
import feign.Util;
......@@ -36,6 +33,9 @@ public class FeignErrorDecoder implements ErrorDecoder {
if (exception instanceof BusinessException) {
log.error("业务异常:" + exception.getMessage());
}
if (exception instanceof ResultException) {
log.error("业务异常:" + exception.getMessage());
}
return exception;
}
} catch (Exception e) {
......
......@@ -12,17 +12,18 @@ import java.io.IOException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
public class ResultDecoder implements Decoder {
public class FeignResultDecoder implements Decoder {
private Decoder decoder;
public ResultDecoder(Decoder decoder) {
public FeignResultDecoder(Decoder decoder) {
this.decoder = decoder;
}
@Override
public Object decode(Response response, Type type) throws IOException, FeignException {
Method method = response.request().requestTemplate().methodMetadata().method();
boolean isResult = method.getReturnType() != Result.class && method.isAnnotationPresent(ResponseResultBody.class);
boolean isResult = method.getReturnType() != Result.class &&
(method.getDeclaringClass().isAnnotationPresent(ResponseResultBody.class) || method.isAnnotationPresent(ResponseResultBody.class));
if (isResult) {
ParameterizedTypeImpl resultType = ParameterizedTypeImpl.make(Result.class, new Type[]{type}, null);
Result<?> result = (Result<?>) this.decoder.decode(response, resultType);
......
......@@ -16,9 +16,9 @@ import java.util.concurrent.TimeUnit;
* 配置 okhttp 与连接池
* ConnectionPool 默认创建5个线程,保持5分钟长连接
*/
@Configuration
@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class) //SpringBoot自动配置
//@Configuration
//@ConditionalOnClass(Feign.class)
//@AutoConfigureBefore(FeignAutoConfiguration.class) //SpringBoot自动配置
public class OkHttpConfig {
@Resource
......
package com.glsx.plat.feign.config;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestConfig {
@Bean
@LoadBalanced
public RestTemplate getRestTemplate() {
return new RestTemplate();
}
}
......@@ -9,7 +9,6 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import javax.annotation.Nullable;
import java.lang.reflect.Method;
import java.util.Objects;
......@@ -21,7 +20,6 @@ public class FunFeignFallback<T> implements MethodInterceptor {
private final String targetName;
private final Throwable cause;
@Nullable
@Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
String errorMessage = cause.getMessage();
......
package com.glsx.plat.feign.fallback;
import com.alibaba.cloud.sentinel.feign.SentinelFeign;
import com.alibaba.csp.sentinel.SphU;
import feign.Feign;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import com.alibaba.cloud.sentinel.feign.FunSentinelFeign;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
@Configuration
//@Configuration
public class FunFeignFallbackConfiguration {
@Bean
......@@ -19,7 +19,7 @@ public class FunFeignFallbackConfiguration {
@ConditionalOnProperty(name = "feign.sentinel.enabled")
@Primary
public Feign.Builder feignSentinelBuilder() {
return FunSentinelFeign.builder();
return SentinelFeign.builder();
}
}
......@@ -47,6 +47,10 @@
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
</exclusions>
</dependency>
......
......@@ -16,6 +16,13 @@
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<exclusions>
<!-- 将ribbon排除 -->
<exclusion>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!