netty 快速入门 Demo 程序

转自: https://blog.csdn.net/the_fool_/article/details/80611148

多处摘抄或手打,为了十积分厚着脸皮标为原创,惭愧惭愧~本篇文章用于快速入门搭建一个简单的netty

应用,如想稍微深入系统的了解,请参照本人下一篇博客,链接:

参考地址:

官方文档:http://netty.io/wiki/user-guide-for-4.x.html

文案部分:

百度百科:https://baike.baidu.com/item/Netty/10061624?fr=aladdin

NIO:https://blog.csdn.net/skiof007/article/details/52873421

代码部分

https://blog.csdn.net/hangge110/article/details/51613988

更多参考:

各个对象以及常量的作用对照源码:https://blog.csdn.net/CoffeeAndIce/article/details/78987542

源代码学习1:https://www.cnblogs.com/katsura/p/5991428.html

源代码学习2:https://www.cnblogs.com/stevenczp/p/7581940.html

Netty

    Netty是由JBOSS提供的一个java开源框架。Netty提供异步的、事件驱动的网络应用程序框架和工具,用以快速开发高性能、高可靠性的网络服务器和客户端程序。

也就是说,Netty 是一个基于NIO的客户、服务器端编程框架,使用Netty 可以确保你快速和简单的开发出一个网络应用,例如实现了某种协议的客户、服务端应用。Netty相当于简化和流线化了网络应用的编程开发过程,例如:基于TCP和UDP的socket服务开发。

NIO基本概念:点击打开链接

IO的方式通常分为几种,同步阻塞的BIO、同步非阻塞的NIO、异步非阻塞的AIO......

