Skip to content

Commit 154a21a

Browse files
committed
fix:ByteBuddyProxyFactory创建RpcClient的时候进行复用,减小了对象反复进行分配(尤其是在高频的RPC场景下)
1 parent 5b7f27b commit 154a21a

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

rpc-core/src/main/java/com/xiaoyu/rpc/core/client/ByteBuddyProxyFactory.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,17 @@
1414

1515
public class ByteBuddyProxyFactory implements ProxyFactory {
1616

17+
private final RpcClient rpcClient;
18+
19+
public ByteBuddyProxyFactory() {
20+
this.rpcClient = new RpcClient();
21+
}
22+
1723
@Override
1824
@SuppressWarnings("unchecked")
1925
public <T> T getProxy(Class<T> clazz) {
2026
try {
21-
return (T) new ByteBuddy()
22-
.subclass(clazz)
23-
.method(ElementMatchers.any())
27+
return (T) new ByteBuddy().subclass(clazz).method(ElementMatchers.any())
2428
.intercept(InvocationHandlerAdapter.of(new InvocationHandler() {
2529
@Override
2630
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
@@ -46,14 +50,9 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
4650
}
4751

4852
RpcRequest request = builder.build();
49-
return new RpcClient().sendRequest(request, method.getReturnType());
53+
return rpcClient.sendRequest(request, method.getReturnType());
5054
}
51-
}))
52-
.make()
53-
.load(clazz.getClassLoader())
54-
.getLoaded()
55-
.getConstructor()
56-
.newInstance();
55+
})).make().load(clazz.getClassLoader()).getLoaded().getConstructor().newInstance();
5756
} catch (Exception e) {
5857
throw new RuntimeException("ByteBuddy代理创建失败", e);
5958
}

0 commit comments

Comments
 (0)