SpringBoot?Webflux創(chuàng)建TCP/UDP?server并使用handler解析數(shù)據(jù)
更新時間:2022年02月24日 09:55:10 作者:peng?chao
這篇文章主要介紹了SpringBoot?Webflux創(chuàng)建TCP/UDP?server并使用handler解析數(shù)據(jù),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
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上報數(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)) //可以添加多個handler
.bindNow(Duration.ofSeconds(30));
}
/**
* 創(chuàng)建TCP Server
* @param tcpDecoderHanlder: 解析TCP Client上報數(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)) //實例只寫了如何添加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上報數(shù)據(jù)");
}
}
4.測試工具
推薦使用SocketTool調(diào)試TCP/UDP協(xié)議

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
selenium-java實現(xiàn)自動登錄跳轉(zhuǎn)頁面方式
利用Selenium和Java語言可以編寫一個腳本自動刷新網(wǎng)頁,首先,需要確保Google瀏覽器和Chrome-Driver驅(qū)動的版本一致,通過指定網(wǎng)站下載對應版本的瀏覽器和驅(qū)動,在Maven項目中添加依賴,編寫腳本實現(xiàn)網(wǎng)頁的自動刷新,此方法適用于需要頻繁刷新網(wǎng)頁的場景,簡化了操作,提高了效率2024-11-11
springboot CompletableFuture異步線程池詳解
這篇文章主要介紹了springboot CompletableFuture異步線程池的使用,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2025-04-04

