Spring Integration接收TCP与UDP请求

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/> <!-- lookup parent from repository -->
    </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;

    /**
     * 创建连接工厂
     * @return
     */
    @Bean
    public AbstractServerConnectionFactory serverConnectionFactory() {
        TcpNioServerConnectionFactory tcpNioServerConnectionFactory = new TcpNioServerConnectionFactory(PORT);
        tcpNioServerConnectionFactory.setUsingDirectBuffers(true);
        return tcpNioServerConnectionFactory;
    }

    /**
     * 创建消息通道
     * 这个bean可以注释,因为会使用默认的
     * @return
     */
    @Bean
    public DirectChannel tcpReceiveChannel() {
        return new DirectChannel();
    }

    /**
     * 创建tcp接收通道适配器
     * @return
     */
    @Bean
    public TcpReceivingChannelAdapter inboundAdapter() {
        TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
        adapter.setConnectionFactory(serverConnectionFactory());
        adapter.setOutputChannelName("tcpReceiveChannel");
        return adapter;
    }

    /**
     * 处理请求器
     * @param message
     */
    @ServiceActivator(inputChannel = "tcpReceiveChannel")
    public void messageReceiver(byte[] message) {
        // 处理接收到的TCP消息
        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;

    /**
     * 创建UDP接收通道
     */
    @Bean
    public DirectChannel udpReceiveChannel() {
        return new DirectChannel();
    }

    /**
     * 创建UDP单播接收通道适配器
     */
    @Bean
    public UnicastReceivingChannelAdapter unicastInboundAdapter() {
        UnicastReceivingChannelAdapter adapter = new UnicastReceivingChannelAdapter(udpPort);
        adapter.setOutputChannelName("udpReceiveChannel");
        return adapter;
    }

    /**
     * 处理UDP消息
     */
    @ServiceActivator(inputChannel = "udpReceiveChannel")
    public void messageReceiver(byte[] message) {
        String payload = new String(message, StandardCharsets.UTF_8);
        System.out.println("Received UDP message: " + payload);
    }
}

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值