当前位置: 首页 > news >正文

Spring Boot 从Socket 到Netty网络编程(下):Netty基本开发与改进【心跳、粘包与拆包、闲置连接】

上一篇:《Spring Boot 从Socket 到Netty网络编程(上):SOCKET 基本开发(BIO)与改进(NIO)》

前言

        前文中我们简单介绍了基于Socket的BIO(阻塞式)与NIO(非阻塞式)网络编程,无疑NIO在网络传输中具备更先进;但是采用Socket的NIO需要大量的优化,对于开发人员来说我们要站在巨人的肩膀上开发,所以Netty才是真爱。

        TCP/IP  -> Socket ->Netty 是这样一个关系,        Netty是基于Socket,Socket是基于TCP/IP。

基本开发

Netty

个人总结:基于NIO,提供简洁的API,开发者专注于业务逻辑实现,而非关注于实现实现细节。

maven

<dependency><groupId>io.netty</groupId><artifactId>netty-all</artifactId><version>4.1.94.Final</version>
</dependency>

服务端 ServerBootstrap

服务

import io.netty.bootstrap.ServerBootstrap;
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.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;public class NettyServer {public static void main(String[] args) throws Exception {// 创建一个线程组,用于接收客户端的连接EventLoopGroup bossGroup = new NioEventLoopGroup();// 创建数据处理线程组,用于处理已经连接的客户端的数据读写操作EventLoopGroup workerGroup = new NioEventLoopGroup();try {//创建服务端的启动对象,设置参数ServerBootstrap bootstrap = new ServerBootstrap();//设置两个线程组boosGroup和workerGroupbootstrap.group(bossGroup, workerGroup)//设置服务端通道实现类型.channel(NioServerSocketChannel.class)//设置线程队列得到连接个数.option(ChannelOption.SO_BACKLOG, 128)//设置保持活动连接状态.childOption(ChannelOption.SO_KEEPALIVE, true)//使用匿名内部类的形式初始化通道对象.childHandler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel socketChannel) throws Exception {//给pipeline管道设置处理器socketChannel.pipeline().addLast(new NettyServerHandler());//服务端处理器}});//给workerGroup的EventLoop对应的管道设置处理器System.out.println("服务已启动...");//绑定端口号,启动服务端ChannelFuture channelFuture = bootstrap.bind(6666).sync();//对关闭通道进行监听channelFuture.channel().closeFuture().sync();} finally {bossGroup.shutdownGracefully();workerGroup.shutdownGracefully();}}
}

方法类


import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;public class NettyClient {public static void main(String[] args) throws Exception {NioEventLoopGroup eventExecutors = new NioEventLoopGroup();try {//创建bootstrap对象,配置参数Bootstrap bootstrap = new Bootstrap();//设置线程组bootstrap.group(eventExecutors)//设置客户端的通道实现类型.channel(NioSocketChannel.class)//使用匿名内部类初始化通道.handler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {//添加客户端通道的处理器ch.pipeline().addLast(new NettyClientHandler());//给pipeline管道设置处理器}});System.out.println("客户端准备就绪");//连接服务端ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 6666).sync();//对通道关闭进行监听channelFuture.channel().closeFuture().sync();} finally {//关闭线程组eventExecutors.shutdownGracefully();}}}

客户端 Bootstrap

客户端

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;public class NettyClient {public static void main(String[] args) throws Exception {NioEventLoopGroup eventExecutors = new NioEventLoopGroup();try {//创建bootstrap对象,配置参数Bootstrap bootstrap = new Bootstrap();//设置线程组bootstrap.group(eventExecutors)//设置客户端的通道实现类型.channel(NioSocketChannel.class)//使用匿名内部类初始化通道.handler(new ChannelInitializer<SocketChannel>() {@Overrideprotected void initChannel(SocketChannel ch) throws Exception {//添加客户端通道的处理器ch.pipeline().addLast(new NettyClientHandler());//给pipeline管道设置处理器}});System.out.println("客户端准备就绪");//连接服务端ChannelFuture channelFuture = bootstrap.connect("127.0.0.1", 6666).sync();//对通道关闭进行监听channelFuture.channel().closeFuture().sync();} finally {//关闭线程组eventExecutors.shutdownGracefully();}}}

方法类

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;import java.io.BufferedReader;
import java.io.InputStreamReader;public class NettyClientHandler extends ChannelInboundHandlerAdapter {@Overridepublic void channelActive(ChannelHandlerContext ctx) throws Exception {String userInput;System.out.println("请输入消息(输入 exit 断开连接):");BufferedReader console = new BufferedReader(new InputStreamReader(System.in));while ((userInput = console.readLine()) != null) {ctx.writeAndFlush(Unpooled.copiedBuffer(userInput , CharsetUtil.UTF_8));if ("exit".equalsIgnoreCase(userInput)) {System.out.println("连接已关闭。");break;}}}@Overridepublic void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {//接收服务端发送过来的消息ByteBuf byteBuf = (ByteBuf) msg;System.out.println("收到服务端" + ctx.channel().remoteAddress() + "的消息:" + byteBuf.toString(CharsetUtil.UTF_8));}
}

效果

优化加强

心跳机制

        为了解决主从之间的服务的断开与连接情况是否更深,所以需要用心跳做连接试验从而在业务上做断开重连等业务需求实现。

原理设计

  • IdleStateHandler 配置心跳
  • 继承类处理心跳事件:userEventTriggered
  • Pipeline中处理

 服务

方法

客户

方法

效果

处理粘包与拆包

        由于网络原因可能会出来数据切割不完整的情况,为了在拆解数据时能得到完整的数据需要用一定的约束使得双方统一成一个固定标准。

原理设计

  • 配置类型:一般使用动态长度 LengthFieldBasedFrameDecoder
  • 自定义加码与解码
  • 配置到 pipeline中去

代码

服务端

客户端

加码

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;import java.util.List;public class NetyyDecoder extends ByteToMessageDecoder {@Overrideprotected void decode(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf, List<Object> list) throws Exception {if (byteBuf.readableBytes() < 4){return;}byteBuf.markReaderIndex();int dataLength = byteBuf.readInt();byte[] data = new byte[dataLength];byteBuf.readBytes(data);list.add(new String(data, "UTF-8"));}
}

解码

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;import java.nio.charset.StandardCharsets;public class NetyyEncoder extends MessageToByteEncoder<String> {@Overrideprotected void encode(ChannelHandlerContext channelHandlerContext, String s, ByteBuf byteBuf) throws Exception {byte[] bytes = s.getBytes(StandardCharsets.UTF_8);int length = bytes.length;byteBuf.writeInt(length);byteBuf.writeBytes(bytes);}
}

后记

以上代码环境是JDK17、Spring Boot 3.X+ ,对Netty这个框架做了一些基本的介绍,当然凭这短短的一篇文章不可能做到详尽,仅供初学都参考。整理不易,如有帮助请收藏,转发请注明出处,关注博主不断更新更多技术文章。

http://www.lqws.cn/news/121933.html

相关文章:

  • java从azure中读取用户信息
  • Docker 常用命令详解
  • docker生命周期
  • Elasticsearch的搜索流程描述
  • 微软的新系统Windows12未来有哪些新特性
  • Python 隐藏法宝:双下划线 _ _Dunder_ _
  • stripe支付测试,ngrok无法使用?免费vscode端口转发,轻松简单!
  • Java Lombok @Data 注解用法详解
  • 打卡Day44
  • 吴恩达机器学习讲义概述
  • 泛型编程技巧——使用std::enable_if实现按类型进行条件编译​
  • 《Coevolutionary computation and its application》协同演化及其应用中文对照·第一章
  • [杰理]蓝牙状态机设计与实现详解
  • unix/linux,sudo,其高级使用
  • AI助力Java开发:减少70%重复编码,实战效能提升解析
  • [Harmony]颜色初始化
  • 【Linux】网络--传输层--深入理解TCP协议
  • 【产品业务设计】支付业务设计规范细节记录,含订单记录、支付业务记录、支付流水记录、退款业务记录
  • Flutter面试题
  • 【Linux】centos软件安装
  • 卫星在轨姿态控制技术详解:从自旋稳定到高精度闭环控制
  • 各个布局的区别以及示例
  • 【学习笔记】Circuit Tracing: Revealing Computational Graphs in Language Models
  • R语言基础| 下载、安装
  • 豆瓣图书评论数据分析与可视化
  • Nginx+Tomcat负载均衡与动静分离架构
  • 夏普比率(Sharpe ratio)​
  • MySQL EXPLAIN 命令详解
  • 【Python金融笔记】加载数据与可视化
  • MCP客户端Client开发流程