将netty4 tcp端点限制为单个实例



我在8001有一个mock端点,它将回显提供给它的任何东西。我有一个http端点,它将把URL的末尾提交给mock端点,并从端点的响应中提供响应。这很好。

挑战在于,我希望http路由只使用到8001端点的1个tcp连接。

我创建了一个工人组,如其他地方所述,并将工人计数设置为1。通过查看源代码,我认为这种方法应该有效。

然而,当我执行这个bash命令时:

for a in {1..5}; do curl "http://localhost:8080/upstream/REQUESTNUM$a"  > $a.txt &  done;

我看到8001有多个连接。我本以为http端点请求必须共享一个池工作程序,但事实并非如此。

也许我遗漏了什么,或者可能有另一种方法可以实现我的目标,即对所有http请求只使用一个后端tcp连接。

我该如何完成它?

<?xml version="1.0" encoding="UTF-8"?>                                                                                                                                                                             
<beans xmlns="http://www.springframework.org/schema/beans"                                                                                                                                                         
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"                                                                                                                                                          
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd                                                                                   
http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">                                                                                                                 
<camelContext                                                                                                                                                                                                  
xmlns="http://camel.apache.org/schema/spring">                                                                                                                                                             
<route id="mockUpstream">                                                                                                                                                                                  
<from                                                                                                                                                                                                  
uri="netty4:tcp://localhost:8001?sync=true&amp;textline=true&amp;keepAlive=true&amp;disconnect=false&amp;reuseChannel=true" />                                                                     
<log message="Incoming to upstream: ${body}" />                                                                                                                                                        
<transform>                                                                                                                                                                                            
<simple>${body}</simple>                                                                                                                                                                           
</transform>                                                                                                                                                                                           
</route>                                                                                                                                                                                                   
<route id="httpServer">                                                                                                                                                                                    
<from                                                                                                                                                                                                  
uri="netty4-http:http://0.0.0.0:8080/upstream?matchOnUriPrefix=true" />                                                                                                                            
<!-- optional just use CamelHttpQuery from header, for full query -->                                                                                                                                  
<log                                                                                                                                                                                                   
message="Incoming http command: ${in.headers[CamelHttpPath]}" />                                                                                                                                   
<transform>                                                                                                                                                                                            
<simple>${in.headers[CamelHttpPath]}</simple>                                                                                                                                                      
</transform>                                                                                                                                                                                           
<to                                                                                                                                                                                                    
uri="netty4:tcp://localhost:8001?workerGroup=#sharedPool&amp;sync=true&amp;textline=true&amp;keepAlive=true&amp;disconnect=false&amp;reuseChannel=true" />                                         
<transform>                                                                                                                                                                                            
<simple>${body}</simple>                                                                                                                                                                           
</transform>                                                                                                                                                                                           
</route>                                                                                                                                                                                                   
</camelContext>                                                                                                                                                                                                
<bean id="poolBuilder"                                                                                                                                                                                         
class="org.apache.camel.component.netty4.NettyWorkerPoolBuilder">                                                                                                                                          
<property name="workerCount" value="1" />                                                                                                                                                                  
</bean>                                                                                                                                                                                                        
<bean id="sharedPool" class="io.netty.channel.EventLoopGroup"                                                                                                                                                  
factory-bean="poolBuilder" factory-method="build"                                                                                                                                                          
destroy-method="shutdown">                                                                                                                                                                                 
</bean>                                                                                                                                                                                                        
</beans>  

查看日志,使用TRACE级别的日志记录,我发现NettyProducer的池确实设置为使用最大1个活动连接,但NettyProducer允许100个空闲连接。我更改了以下行,现在它的行为与预期一致:

<to                                                                 
uri="netty4:tcp://localhost:8001?workerGroup=#sharedPool&amp;sync=true&amp;textline=true&amp;keepAlive=true&amp;disconnect=false&amp;reuseChannel=true&amp;producerPoolMaxActive=1&amp;producerPoolMaxIdle=1" />

我认为"生产者"设置只适用于连接的生产者端(模拟主机路由中的netty(,但看起来消费者端也可以使用它们(http路由中的netty(。

编辑:我混淆了"生产者"one_answers"消费者"这两个术语,并将其颠倒过来。考虑到"to"元素正在生成一个对要消费的东西的请求,生产者*参数对(http路由中的netty(来说是有意义的。(模拟主机路由中的netty(是请求的使用者。

最新更新