diff --git a/core-impl/client/src/main/java/com/alipay/sofa/rpc/client/FailoverCluster.java b/core-impl/client/src/main/java/com/alipay/sofa/rpc/client/FailoverCluster.java index ec2d5dd95..14e317bfd 100644 --- a/core-impl/client/src/main/java/com/alipay/sofa/rpc/client/FailoverCluster.java +++ b/core-impl/client/src/main/java/com/alipay/sofa/rpc/client/FailoverCluster.java @@ -57,6 +57,8 @@ public FailoverCluster(ConsumerBootstrap consumerBootstrap) { public SofaResponse doInvoke(SofaRequest request) throws SofaRpcException { String methodName = request.getMethodName(); int retries = consumerConfig.getMethodRetries(methodName); + // 用户定义的异常 + List> customerExceptions = consumerConfig.getCustomerExceptions(); int time = 0; // 异常日志 SofaRpcException throwable = null; @@ -95,7 +97,14 @@ public SofaResponse doInvoke(SofaRequest request) throws SofaRpcException { throw e; } } - } catch (Exception e) { // 其它异常不重试 + } catch (Exception e) { + // 执行用户自定义异常重试 + for (Class exceptionClass : customerExceptions) { + if (exceptionClass.isInstance(e)) { + time++; + } + } + // 其它异常不重试 throw new SofaRpcException(RpcErrorType.CLIENT_UNDECLARED_ERROR, "Failed to call " + request.getInterfaceName() + "." + request.getMethodName() + " on remote server: " + providerInfo + ", cause by unknown exception: " diff --git a/core/api/src/main/java/com/alipay/sofa/rpc/common/RpcOptions.java b/core/api/src/main/java/com/alipay/sofa/rpc/common/RpcOptions.java index c083625cc..c31875361 100644 --- a/core/api/src/main/java/com/alipay/sofa/rpc/common/RpcOptions.java +++ b/core/api/src/main/java/com/alipay/sofa/rpc/common/RpcOptions.java @@ -299,6 +299,10 @@ public class RpcOptions { * 默认失败重试次数 */ public static final String CONSUMER_RETRIES = "consumer.retries"; + /** + * 用户自定义异常集合 + */ + public static final String CONSUMER_EXCEPTIONS = "consumer.exceptions"; /** * 默认是否异步 */ diff --git a/core/api/src/main/java/com/alipay/sofa/rpc/config/ConsumerConfig.java b/core/api/src/main/java/com/alipay/sofa/rpc/config/ConsumerConfig.java index 1b72bc901..e22d61bb6 100644 --- a/core/api/src/main/java/com/alipay/sofa/rpc/config/ConsumerConfig.java +++ b/core/api/src/main/java/com/alipay/sofa/rpc/config/ConsumerConfig.java @@ -37,7 +37,9 @@ import static com.alipay.sofa.rpc.common.RpcConfigs.getBooleanValue; import static com.alipay.sofa.rpc.common.RpcConfigs.getIntValue; +import static com.alipay.sofa.rpc.common.RpcConfigs.getListValue; import static com.alipay.sofa.rpc.common.RpcConfigs.getStringValue; +import static com.alipay.sofa.rpc.common.RpcOptions.CONSUMER_EXCEPTIONS; import static com.alipay.sofa.rpc.common.RpcOptions.CONSUMER_REJECTED_EXECUTION_POLICY; import static com.alipay.sofa.rpc.common.RpcOptions.CONSUMER_ADDRESS_HOLDER; import static com.alipay.sofa.rpc.common.RpcOptions.CONSUMER_ADDRESS_WAIT; @@ -225,6 +227,11 @@ public class ConsumerConfig extends AbstractInterfaceConfig customerExceptions = getListValue(CONSUMER_EXCEPTIONS); + /** * 接口下每方法的最大可并行执行请求数,配置-1关闭并发过滤器,等于0表示开启过滤但是不限制 */ @@ -442,6 +449,14 @@ public ConsumerConfig setRetries(int retries) { return this; } + public List getCustomerExceptions() { + return customerExceptions; + } + + public void setCustomerExceptions(List customerExceptions) { + this.customerExceptions = customerExceptions; + } + /** * Gets connection holder. * diff --git a/core/common/src/main/resources/com/alipay/sofa/rpc/common/rpc-config-default.json b/core/common/src/main/resources/com/alipay/sofa/rpc/common/rpc-config-default.json index 6ad8f836f..7656d12bb 100644 --- a/core/common/src/main/resources/com/alipay/sofa/rpc/common/rpc-config-default.json +++ b/core/common/src/main/resources/com/alipay/sofa/rpc/common/rpc-config-default.json @@ -171,6 +171,8 @@ PS:大家也看到了,本JSON文档是支持注释的,而标准JSON是不支 "consumer.loadBalancer": "auto", //默认失败重试次数 "consumer.retries": 0, + // 用户自定义异常集合, 默认为空 + "consumer.exceptions": [], //接口下每方法的最大可并行执行请求数,配置-1关闭并发过滤器,等于0表示开启过滤但是不限制 "consumer.concurrents": 0, // 默认是否异步 diff --git a/core/exception/src/main/java/com/alipay/sofa/rpc/core/exception/RpcErrorType.java b/core/exception/src/main/java/com/alipay/sofa/rpc/core/exception/RpcErrorType.java index 5356e1bbd..d5d96a167 100644 --- a/core/exception/src/main/java/com/alipay/sofa/rpc/core/exception/RpcErrorType.java +++ b/core/exception/src/main/java/com/alipay/sofa/rpc/core/exception/RpcErrorType.java @@ -102,4 +102,9 @@ public class RpcErrorType { * 客户端未定义异常 */ public static final int CLIENT_UNDECLARED_ERROR = 299; + + /** + * 用户自定义异常 + */ + public static final int CUSTOMER_DESIGN_ERROR = 310; }