说明

上一篇代码基于 socket 的实现非常简单,但是对于实际生产,一般使用 netty。

至于 netty 的优点可以参考:

为什么选择 netty?

http://houbb.github.io/2019/05/10/netty-definitive-gudie-04-why-netty

代码实现

maven 引入

<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>${netty.version}</version>
</dependency>

引入 netty 对应的 maven 包,此处为 4.1.17.Final。

服务端代码实现

netty 的服务端启动代码是比较固定的。

package com.github.houbb.rpc.server.core;

import com.github.houbb.log.integration.core.Log;
import com.github.houbb.log.integration.core.LogFactory;
import com.github.houbb.rpc.server.constant.RpcServerConst;
import com.github.houbb.rpc.server.handler.RpcServerHandler;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel; /**
* rpc 服务端
* @author binbin.hou
* @since 0.0.1
*/
public class RpcServer extends Thread { private static final Log log = LogFactory.getLog(RpcServer.class); /**
* 端口号
*/
private final int port; public RpcServer() {
this.port = RpcServerConst.DEFAULT_PORT;
} public RpcServer(int port) {
this.port = port;
} @Override
public void run() {
// 启动服务端
log.info("RPC 服务开始启动服务端"); EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup(); try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(workerGroup, bossGroup)
.channel(NioServerSocketChannel.class)
.childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(new RpcServerHandler());
}
})
// 这个参数影响的是还没有被accept 取出的连接
.option(ChannelOption.SO_BACKLOG, 128)
// 这个参数只是过一段时间内客户端没有响应,服务端会发送一个 ack 包,以判断客户端是否还活着。
.childOption(ChannelOption.SO_KEEPALIVE, true); // 绑定端口,开始接收进来的链接
ChannelFuture channelFuture = serverBootstrap.bind(port).syncUninterruptibly();
log.info("RPC 服务端启动完成,监听【" + port + "】端口"); channelFuture.channel().closeFuture().syncUninterruptibly();
log.info("RPC 服务端关闭完成");
} catch (Exception e) {
log.error("RPC 服务异常", e);
} finally {
workerGroup.shutdownGracefully();
bossGroup.shutdownGracefully();
}
} }

为了简单,服务端启动端口号固定,RpcServerConst 常量类内容如下:

public final class RpcServerConst {

    private RpcServerConst(){}

    /**
* 默认端口
* @since 0.0.1
*/
public static final int DEFAULT_PORT = 9627; }

RpcServerHandler

当然,还有一个比较核心的类就是 RpcServerHandler

public class RpcServerHandler extends SimpleChannelInboundHandler {
@Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
// do nothing now
}
}

目前是空实现,后续可以添加对应的日志输出及逻辑处理。

测试

启动测试的代码非常简单:

/**
* 服务启动代码测试
* @param args 参数
*/
public static void main(String[] args) {
new RpcServer().start();
}

说明

上面我们实现了服务端的实现,这一节来一起看一下 client 客户端代码实现。

代码实现

RpcClient

/*
* Copyright (c) 2019. houbinbin Inc.
* rpc All rights reserved.
*/ package com.github.houbb.rpc.client.core; import com.github.houbb.log.integration.core.Log;
import com.github.houbb.log.integration.core.LogFactory;
import com.github.houbb.rpc.client.handler.RpcClientHandler; import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler; /**
* <p> rpc 客户端 </p>
*
* <pre> Created: 2019/10/16 11:21 下午 </pre>
* <pre> Project: rpc </pre>
*
* @author houbinbin
* @since 0.0.2
*/
public class RpcClient extends Thread { private static final Log log = LogFactory.getLog(RpcClient.class); /**
* 监听端口号
*/
private final int port; public RpcClient(int port) {
this.port = port;
} public RpcClient() {
this(9527);
} @Override
public void run() {
// 启动服务端
log.info("RPC 服务开始启动客户端"); EventLoopGroup workerGroup = new NioEventLoopGroup(); try {
Bootstrap bootstrap = new Bootstrap();
ChannelFuture channelFuture = bootstrap.group(workerGroup)
.channel(NioSocketChannel.class)
.option(ChannelOption.SO_KEEPALIVE, true)
.handler(new ChannelInitializer<Channel>(){
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline()
.addLast(new LoggingHandler(LogLevel.INFO))
.addLast(new RpcClientHandler());
}
})
.connect("localhost", port)
.syncUninterruptibly(); log.info("RPC 服务启动客户端完成,监听端口:" + port);
channelFuture.channel().closeFuture().syncUninterruptibly();
log.info("RPC 服务开始客户端已关闭");
} catch (Exception e) {
log.error("RPC 客户端遇到异常", e);
} finally {
workerGroup.shutdownGracefully();
}
} }

.connect("localhost", port) 声明了客户端需要连接的服务端,此处和服务端的端口保持一致。

RpcClientHandler

客户端处理类也比较简单,暂时留空。

/*
* Copyright (c) 2019. houbinbin Inc.
* rpc All rights reserved.
*/ package com.github.houbb.rpc.client.handler; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler; /**
* <p> 客户端处理类 </p>
*
* <pre> Created: 2019/10/16 11:30 下午 </pre>
* <pre> Project: rpc </pre>
*
* @author houbinbin
* @since 0.0.2
*/
public class RpcClientHandler extends SimpleChannelInboundHandler { @Override
protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
// do nothing.
} }

启动测试

服务端

首先启动服务端。

客户端

然后启动客户端连接服务端,实现如下:

/**
* 服务启动代码测试
* @param args 参数
*/
public static void main(String[] args) {
new RpcClient().start();
}

小结

为了便于大家学习,以上源码已经开源:

