1、什么是RPC
- RPC,英文全称是Remote Procedure Call,翻译过来就是远程过程调用。
- 作为一个计算机通信协议,运行在计算机A的程序调用另一台计算机B上的子程序就像调用本地方法一样方便,而这两个程序是分布在不同的服务器上的
- RPC远程过程调用图示:

其中Client就是RPC服务的消费者,Server就是服务提供者,通过上图可以看到完整的调用关系 - RPC就是将以上的步骤全部封装起来,用户直接使用,而不用关心使用细节,部署后直接能像使用本地方法一样使用远程程序
2、设计一个RPC
- 业界比较主流的RPC框架包括阿里巴巴的Dubbo,Google的gRPC等,这里我们模仿Dubbo设计一个
Client和Server约定接口和协议,Client远程调用Server提供的服务,提供者返回一个字符串/也可以是其他对象,消费者使用该数据/本次将该数据打印
系统 - 基于Netty 4.1.20版本进行代码实现,依赖相关部署看我之前的博客传送门
1)创建一个接口,定义相关的抽向方法:用于Client和Server之间的约定;
2)创建一个Server类,负责监听Client的请求并返回约定数据;
3)创建一个Client类,该类能够透明的调用远程方法/自己不存在的方法,Client内部使用Netty请求Server返回数据

3、系统结构
- 在Intellij IDEA中新建一个Maven工程,对Maven不熟悉的小伙伴只需要补充如何在Intellij中安装Maven/使用Maven导入相关依赖/查看关系图等基础操作即可,使用pom.xml文件配置Netty依赖/也可以直接使用Maven导入
- 系统的结构图如下:

4、代码实现
1)HelloService接口
package com.atguigu.netty.dubborpc.publicinterface;
public interface HelloService {
String hello(String mes);
}
2)HelloServiceImpl类
- 实现HelloService接口,当有Client调用该方法的时候就返回一个结果
package com.atguigu.netty.dubborpc.provider;
import com.atguigu.netty.dubborpc.publicinterface.HelloService;
public class HelloServiceImpl implements HelloService{
private static int count = 0;
@Override
public String hello(String mes) {
System.out.println("收到客户端消息=" + mes);
if(mes != null) {
return "你好客户端, 我已经收到你的消息 [" + mes + "] 第" + (++count) + " 次";
} else {
return "你好客户端, 我已经收到你的消息 ";
}
}
}
3)NettyServer类
- Client实现类,关键的是NettyServerHandler,下面有实现代码
package com.atguigu.netty.dubborpc.netty;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
public class NettyServer {
public static void startServer(String hostName, int port) {
startServer0(hostName,port);
}
private static void startServer0(String hostname, int port) {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup,workerGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
pipeline.addLast(new StringDecoder());
pipeline.addLast(new StringEncoder());
pipeline.addLast(new NettyServerHandler());
}
});
ChannelFuture channelFuture = serverBootstrap.bind(hostname, port).sync();
System.out.println("服务提供方开始提供服务~~");
channelFuture.channel().closeFuture().sync();
}catch (Exception e) {
e.printStackTrace();
}
finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
4)NettyServerHandler类
- 继承自ChannelInboundHandlerAdapter
package com.atguigu.netty.dubborpc.netty;
import com.atguigu.netty.dubborpc.customer.ClientBootstrap;
import com.atguigu.netty.dubborpc.provider.HelloServiceImpl;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
public class NettyServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println("msg=" + msg);
if(msg.toString().startsWith(ClientBootstrap.providerName)) {
String result = new HelloServiceImpl().hello(msg.toString().substring(msg.toString().lastIndexOf("#") + 1));
ctx.writeAndFlush(result);
}
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
}
5)ServerBootstrap类/Server启动
package com.atguigu.netty.dubborpc.provider;
import com.atguigu.netty.dubborpc.netty.NettyServer;
public class ServerBootstrap {
public static void main(String[] args) {
NettyServer.startServer("127.0.0.1", 7000);
}
}
6)NettyClientHandler类
- 继承自ChannelInboundHandlerAdapter类,实现了Callable接口
package com.atguigu.netty.dubborpc.netty;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.util.concurrent.Callable;
public class NettyClientHandler extends ChannelInboundHandlerAdapter implements Callable {
private ChannelHandlerContext context;
private String result;
private String para;
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println(" channelActive 被调用 ");
context = ctx;
}
@Override
public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println(" channelRead 被调用 ");
result = msg.toString();
notify();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
public synchronized Object call() throws Exception {
System.out.println(" call1 被调用 ");
context.writeAndFlush(para);
wait();
System.out.println(" call2 被调用 ");
return result;
}
void setPara(String para) {
System.out.println(" setPara ");
this.para = para;
}
}
7) NettyClient类
package com.atguigu.netty.dubborpc.netty;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import java.util.concurrent.Callable;
public class NettyClientHandler extends ChannelInboundHandlerAdapter implements Callable {
private ChannelHandlerContext context;
private String result;
private String para;
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
System.out.println(" channelActive 被调用 ");
context = ctx;
}
@Override
public synchronized void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
System.out.println(" channelRead 被调用 ");
result = msg.toString();
notify();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
ctx.close();
}
public synchronized Object call() throws Exception {
System.out.println(" call1 被调用 ");
context.writeAndFlush(para);
wait();
System.out.println(" call2 被调用 ");
return result;
}
void setPara(String para) {
System.out.println(" setPara ");
this.para = para;
}
}
8)ClientBootstrap类/Client启动
package com.atguigu.netty.dubborpc.customer;
import com.atguigu.netty.dubborpc.netty.NettyClient;
import com.atguigu.netty.dubborpc.publicinterface.HelloService;
public class ClientBootstrap {
public static final String providerName = "HelloService#hello#";
public static void main(String[] args) throws Exception{
NettyClient customer = new NettyClient();
HelloService service = (HelloService) customer.getBean(HelloService.class, providerName);
for (;; ) {
Thread.sleep(2 * 1000);
String res = service.hello("你好 dubbo~");
System.out.println("调用的结果 res= " + res);
}
}
}
5、运行RPC代码
- 运行ServerBootstrap类,启动一个Server
- 运行ClientBootstrap类,启动一个Client
- 查看控制台结果:
