学习 javaGuide,自己动手造个轮子,通过这个简易的轮子,可以学到 RPC 的底层原理和原理以及各种 Java 编码实践的运用。
rpc-framework 是一款基于 Netty+Kyro+Zookeeper 实现的 RPC 框架。
设计思路
一个最简单的 RPC 框架使用示意图如下图所示,这也是这个项目目前的架构 :
- 服务端将服务注册到注册中心并且在服务端监听客户端请求。
- 客户端在消费中心拿到服务,远程调用服务(发送一个request给服务端)。
- 服务端根据request,调用对应的服务并将结果返回给客户端。
- 客户端接收response,结束调用。
Netty + kryo + zookeeper
- Netty:网络应用框架,使用它来进行网络传输。
- kryo:序列化/反序列化工具。
- zookeeper:做注册中心。
下载运行 zookeeper。
发布服务:
@ComponentScan("com.yufa.xz.ly")
public class NettyServerMain {
public static void main(String[] args) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(NettyServerMain.class);
NettyServer nettyServer = applicationContext.getBean(NettyServer.class);
HelloService helloService2 = new HelloServiceImpl();
RpcServiceProperties rpcServiceProperties = RpcServiceProperties.builder()
.group("test2").version("version2").build();
nettyServer.registerService(helloService2, rpcServiceProperties);
nettyServer.start();
}
}
调用服务:
public class NettyClientMain {
public static void main(String[] args) throws InterruptedException {
ClientTransport rpcClient = new NettyClientTransport();
RpcServiceProperties rpcServiceProperties = RpcServiceProperties.builder()
.group("test2").version("version2").build();
RpcClientProxy rpcClientProxy = new RpcClientProxy(rpcClient, rpcServiceProperties);
HelloService helloService = rpcClientProxy.getProxy(HelloService.class);
String result = helloService.hello(new Hello("111", "222"));
System.out.println(result);
Thread.sleep(5000);
for (int i = 0; i < 10; i++) {
result = helloService.hello(new Hello("111", " i = " + i));
System.out.println(result);
}
}
}