Spring Integration接收TCP与UDP请求

news/2024/5/18 14:00:38 标签: spring, tcp/ip, udp

1. 简介

Spring Integration 是一个开源的项目,它是 Spring 生态系统的一部分,旨在简化企业集成(Enterprise Integration)的开发。它提供了一种构建消息驱动的、松散耦合的、可扩展的企业应用集成解决方案的方式。Spring Integration 基于 Spring Framework 构建,使开发者能够更容易地将不同的系统、应用程序和服务整合到一个协调的整体中。

Spring Integration 主要有以下作用

  1. 消息驱动的集成:Spring Integration 基于消息传递的模式,允许系统和应用程序通过消息进行通信。这种模式可以用于异步集成,以确保系统能够松散耦合,以及在高负载和大规模情况下具有良好的性能。
  2. 模块化和可扩展:Spring Integration 提供了一组模块,每个模块都用于处理特定类型的集成需求。这些模块可以按需组合和扩展,使开发者能够根据应用程序的需要选择合适的模块,并自定义它们。
  3. 集成各种传输协议和数据格式:Spring Integration 支持各种传输协议(例如,HTTP、JMS、FTP、SMTP等)和数据格式(例如,JSON、XML、CSV等),以便实现不同系统之间的数据传输和转换。
  4. 企业模式的集成:Spring Integration 提供了一些企业集成模式的实现,例如消息路由、消息转换、消息过滤、消息聚合等,以帮助解决不同场景下的集成挑战。
  5. 与 Spring 生态系统的集成:Spring Integration 与 Spring Framework 和 Spring Boot 紧密集成,开发者可以轻松整合已有的 Spring 应用程序,同时利用 Spring 的依赖注入和 AOP(面向切面编程)等功能。

2. 代码实战

本文主要介绍 Spring Integration 接收TCP与UDP请求的示例。在项目中,我们偶尔需要接收其他服务的TCP与UDP请求,此时使用Netty可能会过度设计,想要一个轻量级nio的TCP、UDP服务端的话,我们可以选择 Spring Integration。

环境:

  1. JDK21
  2. SpringBoot 3.1.4
  3. Spring Integration 6.1.3

2.1 导入依赖

 
<!-- 父工程,主要用作版本管控 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.4</version>
<relativePath />
</parent>
<!-- springboot-web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring-integration -->
<dependency>
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-ip</artifactId>
</dependency>

注意:如果你的SpringBoot版本是2.x版本,那么你需要使用JDK21以下的版本,因为JDK中的包名有所更改。

2.2 建立TCP服务端

新建配置类TcpServerConfig,其中tcp.server.port需要到application.yml或者application.properties中进行配置。或者你也可以直接填写端口。

 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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 {
@Value("${tcp.server.port}")
private int PORT;
/**
* 创建连接工厂
* @return
*/
@Bean
public AbstractServerConnectionFactory serverConnectionFactory() {
TcpNioServerConnectionFactory tcpNioServerConnectionFactory = new TcpNioServerConnectionFactory(PORT);
tcpNioServerConnectionFactory.setUsingDirectBuffers(true);
return tcpNioServerConnectionFactory;
}
/**
* 创建消息通道
* @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消息
log.info("处理TCP请求");
}
}

注意:在发送tcp报文的时候,tcp报文需要以\r\n结尾,否则无法正常接收报文。

udp服务端">2.3 建立UDP服务端

新建配置类UdpServerConfig,其中udp.server.port需要到application.yml或者application.properties中进行配置。或者你也可以直接填写端口。

 
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
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.dsl.IntegrationFlow;
import org.springframework.integration.ip.dsl.Udp;
import org.springframework.messaging.Message;
@Slf4j
@Configuration
public class UdpServerConfig {
@Value("${udp.server.port}")
private int PORT;
/**
* 创建UDP服务器接收通道适配器
* @return
*/
@Bean
public IntegrationFlow udpIn() {
return IntegrationFlow.from(Udp.inboundAdapter(PORT))
.channel("udpReceiveChannel")
.get();
}
/**
* 创建消息接收通道
* @return
*/
@Bean
public DirectChannel udpReceiveChannel() {
return new DirectChannel();
}
/**
* 处理接收到的UDP消息
* @param message
*/
@ServiceActivator(inputChannel = "udpReceiveChannel")
public void udpHandleMessage(Message<byte[]> message) {
// 处理接收到的UDP消息
byte[] payload = message.getPayload();
log.info("处理UDP请求");
}
}

