通過(guò)入門demo簡(jiǎn)單了解netty使用方法
這篇文章主要介紹了通過(guò)入門demo簡(jiǎn)單了解netty使用方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
前言
最近做一個(gè)項(xiàng)目:
大概需求: 多個(gè)溫度傳感器不斷向java服務(wù)發(fā)送溫度數(shù)據(jù),該傳感器采用socket發(fā)送數(shù)據(jù);該數(shù)據(jù)以$符號(hào)開(kāi)頭和結(jié)尾,最后將處理的數(shù)據(jù)存入數(shù)據(jù)庫(kù);
我想到的處理方式:采用netty來(lái)接收和處理數(shù)據(jù),然后用mybatis將處理后的數(shù)據(jù)存入數(shù)據(jù)庫(kù);
我在這之前從來(lái)沒(méi)使用過(guò)netty,在網(wǎng)上倒是看到不少關(guān)于netty的文章,如今就趁著這個(gè)項(xiàng)目寫一下我所學(xué)到的東西和遇到的問(wèn)題,又是怎么去解決的;
接下來(lái)的幾篇文章都是圍繞著這個(gè)項(xiàng)目來(lái)寫的;本篇主要寫netty的入門demo;
正文
代碼部分
新建一個(gè)maven項(xiàng)目
首先在pom.xml中導(dǎo)入:
<!-- https://mvnrepository.com/artifact/io.netty/netty-all --> <dependency> <groupId>io.netty</groupId> <artifactId>netty-all</artifactId> <version>5.0.0.Alpha1</version> </dependency>
服務(wù)端
1. DiscardServer類,netty的服務(wù)端
public class DiscardServer { public void run(int port) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); System.out.println("準(zhǔn)備運(yùn)行端口:" + port); try { ServerBootstrap b = new ServerBootstrap(); b = b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .option(ChannelOption.SO_BACKLOG, 128) .childHandler(new ChildChannelHandler()); //綁定端口,同步等待成功 ChannelFuture f = b.bind(port).sync(); //等待服務(wù)監(jiān)聽(tīng)端口關(guān)閉 f.channel().closeFuture().sync(); } finally { //退出,釋放線程資源 workerGroup.shutdownGracefully(); bossGroup.shutdownGracefully(); } } public static void main(String[] args) throws Exception { new DiscardServer().run(8080); } }
2. ChildChannelHandler類:
public class ChildChannelHandler extends ChannelInitializer<SocketChannel> { protected void initChannel(SocketChannel socketChannel) throws Exception { socketChannel.pipeline().addLast(new DiscardServerHandler()); } }
3. DiscardServerHandler類
在這里是繼承的ChannelHandlerAdapter類,當(dāng)然還可以繼承其他的類,例如SimpleChannelInboundHandler,ChannelInboundHandlerAdapter都可以
public class DiscardServerHandler extends ChannelHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { try { ByteBuf in = (ByteBuf) msg; System.out.println("傳輸內(nèi)容是"); System.out.println(in.toString(CharsetUtil.UTF_8)); ByteBuf resp= Unpooled.copiedBuffer("收到信息$".getBytes()); ctx.writeAndFlush(resp); } finally { ReferenceCountUtil.release(msg); } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // 出現(xiàn)異常就關(guān)閉 cause.printStackTrace(); ctx.close(); } }
啟動(dòng)netty服務(wù);
好了,到這里就能開(kāi)始接收數(shù)據(jù)了;
客服端
1.TimeClient類
public class TimeClient { public void connect(int port,String host)throws Exception{ //配置客戶端 System.out.println(port+"--"+host); EventLoopGroup eventLoopGroup=new NioEventLoopGroup(); try { Bootstrap b=new Bootstrap(); b.group(eventLoopGroup).channel(NioSocketChannel.class) .option(ChannelOption.TCP_NODELAY,true) .handler(new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel socketChannel) throws Exception { socketChannel.pipeline().addLast(new TimeClientHandler()); } }); //綁定端口,同步等待成功 ChannelFuture f = b.connect(host,port).sync(); //等待服務(wù)監(jiān)聽(tīng)端口關(guān)閉 f.channel().closeFuture().sync(); }finally { //優(yōu)雅退出,釋放線程資源 eventLoopGroup.shutdownGracefully(); } } public static void main(String[] args) throws Exception { new TimeClient().connect(8090,"localhost"); } }
2.TimeClientHandler 類
public class TimeClientHandler extends ChannelHandlerAdapter { private byte[] req; public TimeClientHandler(){ req="$tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00$".getBytes(); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { ByteBuf message=null; for(int i=0;i<100;i++){ message=Unpooled.buffer(req.length); message.writeBytes(req); ctx.writeAndFlush(message); } } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) { try { ByteBuf in = (ByteBuf) msg; System.out.println(in.toString(CharsetUtil.UTF_8)); } finally { ReferenceCountUtil.release(msg); } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { // 出現(xiàn)異常就關(guān)閉 cause.printStackTrace(); ctx.close(); } }
在channelActive類中向服務(wù)端發(fā)送100次消息
先啟動(dòng)服務(wù)端,再啟動(dòng)客戶端;
測(cè)試結(jié)果一:
服務(wù)端:
傳輸內(nèi)容是 $tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00$$tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00$$tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00$$tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00$$tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00$$tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00$$tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00$$tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00$$tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00$$tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00$$tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00$$tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00$$tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00$$tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00$$tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00$$tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00$$tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00$$tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00$$tmb00035ET3318/08/22 11:5704026.7 傳輸內(nèi)容是 5,027.31,20.00,20.00$$tmb00035ET3318/08/22
客戶端:
8080--localhost 收到信息收到信息收到信息收到信息收到信息收到信息收到信息收到信息收到信息收到信息收到信息收到信息收到信息收到信息收到信息收到信息收到信息收到信息收到信息收到信息收到信息收到信息收到信息收到信息收到信息收到信息收到信息收到信息
由于內(nèi)容太多,就不都貼出來(lái)了j,直接寫結(jié)果吧:
客戶端發(fā)送100次數(shù)據(jù),但是服務(wù)端只收到了28次,然后服務(wù)端向客戶端返回28次數(shù)據(jù),客戶端卻只收到一次;
可以發(fā)現(xiàn)服務(wù)端接收的數(shù)據(jù)不是完整接收的,這里出現(xiàn)了拆包,粘包的問(wèn)題
這里就不討論拆包,粘包了,百度一大堆,相信你也能看明白;
解決粘包,拆包的問(wèn)題
解決拆包粘包的方法有很多:
- 消息定長(zhǎng),固定每個(gè)消息的固定長(zhǎng)度
- 在消息末尾使用換行符對(duì)消息進(jìn)行分割,或者使用其他特殊字符來(lái)對(duì)消息進(jìn)行分割;
- 將消息分為消息頭和消息體,消息頭中包含標(biāo)識(shí)消息總長(zhǎng)度;
- 更復(fù)雜的,或者其他的協(xié)議。
由于我負(fù)責(zé)的這個(gè)項(xiàng)目戶端發(fā)送是由$開(kāi)始和結(jié)束的數(shù)據(jù),返回的數(shù)據(jù)我也設(shè)置的$結(jié)束,所以我選擇了第二種方法;
只需要在服務(wù)端的DiscardServerHandler中和客戶端的ChannelInitializer中添加幾行相同的代碼就行了;
服務(wù)端:
public class ChildChannelHandler extends ChannelInitializer<SocketChannel> { protected void initChannel(SocketChannel socketChannel) throws Exception { ByteBuf byteBuf= Unpooled.copiedBuffer("$".getBytes()); socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,byteBuf)); socketChannel.pipeline().addLast(new DiscardServerHandler()); } }
客戶端:
在如下的位置添加如下的代碼:
.handler(new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel socketChannel) throws Exception { ByteBuf byteBuf= Unpooled.copiedBuffer("$".getBytes()); socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(1024,byteBuf)); socketChannel.pipeline().addLast(new TimeClientHandler()); } });
測(cè)試結(jié)果
這里我就不發(fā)送100次數(shù)據(jù)了,值發(fā)送10次:
服務(wù)端:
傳輸內(nèi)容是 tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00 傳輸內(nèi)容是 tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00 傳輸內(nèi)容是 tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00 傳輸內(nèi)容是 tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00 傳輸內(nèi)容是 tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00 傳輸內(nèi)容是 tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00 傳輸內(nèi)容是 tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00 傳輸內(nèi)容是 tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00 傳輸內(nèi)容是 tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00 傳輸內(nèi)容是 tmb00035ET3318/08/22 11:5704026.75,027.31,20.00,20.00
客戶端:
收到信息 收到信息 收到信息 收到信息 收到信息 收到信息 收到信息 收到信息 收到信息 收到信息
解決我所遇到的問(wèn)題了;
總結(jié)
- 本來(lái)我只需要寫服務(wù)端的代碼的,但是為了更好的演示,所以我寫了客戶端
- 本篇文章主要就是使用netty發(fā)送和接收數(shù)據(jù),還有就是拆包和粘包的問(wèn)題,當(dāng)然,netty還可以做其他很多的事情;
- netty針對(duì)對(duì)拆包粘包的問(wèn)題有很多種解決辦法:例如可以用LineBasedFrameDecoder和StringDecoder組合將信息已換行符來(lái)進(jìn)行拆分;也可以用我上邊的解決方法來(lái)解決以特殊字符結(jié)束的信息;
- 在解決拆包粘包信息的時(shí)候,注意信息是否符合定義的規(guī)則,不然會(huì)處理不了數(shù)據(jù):例如我上邊的例子,如果服務(wù)端在返回信息是不以$符結(jié)尾的話,客戶端是打印不出來(lái)信息的,因?yàn)榭蛻舳藭?huì)認(rèn)為服務(wù)端還沒(méi)有發(fā)送完信息,會(huì)一直等待,而且打印不出數(shù)據(jù);
- 這篇文章只是我入門netty的一個(gè)小demo,對(duì)我還是很有幫助的,當(dāng)然也希望對(duì)閱讀者有那么一點(diǎn)點(diǎn)幫助;
- 有什么不對(duì)的地方還請(qǐng)指正,建議也是多多益善;
- 源碼地址
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的網(wǎng)絡(luò)爬蟲(chóng)代碼示例
這篇文章主要介紹了java實(shí)現(xiàn)一個(gè)簡(jiǎn)單的網(wǎng)絡(luò)爬蟲(chóng)代碼示例,還是挺不錯(cuò)的,這里分享給大家,需要的朋友可以參考下。2017-11-11java截取字符串中的指定字符的兩種方法(以base64圖片為例)
本文介紹了使用Java截取字符串中指定字符的方法,通過(guò)substring索引和正則實(shí)現(xiàn),文章詳細(xì)介紹了實(shí)現(xiàn)步驟和示例代碼,對(duì)于想要了解如何使用Java截取字符串指定字符的讀者具有一定的參考價(jià)值2023-08-08Mac使用Idea配置傳統(tǒng)SSM項(xiàng)目(非maven項(xiàng)目)
本文主要介紹了Mac使用Idea配置傳統(tǒng)SSM項(xiàng)目(非maven項(xiàng)目),將展示如何設(shè)置項(xiàng)目結(jié)構(gòu)、添加依賴關(guān)系等,具有一定的參考價(jià)值,感興趣的可以了解一下2024-01-01

創(chuàng)建網(wǎng)關(guān)項(xiàng)目(Spring Cloud Gateway)過(guò)程詳解

SpringBoot統(tǒng)計(jì)一個(gè)Bean中方法的調(diào)用次數(shù)的實(shí)現(xiàn)步驟

關(guān)于Java中BeanMap進(jìn)行對(duì)象與Map的相互轉(zhuǎn)換問(wèn)題

idea install 時(shí)提示jdk的某個(gè)jar包的包不存在的問(wèn)題

java對(duì)象轉(zhuǎn)化成String類型的四種方法小結(jié)