mirror of
http://git.pig4cloud.com/pig/pigx.git
synced 2026-11-09 04:49:02 +08:00
⏪ 回滚feign 自动降级的逻辑,方便调用方异常处理
This commit is contained in:
1 parent
3ca88753a9
commit
3226120411
3 files changed
+45
-19
No files matched your search
+39
-18
@@ -21,12 +21,14 @@ import com.alibaba.cloud.sentinel.feign.SentinelFeignAutoConfiguration;
|
||||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler;
|
||||
import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.RequestOriginParser;
|
||||
import com.pig4cloud.pigx.common.sentinel.feign.PigxSentinelFeign;
|
||||
import com.pig4cloud.pigx.common.sentinel.handle.GlobalBizExceptionHandler;
|
||||
import com.pig4cloud.pigx.common.sentinel.handle.PigxUrlBlockHandler;
|
||||
import com.pig4cloud.pigx.common.sentinel.parser.PigxHeaderRequestOriginParser;
|
||||
import feign.Feign;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Scope;
|
||||
@@ -39,26 +41,45 @@ import org.springframework.context.annotation.Scope;
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@AutoConfigureBefore(SentinelFeignAutoConfiguration.class)
|
||||
public class SentinelAutoConfiguration {
|
||||
public class PigxSentinelAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@Scope("prototype")
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnProperty(name = "feign.sentinel.enabled")
|
||||
public Feign.Builder feignSentinelBuilder() {
|
||||
return PigxSentinelFeign.builder();
|
||||
}
|
||||
@Bean
|
||||
@Scope("prototype")
|
||||
@ConditionalOnMissingBean
|
||||
@ConditionalOnProperty(name = "spring.cloud.openfeign.sentinel.enabled",matchIfMissing = true)
|
||||
public Feign.Builder feignSentinelBuilder() {
|
||||
return PigxSentinelFeign.builder();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public BlockExceptionHandler blockExceptionHandler() {
|
||||
return new PigxUrlBlockHandler();
|
||||
}
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
/**
|
||||
* 创建一个块异常处理器实例
|
||||
* 如果没有定义块异常处理器,则创建一个PigxUrlBlockHandler实例
|
||||
*/
|
||||
public BlockExceptionHandler blockExceptionHandler() {
|
||||
return new PigxUrlBlockHandler();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
/**
|
||||
* 创建一个请求源解析器实例
|
||||
* 如果没有定义请求源解析器,则创建一个PigxHeaderRequestOriginParser实例
|
||||
*/
|
||||
public RequestOriginParser requestOriginParser() {
|
||||
return new PigxHeaderRequestOriginParser();
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个全局业务异常处理器实例
|
||||
* 用于处理全局的业务异常
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
|
||||
public GlobalBizExceptionHandler globalBizExceptionHandler() {
|
||||
return new GlobalBizExceptionHandler();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public RequestOriginParser requestOriginParser() {
|
||||
return new PigxHeaderRequestOriginParser();
|
||||
}
|
||||
|
||||
}
|
||||
+5
@@ -8,6 +8,7 @@ import com.alibaba.csp.sentinel.SphU;
|
||||
import com.alibaba.csp.sentinel.Tracer;
|
||||
import com.alibaba.csp.sentinel.context.ContextUtil;
|
||||
import com.alibaba.csp.sentinel.slots.block.BlockException;
|
||||
import com.pig4cloud.pigx.common.core.util.R;
|
||||
import feign.Feign;
|
||||
import feign.InvocationHandlerFactory;
|
||||
import feign.MethodMetadata;
|
||||
@@ -118,6 +119,10 @@ public class PigxSentinelInvocationHandler implements InvocationHandler {
|
||||
throw new AssertionError(e.getCause());
|
||||
}
|
||||
}
|
||||
else if (R.class == method.getReturnType()) {
|
||||
log.error("feign 服务间调用异常", ex);
|
||||
return R.failed(ex.getLocalizedMessage());
|
||||
}
|
||||
else {
|
||||
throw ex;
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,2 +1,2 @@
|
||||
com.pig4cloud.pigx.common.sentinel.SentinelAutoConfiguration
|
||||
com.pig4cloud.pigx.common.sentinel.PigxSentinelAutoConfiguration
|
||||
com.pig4cloud.pigx.common.sentinel.handle.GlobalBizExceptionHandler
|
||||
Reference in new issue
Block a user