【问题标题】:Configure keep alive to keep connection alive all the time配置keep alive以保持连接一直处于活动状态
【发布时间】:2017-10-05 10:45:24
【问题描述】:

我有一个 tcp 客户端-服务器应用程序,其中客户端打开一个供一次性使用的连接,并在配置时间后断开连接。如何配置它始终保持连接处于活动状态,在关闭的连接上重新连接并确保多个客户端连接对服务器开放。

客户端配置:

<int-ip:tcp-connection-factory id="client"
    type="client"
    host="${server.TCP.host}"
    port="${server.TCP.port}"
    single-use="true"
    so-timeout="${client.TCP.socketTimeOut}" />

<int-ip:tcp-outbound-gateway id="outGateway"
    request-channel="bytesOut"
    reply-channel="bytesIn"
    connection-factory="client"
    request-timeout="${client.TCP.requestTimeOut}"
    reply-timeout="${client.TCP.replyTimeout}" />

服务器配置:

<int-ip:tcp-connection-factory id="tcpServerConnFactory"
    type="server" 
    port="${service.tcp.port}" 
    using-nio="true"
    single-use="false" 
    so-timeout="${service.tcp.socketTimeout}"
    task-executor="taskExecutor"/>

<int-ip:tcp-inbound-gateway 
    id="tcpInboundGateway" 
    connection-factory="tcpServerConnFactory" 
    request-channel="bytesInChannel"
    reply-channel="bytesOutChannel"
    error-channel="errorChannel" />

【问题讨论】:

    标签: spring-integration


    【解决方案1】:

    在客户端单次使用意味着——每个套接字用于一个请求/回复然后关闭。

    single-use="false" 时,一个共享连接用于所有请求/回复 - 每个调用者都会阻塞等待套接字。

    您可以使用“CachingClientConnectionFactory”来维护一个永久连接池 - 请参阅the documentation

    如上所述,TCP 套接字可以是一次性的(一个请求/响应)或共享的。在大容量环境中,共享套接字与出站网关的性能不佳,因为套接字一次只能处理一个请求/响应。

    为了提高性能,用户可以使用协作通道适配器而不是网关,但这需要应用程序级别的消息关联。有关详细信息,请参阅第 31.8 节“TCP 消息关联”。

    Spring Integration 2.2 引入了缓存客户端连接工厂,其中使用共享套接字池,允许网关使用共享连接池处理多个并发请求。

    即将发布的 5.0 版本 - 目前处于里程碑 7 (5.0.0.M7)。有一个Thread Affinity Connection Factory,它为每个调用线程保持连接打开。

    【讨论】:

    • 删除客户端和服务器上的 so-timeout 并在客户端设置 single-use=false 实现了我想要的。但是,如果服务器重新启动,我如何保证客户端会自动连接?
    • 如果当前连接关闭,发送下一条消息时会自动创建一个新连接。
    • 感谢@Gary 的宝贵建议。
    猜你喜欢
    • 2019-09-28
    • 2012-01-03
    • 2021-09-13
    • 2013-08-27
    • 2019-09-01
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多