1:在项目中,需要接收其他服务的TCP与UDP请求,此时使用Netty可能会过度设计,想要一个轻量级nio的TCP、UDP服务端的话,我们可以选择 Spring Integration
2:pom.xml
<?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 https://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.4.8</ version>
< relativePath/>
</ parent>
< groupId> com.demo</ groupId>
< artifactId> tcp-and-udp</ artifactId>
< version> 0.0.1-SNAPSHOT</ version>
< name> tcp-udp-message</ name>
< description> tcp与udp通讯</ description>
< properties>
< java.version> 21</ java.version>
</ properties>
< dependencies>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-web</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.integration</ groupId>
< artifactId> spring-integration-ip</ artifactId>
</ dependency>
</ dependencies>
< build>
< plugins>
< plugin>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-maven-plugin</ artifactId>
</ plugin> < plugin> < groupId> org.apache.maven.plugins</ groupId> < artifactId> maven-compiler-plugin</ artifactId> < configuration> < source> 8</ source> < target> 8</ target> </ configuration> </ plugin>
</ plugins>
</ build>
</ project>
3:建立TCP服务端(注意:在发送tcp报文的时候,tcp报文需要以\r\n结尾,否则无法正常接收报文,如果需要发送、\r类的内容,需要加\,变成\r)
package com. demo. tcpAndUdp ;
import lombok. extern. slf4j. Slf4j ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
import org. springframework. integration. annotation. ServiceActivator ;
import org. springframework. integration. channel. DirectChannel ;
import org. springframework. integration. ip. tcp. TcpReceivingChannelAdapter ;
import org. springframework. integration. ip. tcp. connection. AbstractServerConnectionFactory ;
import org. springframework. integration. ip. tcp. connection. TcpNioServerConnectionFactory ;
@Slf4j
@Configuration
public class TcpServerConfig {
private int PORT = 8989 ;
@Bean
public AbstractServerConnectionFactory serverConnectionFactory ( ) {
TcpNioServerConnectionFactory tcpNioServerConnectionFactory = new TcpNioServerConnectionFactory ( PORT ) ;
tcpNioServerConnectionFactory. setUsingDirectBuffers ( true ) ;
return tcpNioServerConnectionFactory;
}
@Bean
public DirectChannel tcpReceiveChannel ( ) {
return new DirectChannel ( ) ;
}
@Bean
public TcpReceivingChannelAdapter inboundAdapter ( ) {
TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter ( ) ;
adapter. setConnectionFactory ( serverConnectionFactory ( ) ) ;
adapter. setOutputChannelName ( "tcpReceiveChannel" ) ;
return adapter;
}
@ServiceActivator ( inputChannel = "tcpReceiveChannel" )
public void messageReceiver ( byte [ ] message) {
String payload = new String ( message) ;
System . out. println ( "Received message: " + payload) ;
log. info ( "处理TCP请求:" + message) ;
}
}
4:建立UDP服务端(注意:发送中文可能会乱码)
package com. demo. study. mybatis. tcpAndUdp ;
import org. springframework. context. annotation. Bean ;
import org. springframework. context. annotation. Configuration ;
import org. springframework. integration. annotation. ServiceActivator ;
import org. springframework. integration. channel. DirectChannel ;
import org. springframework. integration. ip. udp. UnicastReceivingChannelAdapter ;
import java. nio. charset. StandardCharsets ;
@Configuration
public class UdpServerConfig {
private int udpPort = 9999 ;
@Bean
public DirectChannel udpReceiveChannel ( ) {
return new DirectChannel ( ) ;
}
@Bean
public UnicastReceivingChannelAdapter unicastInboundAdapter ( ) {
UnicastReceivingChannelAdapter adapter = new UnicastReceivingChannelAdapter ( udpPort) ;
adapter. setOutputChannelName ( "udpReceiveChannel" ) ;
return adapter;
}
@ServiceActivator ( inputChannel = "udpReceiveChannel" )
public void messageReceiver ( byte [ ] message) {
String payload = new String ( message, StandardCharsets . UTF_8 ) ;
System . out. println ( "Received UDP message: " + payload) ;
}
}