成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Java 從零開(kāi)始手寫 RPC-Netty4 實(shí)現(xiàn)客戶端和服務(wù)端

開(kāi)發(fā) 后端
Netty是由JBOSS提供的一個(gè)java開(kāi)源框架,現(xiàn)為 Github上的獨(dú)立項(xiàng)目。Netty提供異步的、事件驅(qū)動(dòng)的網(wǎng)絡(luò)應(yīng)用程序框架和工具,用以快速開(kāi)發(fā)高性能、高可靠性的網(wǎng)絡(luò)服務(wù)器和客戶端程序。

[[428830]]

說(shuō)明

上一篇代碼基于 socket 的實(shí)現(xiàn)非常簡(jiǎn)單,但是對(duì)于實(shí)際生產(chǎn),一般使用 netty。

至于 netty 的優(yōu)點(diǎn)可以參考:

為什么選擇 netty?[1]

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

java 從零開(kāi)始手寫 RPC (02)-netty4 實(shí)現(xiàn)客戶端和服務(wù)端

代碼實(shí)現(xiàn)

maven 引入

  1. <dependency> 
  2.     <groupId>io.netty</groupId> 
  3.     <artifactId>netty-all</artifactId> 
  4.     <version>${netty.version}</version> 
  5. </dependency> 

引入 netty 對(duì)應(yīng)的 maven 包,此處為 4.1.17.Final。

服務(wù)端代碼實(shí)現(xiàn)