DEMO(手打https://blog.csdn.net/hangge110/article/details/51613988):

1、服务端

 
  1. package com.zx.test;

  2.  
  3. import com.lk.netty.mulchat.dome.ServerIniterHandler;

  4. import com.lk.netty.mulchat.dome.ServerMain;

  5. import io.netty.bootstrap.ServerBootstrap;

  6. import io.netty.channel.*;

  7. import io.netty.channel.nio.NioEventLoopGroup;

  8. import io.netty.channel.socket.SocketChannel;

  9. import io.netty.channel.socket.nio.NioServerSocketChannel;

  10. import org.omg.CORBA.PRIVATE_MEMBER;

  11.  
  12. /**

  13. * 服务端生产者对象

  14. * This is an netty Server

  15. * @author ZX

  16. * @date 2018年6月7日

  17. * Netty中,通讯的双方建立连接后,会把数据按照ByteBuf的方式进行传输,

  18. * 例如http协议中,就是通过HttpRequestDecoder对ByteBuf数据流进行处理,转换成http的对象。

  19. * 深入学习:

  20. * https://www.cnblogs.com/katsura/p/5991428.html

  21. * https://www.cnblogs.com/stevenczp/p/7581940.html

  22. */

  23. public class ServerTest {

  24. /**

  25. * 服务端口

  26. */

  27. private int port=9999;

  28. /**

  29. * 开启服务的方法

  30. */

  31. public void StartNetty(){

  32.  
  33. /**创建两个EventLoop的组,EventLoop 这个相当于一个处理线程,

  34. 是Netty接收请求和处理IO请求的线程。不理解的话可以百度NIO图解*/

  35. /*

  36. 相关资料:NioEventLoopGroup是一个处理I/O操作的多线程事件循环。

  37. Netty为不同类型的传输提供了各种EventLoopGroup实现。

  38. 在本例中,我们正在实现一个服务器端应用程序,因此将使用两个NioEventLoopGroup。

  39. 第一个,通常称为“boss”,接受传入的连接。

  40. 第二个,通常称为“worker”,当boss接受连接并注册被接受的连接到worker时,处理被接受连接的流量。

  41. 使用了多少线程以及如何将它们映射到创建的通道取决于EventLoopGroup实现,甚至可以通过构造函数进行配置。

  42. */

  43. EventLoopGroup acceptor = new NioEventLoopGroup();

  44. EventLoopGroup worker = new NioEventLoopGroup();

  45. try {

  46. //1、创建启动类

  47. ServerBootstrap bootstrap = new ServerBootstrap();

  48. //2、配置启动参数等

  49. /**设置循环线程组,前者用于处理客户端连接事件,后者用于处理网络IO(server使用两个参数这个)

  50. *public ServerBootstrap group(EventLoopGroup group)

  51. *public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup)

  52. */

  53. bootstrap.group(acceptor,worker);

  54. /**设置选项

  55. * 参数:Socket的标准参数(key,value),可自行百度

  56. * eg:

  57. * bootstrap.option(ChannelOption.SO_BACKLOG, 1024);

  58. *bootstrap.option(ChannelOption.SO_KEEPALIVE, true);

  59. * */

  60. bootstrap.option(ChannelOption.SO_BACKLOG, 1024);

  61.  
  62. //用于构造socketchannel工厂

  63. bootstrap.channel(NioServerSocketChannel.class);

  64. /**

  65. * 传入自定义客户端Handle(服务端在这里搞事情)

  66. */

  67. bootstrap.childHandler(new ChannelInitializer<SocketChannel>() {

  68. @Override

  69. public void initChannel(SocketChannel ch) throws Exception {

  70. // 注册handler

  71. ch.pipeline().addLast(new SimpleServerHandler());

  72. }

  73. });

  74.  
  75. // 绑定端口,开始接收进来的连接

  76. ChannelFuture f = bootstrap.bind(port).sync();

  77.  
  78.  
  79. // 等待服务器 socket 关闭 。

  80. f.channel().closeFuture().sync();

  81. } catch (InterruptedException e) {

  82. e.printStackTrace();

  83. } finally {

  84. acceptor.shutdownGracefully();

  85. worker.shutdownGracefully();

  86. }

  87.  
  88. }

  89.  
  90.  
  91.  
  92. public static void main(String[] args) {

  93. new ServerTest().StartNetty();

  94. }

  95. }


 

2、服务端处理类

 
  1. package com.zx.test;

  2.  
  3. import io.netty.buffer.ByteBuf;

  4. import io.netty.channel.ChannelHandlerContext;

  5. import io.netty.channel.ChannelInboundHandlerAdapter;

  6.  
  7. public class SimpleServerHandler extends ChannelInboundHandlerAdapter {

  8. /**

  9. * 本方法用于读取客户端发送的信息

  10. * @param ctx

  11. * @param msg

  12. * @throws Exception

  13. */

  14. @Override

  15. public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

  16. System.out.println("SimpleServerHandler.channelRead");

  17. ByteBuf result = (ByteBuf) msg;

  18. byte[] result1 = new byte[result.readableBytes()];

  19. // msg中存储的是ByteBuf类型的数据,把数据读取到byte[]中

  20. result.readBytes(result1);

  21. String resultStr = new String(result1);

  22. // 接收并打印客户端的信息

  23. System.out.println("Client said:" + resultStr);

  24. // 释放资源,这行很关键

  25. result.release();

  26.  
  27. // 向客户端发送消息

  28. String response = "hello client!";

  29. // 在当前场景下,发送的数据必须转换成ByteBuf数组

  30. ByteBuf encoded = ctx.alloc().buffer(4 * response.length());

  31. encoded.writeBytes(response.getBytes());

  32. ctx.write(encoded);

  33. ctx.flush();

  34. }

  35.  
  36. /**

  37. * 本方法用作处理异常

  38. * @param ctx

  39. * @param cause

  40. * @throws Exception

  41. */

  42. @Override

  43. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

  44. // 当出现异常就关闭连接

  45. cause.printStackTrace();

  46. ctx.close();

  47. }

  48.  
  49. /**

  50. * 信息获取完毕后操作

  51. * @param ctx

  52. * @throws Exception

  53. */

  54. @Override

  55. public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {

  56. ctx.flush();

  57. }

  58.  
  59. }

