经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
使用Spring Integration接收TCP与UDP请求
来源:cnblogs  作者:护发师兄  时间:2023/10/23 9:01:21  对本文有异议

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 导入依赖

  1. <!-- 父工程,主要用作版本管控 -->
  2. <parent>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-parent</artifactId>
  5. <version>3.1.4</version>
  6. <relativePath />
  7. </parent>
  8. <!-- springboot-web -->
  9. <dependency>
  10. <groupId>org.springframework.boot</groupId>
  11. <artifactId>spring-boot-starter-web</artifactId>
  12. </dependency>
  13. <!-- spring-integration -->
  14. <dependency>
  15. <groupId>org.springframework.integration</groupId>
  16. <artifactId>spring-integration-ip</artifactId>
  17. </dependency>

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

2.2 建立TCP服务端

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

  1. import lombok.extern.slf4j.Slf4j;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.integration.annotation.ServiceActivator;
  7. import org.springframework.integration.channel.DirectChannel;
  8. import org.springframework.integration.ip.tcp.TcpReceivingChannelAdapter;
  9. import org.springframework.integration.ip.tcp.connection.AbstractServerConnectionFactory;
  10. import org.springframework.integration.ip.tcp.connection.TcpNioServerConnectionFactory;
  11. @Slf4j
  12. @Configuration
  13. public class TcpServerConfig {
  14. @Value("${tcp.server.port}")
  15. private int PORT;
  16. /**
  17. * 创建连接工厂
  18. * @return
  19. */
  20. @Bean
  21. public AbstractServerConnectionFactory serverConnectionFactory() {
  22. TcpNioServerConnectionFactory tcpNioServerConnectionFactory = new TcpNioServerConnectionFactory(PORT);
  23. tcpNioServerConnectionFactory.setUsingDirectBuffers(true);
  24. return tcpNioServerConnectionFactory;
  25. }
  26. /**
  27. * 创建消息通道
  28. * @return
  29. */
  30. @Bean
  31. public DirectChannel tcpReceiveChannel() {
  32. return new DirectChannel();
  33. }
  34. /**
  35. * 创建tcp接收通道适配器
  36. * @return
  37. */
  38. @Bean
  39. public TcpReceivingChannelAdapter inboundAdapter() {
  40. TcpReceivingChannelAdapter adapter = new TcpReceivingChannelAdapter();
  41. adapter.setConnectionFactory(serverConnectionFactory());
  42. adapter.setOutputChannelName("tcpReceiveChannel");
  43. return adapter;
  44. }
  45. /**
  46. * 处理请求器
  47. * @param message
  48. */
  49. @ServiceActivator(inputChannel = "tcpReceiveChannel")
  50. public void messageReceiver(byte[] message) {
  51. // 处理接收到的TCP消息
  52. log.info("处理TCP请求");
  53. }
  54. }

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

2.3 建立UDP服务端

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

  1. import lombok.extern.slf4j.Slf4j;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.beans.factory.annotation.Value;
  4. import org.springframework.context.annotation.Bean;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.integration.annotation.ServiceActivator;
  7. import org.springframework.integration.channel.DirectChannel;
  8. import org.springframework.integration.dsl.IntegrationFlow;
  9. import org.springframework.integration.ip.dsl.Udp;
  10. import org.springframework.messaging.Message;
  11. @Slf4j
  12. @Configuration
  13. public class UdpServerConfig {
  14. @Value("${udp.server.port}")
  15. private int PORT;
  16. /**
  17. * 创建UDP服务器接收通道适配器
  18. * @return
  19. */
  20. @Bean
  21. public IntegrationFlow udpIn() {
  22. return IntegrationFlow.from(Udp.inboundAdapter(PORT))
  23. .channel("udpReceiveChannel")
  24. .get();
  25. }
  26. /**
  27. * 创建消息接收通道
  28. * @return
  29. */
  30. @Bean
  31. public DirectChannel udpReceiveChannel() {
  32. return new DirectChannel();
  33. }
  34. /**
  35. * 处理接收到的UDP消息
  36. * @param message
  37. */
  38. @ServiceActivator(inputChannel = "udpReceiveChannel")
  39. public void udpHandleMessage(Message<byte[]> message) {
  40. // 处理接收到的UDP消息
  41. byte[] payload = message.getPayload();
  42. log.info("处理UDP请求");
  43. }
  44. }

3. 总结

对比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

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号