Skip to content

Commit

Permalink
support customer exception
Browse files Browse the repository at this point in the history
  • Loading branch information
呈铭 committed Apr 16, 2024
1 parent 91e73b9 commit 2812056
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
import java.util.ArrayList;
import java.util.List;

import static com.alipay.sofa.rpc.common.RpcConfigs.getListValue;
import static com.alipay.sofa.rpc.common.RpcOptions.CONSUMER_EXCEPTIONS;

/**
* 故障转移,支持重试和指定地址调用
*
Expand All @@ -60,7 +57,8 @@ public FailoverCluster(ConsumerBootstrap consumerBootstrap) {
public SofaResponse doInvoke(SofaRequest request) throws SofaRpcException {
String methodName = request.getMethodName();
int retries = consumerConfig.getMethodRetries(methodName);
consumerConfig.
// 用户定义的异常
List<Class<? extends Throwable>> customerExceptions = consumerConfig.getCustomerExceptions();
int time = 0;
// 异常日志
SofaRpcException throwable = null;
Expand Down Expand Up @@ -99,7 +97,14 @@ public SofaResponse doInvoke(SofaRequest request) throws SofaRpcException {
throw e;
}
}
} catch (Exception e) { // 其它异常不重试
} catch (Exception e) {
// 执行用户自定义异常重试
for (Class<? extends Throwable> 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: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -925,11 +925,6 @@ public int getMethodRetries(String methodName) {
getRetries());
}

public List<String> getCustomerExceptions() {
return getMethodConfigValue(methodName, RpcConstants.CONFIG_KEY_RETRIES,
getRetries());
}

/**
* Gets the timeout corresponding to the method name
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,9 @@ public class RpcErrorType {
* 客户端未定义异常
*/
public static final int CLIENT_UNDECLARED_ERROR = 299;

/**
* 用户自定义异常
*/
public static final int CUSTOMER_DESIGN_ERROR = 310;
}

0 comments on commit 2812056

Please sign in to comment.