Spring Integration 是一个开源的项目,它是 Spring 生态系统的一部分,旨在简化企业集成(Enterprise Integration)的开发。它提供了一种构建消息驱动的、松散耦合的、可扩展的企业应用集成解决方案的方式。Spring Integration 基于 Spring Framework 构建,使开发者能够更容易地将不同的系统、应用程序和服务整合到一个协调的整体中。
Spring Integration 主要有以下作用
本文主要介绍 Spring Integration 接收TCP与UDP请求的示例。在项目中,我们偶尔需要接收其他服务的TCP与UDP请求,此时使用Netty可能会过度设计,想要一个轻量级nio的TCP、UDP服务端的话,我们可以选择 Spring Integration。
环境: JDK21 SpringBoot 3.1.4 Spring Integration 6.1.3
环境:
<!-- 父工程,主要用作版本管控 --><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>
<!-- 父工程,主要用作版本管控 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.1.4</version>
<relativePath />
</parent>
<!-- springboot-web -->
<dependency>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- spring-integration -->
<groupId>org.springframework.integration</groupId>
<artifactId>spring-integration-ip</artifactId>
注意:如果你的SpringBoot版本是2.x版本,那么你需要使用JDK21以下的版本,因为JDK中的包名有所更改。
新建配置类TcpServerConfig,其中tcp.server.port需要到application.yml或者application.properties中进行配置。或者你也可以直接填写端口。
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@Configurationpublic 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请求"); }}
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;
}
* 创建消息通道
public DirectChannel tcpReceiveChannel() {
return new DirectChannel();
* 创建tcp接收通道适配器
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结尾,否则无法正常接收报文。
\r\n
新建配置类UdpServerConfig,其中udp.server.port需要到application.yml或者application.properties中进行配置。或者你也可以直接填写端口。
UdpServerConfig
udp.server.port
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@Configurationpublic 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请求"); }}
import org.springframework.integration.dsl.IntegrationFlow;
import org.springframework.integration.ip.dsl.Udp;
import org.springframework.messaging.Message;
public class UdpServerConfig {
@Value("${udp.server.port}")
* 创建UDP服务器接收通道适配器
public IntegrationFlow udpIn() {
return IntegrationFlow.from(Udp.inboundAdapter(PORT))
.channel("udpReceiveChannel")
.get();
* 创建消息接收通道
public DirectChannel udpReceiveChannel() {
* 处理接收到的UDP消息
@ServiceActivator(inputChannel = "udpReceiveChannel")
public void udpHandleMessage(Message<byte[]> message) {
// 处理接收到的UDP消息
byte[] payload = message.getPayload();
log.info("处理UDP请求");
对比Netty,Spring Integration比较轻量级,也更容易集成到 SpringBoot 中,但是性能肯定不如Netty。这里也只是给接收TCP、UDP请求设计方面多一个选择。
原文链接:https://www.cnblogs.com/jonil/p/17778628.html
本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728