Skip to content

Commit 8b53029

Browse files
committed
rpc 统一异常处理 & 服务调用范型改造
1 parent 7fc6668 commit 8b53029

6 files changed

Lines changed: 76 additions & 7 deletions

File tree

07rpc/rpc01/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@
4848
</exclusion>
4949
</exclusions>
5050
</dependency>
51+
52+
<dependency>
53+
<groupId>org.projectlombok</groupId>
54+
<artifactId>lombok</artifactId>
55+
</dependency>
5156
</dependencies>
5257

5358
</project>

07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/api/RpcfxResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
public interface RpcfxResolver {
44

5-
Object resolve(String serviceClass);
5+
<T> T resolve(String serviceClass);
66

77
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package io.kimmking.rpcfx.common;
2+
3+
/**
4+
* 业务异常
5+
*/
6+
public class BusinessException extends RuntimeException {
7+
static final long serialVersionUID = 1352244900202885015L;
8+
9+
public BusinessException(String msg) {
10+
super(msg);
11+
}
12+
13+
public BusinessException(String msg, Throwable cause) {
14+
super(msg, cause);
15+
}
16+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package io.kimmking.rpcfx.common;
2+
3+
/**
4+
* 远程调用异常
5+
*/
6+
public class RpcfxException extends RuntimeException {
7+
8+
static final long serialVersionUID = -472231417416444970L;
9+
10+
/**
11+
* This field holds the target if the
12+
* InvocationTargetException(Throwable target) constructor was
13+
* used to instantiate the object
14+
*
15+
* @serial
16+
*
17+
*/
18+
private Throwable target;
19+
20+
public RpcfxException(String msg) {
21+
super(msg);
22+
}
23+
24+
public RpcfxException(Throwable target, String msg) {
25+
super(msg);
26+
this.target = target;
27+
}
28+
}

07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/server/RpcfxInvoker.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@
55
import io.kimmking.rpcfx.api.RpcfxRequest;
66
import io.kimmking.rpcfx.api.RpcfxResolver;
77
import io.kimmking.rpcfx.api.RpcfxResponse;
8+
import io.kimmking.rpcfx.common.RpcfxException;
9+
import lombok.extern.slf4j.Slf4j;
810

911
import java.lang.reflect.InvocationTargetException;
1012
import java.lang.reflect.Method;
1113
import java.util.Arrays;
1214

15+
@Slf4j
1316
public class RpcfxInvoker {
1417

1518
private RpcfxResolver resolver;
1619

17-
public RpcfxInvoker(RpcfxResolver resolver){
20+
public RpcfxInvoker(RpcfxResolver resolver) {
1821
this.resolver = resolver;
1922
}
2023

@@ -23,6 +26,7 @@ public RpcfxResponse invoke(RpcfxRequest request) {
2326
String serviceClass = request.getServiceClass();
2427

2528
// 作业1:改成泛型和反射
29+
// TODO 处理beanException
2630
Object service = resolver.resolve(serviceClass);//this.applicationContext.getBean(serviceClass);
2731

2832
try {
@@ -32,14 +36,21 @@ public RpcfxResponse invoke(RpcfxRequest request) {
3236
response.setResult(JSON.toJSONString(result, SerializerFeature.WriteClassName));
3337
response.setStatus(true);
3438
return response;
35-
} catch ( IllegalAccessException | InvocationTargetException e) {
39+
} catch (IllegalAccessException | InvocationTargetException e) {
3640

3741
// 3.Xstream
3842

3943
// 2.封装一个统一的RpcfxException
4044
// 客户端也需要判断异常
41-
e.printStackTrace();
42-
response.setException(e);
45+
String msg = "invoke error: " + request.getServiceClass() + "." + request.getMethod();
46+
log.error(msg, e);
47+
RpcfxException rpcfxException;
48+
if (e instanceof InvocationTargetException) {
49+
rpcfxException = new RpcfxException(((InvocationTargetException)e).getTargetException(), msg);
50+
} else {
51+
rpcfxException = new RpcfxException(e, msg);
52+
}
53+
response.setException(rpcfxException);
4354
response.setStatus(false);
4455
return response;
4556
}

07rpc/rpc01/rpcfx-demo-provider/src/main/java/io/kimmking/rpcfx/demo/provider/DemoResolver.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.kimmking.rpcfx.demo.provider;
22

33
import io.kimmking.rpcfx.api.RpcfxResolver;
4+
import io.kimmking.rpcfx.common.BusinessException;
5+
import org.springframework.beans.BeansException;
46
import org.springframework.context.ApplicationContext;
57
import org.springframework.context.ApplicationContextAware;
68

@@ -14,7 +16,14 @@ public void setApplicationContext(ApplicationContext applicationContext) {
1416
}
1517

1618
@Override
17-
public Object resolve(String serviceClass) {
18-
return this.applicationContext.getBean(serviceClass);
19+
public <T> T resolve(String serviceClass) {
20+
Class<T> clazz;
21+
try {
22+
clazz = (Class<T>) Class.forName(serviceClass);
23+
} catch (ClassNotFoundException e) {
24+
throw new BusinessException("illegalArgument of serviceClass: " + serviceClass);
25+
}
26+
27+
return this.applicationContext.getBean(clazz);
1928
}
2029
}

0 commit comments

Comments
 (0)