3. 总结

对比Netty,Spring Integration比较轻量级,也更容易集成到 SpringBoot 中,但是性能肯定不如Netty。这里也只是给接收TCP、UDP请求设计方面多一个选择。


http://www.niftyadmin.cn/n/5271684.html

相关文章

记录 | Visual Studio报错:const char*类型的值不能用于初始化char*类型

Visual Studio 报错&#xff1a; const char *”类型的值不能用于初始化“char *”类型的实体错误 解决办法&#xff1a; 1&#xff0c;强制类型转换&#xff0c;例如&#xff1a; char * Singer::pv[] {(char*)"other", (char*)"alto", (char*)"c…

​springboot代码混淆及反混淆代码工具

​springboot代码混淆及反混淆代码工具 目录 介绍 什么是混淆 为什么用混淆&#xff1f; 基础混淆 高级混淆工具 #0x1 ipaguard Tool - springboot混淆工具 ipaguard界面概览 ipaguard启动界面 ipaguard代码混淆界面 资源文件混淆界面 重签名界面 尽管到目前为止&a…

MILP加速运算技巧——模型对称性的预处理

文章目录 整数规划的对称性什么是对称性对称性的影响 对称性的预处理方法 整数规划的对称性 什么是对称性 许多整数规划问题存在对称性&#xff0c;这种对称性是指问题解空间的对称&#xff0c;即在对称的解空间当中解的优化目标值上是相同的。这种对称性并不会改变问题的最优…

更新钉钉文档封装好的代码

import json import requestsclass OperateKnowledgeBaseExcel():robot_code #机器人agent_id app_key #机器人密钥 获取方法见我的其他博客app_secret #机器人密钥def __init__(self,union_id, workbook_id, worksheet_id):self.union_…

苹果明年春季将发布多款新品!

大家期待已久的iPad系列终于要迎来更新了&#xff0c;据彭博社记者马克古尔曼最新爆料称&#xff0c;苹果为了提振下滑的iPad销售&#xff0c;计划在2024年初对iPad系列进行重大更新&#xff0c;包括2款不同尺寸的新iPad Air和新iPad Pro&#xff0c;这些机型都将有显著的变化。…

QUIC在零信任解决方案的落地实践

一 前言 ZTNA为以“网络为中心”的传统企业体系架构向以“身份为中心”的新型企业安全体系架构转变&#xff0c;提供解决方案。随着传统网络边界不断弱化&#xff0c;企业SaaS规模化日益增多&#xff0c;给终端安全访问接入创造了多元化的空间。其中BYOD办公方式尤为突出&#…

【JAVA】CyclicBarrier源码解析以及示例

文章目录 前言CyclicBarrier源码解析以及示例主要成员变量核心方法 应用场景任务分解与合并应用示例 并行计算应用示例 游戏开发应用示例输出结果 数据加载应用示例 并发工具的协同应用示例 CyclicBarrier和CountDownLatch的区别循环性&#xff1a;计数器的变化&#xff1a;用途…

bottom-up-attention-vqa-master 成功复现!!!

代码地址 1、create_dictionary.py 建立词典和使用预训练的glove向量 &#xff08;1&#xff09;create_dictionary() 遍历每个question文件取出所关注的question部分&#xff0c;qs 遍历qs&#xff0c;对每个问题的文本内容进行分词&#xff0c;并将分词结果添加到字典中&…