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 out Object obj = SerializationUtil.deserialize(data, genericClass); out.add(obj); } + + public static class RpcEncoder extends MessageToByteEncoder { + + private Class genericClass; + + public RpcEncoder(Class genericClass) { + this.genericClass = genericClass; + } + + @Override + public void encode(ChannelHandlerContext ctx, Object in, ByteBuf out) throws Exception { + if (genericClass.isInstance(in)) { + byte[] data = SerializationUtil.serialize(in); + out.writeInt(data.length); + out.writeBytes(data); + } + } + } } diff --git a/rpc_common/src/main/java/xyz/anduo/rpc/common/RpcEncoder.java b/rpc_common/src/main/java/xyz/anduo/rpc/common/RpcEncoder.java new file mode 100644 index 0000000..f81060e --- /dev/null +++ b/rpc_common/src/main/java/xyz/anduo/rpc/common/RpcEncoder.java @@ -0,0 +1,32 @@ +// Copyright (C) 2015 meituan +// All rights reserved +package xyz.anduo.rpc.common; + +import io.netty.buffer.ByteBuf; +import io.netty.channel.ChannelHandlerContext; +import io.netty.handler.codec.MessageToByteEncoder; + +/** + * Summary: RPC加密 + * Author : anduo@qq.com + * Version: 1.0 + * Date : 15/4/26 + * time : 21:51 + */ +public class RpcEncoder extends MessageToByteEncoder { + + private Class genericClass; + + public RpcEncoder(Class genericClass) { + this.genericClass = genericClass; + } + + @Override + public void encode(ChannelHandlerContext ctx, Object in, ByteBuf out) throws Exception { + if (genericClass.isInstance(in)) { + byte[] data = SerializationUtil.serialize(in); + out.writeInt(data.length); + out.writeBytes(data); + } + } +} \ No newline at end of file diff --git a/rpc_common/src/main/java/xyz/anduo/rpc/common/SerializationUtil.java b/rpc_common/src/main/java/xyz/anduo/rpc/common/SerializationUtil.java new file mode 100644 index 0000000..c4fb4a5 --- /dev/null +++ b/rpc_common/src/main/java/xyz/anduo/rpc/common/SerializationUtil.java @@ -0,0 +1,58 @@ +package xyz.anduo.rpc.common; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.objenesis.Objenesis; +import org.objenesis.ObjenesisStd; + +import com.dyuproject.protostuff.LinkedBuffer; +import com.dyuproject.protostuff.ProtostuffIOUtil; +import com.dyuproject.protostuff.Schema; +import com.dyuproject.protostuff.runtime.RuntimeSchema; + +public class SerializationUtil { + private static Map, Schema> cachedSchema = new ConcurrentHashMap<>(); + + private static Objenesis objenesis = new ObjenesisStd(true); + + private SerializationUtil() { + } + + @SuppressWarnings("unchecked") + private static Schema getSchema(Class cls) { + Schema schema = (Schema) cachedSchema.get(cls); + if (schema == null) { + schema = RuntimeSchema.createFrom(cls); + if (schema != null) { + cachedSchema.put(cls, schema); + } + } + return schema; + } + + @SuppressWarnings("unchecked") + public static byte[] serialize(T obj) { + Class cls = (Class) obj.getClass(); + LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); + try { + Schema schema = getSchema(cls); + return ProtostuffIOUtil.toByteArray(obj, schema, buffer); + } catch (Exception e) { + throw new IllegalStateException(e.getMessage(), e); + } finally { + buffer.clear(); + } + } + + public static T deserialize(byte[] data, Class cls) { + try { + T message = (T) objenesis.newInstance(cls); + Schema schema = getSchema(cls); + ProtostuffIOUtil.mergeFrom(data, message, schema); + return message; + } catch (Exception e) { + throw new IllegalStateException(e.getMessage(), e); + } + } +} diff --git a/rpc_sieve/pom.xml b/rpc_sieve/pom.xml new file mode 100644 index 0000000..bed539c --- /dev/null +++ b/rpc_sieve/pom.xml @@ -0,0 +1,88 @@ + + + + rpc + xyz.anduo + 0.0.1-SNAPSHOT + + 4.0.0 + + rpc-sieve + + + + xyz.anduo + rpc-client + 0.0.1-SNAPSHOT + + + xyz.anduo + rcp-common + 0.0.1-SNAPSHOT + + + + junit + junit + + + + + org.slf4j + slf4j-log4j12 + + + + + org.springframework + spring-context + + + org.springframework + spring-test + + + + + 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/server/RpcBootstrap.java b/rpc_sieve/src/main/java/xyz/anduo/rpc/sieve/core/RpcBootstrap.java similarity index 87% rename from src/main/java/xyz/anduo/rpc/server/RpcBootstrap.java rename to rpc_sieve/src/main/java/xyz/anduo/rpc/sieve/core/RpcBootstrap.java index 1566e80..34a1323 100644 --- a/src/main/java/xyz/anduo/rpc/server/RpcBootstrap.java +++ b/rpc_sieve/src/main/java/xyz/anduo/rpc/sieve/core/RpcBootstrap.java @@ -1,4 +1,4 @@ -package xyz.anduo.rpc.server; +package xyz.anduo.rpc.sieve.core; import org.springframework.context.support.ClassPathXmlApplicationContext; diff --git a/src/main/java/xyz/anduo/rpc/server/RpcHandler.java b/rpc_sieve/src/main/java/xyz/anduo/rpc/sieve/core/RpcHandler.java similarity index 94% rename from src/main/java/xyz/anduo/rpc/server/RpcHandler.java rename to rpc_sieve/src/main/java/xyz/anduo/rpc/sieve/core/RpcHandler.java index 6662765..14a8708 100644 --- a/src/main/java/xyz/anduo/rpc/server/RpcHandler.java +++ b/rpc_sieve/src/main/java/xyz/anduo/rpc/sieve/core/RpcHandler.java @@ -1,4 +1,4 @@ -package xyz.anduo.rpc.server; +package xyz.anduo.rpc.sieve.core; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; @@ -11,6 +11,8 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import xyz.anduo.rpc.client.RpcRequest; +import xyz.anduo.rpc.client.RpcResponse; public class RpcHandler extends SimpleChannelInboundHandler { diff --git a/src/main/java/xyz/anduo/rpc/server/RpcServer.java b/rpc_sieve/src/main/java/xyz/anduo/rpc/sieve/core/RpcServer.java similarity index 87% rename from src/main/java/xyz/anduo/rpc/server/RpcServer.java rename to rpc_sieve/src/main/java/xyz/anduo/rpc/sieve/core/RpcServer.java index 3b52844..757e66d 100644 --- a/src/main/java/xyz/anduo/rpc/server/RpcServer.java +++ b/rpc_sieve/src/main/java/xyz/anduo/rpc/sieve/core/RpcServer.java @@ -1,4 +1,4 @@ -package xyz.anduo.rpc.server; +package xyz.anduo.rpc.sieve.core; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; @@ -20,7 +20,9 @@ import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; -import xyz.anduo.rpc.registry.ServiceRegistry; +import xyz.anduo.rpc.client.RpcRequest; +import xyz.anduo.rpc.client.RpcResponse; +import xyz.anduo.rpc.common.RpcDecoder; public class RpcServer implements ApplicationContextAware, InitializingBean { @@ -29,7 +31,7 @@ public class RpcServer implements ApplicationContextAware, InitializingBean { private String serverAddress; private ServiceRegistry serviceRegistry; - private Map handlerMap = new HashMap<>(); // 存放接口名与服务对象之间的映射关系 + private Map handlerMap = new HashMap(); // 存放接口名与服务对象之间的映射关系 public RpcServer(String serverAddress) { this.serverAddress = serverAddress; @@ -42,11 +44,8 @@ public RpcServer(String serverAddress, ServiceRegistry serviceRegistry) { @Override public void setApplicationContext(ApplicationContext ctx) throws BeansException { - Map serviceBeanMap = ctx.getBeansWithAnnotation(RpcService.class); // 获取所有带有 - // RpcService - // 注解的 - // Spring - // Bean + // 获取所有带有RpcService注解的SpringBean + Map serviceBeanMap = ctx.getBeansWithAnnotation(RpcService.class); if (MapUtils.isNotEmpty(serviceBeanMap)) { for (Object serviceBean : serviceBeanMap.values()) { String interfaceName = serviceBean.getClass().getAnnotation(RpcService.class).value().getName(); diff --git a/src/main/java/xyz/anduo/rpc/server/RpcService.java b/rpc_sieve/src/main/java/xyz/anduo/rpc/sieve/core/RpcService.java similarity index 91% rename from src/main/java/xyz/anduo/rpc/server/RpcService.java rename to rpc_sieve/src/main/java/xyz/anduo/rpc/sieve/core/RpcService.java index ddb4b3d..d5f7caa 100644 --- a/src/main/java/xyz/anduo/rpc/server/RpcService.java +++ b/rpc_sieve/src/main/java/xyz/anduo/rpc/sieve/core/RpcService.java @@ -1,4 +1,4 @@ -package xyz.anduo.rpc.server; +package xyz.anduo.rpc.sieve.core; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/rpc_sieve/src/main/java/xyz/anduo/rpc/sieve/core/ServiceRegistry.java b/rpc_sieve/src/main/java/xyz/anduo/rpc/sieve/core/ServiceRegistry.java new file mode 100644 index 0000000..82c98f7 --- /dev/null +++ b/rpc_sieve/src/main/java/xyz/anduo/rpc/sieve/core/ServiceRegistry.java @@ -0,0 +1,65 @@ +package xyz.anduo.rpc.sieve.core; + +import java.io.IOException; +import java.util.concurrent.CountDownLatch; + +import org.apache.zookeeper.CreateMode; +import org.apache.zookeeper.KeeperException; +import org.apache.zookeeper.WatchedEvent; +import org.apache.zookeeper.Watcher; +import org.apache.zookeeper.ZooDefs; +import org.apache.zookeeper.ZooKeeper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import xyz.anduo.rpc.common.Constant; + + +public class ServiceRegistry { + + private static final Logger LOGGER = LoggerFactory.getLogger(ServiceRegistry.class); + + private CountDownLatch latch = new CountDownLatch(1); + + private String registryAddress; + + public ServiceRegistry(String registryAddress) { + this.registryAddress = registryAddress; + } + + public void register(String data) { + if (data != null) { + ZooKeeper zk = connectServer(); + if (zk != null) { + createNode(zk, data); + } + } + } + + private ZooKeeper connectServer() { + ZooKeeper zk = null; + try { + zk = new ZooKeeper(registryAddress, Constant.ZK_SESSION_TIMEOUT, new Watcher() { + + public void process(WatchedEvent event) { + if (event.getState() == Event.KeeperState.SyncConnected) { + latch.countDown(); + } + } + }); + latch.await(); + } catch (IOException | InterruptedException e) { + LOGGER.error("", e); + } + return zk; + } + + private void createNode(ZooKeeper zk, String data) { + try { + byte[] bytes = data.getBytes(); + String path = zk.create(Constant.ZK_DATA_PATH, bytes, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); + LOGGER.debug("create zookeeper node ({} => {})", path, data); + } catch (KeeperException | InterruptedException e) { + LOGGER.error("", e); + } + } +} diff --git a/src/main/java/xyz/anduo/rpc/service/HelloService.java b/rpc_sieve/src/main/java/xyz/anduo/rpc/sieve/modules/simple/HelloService.java similarity index 59% rename from src/main/java/xyz/anduo/rpc/service/HelloService.java rename to rpc_sieve/src/main/java/xyz/anduo/rpc/sieve/modules/simple/HelloService.java index 1b150a7..ca752fe 100644 --- a/src/main/java/xyz/anduo/rpc/service/HelloService.java +++ b/rpc_sieve/src/main/java/xyz/anduo/rpc/sieve/modules/simple/HelloService.java @@ -1,4 +1,4 @@ -package xyz.anduo.rpc.service; +package xyz.anduo.rpc.sieve.modules.simple; public interface HelloService { diff --git a/src/main/java/xyz/anduo/rpc/service/impl/HelloServiceImpl.java b/rpc_sieve/src/main/java/xyz/anduo/rpc/sieve/modules/simple/impl/HelloServiceImpl.java similarity index 56% rename from src/main/java/xyz/anduo/rpc/service/impl/HelloServiceImpl.java rename to rpc_sieve/src/main/java/xyz/anduo/rpc/sieve/modules/simple/impl/HelloServiceImpl.java index d146e51..9f45025 100644 --- a/src/main/java/xyz/anduo/rpc/service/impl/HelloServiceImpl.java +++ b/rpc_sieve/src/main/java/xyz/anduo/rpc/sieve/modules/simple/impl/HelloServiceImpl.java @@ -1,7 +1,7 @@ -package xyz.anduo.rpc.service.impl; +package xyz.anduo.rpc.sieve.modules.simple.impl; -import xyz.anduo.rpc.server.RpcService; -import xyz.anduo.rpc.service.HelloService; +import xyz.anduo.rpc.sieve.core.RpcService; +import xyz.anduo.rpc.sieve.modules.simple.HelloService; // 指定远程接口 @RpcService(HelloService.class) diff --git a/src/main/resources/server-config.properties b/rpc_sieve/src/main/resources/server-config.properties similarity index 100% rename from src/main/resources/server-config.properties rename to rpc_sieve/src/main/resources/server-config.properties diff --git a/src/main/resources/spring-server.xml b/rpc_sieve/src/main/resources/spring-server.xml similarity index 90% rename from src/main/resources/spring-server.xml rename to rpc_sieve/src/main/resources/spring-server.xml index 6bfda6a..625e73c 100644 --- a/src/main/resources/spring-server.xml +++ b/rpc_sieve/src/main/resources/spring-server.xml @@ -6,12 +6,12 @@ - + - + diff --git a/rpc_simple/pom.xml b/rpc_simple/pom.xml new file mode 100644 index 0000000..15e21ee --- /dev/null +++ b/rpc_simple/pom.xml @@ -0,0 +1,32 @@ + + + + rpc + xyz.anduo + 0.0.1-SNAPSHOT + + 4.0.0 + + rpc-simple + + + + junit + junit + test + + + org.springframework + spring-test + test + + + org.springframework + spring-context + + + + + \ No newline at end of file diff --git a/src/test/java/rpc/HelloServiceTest.java b/rpc_simple/src/test/java/xyz/anduo/rpc/simple/HelloServiceTest.java similarity index 87% rename from src/test/java/rpc/HelloServiceTest.java rename to rpc_simple/src/test/java/xyz/anduo/rpc/simple/HelloServiceTest.java index e109dd8..56e71b9 100644 --- a/src/test/java/rpc/HelloServiceTest.java +++ b/rpc_simple/src/test/java/xyz/anduo/rpc/simple/HelloServiceTest.java @@ -1,8 +1,8 @@ -package rpc; +package xyz.anduo.rpc.simple; -import junit.framework.Assert; +import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -10,7 +10,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import xyz.anduo.rpc.client.RpcProxy; -import xyz.anduo.rpc.service.HelloService; +import xyz.anduo.rpc.sieve.modules.simple.HelloService; + @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:spring-client.xml") public class HelloServiceTest { diff --git a/src/main/java/xyz/anduo/rpc/client/RpcProxy.java b/src/main/java/xyz/anduo/rpc/client/RpcProxy.java deleted file mode 100644 index fe07cfb..0000000 --- a/src/main/java/xyz/anduo/rpc/client/RpcProxy.java +++ /dev/null @@ -1,60 +0,0 @@ -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; -import xyz.anduo.rpc.server.RpcRequest; -import xyz.anduo.rpc.server.RpcResponse; - -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/registry/ServiceRegistry.java b/src/main/java/xyz/anduo/rpc/registry/ServiceRegistry.java deleted file mode 100644 index ef7a81a..0000000 --- a/src/main/java/xyz/anduo/rpc/registry/ServiceRegistry.java +++ /dev/null @@ -1,66 +0,0 @@ -package xyz.anduo.rpc.registry; - -import java.io.IOException; -import java.util.concurrent.CountDownLatch; - -import org.apache.zookeeper.CreateMode; -import org.apache.zookeeper.KeeperException; -import org.apache.zookeeper.WatchedEvent; -import org.apache.zookeeper.Watcher; -import org.apache.zookeeper.ZooDefs; -import org.apache.zookeeper.ZooKeeper; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import xyz.anduo.rpc.server.Constant; - -public class ServiceRegistry { - - private static final Logger LOGGER = LoggerFactory.getLogger(ServiceRegistry.class); - - private CountDownLatch latch = new CountDownLatch(1); - - private String registryAddress; - - public ServiceRegistry(String registryAddress) { - this.registryAddress = registryAddress; - } - - public void register(String data) { - if (data != null) { - ZooKeeper zk = connectServer(); - if (zk != null) { - createNode(zk, data); - } - } - } - - private ZooKeeper connectServer() { - ZooKeeper zk = null; - try { - zk = new ZooKeeper(registryAddress, Constant.ZK_SESSION_TIMEOUT, new Watcher() { - - public void process(WatchedEvent event) { - if (event.getState() == Event.KeeperState.SyncConnected) { - latch.countDown(); - } - } - }); - latch.await(); - } catch (IOException | InterruptedException e) { - LOGGER.error("", e); - } - return zk; - } - - private void createNode(ZooKeeper zk, String data) { - try { - byte[] bytes = data.getBytes(); - String path = zk.create(Constant.ZK_DATA_PATH, bytes, ZooDefs.Ids.OPEN_ACL_UNSAFE, - CreateMode.EPHEMERAL_SEQUENTIAL); - LOGGER.debug("create zookeeper node ({} => {})", path, data); - } catch (KeeperException | InterruptedException e) { - LOGGER.error("", e); - } - } -} diff --git a/src/main/java/xyz/anduo/rpc/server/RpcEncoder.java b/src/main/java/xyz/anduo/rpc/server/RpcEncoder.java deleted file mode 100644 index 7ee10fa..0000000 --- a/src/main/java/xyz/anduo/rpc/server/RpcEncoder.java +++ /dev/null @@ -1,23 +0,0 @@ -package xyz.anduo.rpc.server; - -import io.netty.buffer.ByteBuf; -import io.netty.channel.ChannelHandlerContext; -import io.netty.handler.codec.MessageToByteEncoder; - -public class RpcEncoder extends MessageToByteEncoder { - - private Class genericClass; - - public RpcEncoder(Class genericClass) { - this.genericClass = genericClass; - } - - @Override - public void encode(ChannelHandlerContext ctx, Object in, ByteBuf out) throws Exception { - if (genericClass.isInstance(in)) { - byte[] data = SerializationUtil.serialize(in); - out.writeInt(data.length); - out.writeBytes(data); - } - } -} diff --git a/src/main/java/xyz/anduo/rpc/server/SerializationUtil.java b/src/main/java/xyz/anduo/rpc/server/SerializationUtil.java deleted file mode 100644 index a2c7a65..0000000 --- a/src/main/java/xyz/anduo/rpc/server/SerializationUtil.java +++ /dev/null @@ -1,58 +0,0 @@ -package xyz.anduo.rpc.server; - -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.objenesis.Objenesis; -import org.objenesis.ObjenesisStd; - -import com.dyuproject.protostuff.LinkedBuffer; -import com.dyuproject.protostuff.ProtostuffIOUtil; -import com.dyuproject.protostuff.Schema; -import com.dyuproject.protostuff.runtime.RuntimeSchema; - -public class SerializationUtil { - private static Map, Schema> cachedSchema = new ConcurrentHashMap<>(); - - private static Objenesis objenesis = new ObjenesisStd(true); - - private SerializationUtil() { - } - - @SuppressWarnings("unchecked") - private static Schema getSchema(Class cls) { - Schema schema = (Schema) cachedSchema.get(cls); - if (schema == null) { - schema = RuntimeSchema.createFrom(cls); - if (schema != null) { - cachedSchema.put(cls, schema); - } - } - return schema; - } - - @SuppressWarnings("unchecked") - public static byte[] serialize(T obj) { - Class cls = (Class) obj.getClass(); - LinkedBuffer buffer = LinkedBuffer.allocate(LinkedBuffer.DEFAULT_BUFFER_SIZE); - try { - Schema schema = getSchema(cls); - return ProtostuffIOUtil.toByteArray(obj, schema, buffer); - } catch (Exception e) { - throw new IllegalStateException(e.getMessage(), e); - } finally { - buffer.clear(); - } - } - - public static T deserialize(byte[] data, Class cls) { - try { - T message = (T) objenesis.newInstance(cls); - Schema schema = getSchema(cls); - ProtostuffIOUtil.mergeFrom(data, message, schema); - return message; - } catch (Exception e) { - throw new IllegalStateException(e.getMessage(), e); - } - } -} diff --git a/src/main/resources/spring-client.xml b/src/main/resources/spring-client.xml deleted file mode 100644 index fa27fa7..0000000 --- a/src/main/resources/spring-client.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - - - - diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml deleted file mode 100644 index 9f88c1f..0000000 --- a/src/main/webapp/WEB-INF/web.xml +++ /dev/null @@ -1,7 +0,0 @@ - - - - Archetype Created Web Application - diff --git a/src/main/webapp/index.jsp b/src/main/webapp/index.jsp deleted file mode 100644 index c38169b..0000000 --- a/src/main/webapp/index.jsp +++ /dev/null @@ -1,5 +0,0 @@ - - -