netty 的服務(wù)端啟動(dòng)代碼是比較固定的。

  1. package com.github.houbb.rpc.server.core; 
  2.  
  3.  
  4. import com.github.houbb.log.integration.core.Log; 
  5. import com.github.houbb.log.integration.core.LogFactory; 
  6. import com.github.houbb.rpc.server.constant.RpcServerConst; 
  7. import com.github.houbb.rpc.server.handler.RpcServerHandler; 
  8. import io.netty.bootstrap.ServerBootstrap; 
  9. import io.netty.channel.*; 
  10. import io.netty.channel.nio.NioEventLoopGroup; 
  11. import io.netty.channel.socket.nio.NioServerSocketChannel; 
  12.  
  13.  
  14. /** 
  15.  * rpc 服務(wù)端 
  16.  * @author binbin.hou 
  17.  * @since 0.0.1 
  18.  */ 
  19. public class RpcServer extends Thread { 
  20.  
  21.  
  22.     private static final Log log = LogFactory.getLog(RpcServer.class); 
  23.  
  24.  
  25.     /** 
  26.      * 端口號(hào) 
  27.      */ 
  28.     private final int port; 
  29.  
  30.  
  31.     public RpcServer() { 
  32.         this.port = RpcServerConst.DEFAULT_PORT; 
  33.     } 
  34.  
  35.  
  36.     public RpcServer(int port) { 
  37.         this.port = port; 
  38.     } 
  39.  
  40.  
  41.     @Override 
  42.     public void run() { 
  43.         // 啟動(dòng)服務(wù)端 
  44.         log.info("RPC 服務(wù)開(kāi)始啟動(dòng)服務(wù)端"); 
  45.  
  46.  
  47.         EventLoopGroup bossGroup = new NioEventLoopGroup(); 
  48.         EventLoopGroup workerGroup = new NioEventLoopGroup(); 
  49.  
  50.  
  51.         try { 
  52.             ServerBootstrap serverBootstrap = new ServerBootstrap(); 
  53.             serverBootstrap.group(workerGroup, bossGroup) 
  54.                     .channel(NioServerSocketChannel.class) 
  55.                     .childHandler(new ChannelInitializer<Channel>() { 
  56.                         @Override 
  57.                         protected void initChannel(Channel ch) throws Exception { 
  58.                             ch.pipeline().addLast(new RpcServerHandler()); 
  59.                         } 
  60.                     }) 
  61.                     // 這個(gè)參數(shù)影響的是還沒(méi)有被accept 取出的連接 
  62.                     .option(ChannelOption.SO_BACKLOG, 128) 
  63.                     // 這個(gè)參數(shù)只是過(guò)一段時(shí)間內(nèi)客戶端沒(méi)有響應(yīng),服務(wù)端會(huì)發(fā)送一個(gè) ack 包,以判斷客戶端是否還活著。 
  64.                     .childOption(ChannelOption.SO_KEEPALIVE, true); 
  65.  
  66.  
  67.             // 綁定端口,開(kāi)始接收進(jìn)來(lái)的鏈接 
  68.             ChannelFuture channelFuture = serverBootstrap.bind(port).syncUninterruptibly(); 
  69.             log.info("RPC 服務(wù)端啟動(dòng)完成,監(jiān)聽(tīng)【" + port + "】端口"); 
  70.  
  71.  
  72.             channelFuture.channel().closeFuture().syncUninterruptibly(); 
  73.             log.info("RPC 服務(wù)端關(guān)閉完成"); 
  74.         } catch (Exception e) { 
  75.             log.error("RPC 服務(wù)異常", e); 
  76.         } finally { 
  77.             workerGroup.shutdownGracefully(); 
  78.             bossGroup.shutdownGracefully(); 
  79.         } 
  80.     } 
  81.  
  82.  

為了簡(jiǎn)單,服務(wù)端啟動(dòng)端口號(hào)固定,RpcServerConst 常量類內(nèi)容如下:

  1. public final class RpcServerConst { 
  2.  
  3.  
  4.     private RpcServerConst(){} 
  5.  
  6.  
  7.     /** 
  8.      * 默認(rèn)端口 
  9.      * @since 0.0.1 
  10.      */ 
  11.     public static final int DEFAULT_PORT = 9627; 
  12.  
  13.  

RpcServerHandler

當(dāng)然,還有一個(gè)比較核心的類就是 RpcServerHandler

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

目前是空實(shí)現(xiàn),后續(xù)可以添加對(duì)應(yīng)的日志輸出及邏輯處理。

測(cè)試

啟動(dòng)測(cè)試的代碼非常簡(jiǎn)單:

  1. /** 
  2.  * 服務(wù)啟動(dòng)代碼測(cè)試 
  3.  * @param args 參數(shù) 
  4.  */ 
  5. public static void main(String[] args) { 
  6.     new RpcServer().start(); 

說(shuō)明

上面我們實(shí)現(xiàn)了服務(wù)端的實(shí)現(xiàn),這一節(jié)來(lái)一起看一下 client 客戶端代碼實(shí)現(xiàn)。

代碼實(shí)現(xiàn)

RpcClient

  1. /* 
  2.  * Copyright (c)  2019. houbinbin Inc. 
  3.  * rpc All rights reserved. 
  4.  */ 
  5.  
  6.  
  7. package com.github.houbb.rpc.client.core; 
  8.  
  9.  
  10. import com.github.houbb.log.integration.core.Log; 
  11. import com.github.houbb.log.integration.core.LogFactory; 
  12. import com.github.houbb.rpc.client.handler.RpcClientHandler; 
  13.  
  14.  
  15. import io.netty.bootstrap.Bootstrap; 
  16. import io.netty.channel.Channel; 
  17. import io.netty.channel.ChannelFuture; 
  18. import io.netty.channel.ChannelInitializer; 
  19. import io.netty.channel.ChannelOption; 
  20. import io.netty.channel.EventLoopGroup; 
  21. import io.netty.channel.nio.NioEventLoopGroup; 
  22. import io.netty.channel.socket.nio.NioSocketChannel; 
  23. import io.netty.handler.logging.LogLevel; 
  24. import io.netty.handler.logging.LoggingHandler; 
  25.  
  26.  
  27. /** 
  28.  * <p> rpc 客戶端 </p> 
  29.  * 
  30.  * <pre> Created: 2019/10/16 11:21 下午  </pre> 
  31.  * <pre> Project: rpc  </pre> 
  32.  * 
  33.  * @author houbinbin 
  34.  * @since 0.0.2 
  35.  */ 
  36. public class RpcClient extends Thread { 
  37.  
  38.  
  39.     private static final Log log = LogFactory.getLog(RpcClient.class); 
  40.  
  41.  
  42.     /** 
  43.      * 監(jiān)聽(tīng)端口號(hào) 
  44.      */ 
  45.     private final int port; 
  46.  
  47.  
  48.     public RpcClient(int port) { 
  49.         this.port = port; 
  50.     } 
  51.  
  52.  
  53.     public RpcClient() { 
  54.         this(9527); 
  55.     } 
  56.  
  57.  
  58.     @Override 
  59.     public void run() { 
  60.         // 啟動(dòng)服務(wù)端 
  61.         log.info("RPC 服務(wù)開(kāi)始啟動(dòng)客戶端"); 
  62.  
  63.  
  64.         EventLoopGroup workerGroup = new NioEventLoopGroup(); 
  65.  
  66.  
  67.         try { 
  68.             Bootstrap bootstrap = new Bootstrap(); 
  69.             ChannelFuture channelFuture = bootstrap.group(workerGroup) 
  70.                     .channel(NioSocketChannel.class) 
  71.                     .option(ChannelOption.SO_KEEPALIVE, true
  72.                     .handler(new ChannelInitializer<Channel>(){ 
  73.                         @Override 
  74.                         protected void initChannel(Channel ch) throws Exception { 
  75.                             ch.pipeline() 
  76.                                     .addLast(new LoggingHandler(LogLevel.INFO)) 
  77.                                     .addLast(new RpcClientHandler()); 
  78.                         } 
  79.                     }) 
  80.                     .connect("localhost", port) 
  81.                     .syncUninterruptibly(); 
  82.  
  83.  
  84.             log.info("RPC 服務(wù)啟動(dòng)客戶端完成,監(jiān)聽(tīng)端口:" + port); 
  85.             channelFuture.channel().closeFuture().syncUninterruptibly(); 
  86.             log.info("RPC 服務(wù)開(kāi)始客戶端已關(guān)閉"); 
  87.         } catch (Exception e) { 
  88.             log.error("RPC 客戶端遇到異常", e); 
  89.         } finally { 
  90.             workerGroup.shutdownGracefully(); 
  91.         } 
  92.     } 
  93.  
  94.  

.connect("localhost", port) 聲明了客戶端需要連接的服務(wù)端,此處和服務(wù)端的端口保持一致。

RpcClientHandler

客戶端處理類也比較簡(jiǎn)單,暫時(shí)留空。

  1. /* 
  2.  * Copyright (c)  2019. houbinbin Inc. 
  3.  * rpc All rights reserved. 
  4.  */ 
  5.  
  6.  
  7. package com.github.houbb.rpc.client.handler; 
  8.  
  9.  
  10. import io.netty.channel.ChannelHandlerContext; 
  11. import io.netty.channel.SimpleChannelInboundHandler; 
  12.  
  13.  
  14. /** 
  15.  * <p> 客戶端處理類 </p> 
  16.  * 
  17.  * <pre> Created: 2019/10/16 11:30 下午  </pre> 
  18.  * <pre> Project: rpc  </pre> 
  19.  * 
  20.  * @author houbinbin 
  21.  * @since 0.0.2 
  22.  */ 
  23. public class RpcClientHandler extends SimpleChannelInboundHandler { 
  24.  
  25.  
  26.     @Override 
  27.     protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { 
  28.         // do nothing. 
  29.     } 
  30.  
  31.  

啟動(dòng)測(cè)試

服務(wù)端

首先啟動(dòng)服務(wù)端。

客戶端

然后啟動(dòng)客戶端連接服務(wù)端,實(shí)現(xiàn)如下:

  1. /** 
  2.  * 服務(wù)啟動(dòng)代碼測(cè)試 
  3.  * @param args 參數(shù) 
  4.  */ 
  5. public static void main(String[] args) { 
  6.     new RpcClient().start(); 

小結(jié)

為了便于大家學(xué)習(xí),以上源碼已經(jīng)開(kāi)源:

https://github.com/houbb/rpc

我是老馬,期待與你的下次重逢。

References

[1] 為什么選擇 netty?: http://houbb.github.io/2019/05/10/netty-definitive-gudie-04-why-netty

 

責(zé)任編輯:姜華 來(lái)源: 今日頭條
相關(guān)推薦

2021-10-19 08:58:48

Java 語(yǔ)言 Java 基礎(chǔ)

2021-10-21 08:21:10

Java Reflect Java 基礎(chǔ)

2021-10-27 08:10:15

Java 客戶端 Java 基礎(chǔ)

2022-06-14 15:07:04

IPC客戶端服務(wù)端

2021-10-13 08:21:52

Java websocket Java 基礎(chǔ)

2018-12-20 08:50:53

TCPIP服務(wù)器

2010-03-01 16:10:32

Linux Samba

2012-05-07 13:55:41

JavaJava Web

2010-03-19 09:26:34

Java Socket

2021-10-20 08:05:18

Java 序列化 Java 基礎(chǔ)

2010-01-11 13:05:24

VNC server配

2022-01-05 08:03:23

C#通信Rest

2009-08-21 15:36:41

服務(wù)端與客戶端

2009-08-21 15:54:40

服務(wù)端與客戶端

2021-10-29 08:07:30

Java timeout Java 基礎(chǔ)

2023-11-17 09:13:36

2010-03-18 17:47:07

Java 多客戶端通信

2009-08-21 16:14:52

服務(wù)端與客戶端通信

2011-09-09 09:44:23

WCF

2009-08-21 15:59:22

服務(wù)端與客戶端通信
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)

主站蜘蛛池模板: 精品久久香蕉国产线看观看亚洲 | 欧美一区 | 欧美精品一区二区三区四区五区 | 欧美精品一区二区三 | 日本精品视频在线观看 | 久久综合久色欧美综合狠狠 | 国产欧美在线 | 久久久久久亚洲 | 成人h视频在线 | 久久久久久国产精品免费免费男同 | 成人在线国产 | 青青草精品视频 | 99热这里有精品 | 国产精品3区 | 亚洲精品在| 国产亚洲精品久久情网 | 久久久久国产精品午夜一区 | 北条麻妃99精品青青久久 | 国产成人精品一区二区三区四区 | 一级欧美 | 精品国产一区二区三区免费 | 久久一区| 黄色成人亚洲 | 在线成人| 国产一级淫片a直接免费看 免费a网站 | 国产大片黄色 | 日韩国产精品一区二区三区 | 91影片| 亚洲高清视频在线观看 | 亚洲精品一区二区在线观看 | 日韩在线免费视频 | 在线国产一区 | 天堂在线www | 久久国内| gogo肉体亚洲高清在线视 | 亚洲精品日韩视频 | 最新中文字幕久久 | 亚洲a视频 | 伊人影院99 | 成人精品国产 | 亚洲精品粉嫩美女一区 |