RabbitMQ-Spring AMQP【翻译】4.1.2——Connecting to a Cluster & Routing Connection Factory

版本:2.2.4.RELEASE

配置连接集群

如果要连接集群,请在CachingConnectionFactory配置addresses属性:

@Bean
public CachingConnectionFactory ccf() {
    CachingConnectionFactory ccf = new CachingConnectionFactory();
    ccf.setAddresses("host1:5672,host2:5672,host3:5672");
    return ccf;
}

每当建立新连接时,基础的连接工厂降维尝试按照顺序连接每个host。从2.1.8开始,可以通过将shuffleAddresses 属性设置为true,来达到随机挑选地址创建的目的。

@Bean
public CachingConnectionFactory ccf() {
    CachingConnectionFactory ccf = new CachingConnectionFactory();
    ccf.setAddresses("host1:5672,host2:5672,host3:5672");
    ccf.setShuffleAddresses(true);
    return ccf;
}

路由连接工厂

从1.3版本开始,已经初次使用了AbstractRoutingConnectionFactory 。这个工厂提供了一种机制,可以配置映射多个ConnectionFactory,并在运行时通过查找键确定目标ConnectionFactory。通常,实现要检查线程绑定的上下文。为了方便起见,Spring AMQP提供了SimpleRoutingConnectionFactory,它从SimpleResourceHolder获取当前线程绑定的lookupKey。下面的例子展示了如何配置一个SimpleRoutingConnectionFactory

<bean id="connectionFactory"
      class="org.springframework.amqp.rabbit.connection.SimpleRoutingConnectionFactory">
	<property name="targetConnectionFactories">
		<map>
			<entry key="#{connectionFactory1.virtualHost}" ref="connectionFactory1"/>
			<entry key="#{connectionFactory2.virtualHost}" ref="connectionFactory2"/>
		</map>
	</property>
</bean>

<rabbit:template id="template" connection-factory="connectionFactory" />
public class MyService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void service(String vHost, String payload) {
        SimpleResourceHolder.bind(rabbitTemplate.getConnectionFactory(), vHost);
        rabbitTemplate.convertAndSend(payload);
        SimpleResourceHolder.unbind(rabbitTemplate.getConnectionFactory());
    }

}

再使用后,解除绑定是十分重要的。可以看JavaDoc了解更多关于AbstractRoutingConnectionFactory的信息。

从1.4版本开始,RabbitTemplate支持SpELsendConnectionFactorySelectorExpression receiveConnectionFactorySelectorExpression 属性,这些属性每个在每个AMQP协议的交互操作(sendsendAndReceivereceivereceiveAndReply)都会计算解析出来AbstractRoutingConnectionFactory提供的lookupKey值。
您可以使用bean引用,例如在表达式中使用@vhostersolvergetVHost(#root)。在send操作中,要发送的消息是根源计算对象。在receive操作中,queueName是根源计算对象。

路由算法如下:如果选择器表达式为null或计算结果为null,或者提供的ConnectionFactory不是AbstractRoutingConnectionFactory的实例,那么一切都会像以前一样工作,依赖于提供的ConnectionFactory实现。如果求值结果不为null,但该查找键没有目标ConnectionFactory,并且AbstractRoutingConnectionFactory配置为lenientFallback=true,则会发生同样的情况。对于AbstractRoutingConnectionFactory,它会基于determineCurrentLookupKey()回退到其路由实现。但是,如果lenientFallback=false,则会引发IllegalStateException

名称空间还支持send-connection-factory-selector-expressionreceive-connection-factory-selector-expression属性在<rabbit:template>组件上。

另外,1.4版本后,你可以在listener容器中配置一个路由连接工厂。在这种清空下,队列名称列表可以错位lookupkey。例如你可以设置listener容器setQueueNames("thing1","thing2"),lookup key设置为[thing1,thing](请注意,键中没有空格)

从1.6.9开始,你可以使用listener容器的setLookupKeyQualifier方法给lookup key可以加上限定符。这样做可以,监听的队列拥有相同的名字,但是在不同的virtual host上(在这里,每个都有一个连接工厂)。

例如,lookup key 指定为thing1,容器监听队列thing2,你可以将lookup key注册到目标工厂上thing1[thing2]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值