SpringBoot?Webflux創(chuàng)建TCP/UDP?server并使用handler解析數(shù)據(jù)
更新時(shí)間:2022年02月24日 09:55:10 作者:peng?chao
這篇文章主要介紹了SpringBoot?Webflux創(chuàng)建TCP/UDP?server并使用handler解析數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
1.pom依賴
引用spring-boot-starter-webflux依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2.創(chuàng)建UDP/TCP Server
package com.example.demo; import com.example.demo.handler.TcpDecoderHanlder; import com.example.demo.handler.UdpDecoderHanlder; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import reactor.core.publisher.Flux; import reactor.netty.tcp.TcpServer; import reactor.netty.udp.UdpServer; import java.time.Duration; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Bean CommandLineRunner serverRunner(UdpDecoderHandler udpDecoderHandler, TcpDecoderHandler tcpDecoderHandler) { return strings -> { createUdpServer(udpDecoderHandler); createTcpServer(tcpDecoderHandler); }; } /** * 創(chuàng)建UDP Server * @param udpDecoderHanlder: 解析UDP Client上報(bào)數(shù)據(jù)handler */ private void createUdpServer(UdpDecoderHandler udpDecoderHandler) { UdpServer.create() .handle((in,out) -> { in.receive() .asByteArray() .subscribe(); return Flux.never(); }) .port(8888) .doOnBound(conn -> conn.addHandlerLast("decoder",udpDecoderHandler)) //可以添加多個(gè)handler .bindNow(Duration.ofSeconds(30)); } /** * 創(chuàng)建TCP Server * @param tcpDecoderHanlder: 解析TCP Client上報(bào)數(shù)據(jù)的handler */ private void createTcpServer(TcpDecoderHandler tcpDecoderHandler) { TcpServer.create() .handle((in,out) -> { in.receive() .asByteArray() .subscribe(); return Flux.never(); }) .doOnConnection(conn -> conn.addHandler(tcpDecoderHandler)) //實(shí)例只寫了如何添加handler,可添加delimiter,tcp生命周期,decoder,encoder等handler .port(9999) .bindNow(); } }
3.數(shù)據(jù)解析handler(具體解析根據(jù)協(xié)議來)
解析UDP數(shù)據(jù)handler
package com.example.demo.handler; import io.netty.buffer.ByteBuf; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.socket.DatagramPacket; import io.netty.handler.codec.MessageToMessageDecoder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import java.util.List; @Service public class UdpDecoderHandler extends MessageToMessageDecoder<DatagramPacket> { private static final Logger LOGGER = LoggerFactory.getLogger(TcpDecoderHandler.class); @Override protected void decode(ChannelHandlerContext channelHandlerContext, DatagramPacket byteBuf, List<Object> list) throws Exception { ByteBuf byteBuf1 = byteBuf.content(); int size = byteBuf1.readableBytes(); byte[] data = new byte[size]; byteBuf1.readBytes(data); LOGGER.info(new String(data)); } }
解析TCP數(shù)據(jù)handler
package com.example.demo.handler; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToMessageDecoder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; import java.util.List; @Service public class TcpDecoderHandler extends MessageToMessageDecoder { private static final Logger LOGGER = LoggerFactory.getLogger(TcpDecoderHandler.class); @Override protected void decode(ChannelHandlerContext channelHandlerContext, Object o, List list){ LOGGER.info("解析client上報(bào)數(shù)據(jù)"); } }
4.測(cè)試工具
推薦使用SocketTool調(diào)試TCP/UDP協(xié)議
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中由substring方法引發(fā)的內(nèi)存泄漏詳解
這篇文章主要介紹了Java中由substring方法引發(fā)的內(nèi)存泄漏詳解,涉及substring方法引發(fā)的內(nèi)存泄漏簡(jiǎn)介,substring的作用和實(shí)現(xiàn)原理等相關(guān)內(nèi)容,具有一定借鑒價(jià)值,需要的朋友可以參考下2017-12-12Python動(dòng)態(tài)類型實(shí)現(xiàn)原理及過程解析
這篇文章主要介紹了Python動(dòng)態(tài)類型實(shí)現(xiàn)原理及過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08Java隊(duì)列同步器之CountDownLatch實(shí)現(xiàn)詳解
這篇文章主要介紹了Java隊(duì)列同步器之CountDownLatch實(shí)現(xiàn)詳解,CountDownLatch是一個(gè)同步工具類,它允許一個(gè)或多個(gè)線程一直等待,直到其他線程執(zhí)行完后再執(zhí)行,例如,應(yīng)用程序的主線程希望在負(fù)責(zé)啟動(dòng)框架服務(wù)的線程已經(jīng)啟動(dòng)所有框架服務(wù)之后執(zhí)行,需要的朋友可以參考下2023-12-12Java實(shí)現(xiàn)公眾號(hào)功能、關(guān)注及消息推送實(shí)例代碼
公眾號(hào)開發(fā)近些年是一個(gè)比較熱門的方向,下面這篇文章主要給大家介紹了關(guān)于Java實(shí)現(xiàn)公眾號(hào)功能、關(guān)注及消息推送的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11Springboot整合hutool驗(yàn)證碼的實(shí)例代碼
在 Spring Boot 中,你可以將 Hutool 生成驗(yàn)證碼的功能集成到 RESTful API 接口中,這篇文章主要介紹了Springboot整合hutool驗(yàn)證碼,需要的朋友可以參考下2024-08-08