|
此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Integration 6.4.0! |
UDP 适配器
本节介绍如何配置和使用 UDP 适配器。
出站 UDP 适配器 (XML 配置)
以下示例配置 UDP 出站通道适配器:
<int-ip:udp-outbound-channel-adapter id="udpOut"
host="somehost"
port="11111"
multicast="false"
socket-customizer="udpCustomizer"
channel="exampleChannel"/>
设置multicast自true,您还应在 host 属性中提供多播地址。 |
UDP 是一种高效但不可靠的协议。
Spring 集成添加了两个属性来提高可靠性:check-length和acknowledge.
什么时候check-length设置为true,适配器在消息数据前面有一个 length 字段(按网络字节顺序排列的四个字节)。
这使接收方能够验证收到的数据包的长度。
如果接收系统使用的缓冲区太短而无法包含数据包,则数据包可能会被截断。
这lengthheader 提供了一种检测此情况的机制。
从版本 4.3 开始,您可以设置port自0,在这种情况下,作系统将选择端口。
可以通过调用getPort()在适配器启动后,并且isListening()返回true.
从版本 5.3.3 开始,您可以添加SocketCustomizerbean 来修改DatagramSocket创建后(例如,调用setTrafficClass(0x10)).
以下示例显示了一个出站通道适配器,该适配器向数据报数据包添加了长度检查:
<int-ip:udp-outbound-channel-adapter id="udpOut"
host="somehost"
port="11111"
multicast="false"
check-length="true"
channel="exampleChannel"/>
数据包的接收方还必须配置为预期长度在实际数据之前。
对于 Spring 集成 UDP 入站通道适配器,请将其check-length属性。 |
第二个可靠性改进允许使用应用程序级确认协议。 接收方必须在指定时间内向发送方发送确认。
以下示例显示了一个出站通道适配器,该适配器向数据报数据包添加长度检查并等待确认:
<int-ip:udp-outbound-channel-adapter id="udpOut"
host="somehost"
port="11111"
multicast="false"
check-length="true"
acknowledge="true"
ack-host="thishost"
ack-port="22222"
ack-timeout="10000"
channel="exampleChannel"/>
设置acknowledge自true意味着数据包的接收者可以解释添加到包含确认数据(主机和端口)的数据包的报头。
最有可能的是,接收者是 Spring 集成入站通道适配器。 |
当 multicast 为 true 时,附加属性 (min-acks-for-success) 指定必须在ack-timeout. |
从版本 4.3 开始,您可以设置ackPort自0,在这种情况下,作系统将选择端口。
出站 UDP 适配器 (Java 配置)
以下示例演示如何使用 Java 配置出站 UDP 适配器:
@Bean
@ServiceActivator(inputChannel = "udpOut")
public UnicastSendingMessageHandler handler() {
return new UnicastSendingMessageHandler("localhost", 11111);
}
(或MulticastSendingChannelAdapter进行多播)。
出站 UDP 适配器 (Java DSL 配置)
以下示例说明如何使用 Java DSL 配置出站 UDP 适配器:
@Bean
public IntegrationFlow udpOutFlow() {
return f -> f.handle(Udp.outboundAdapter("localhost", 1234)
.configureSocket(socket -> socket.setTrafficClass(0x10)))
.get();
}
入站 UDP 适配器 (XML 配置)
以下示例说明如何配置基本的单播入站 udp 通道适配器。
<int-ip:udp-inbound-channel-adapter id="udpReceiver"
channel="udpOutChannel"
port="11111"
receive-buffer-size="500"
multicast="false"
socket-customizer="udpCustomizer"
check-length="true"/>
以下示例说明如何配置基本的多播入站 udp 通道适配器:
<int-ip:udp-inbound-channel-adapter id="udpReceiver"
channel="udpOutChannel"
port="11111"
receive-buffer-size="500"
multicast="true"
multicast-address="225.6.7.8"
check-length="true"/>
默认情况下,不会对入站数据包执行反向 DNS 查找:在未配置 DNS 的环境中(例如 Docker 容器),这可能会导致连接延迟。
要将 IP 地址转换为主机名以用于邮件报头,可以通过设置lookup-host属性设置为true.
从版本 5.3.3 开始,您可以添加SocketCustomizerbean 来修改DatagramSocket创建后。
它被调用用于接收套接字和为发送 ack 创建的任何套接字。
入站 UDP 适配器 (Java 配置)
以下示例显示如何使用 Java 配置入站 UDP 适配器:
@Bean
public UnicastReceivingChannelAdapter udpIn() {
UnicastReceivingChannelAdapter adapter = new UnicastReceivingChannelAdapter(11111);
adapter.setOutputChannelName("udpChannel");
return adapter;
}
以下示例显示了如何使用 Java DSL 配置入站 UDP 适配器:
入站 UDP 适配器 (Java DSL 配置)
@Bean
public IntegrationFlow udpIn() {
return IntegrationFlow.from(Udp.inboundAdapter(11111))
.channel("udpChannel")
.get();
}
服务器侦听事件
从版本 5.0.2 开始,UdpServerListeningEvent在入站适配器启动并开始侦听时发出。
当适配器配置为侦听端口0,这意味着作系统选择端口。
它也可以用来代替轮询isListening(),如果您需要等待一段时间才能启动将连接到 socket 的其他进程。
高级出站配置
这<int-ip:udp-outbound-channel-adapter> (UnicastSendingMessageHandler) 具有destination-expression和socket-expression选项。
您可以使用destination-expression作为硬编码的host-portpair 确定传出数据报数据包的目标地址与requestMessage(使用评估上下文的根对象)。
表达式的计算结果必须为URI一个String在 URI 样式中(请参阅 RFC-2396),或者SocketAddress.
您还可以使用入站IpHeaders.PACKET_ADDRESS标头。
在框架中,DatagramPacketMessageMapper当我们在UnicastReceivingChannelAdapter并将其转换为消息。
header 值正是DatagramPacket.getSocketAddress()的传入数据报。
使用socket-expression,出站通道适配器可以使用(例如)入站通道适配器套接字通过接收数据的同一端口发送数据报。
当我们的应用程序作为 UDP 服务器工作,而客户端在网络地址转换 (NAT) 后面运行时,它非常有用。
此表达式的计算结果必须为DatagramSocket.
这requestMessage用作评估上下文的根对象。
您不能使用socket-expression参数替换为multicast和acknowledge参数。
以下示例说明如何使用转换为大写并使用套接字的转换器配置 UDP 入站通道适配器:
<int-ip:udp-inbound-channel-adapter id="inbound" port="0" channel="in" />
<int:channel id="in" />
<int:transformer expression="new String(payload).toUpperCase()"
input-channel="in" output-channel="out"/>
<int:channel id="out" />
<int-ip:udp-outbound-channel-adapter id="outbound"
socket-expression="@inbound.socket"
destination-expression="headers['ip_packetAddress']"
channel="out" />
以下示例显示了 Java DSL 的等效配置:
@Bean
public IntegrationFlow udpEchoUpcaseServer() {
return IntegrationFlow.from(Udp.inboundAdapter(11111).id("udpIn"))
.<byte[], String>transform(p -> new String(p).toUpperCase())
.handle(Udp.outboundAdapter("headers['ip_packetAddress']")
.socketExpression("@udpIn.socket"))
.get();
}