3、客户端

 
  1. package com.zx.test;

  2. import io.netty.bootstrap.Bootstrap;

  3. import io.netty.bootstrap.ServerBootstrap;

  4. import io.netty.channel.ChannelFuture;

  5. import io.netty.channel.ChannelInitializer;

  6. import io.netty.channel.ChannelOption;

  7. import io.netty.channel.EventLoopGroup;

  8. import io.netty.channel.nio.NioEventLoopGroup;

  9. import io.netty.channel.socket.SocketChannel;

  10. import io.netty.channel.socket.nio.NioServerSocketChannel;

  11. import io.netty.channel.socket.nio.NioSocketChannel;

  12.  
  13. /**

  14. * 客户端消费者对象

  15. * This is an netty Client

  16. * @author ZX

  17. * @date 2018年6月7日

  18. */

  19. public class ClientTest {

  20. public void connect(String host, int port) throws Exception {

  21. EventLoopGroup worker = new NioEventLoopGroup();

  22. try {

  23. Bootstrap b = new Bootstrap();

  24. /**

  25. *EventLoop的组

  26. */

  27. b.group(worker);

  28. /**

  29. * 用于构造socketchannel工厂

  30. */

  31. b.channel(NioSocketChannel.class);

  32. /**设置选项

  33. * 参数:Socket的标准参数(key,value),可自行百度

  34. 保持呼吸,不要断气!

  35. * */

  36. b.option(ChannelOption.SO_KEEPALIVE, true);

  37. /**

  38. * 自定义客户端Handle(客户端在这里搞事情)

  39. */

  40. b.handler(new ChannelInitializer<SocketChannel>() {

  41. @Override

  42. public void initChannel(SocketChannel ch) throws Exception {

  43. ch.pipeline().addLast(new SimpleClientHandler());

  44. }

  45. });

  46. /** 开启客户端监听*/

  47. ChannelFuture f = b.connect(host, port).sync();

  48. /**等待数据直到客户端关闭*/

  49. f.channel().closeFuture().sync();

  50. } finally {

  51. worker.shutdownGracefully();

  52. }

  53. }

  54.  
  55. public static void main(String[] args) throws Exception {

  56. ClientTest client=new ClientTest();

  57. client.connect("127.0.0.1", 9999);

  58.  
  59. }

  60. }

4、客户端处理类

 
  1. package com.zx.test;

  2.  
  3.  
  4. import io.netty.buffer.ByteBuf;

  5. import io.netty.channel.ChannelHandlerContext;

  6. import io.netty.channel.ChannelInboundHandlerAdapter;

  7. public class SimpleClientHandler extends ChannelInboundHandlerAdapter {

  8. /**

  9. * 本方法用于接收服务端发送过来的消息

  10. * @param ctx

  11. * @param msg

  12. * @throws Exception

  13. */

  14. @Override

  15. public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

  16. System.out.println("SimpleClientHandler.channelRead");

  17. ByteBuf result = (ByteBuf) msg;

  18. byte[] result1 = new byte[result.readableBytes()];

  19. result.readBytes(result1);

  20. System.out.println("Server said:" + new String(result1));

  21. result.release();

  22. }

  23.  
  24. /**

  25. * 本方法用于处理异常

  26. * @param ctx

  27. * @param cause

  28. * @throws Exception

  29. */

  30. @Override

  31. public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

  32. // 当出现异常就关闭连接

  33. cause.printStackTrace();

  34. ctx.close();

  35. }

  36.  
  37.  
  38. /**

  39. * 本方法用于向服务端发送信息

  40. * @param ctx

  41. * @throws Exception

  42. */

  43. @Override

  44. public void channelActive(ChannelHandlerContext ctx) throws Exception {

  45. String msg = "hello Server!";

  46. ByteBuf encoded = ctx.alloc().buffer(4 * msg.length());

  47. encoded.writeBytes(msg.getBytes());

  48. ctx.write(encoded);

  49. ctx.flush();

  50. }

  51. }

5、运行结果:

服务端:

SimpleServerHandler.channelRead
Client said:hello Server!
客户端:
SimpleClientHandler.channelRead

Server said:hello client!

后话:

      好了,看完整篇博文,复制代码,跑起来了!然后你也有了解决问题的思路,不管对错能解决问题了。但是每个对象,每个参数是什么意思能搞懂吗?博主不才,虽然博主后来再次添加了很多注释,但是对很多对象以及参数理解不深,也不知道自己的用法是否是出于设计者本意!

      所以,下一篇博主会稍微深入一些介绍对象以及使用,会让你更加了解自己使用的这个NIO中间件工具,若是想成为专家大牛,就自己去下载源码分析吧~博主目前在拓展知识体系阶段,还没有如此多精力去深入,这个需要很多技巧。

    废话不多说了,下一篇地址:https://blog.csdn.net/the_fool_/article/details/80623299

--------------------- 本文来自 the_fool_ 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/the_fool_/article/details/80611148?utm_source=copy

猜你喜欢

转载自blog.csdn.net/hemeinvyiqiluoben/article/details/82947401