8000 用Cglib方式代替proxy · JavaCourse00/JavaCourseCodes@31970a4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 31970a4

Browse files
committed
用Cglib方式代替proxy
1 parent 1fa8b77 commit 31970a4

File tree

1 file changed

+29
-19
lines changed
  • 07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/client

1 file changed

+29
-19
lines changed

07rpc/rpc01/rpcfx-core/src/main/java/io/kimmking/rpcfx/client/Rpcfx.java

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
import okhttp3.OkHttpClient;
1313
import okhttp3.Request;
1414
import okhttp3.RequestBody;
15+
import org.springframework.cglib.proxy.Enhancer;
16+
import org.springframework.cglib.proxy.MethodInterceptor;
17+
import org.springframework.cglib.proxy.MethodProxy;
1518

1619
import java.io.IOException;
1720
import java.lang.reflect.InvocationHandler;
@@ -27,13 +30,17 @@ public final class Rpcfx {
2730
}
2831

2932
public static <T> T create(final Class<T> serviceClass, final String url) {
30-
33+
//Gglib方式
34+
Enhancer enhancer = new Enhancer();
35+
enhancer.setCallback(new RpcfxInvocationHandler(serviceClass, url));
36+
enhancer.setSuperclass(serviceClass);
37+
return (T) enhancer.create();
3138
// 0. 替换动态代理 -> AOP
32-
return (T) Proxy.newProxyInstance(Rpcfx.class.getClassLoader(), new Class[]{serviceClass}, new RpcfxInvocationHandler(serviceClass, url));
39+
//return (T) Proxy.newProxyInstance(Rpcfx.class.getClassLoader(), new Class[]{serviceClass}, new RpcfxInvocationHandler(serviceClass, url));
3340

3441
}
3542

36-
public static class RpcfxInvocationHandler implements InvocationHandler {
43+
public static class RpcfxInvocationHandler implements InvocationHandler, MethodInterceptor {
3744

3845
public static final MediaType JSONTYPE = MediaType.get("application/json; charset=utf-8");
3946

@@ -52,23 +59,15 @@ public <T> RpcfxInvocationHandler(Class<T> serviceClass, String url) {
5259

5360
@Override
5461
public Object invoke(Object proxy, Method method, Object[] params) throws Throwable {
55-
56-
RpcfxRequest request = new RpcfxRequest();
57-
request.setServiceClass(this.serviceClass.getName());
58-
request.setMethod(method.getName());
59-
request.setParams(params);
60-
61-
RpcfxResponse response = post(request, url);
62-
// 这里判断response.status,处理异常
63-
// 考虑封装一个全局的RpcfxException
64-
if (!response.isStatus()) {
65-
throw new RpcfxException("invoke error", response.getException());
66-
}
67-
return XStreamUtils.fromBean(stream, response.getResult().toString());
62+
return post(method, params, url);
6863
}
6964

70-
private RpcfxResponse post(RpcfxRequest req, String url) throws IOException {
71-
String reqJson = JSON.toJSONString(req);
65+
private Object post(Method method, Object[] params, String url) throws IOException, RpcfxException {
66+
RpcfxRequest rpcfxRequest = new RpcfxRequest();
67+
rpcfxRequest.setServiceClass(this.serviceClass.getName());
68+
rpcfxRequest.setMethod(method.getName());
69+
rpcfxRequest.setParams(params);
70+
String reqJson = JSON.toJSONString(rpcfxRequest);
7271
System.out.println("req json: " + reqJson);
7372

7473
// 1.可以复用client
@@ -80,7 +79,18 @@ private RpcfxResponse post(RpcfxRequest req, String url) throws IOException {
8079
.build();
8180
String respJson = client.newCall(request).execute().body().string();
8281
System.out.println("resp json: " + respJson);
83-
return JSON.parseObject(respJson, RpcfxResponse.class);
82+
RpcfxResponse response = JSON.parseObject(respJson, RpcfxResponse.class);
83+
// 这里判断response.status,处理异常
84+
// 考虑封装一个全局的RpcfxException
85+
if (!response.isStatus()) {
86+
throw new RpcfxException("invoke error", response.getException());
87+
}
88+
return XStreamUtils.fromBean(stream, response.getResult().toString());
89+
}
90+
91+
@Override
92+
public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
93+
return post(method, objects, url);
8494
}
8595
}
8696
}

0 commit comments

Comments
 (0)
0