Due to work needs, the process of SpringBoot building TCP communication was studied. For those who need the project, they just want to quickly build an available service.
I have read a lot of other tutorials, and I feel that it is too complicated and easy to mess up. Here I will only talk about efficiency and show the rapid construction process.
TCPServer
Since the TCP protocol is implemented by Netty, the Netty dependencies are introduced
<dependency> <groupId></groupId> <artifactId>netty-all</artifactId> <version>4.1.</version> </dependency>
Configure TCPServer
@Component @Slf4j @Data @ConfigurationProperties(prefix = "") 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(); (bossGroup, workerGroup) .channel() .childHandler(new ChannelInitializer<Channel>() { @Override protected void initChannel(Channel channel) throws Exception { ChannelPipeline pipeline = (); (new StringEncoder()); (new StringDecoder()); (new TCPServerHandler()); } }) .option(ChannelOption.SO_BACKLOG, 128) .childOption(ChannelOption.SO_KEEPALIVE, true); ChannelFuture future = (port).sync(); ("TCP server started and listening on port " + port); ().closeFuture().sync(); } finally { (); (); } } }
Configuration File
tcp: server: port: 8888 #Server Port
Configure TCPServerHandler
@Slf4j @Component public class TCPServerHandler extends SimpleChannelInboundHandler<String> { @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) { ("Received client message:/n"+ msg); Object parse = (msg); ("parse = " + parse); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { ("Exception occurred in TCPServer", cause); (); } }
TCPClient
The configuration of the client is similar
Netty dependencies
<dependency> <groupId></groupId> <artifactId>netty-all</artifactId> <version>4.1.</version> </dependency>
Configure TCPClient
@Component @Slf4j @Data @ConfigurationProperties(prefix = "") 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() .handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = (); (new StringEncoder()); (new StringDecoder()); (new TCPClientHandler()); } }); ChannelFuture future = (host, port).sync(); ("TCPClient Start , Connect host:"+host+":"+port); ().closeFuture().sync(); } catch (Exception e) { ("TCPClient Error", e); } finally { (); } } }
Configuration File
tcp: client: port: 8080 #Connected server port host: 127.0.0.1 #The domain name of the connected server
Configure TCPServerHandler
@Slf4j @Component public class TCPClientHandler extends SimpleChannelInboundHandler<String> { @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) { ("Receive TCPServer Message:\n"+ msg); } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { ("TCPClient Error", cause); (); } }
This completes the entire construction process. The important thing is the server port and the client to connect to the server URL and handle messages. You can slowly explore other details by yourself.
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.