由于工作需要,研究了springboot搭建tcp通信的过程,对于工程需要的小伙伴,只是想快速搭建一个可用的服务。
其他的教程看了许多,感觉讲得太复杂,很容易弄乱,这里我只讲效率,展示快速搭建过程。
tcpserver
由于tcp协议是netty实现的,所以引入netty的依赖
<dependency> <groupid>io.netty</groupid> <artifactid>netty-all</artifactid> <version>4.1.25.final</version> </dependency>
配置tcpserver
@component @slf4j @data @configurationproperties(prefix = "tcp.server") public class tcpserver implements commandlinerunner { private integer port; @override public void run(string... args) throws exception { eventloopgroup bossgroup = new nioeventloopgroup(); eventloopgroup workergroup = new nioeventloopgroup(); try { serverbootstrap bootstrap = new serverbootstrap(); bootstrap.group(bossgroup, workergroup) .channel(nioserversocketchannel.class) .childhandler(new channelinitializer<channel>() { @override protected void initchannel(channel channel) throws exception { channelpipeline pipeline = channel.pipeline(); pipeline.addlast(new stringencoder()); pipeline.addlast(new stringdecoder()); pipeline.addlast(new tcpserverhandler()); } }) .option(channeloption.so_backlog, 128) .childoption(channeloption.so_keepalive, true); channelfuture future = bootstrap.bind(port).sync(); log.info("tcp server started and listening on port " + port); future.channel().closefuture().sync(); } finally { workergroup.shutdowngracefully(); bossgroup.shutdowngracefully(); } } }
application.yml配置文件
tcp: server: port: 8888 #服务器端口
配置tcpserverhandler
@slf4j @component public class tcpserverhandler extends simplechannelinboundhandler<string> { @override protected void channelread0(channelhandlercontext ctx, string msg) { log.info("收到客户端消息:/n"+ msg); object parse = jsonutils.parse(msg); system.out.println("parse = " + parse); } @override public void exceptioncaught(channelhandlercontext ctx, throwable cause) { log.error("tcpserver出现异常", cause); ctx.close(); } }
tcpclient
客户端的配置大同小异
netty依赖
<dependency> <groupid>io.netty</groupid> <artifactid>netty-all</artifactid> <version>4.1.25.final</version> </dependency>
配置tcpclient
@component @slf4j @data @configurationproperties(prefix = "tcp.client") public class tcpclient implements commandlinerunner { private string host ; private integer port; @override public void run(string... args) throws exception { eventloopgroup group = new nioeventloopgroup(); try { bootstrap bootstrap = new bootstrap() .group(group) .channel(niosocketchannel.class) .handler(new channelinitializer<socketchannel>() { @override protected void initchannel(socketchannel ch) throws exception { channelpipeline pipeline = ch.pipeline(); pipeline.addlast(new stringencoder()); pipeline.addlast(new stringdecoder()); pipeline.addlast(new tcpclienthandler()); } }); channelfuture future = bootstrap.connect(host, port).sync(); log.info("tcpclient start , connect host:"+host+":"+port); future.channel().closefuture().sync(); } catch (exception e) { log.error("tcpclient error", e); } finally { group.shutdowngracefully(); } } }
application.yml配置文件
tcp: client: port: 8080 #连接的服务器端口 host: 127.0.0.1 #连接的服务器域名
配置tcpserverhandler
@slf4j @component public class tcpclienthandler extends simplechannelinboundhandler<string> { @override protected void channelread0(channelhandlercontext ctx, string msg) { log.info("receive tcpserver message:\n"+ msg); } @override public void exceptioncaught(channelhandlercontext ctx, throwable cause) { log.error("tcpclient error", cause); ctx.close(); } }
这样就完成了整个搭建过程,重要的就是服务端的端口和客户端连接服务端的url和接受消息的处理方式,其他的细节可以自己慢慢探索。
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论