https://github.com/houbb/rpc

我是老马,期待与你的下次重逢。

java 从零开始手写 RPC (02)-netty4 实现客户端和服务端的更多相关文章

  1. java 从零开始手写 RPC (03) 如何实现客户端调用服务端?

    说明 java 从零开始手写 RPC (01) 基于 socket 实现 java 从零开始手写 RPC (02)-netty4 实现客户端和服务端 写完了客户端和服务端,那么如何实现客户端和服务端的 ...

  2. java 从零开始手写 RPC (04) -序列化

    序列化 java 从零开始手写 RPC (01) 基于 socket 实现 java 从零开始手写 RPC (02)-netty4 实现客户端和服务端 java 从零开始手写 RPC (03) 如何实 ...

  3. java 从零开始手写 RPC (05) reflect 反射实现通用调用之服务端

    通用调用 java 从零开始手写 RPC (01) 基于 socket 实现 java 从零开始手写 RPC (02)-netty4 实现客户端和服务端 java 从零开始手写 RPC (03) 如何 ...

  4. java 从零开始手写 RPC (07)-timeout 超时处理

    <过时不候> 最漫长的莫过于等待 我们不可能永远等一个人 就像请求 永远等待响应 超时处理 java 从零开始手写 RPC (01) 基于 socket 实现 java 从零开始手写 RP ...

  5. java 从零开始手写 RPC (01) 基于 websocket 实现

    RPC 解决的问题 RPC 主要是为了解决的两个问题: 解决分布式系统中,服务之间的调用问题. 远程调用时,要能够像本地调用一样方便,让调用者感知不到远程调用的逻辑. 这一节我们来学习下如何基于 we ...

  6. 从零开始手写 dubbo rpc 框架

    rpc rpc 是基于 netty 实现的 java rpc 框架,类似于 dubbo. 主要用于个人学习,由渐入深,理解 rpc 的底层实现原理. 前言 工作至今,接触 rpc 框架已经有很长时间. ...

  7. 手写RPC框架指北另送贴心注释代码一套

    Angular8正式发布了,Java13再过几个月也要发布了,技术迭代这么快,框架的复杂度越来越大,但是原理是基本不变的.所以沉下心看清代码本质很重要,这次给大家带来的是手写RPC框架. 完整代码以及 ...

  8. 看了这篇你就会手写RPC框架了

    一.学习本文你能学到什么? RPC的概念及运作流程 RPC协议及RPC框架的概念 Netty的基本使用 Java序列化及反序列化技术 Zookeeper的基本使用(注册中心) 自定义注解实现特殊业务逻 ...

  9. 手写RPC框架(六)整合Netty

    手写RPC框架(六)整合Netty Netty简介: Netty是一个基于NIO的,提供异步,事件驱动的网络应用工具,具有高性能高可靠性等特点. 使用传统的Socket来进行网络通信,服务端每一个连接 ...

  10. java客户端与服务端交互通用处理 框架解析

    一.综述 java 客户端与服务端交互过程中,采用NIO通讯是异步的,客户端基本采用同一处理范式,来进行同异步的调用处理. 处理模型有以下几个要素: 1. NIO发送消息后返回的Future 2. 每 ...

随机推荐

  1. 电子科技大学--ARM实验Keil uvison5 安装以及S3C2440A芯片包下载

    1.简介 本教程主要服务于电子科技大学软工嵌入式新生,节省一些查找资料的时间 2.过程 请按如下顺序安装: 1,安装mdk_514.exe (主体文件) 2,安装Keil.STM32F1xx_DFP. ...

  2. 【Git】Git与Repo入门

    [来源]https://www.cnblogs.com/angeldevil/archive/2013/11/26/3238470.html

  3. Shell-表达式-比较-文件判断-权限判断-条件-逻辑

  4. [转帖]堆表(HOT)和索引组织表(IOT)优缺点

      转载于: https://www.ywnds.com/?p=7702 一.堆表和索引组织表 NOTE 堆表也可以称之为 HOT,索引组织表也可以称之为 IOT,下面没有特别说明,两者都是一个意思. ...

  5. [转帖]数据库的快照隔离级别(Snapshot Isolation)

    https://www.cnblogs.com/gered/p/10251744.html 总结:已提交读快照只影响语句级别的锁定行为,而快照隔离影响整个事务.  转自:https://www.cnb ...

  6. [转帖]KV数据库调研

    https://zhuanlan.zhihu.com/p/499313638 Redis作为NoSQL领域的代表,拥有很高的读写性能,支持比较丰富的数据类型,但是Redis也存在一些缺陷. l 内存数 ...

  7. [转帖]TiFlash DeltaTree 存储引擎设计及实现分析 - Part 1

    https://tidb.net/book/book-rush/features/tiflash-code/tiflash-deltatree TiFlash 是 TiDB 的分析引擎,是 TiDB ...

  8. [粘贴]TiFlash

    TiFlash 是 TiDB HTAP 形态的关键组件,它是 TiKV 的列存扩展,在提供了良好的隔离性的同时,也兼顾了强一致性.列存副本通过 Raft Learner 协议异步复制,但是在读取的时候 ...

  9. [转帖]Linux文件权限除了r、w、x外还有s、t、i、a权限

    https://www.cnblogs.com/hiyang/p/15122714.html setuid 是 set user ID upon execution 再次缩写为suid setgid  ...

  10. [转帖]win10下使用Rclone将OneDrive映射到本地磁盘教程(开机自动挂载)

    win10下使用Rclone将OneDrive映射到本地磁盘教程(开机自动挂载) 下载rclone,winfsp和Git bash Rclone. Winfsp. 和 [Git bash](https ...