Commit 2d90978c by 刘宇峰

框架完善

1 parent 18ccf57f
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
<parent> <parent>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.11</version> <version>2.6.13</version>
</parent> </parent>
<groupId>com.glsx</groupId> <groupId>com.glsx</groupId>
...@@ -24,8 +24,8 @@ ...@@ -24,8 +24,8 @@
<maven.compiler.target>8</maven.compiler.target> <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--> <!-- 版本说明: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.version>2021.0.5</spring-cloud.version>
<spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version> <spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
<revision>2.0.0-SNAPSHOT</revision> <revision>2.0.0-SNAPSHOT</revision>
<!-- 本地发布服务器 --> <!-- 本地发布服务器 -->
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
<properties> <properties>
<curator.version>2.10.0</curator.version> <curator.version>2.10.0</curator.version>
<guava.version>18.0</guava.version>
</properties> </properties>
<dependencies> <dependencies>
......
...@@ -11,48 +11,27 @@ ...@@ -11,48 +11,27 @@
<artifactId>rest-admin-feign</artifactId> <artifactId>rest-admin-feign</artifactId>
<properties>
<feign.form.version>3.8.0</feign.form.version>
</properties>
<dependencies> <dependencies>
<!-- feign -->
<dependency> <dependency>
<groupId>org.springframework.cloud</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId> <artifactId>spring-cloud-starter-openfeign</artifactId>
<exclusions>
<exclusion>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</exclusion>
</exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>com.glsx</groupId> <groupId>org.springframework.cloud</groupId>
<artifactId>rest-admin-sentinel</artifactId> <artifactId>spring-cloud-starter-loadbalancer</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.github.openfeign</groupId> <groupId>io.github.openfeign</groupId>
<artifactId>feign-httpclient</artifactId> <artifactId>feign-httpclient</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.github.openfeign</groupId> <groupId>io.github.openfeign</groupId>
<artifactId>feign-okhttp</artifactId> <artifactId>feign-okhttp</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.github.openfeign</groupId> <groupId>com.glsx</groupId>
<artifactId>feign-gson</artifactId> <artifactId>rest-admin-sentinel</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency> </dependency>
<!--json--> <!--json-->
<dependency> <dependency>
<groupId>com.alibaba</groupId> <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 \ No newline at end of file
...@@ -16,6 +16,7 @@ import org.springframework.beans.factory.ObjectFactory; ...@@ -16,6 +16,7 @@ import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters; 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.FeignAutoConfiguration;
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder; import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
import org.springframework.cloud.openfeign.support.SpringDecoder; import org.springframework.cloud.openfeign.support.SpringDecoder;
...@@ -33,7 +34,7 @@ import java.util.List; ...@@ -33,7 +34,7 @@ import java.util.List;
* @author payu * @author payu
*/ */
@Configuration @Configuration
@Import({FeignAutoConfiguration.class, OkHttpConfig.class}) @Import({FeignAutoConfiguration.class, LoadBalancerAutoConfiguration.class})
public class FeignConfig { public class FeignConfig {
@Autowired @Autowired
...@@ -68,13 +69,12 @@ public class FeignConfig { ...@@ -68,13 +69,12 @@ public class FeignConfig {
@Bean @Bean
public Encoder feignEncoder() { public Encoder feignEncoder() {
return new SpringEncoder(feignHttpMessageConverter()); return new SpringEncoder(messageConverters);
} }
@Bean @Bean
public Decoder feignDecoder() { public Decoder feignDecoder() {
// return new SpringDecoder(feignHttpMessageConverter()); return new OptionalDecoder(new ResponseEntityDecoder(new FeignResultDecoder(new SpringDecoder(messageConverters))));
return new OptionalDecoder(new ResponseEntityDecoder(new ResultDecoder(new SpringDecoder(feignHttpMessageConverter()))));
} }
private ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() { private ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() {
......
...@@ -2,10 +2,7 @@ package com.glsx.plat.feign.config; ...@@ -2,10 +2,7 @@ package com.glsx.plat.feign.config;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference; import com.alibaba.fastjson.TypeReference;
import com.glsx.plat.exception.BusinessException; import com.glsx.plat.exception.*;
import com.glsx.plat.exception.ExceptionInfo;
import com.glsx.plat.exception.ServiceException;
import com.glsx.plat.exception.SystemMessage;
import feign.FeignException; import feign.FeignException;
import feign.Response; import feign.Response;
import feign.Util; import feign.Util;
...@@ -36,6 +33,9 @@ public class FeignErrorDecoder implements ErrorDecoder { ...@@ -36,6 +33,9 @@ public class FeignErrorDecoder implements ErrorDecoder {
if (exception instanceof BusinessException) { if (exception instanceof BusinessException) {
log.error("业务异常:" + exception.getMessage()); log.error("业务异常:" + exception.getMessage());
} }
if (exception instanceof ResultException) {
log.error("业务异常:" + exception.getMessage());
}
return exception; return exception;
} }
} catch (Exception e) { } catch (Exception e) {
......
...@@ -12,17 +12,18 @@ import java.io.IOException; ...@@ -12,17 +12,18 @@ import java.io.IOException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.lang.reflect.Type; import java.lang.reflect.Type;
public class ResultDecoder implements Decoder { public class FeignResultDecoder implements Decoder {
private Decoder decoder; private Decoder decoder;
public ResultDecoder(Decoder decoder) { public FeignResultDecoder(Decoder decoder) {
this.decoder = decoder; this.decoder = decoder;
} }
@Override @Override
public Object decode(Response response, Type type) throws IOException, FeignException { public Object decode(Response response, Type type) throws IOException, FeignException {
Method method = response.request().requestTemplate().methodMetadata().method(); 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) { if (isResult) {
ParameterizedTypeImpl resultType = ParameterizedTypeImpl.make(Result.class, new Type[]{type}, null); ParameterizedTypeImpl resultType = ParameterizedTypeImpl.make(Result.class, new Type[]{type}, null);
Result<?> result = (Result<?>) this.decoder.decode(response, resultType); Result<?> result = (Result<?>) this.decoder.decode(response, resultType);
......
...@@ -16,9 +16,9 @@ import java.util.concurrent.TimeUnit; ...@@ -16,9 +16,9 @@ import java.util.concurrent.TimeUnit;
* 配置 okhttp 与连接池 * 配置 okhttp 与连接池
* ConnectionPool 默认创建5个线程,保持5分钟长连接 * ConnectionPool 默认创建5个线程,保持5分钟长连接
*/ */
@Configuration //@Configuration
@ConditionalOnClass(Feign.class) //@ConditionalOnClass(Feign.class)
@AutoConfigureBefore(FeignAutoConfiguration.class) //SpringBoot自动配置 //@AutoConfigureBefore(FeignAutoConfiguration.class) //SpringBoot自动配置
public class OkHttpConfig { public class OkHttpConfig {
@Resource @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; ...@@ -9,7 +9,6 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.proxy.MethodInterceptor; import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy; import org.springframework.cglib.proxy.MethodProxy;
import javax.annotation.Nullable;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Objects; import java.util.Objects;
...@@ -21,7 +20,6 @@ public class FunFeignFallback<T> implements MethodInterceptor { ...@@ -21,7 +20,6 @@ public class FunFeignFallback<T> implements MethodInterceptor {
private final String targetName; private final String targetName;
private final Throwable cause; private final Throwable cause;
@Nullable
@Override @Override
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable { public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
String errorMessage = cause.getMessage(); String errorMessage = cause.getMessage();
......
package com.glsx.plat.feign.fallback; package com.glsx.plat.feign.fallback;
import com.alibaba.cloud.sentinel.feign.SentinelFeign;
import com.alibaba.csp.sentinel.SphU; import com.alibaba.csp.sentinel.SphU;
import feign.Feign; import feign.Feign;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 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.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;
@Configuration //@Configuration
public class FunFeignFallbackConfiguration { public class FunFeignFallbackConfiguration {
@Bean @Bean
...@@ -19,7 +19,7 @@ public class FunFeignFallbackConfiguration { ...@@ -19,7 +19,7 @@ public class FunFeignFallbackConfiguration {
@ConditionalOnProperty(name = "feign.sentinel.enabled") @ConditionalOnProperty(name = "feign.sentinel.enabled")
@Primary @Primary
public Feign.Builder feignSentinelBuilder() { public Feign.Builder feignSentinelBuilder() {
return FunSentinelFeign.builder(); return SentinelFeign.builder();
} }
} }
...@@ -47,6 +47,10 @@ ...@@ -47,6 +47,10 @@
<groupId>org.mybatis</groupId> <groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId> <artifactId>mybatis</artifactId>
</exclusion> </exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
......
...@@ -16,6 +16,13 @@ ...@@ -16,6 +16,13 @@
<dependency> <dependency>
<groupId>com.alibaba.cloud</groupId> <groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> <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> </dependency>
</dependencies> </dependencies>
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!