从零开始手写 dubbo rpc 框架-02-客户端启动

mac2025-06-07  53

客户端启动

功能

实现 client 端服务启动。

核心代码

简单的一个 netty client 端启动代码。

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(); } } }

测试代码

启动服务端

代码 /** * 服务启动代码测试 * @param args 参数 */ public static void main(String[] args) { new RpcServer().start(); } 日志 [INFO] [2019-11-01 13:46:31.434] [Thread-0] [c.g.h.r.s.c.RpcServer.run] - RPC 服务开始启动服务端 [INFO] [2019-11-01 13:46:32.442] [Thread-0] [c.g.h.r.s.c.RpcServer.run] - RPC 服务端启动完成,监听【9527】端口 十一月 01, 2019 1:46:32 下午 io.netty.handler.logging.LoggingHandler channelRegistered 信息: [id: 0xd639f332] REGISTERED 十一月 01, 2019 1:46:32 下午 io.netty.handler.logging.LoggingHandler bind 信息: [id: 0xd639f332] BIND: 0.0.0.0/0.0.0.0:9527 十一月 01, 2019 1:46:32 下午 io.netty.handler.logging.LoggingHandler channelActive 信息: [id: 0xd639f332, L:/0:0:0:0:0:0:0:0:9527] ACTIVE

启动客户端

代码 /** * 服务启动代码测试 * @param args 参数 */ public static void main(String[] args) { new RpcClient().start(); } 日志 [DEBUG] [2019-11-01 13:47:17.963] [main] [c.g.h.l.i.c.LogFactory.setImplementation] - Logging initialized using 'class com.github.houbb.log.integration.adaptors.stdout.StdOutExImpl' adapter. [INFO] [2019-11-01 13:47:17.967] [Thread-0] [c.g.h.r.c.c.RpcClient.run] - RPC 服务开始启动客户端 [INFO] [2019-11-01 13:47:18.915] [Thread-0] [c.g.h.r.c.c.RpcClient.run] - RPC 服务启动客户端完成,监听端口:9527 十一月 01, 2019 1:47:18 下午 io.netty.handler.logging.LoggingHandler channelRegistered 信息: [id: 0x906f5639] REGISTERED 十一月 01, 2019 1:47:18 下午 io.netty.handler.logging.LoggingHandler connect 信息: [id: 0x906f5639] CONNECT: localhost/127.0.0.1:9527 十一月 01, 2019 1:47:18 下午 io.netty.handler.logging.LoggingHandler channelActive 信息: [id: 0x906f5639, L:/127.0.0.1:55743 - R:localhost/127.0.0.1:9527] ACTIVE
最新回复(0)