Hello World!

- - diff --git a/target/classes/client-config.properties b/target/classes/client-config.properties deleted file mode 100644 index 6327ddc..0000000 --- a/target/classes/client-config.properties +++ /dev/null @@ -1,2 +0,0 @@ -# ZooKeeper \u670d\u52a1\u5668 -registry.address=127.0.0.1:2181 \ No newline at end of file diff --git a/target/classes/server-config.properties b/target/classes/server-config.properties deleted file mode 100644 index 7f95f2d..0000000 --- a/target/classes/server-config.properties +++ /dev/null @@ -1,4 +0,0 @@ -# ZooKeeper \u670d\u52a1\u5668 -registry.address=127.0.0.1:2181 -# RPC \u670d\u52a1\u5668 -server.address=127.0.0.1:8000 \ No newline at end of file diff --git a/target/classes/spring-client.xml b/target/classes/spring-client.xml deleted file mode 100644 index fa27fa7..0000000 --- a/target/classes/spring-client.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - - - - diff --git a/target/classes/spring-server.xml b/target/classes/spring-server.xml deleted file mode 100644 index 6bfda6a..0000000 --- a/target/classes/spring-server.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - - - - - - - - - - diff --git a/target/classes/xyz/anduo/rpc/client/RpcClient$1.class b/target/classes/xyz/anduo/rpc/client/RpcClient$1.class deleted file mode 100644 index 6d475ae..0000000 Binary files a/target/classes/xyz/anduo/rpc/client/RpcClient$1.class and /dev/null differ diff --git a/target/classes/xyz/anduo/rpc/client/RpcClient.class b/target/classes/xyz/anduo/rpc/client/RpcClient.class deleted file mode 100644 index a4bbca2..0000000 Binary files a/target/classes/xyz/anduo/rpc/client/RpcClient.class and /dev/null differ diff --git a/target/classes/xyz/anduo/rpc/client/RpcProxy$1.class b/target/classes/xyz/anduo/rpc/client/RpcProxy$1.class deleted file mode 100644 index 3908634..0000000 Binary files a/target/classes/xyz/anduo/rpc/client/RpcProxy$1.class and /dev/null differ diff --git a/target/classes/xyz/anduo/rpc/client/RpcProxy.class b/target/classes/xyz/anduo/rpc/client/RpcProxy.class deleted file mode 100644 index 475042a..0000000 Binary files a/target/classes/xyz/anduo/rpc/client/RpcProxy.class and /dev/null differ diff --git a/target/classes/xyz/anduo/rpc/client/ServiceDiscovery$1.class b/target/classes/xyz/anduo/rpc/client/ServiceDiscovery$1.class deleted file mode 100644 index 20b8ce0..0000000 Binary files a/target/classes/xyz/anduo/rpc/client/ServiceDiscovery$1.class and /dev/null differ diff --git a/target/classes/xyz/anduo/rpc/client/ServiceDiscovery$2.class b/target/classes/xyz/anduo/rpc/client/ServiceDiscovery$2.class deleted file mode 100644 index ea9ddc7..0000000 Binary files a/target/classes/xyz/anduo/rpc/client/ServiceDiscovery$2.class and /dev/null differ diff --git a/target/classes/xyz/anduo/rpc/client/ServiceDiscovery.class b/target/classes/xyz/anduo/rpc/client/ServiceDiscovery.class deleted file mode 100644 index 67ac7aa..0000000 Binary files a/target/classes/xyz/anduo/rpc/client/ServiceDiscovery.class and /dev/null differ diff --git a/target/classes/xyz/anduo/rpc/registry/ServiceRegistry$1.class b/target/classes/xyz/anduo/rpc/registry/ServiceRegistry$1.class deleted file mode 100644 index 69b231a..0000000 Binary files a/target/classes/xyz/anduo/rpc/registry/ServiceRegistry$1.class and /dev/null differ diff --git a/target/classes/xyz/anduo/rpc/registry/ServiceRegistry.class b/target/classes/xyz/anduo/rpc/registry/ServiceRegistry.class deleted file mode 100644 index 62f95f6..0000000 Binary files a/target/classes/xyz/anduo/rpc/registry/ServiceRegistry.class and /dev/null differ diff --git a/target/classes/xyz/anduo/rpc/server/Constant.class b/target/classes/xyz/anduo/rpc/server/Constant.class deleted file mode 100644 index 8b946f4..0000000 Binary files a/target/classes/xyz/anduo/rpc/server/Constant.class and /dev/null differ diff --git a/target/classes/xyz/anduo/rpc/server/RpcBootstrap.class b/target/classes/xyz/anduo/rpc/server/RpcBootstrap.class deleted file mode 100644 index 44cb139..0000000 Binary files a/target/classes/xyz/anduo/rpc/server/RpcBootstrap.class and /dev/null differ diff --git a/target/classes/xyz/anduo/rpc/server/RpcDecoder.class b/target/classes/xyz/anduo/rpc/server/RpcDecoder.class deleted file mode 100644 index cc9b23b..0000000 Binary files a/target/classes/xyz/anduo/rpc/server/RpcDecoder.class and /dev/null differ diff --git a/target/classes/xyz/anduo/rpc/server/RpcEncoder.class b/target/classes/xyz/anduo/rpc/server/RpcEncoder.class deleted file mode 100644 index 58b30d4..0000000 Binary files a/target/classes/xyz/anduo/rpc/server/RpcEncoder.class and /dev/null differ diff --git a/target/classes/xyz/anduo/rpc/server/RpcHandler.class b/target/classes/xyz/anduo/rpc/server/RpcHandler.class deleted file mode 100644 index 14d2112..0000000 Binary files a/target/classes/xyz/anduo/rpc/server/RpcHandler.class and /dev/null differ diff --git a/target/classes/xyz/anduo/rpc/server/RpcRequest.class b/target/classes/xyz/anduo/rpc/server/RpcRequest.class deleted file mode 100644 index 0bc52c0..0000000 Binary files a/target/classes/xyz/anduo/rpc/server/RpcRequest.class and /dev/null differ diff --git a/target/classes/xyz/anduo/rpc/server/RpcResponse.class b/target/classes/xyz/anduo/rpc/server/RpcResponse.class deleted file mode 100644 index f5b4f8f..0000000 Binary files a/target/classes/xyz/anduo/rpc/server/RpcResponse.class and /dev/null differ diff --git a/target/classes/xyz/anduo/rpc/server/RpcServer$1.class b/target/classes/xyz/anduo/rpc/server/RpcServer$1.class deleted file mode 100644 index e80ff4e..0000000 Binary files a/target/classes/xyz/anduo/rpc/server/RpcServer$1.class and /dev/null differ diff --git a/target/classes/xyz/anduo/rpc/server/RpcServer.class b/target/classes/xyz/anduo/rpc/server/RpcServer.class deleted file mode 100644 index d3e5ebe..0000000 Binary files a/target/classes/xyz/anduo/rpc/server/RpcServer.class and /dev/null differ diff --git a/target/classes/xyz/anduo/rpc/server/RpcService.class b/target/classes/xyz/anduo/rpc/server/RpcService.class deleted file mode 100644 index 0824e90..0000000 Binary files a/target/classes/xyz/anduo/rpc/server/RpcService.class and /dev/null differ diff --git a/target/classes/xyz/anduo/rpc/server/SerializationUtil.class b/target/classes/xyz/anduo/rpc/server/SerializationUtil.class deleted file mode 100644 index 2d576bb..0000000 Binary files a/target/classes/xyz/anduo/rpc/server/SerializationUtil.class and /dev/null differ diff --git a/target/classes/xyz/anduo/rpc/service/HelloService.class b/target/classes/xyz/anduo/rpc/service/HelloService.class deleted file mode 100644 index 0ef9321..0000000 Binary files a/target/classes/xyz/anduo/rpc/service/HelloService.class and /dev/null differ diff --git a/target/classes/xyz/anduo/rpc/service/impl/HelloServiceImpl.class b/target/classes/xyz/anduo/rpc/service/impl/HelloServiceImpl.class deleted file mode 100644 index 84f6af5..0000000 Binary files a/target/classes/xyz/anduo/rpc/service/impl/HelloServiceImpl.class and /dev/null differ diff --git a/target/m2e-wtp/web-resources/META-INF/MANIFEST.MF b/target/m2e-wtp/web-resources/META-INF/MANIFEST.MF deleted file mode 100644 index ac10750..0000000 --- a/target/m2e-wtp/web-resources/META-INF/MANIFEST.MF +++ /dev/null @@ -1,5 +0,0 @@ -Manifest-Version: 1.0 -Built-By: anduo -Build-Jdk: 1.7.0_51 -Created-By: Maven Integration for Eclipse - diff --git a/target/m2e-wtp/web-resources/META-INF/maven/xyz.anduo/rpc/pom.properties b/target/m2e-wtp/web-resources/META-INF/maven/xyz.anduo/rpc/pom.properties deleted file mode 100644 index 0ee07b2..0000000 --- a/target/m2e-wtp/web-resources/META-INF/maven/xyz.anduo/rpc/pom.properties +++ /dev/null @@ -1,7 +0,0 @@ -#Generated by Maven Integration for Eclipse -#Sun Jan 04 14:34:43 GMT+08:00 2015 -version=0.0.1-SNAPSHOT -groupId=xyz.anduo -m2e.projectName=rpc_learn -m2e.projectLocation=D\:\\Java\\eclipse-jee-kepler-SR2-win32-x86_64\\workspace\\rpc_learn -artifactId=rpc diff --git a/target/m2e-wtp/web-resources/META-INF/maven/xyz.anduo/rpc/pom.xml b/target/m2e-wtp/web-resources/META-INF/maven/xyz.anduo/rpc/pom.xml deleted file mode 100644 index d15f583..0000000 --- a/target/m2e-wtp/web-resources/META-INF/maven/xyz.anduo/rpc/pom.xml +++ /dev/null @@ -1,89 +0,0 @@ - - 4.0.0 - xyz.anduo - rpc - war - 0.0.1-SNAPSHOT - rpc Maven Webapp - http://maven.apache.org - - - - junit - junit - 4.11 - test - - - - - org.slf4j - slf4j-log4j12 - 1.7.7 - - - - - org.springframework - spring-context - 3.2.12.RELEASE - - - org.springframework - spring-test - 3.2.12.RELEASE - test - - - - - io.netty - netty-all - 4.0.24.Final - - - - - com.dyuproject.protostuff - protostuff-core - 1.0.8 - - - com.dyuproject.protostuff - protostuff-runtime - 1.0.8 - - - - - org.apache.zookeeper - zookeeper - 3.4.6 - - - - - org.apache.commons - commons-collections4 - 4.0 - - - - - org.objenesis - objenesis - 2.1 - - - - - cglib - cglib - 3.1 - - - - rpc - - diff --git a/target/test-classes/rpc/HelloServiceTest.class b/target/test-classes/rpc/HelloServiceTest.class deleted file mode 100644 index 9e0dbbd..0000000 Binary files a/target/test-classes/rpc/HelloServiceTest.class and /dev/null differ