diff --git a/pom.xml b/pom.xml
index d15f583..20fa41d 100644
--- a/pom.xml
+++ b/pom.xml
@@ -1,89 +1,99 @@
- 4.0.0
- xyz.anduo
- rpc
- war
- 0.0.1-SNAPSHOT
- rpc Maven Webapp
- http://maven.apache.org
-
-
-
- junit
- junit
- 4.11
- test
-
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+ 4.0.0
+ xyz.anduo
+ rpc
+ pom
+ 0.0.1-SNAPSHOT
+
+ rpc_client
+ rpc_sieve
+ rpc_common
+ rpc_simple
+
+ rpc
-
-
- org.slf4j
- slf4j-log4j12
- 1.7.7
-
+
+
+
+
+ junit
+ junit
+ 4.11
+ test
+
-
-
- org.springframework
- spring-context
- 3.2.12.RELEASE
-
-
- org.springframework
- spring-test
- 3.2.12.RELEASE
- test
-
+
+
+ org.slf4j
+ slf4j-log4j12
+ 1.7.7
+
-
-
- io.netty
- netty-all
- 4.0.24.Final
-
+
+
+ org.springframework
+ spring-context
+ 3.2.12.RELEASE
+
+
+ org.springframework
+ spring-test
+ 3.2.12.RELEASE
+ test
+
-
-
- com.dyuproject.protostuff
- protostuff-core
- 1.0.8
-
-
- com.dyuproject.protostuff
- protostuff-runtime
- 1.0.8
-
+
+
+ io.netty
+ netty-all
+ 4.0.24.Final
+
-
-
- org.apache.zookeeper
- zookeeper
- 3.4.6
-
+
+
+ com.dyuproject.protostuff
+ protostuff-core
+ 1.0.8
+
+
+ com.dyuproject.protostuff
+ protostuff-runtime
+ 1.0.8
+
-
-
- org.apache.commons
- commons-collections4
- 4.0
-
+
+
+ org.apache.zookeeper
+ zookeeper
+ 3.4.6
+
-
-
- org.objenesis
- objenesis
- 2.1
-
+
+
+ org.apache.commons
+ commons-collections4
+ 4.0
+
-
-
- cglib
- cglib
- 3.1
-
-
-
- rpc
-
+
+
+ org.objenesis
+ objenesis
+ 2.1
+
+
+
+
+ cglib
+ cglib
+ 3.1
+
+
+
+
+
+
+ rpc
+
diff --git a/rpc_client/pom.xml b/rpc_client/pom.xml
new file mode 100644
index 0000000..8a97722
--- /dev/null
+++ b/rpc_client/pom.xml
@@ -0,0 +1,71 @@
+
+
+
+ rpc
+ xyz.anduo
+ 0.0.1-SNAPSHOT
+
+ 4.0.0
+ rpc-client
+
+
+
+ xyz.anduo
+ rpc-common
+ 0.0.1-SNAPSHOT
+
+
+
+ org.slf4j
+ slf4j-log4j12
+
+
+
+
+ org.springframework
+ spring-context
+
+
+
+ io.netty
+ netty-all
+
+
+
+
+ com.dyuproject.protostuff
+ protostuff-core
+
+
+ com.dyuproject.protostuff
+ protostuff-runtime
+
+
+
+
+ org.apache.zookeeper
+ zookeeper
+
+
+
+
+ org.apache.commons
+ commons-collections4
+
+
+
+
+ org.objenesis
+ objenesis
+
+
+
+
+ cglib
+ cglib
+
+
+
+
\ No newline at end of file
diff --git a/src/main/java/xyz/anduo/rpc/client/RpcClient.java b/rpc_client/src/main/java/xyz/anduo/rpc/client/RpcClient.java
similarity index 84%
rename from src/main/java/xyz/anduo/rpc/client/RpcClient.java
rename to rpc_client/src/main/java/xyz/anduo/rpc/client/RpcClient.java
index ef01f02..b9b268d 100644
--- a/src/main/java/xyz/anduo/rpc/client/RpcClient.java
+++ b/rpc_client/src/main/java/xyz/anduo/rpc/client/RpcClient.java
@@ -13,11 +13,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import xyz.anduo.rpc.common.RpcDecoder;
+import xyz.anduo.rpc.common.RpcEncoder;
-import xyz.anduo.rpc.server.RpcDecoder;
-import xyz.anduo.rpc.server.RpcEncoder;
-import xyz.anduo.rpc.server.RpcRequest;
-import xyz.anduo.rpc.server.RpcResponse;
public class RpcClient extends SimpleChannelInboundHandler {
@@ -38,7 +36,6 @@ public RpcClient(String host, int port) {
@Override
public void channelRead0(ChannelHandlerContext ctx, RpcResponse response) throws Exception {
this.response = response;
-
synchronized (obj) {
obj.notifyAll(); // 收到响应,唤醒线程
}
@@ -57,11 +54,8 @@ public RpcResponse send(RpcRequest request) throws Exception {
bootstrap.group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer() {
@Override
public void initChannel(SocketChannel channel) throws Exception {
- channel.pipeline().addLast(new RpcEncoder(RpcRequest.class)) // 将
- // RPC
- // 请求进行编码(为了发送请求)
- .addLast(new RpcDecoder(RpcResponse.class)) // 将 RPC
- // 响应进行解码(为了处理响应)
+ channel.pipeline().addLast(new RpcEncoder(RpcRequest.class)) // 将 RPC 请求进行编码(为了发送请求)
+ .addLast(new RpcDecoder(RpcResponse.class)) // 将 RPC 响应进行解码(为了处理响应)
.addLast(RpcClient.this); // 使用 RpcClient 发送 RPC 请求
}
}).option(ChannelOption.SO_KEEPALIVE, true);
diff --git a/rpc_client/src/main/java/xyz/anduo/rpc/client/RpcProxy.java b/rpc_client/src/main/java/xyz/anduo/rpc/client/RpcProxy.java
new file mode 100644
index 0000000..ccc5fae
--- /dev/null
+++ b/rpc_client/src/main/java/xyz/anduo/rpc/client/RpcProxy.java
@@ -0,0 +1,51 @@
+package xyz.anduo.rpc.client;
+
+import java.lang.reflect.Method;
+import java.util.UUID;
+
+import net.sf.cglib.proxy.InvocationHandler;
+import net.sf.cglib.proxy.Proxy;
+
+public class RpcProxy {
+ private String serverAddress;
+ private ServiceDiscovery serviceDiscovery;
+
+ public RpcProxy(String serverAddress) {
+ this.serverAddress = serverAddress;
+ }
+
+ public RpcProxy(ServiceDiscovery serviceDiscovery) {
+ this.serviceDiscovery = serviceDiscovery;
+ }
+
+ @SuppressWarnings("unchecked")
+ public T create(Class> interfaceClass) {
+ return (T) Proxy.newProxyInstance(interfaceClass.getClassLoader(), new Class>[]{interfaceClass},
+ new InvocationHandler() {
+ @Override
+ public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+ RpcRequest request = new RpcRequest(); // 创建并初始化 RPC 请求
+ request.setRequestId(UUID.randomUUID().toString());
+ request.setClassName(method.getDeclaringClass().getName());
+ request.setMethodName(method.getName());
+ request.setParameterTypes(method.getParameterTypes());
+ request.setParameters(args);
+
+ if (serviceDiscovery != null) {
+ serverAddress = serviceDiscovery.discover(); // 发现服务
+ }
+ String[] array = serverAddress.split(":");
+ String host = array[0];
+ int port = Integer.parseInt(array[1]);
+
+ RpcClient client = new RpcClient(host, port); // 初始化 RPC 客户端
+ RpcResponse response = client.send(request); // 通过 RPC客户端发送RPC请求并获取RPC响应
+ if (response.isError()) {
+ throw response.getError();
+ } else {
+ return response.getResult();
+ }
+ }
+ });
+ }
+}
diff --git a/src/main/java/xyz/anduo/rpc/server/RpcRequest.java b/rpc_client/src/main/java/xyz/anduo/rpc/client/RpcRequest.java
similarity index 96%
rename from src/main/java/xyz/anduo/rpc/server/RpcRequest.java
rename to rpc_client/src/main/java/xyz/anduo/rpc/client/RpcRequest.java
index 2826751..deff16a 100644
--- a/src/main/java/xyz/anduo/rpc/server/RpcRequest.java
+++ b/rpc_client/src/main/java/xyz/anduo/rpc/client/RpcRequest.java
@@ -1,4 +1,4 @@
-package xyz.anduo.rpc.server;
+package xyz.anduo.rpc.client;
public class RpcRequest {
private String requestId;
diff --git a/src/main/java/xyz/anduo/rpc/server/RpcResponse.java b/rpc_client/src/main/java/xyz/anduo/rpc/client/RpcResponse.java
similarity index 94%
rename from src/main/java/xyz/anduo/rpc/server/RpcResponse.java
rename to rpc_client/src/main/java/xyz/anduo/rpc/client/RpcResponse.java
index 89eaf18..ed4443d 100644
--- a/src/main/java/xyz/anduo/rpc/server/RpcResponse.java
+++ b/rpc_client/src/main/java/xyz/anduo/rpc/client/RpcResponse.java
@@ -1,4 +1,4 @@
-package xyz.anduo.rpc.server;
+package xyz.anduo.rpc.client;
public class RpcResponse {
private String requestId;
diff --git a/src/main/java/xyz/anduo/rpc/client/ServiceDiscovery.java b/rpc_client/src/main/java/xyz/anduo/rpc/client/ServiceDiscovery.java
similarity index 95%
rename from src/main/java/xyz/anduo/rpc/client/ServiceDiscovery.java
rename to rpc_client/src/main/java/xyz/anduo/rpc/client/ServiceDiscovery.java
index 4e2c801..b05d588 100644
--- a/src/main/java/xyz/anduo/rpc/client/ServiceDiscovery.java
+++ b/rpc_client/src/main/java/xyz/anduo/rpc/client/ServiceDiscovery.java
@@ -13,15 +13,15 @@
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import xyz.anduo.rpc.common.Constant;
-import xyz.anduo.rpc.server.Constant;
public class ServiceDiscovery {
private static final Logger LOGGER = LoggerFactory.getLogger(ServiceDiscovery.class);
private CountDownLatch latch = new CountDownLatch(1);
- private volatile List dataList = new ArrayList<>();
+ private volatile List dataList = new ArrayList();
private String registryAddress;
diff --git a/src/main/resources/client-config.properties b/rpc_client/src/main/resources/client-config.properties
similarity index 100%
rename from src/main/resources/client-config.properties
rename to rpc_client/src/main/resources/client-config.properties
diff --git a/rpc_client/src/main/resources/spring-client.xml b/rpc_client/src/main/resources/spring-client.xml
new file mode 100644
index 0000000..842a1ff
--- /dev/null
+++ b/rpc_client/src/main/resources/spring-client.xml
@@ -0,0 +1,18 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/rpc_common/pom.xml b/rpc_common/pom.xml
new file mode 100644
index 0000000..7d36a7d
--- /dev/null
+++ b/rpc_common/pom.xml
@@ -0,0 +1,44 @@
+
+
+
+ rpc
+ xyz.anduo
+ 0.0.1-SNAPSHOT
+
+ 4.0.0
+
+
+ rpc-common
+
+
+
+ org.slf4j
+ slf4j-log4j12
+
+
+
+ io.netty
+ netty-all
+
+
+
+ com.dyuproject.protostuff
+ protostuff-core
+
+
+ com.dyuproject.protostuff
+ protostuff-runtime
+
+
+
+ org.apache.zookeeper
+ zookeeper
+
+
+ org.objenesis
+ objenesis
+
+
+
diff --git a/src/main/java/xyz/anduo/rpc/server/Constant.java b/rpc_common/src/main/java/xyz/anduo/rpc/common/Constant.java
similarity index 83%
rename from src/main/java/xyz/anduo/rpc/server/Constant.java
rename to rpc_common/src/main/java/xyz/anduo/rpc/common/Constant.java
index 6ca23fa..f31f444 100644
--- a/src/main/java/xyz/anduo/rpc/server/Constant.java
+++ b/rpc_common/src/main/java/xyz/anduo/rpc/common/Constant.java
@@ -1,4 +1,4 @@
-package xyz.anduo.rpc.server;
+package xyz.anduo.rpc.common;
public interface Constant {
int ZK_SESSION_TIMEOUT = 5000;
diff --git a/src/main/java/xyz/anduo/rpc/server/RpcDecoder.java b/rpc_common/src/main/java/xyz/anduo/rpc/common/RpcDecoder.java
similarity index 55%
rename from src/main/java/xyz/anduo/rpc/server/RpcDecoder.java
rename to rpc_common/src/main/java/xyz/anduo/rpc/common/RpcDecoder.java
index cd994f8..cc66c25 100644
--- a/src/main/java/xyz/anduo/rpc/server/RpcDecoder.java
+++ b/rpc_common/src/main/java/xyz/anduo/rpc/common/RpcDecoder.java
@@ -1,8 +1,9 @@
-package xyz.anduo.rpc.server;
+package xyz.anduo.rpc.common;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
+import io.netty.handler.codec.MessageToByteEncoder;
import java.util.List;
@@ -33,4 +34,22 @@ public final void decode(ChannelHandlerContext ctx, ByteBuf in, List