ssh配置多地址rabbitmq

1.属性文件配置rabbitmq连接信息
test.mq.host=xxx.xxx.xxx.xxx
test.mq.username=guest
test.mq.password=guest
test.mq.port=5672
test.mq.virtualhost=/

test.mq2.host=127.0.0.1
test.mq2.username=guest
test.mq2.password=guest
test.mq.port=5672
test.mq.virtualhost=/

2.分别在两个连接地址下创建ttt_queue和test_queue

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:int="http://www.springframework.org/schema/integration"
	xmlns:int-http="http://www.springframework.org/schema/integration/http"
	xmlns:int-xml="http://www.springframework.org/schema/integration/xml"
	xmlns:rabbit="http://www.springframework.org/schema/rabbit"
	xmlns:int-amqp="http://www.springframework.org/schema/integration/amqp"
	xmlns:int-stream="http://www.springframework.org/schema/integration/stream" 
	xsi:schemaLocation="  
		     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd 
		     http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.3.xsd
             http://www.springframework.org/schema/integration/amqp http://www.springframework.org/schema/integration/amqp/spring-integration-amqp-4.3.xsd 
			 http://www.springframework.org/schema/integration/xml http://www.springframework.org/schema/integration/xml/spring-integration-xml.xsd 	 
			 http://www.springframework.org/schema/integration/stream http://www.springframework.org/schema/integration/stream/spring-integration-stream.xsd 
			 http://www.springframework.org/schema/integration/http http://www.springframework.org/schema/integration/http/spring-integration-http.xsd 
             http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit-1.7.xsd">

	<!-- A reference to the org.springframework.amqp.rabbit.connection.ConnectionFactory -->
	<rabbit:connection-factory id="connectionFactory"
		host="${${switch}.mq.host}" username="${${switch}.mq.username}" password="${${switch}.mq.password}" port="${${switch}.mq.port}" virtual-host="${${switch}.mq.virtualhost}" 
		cache-mode="CONNECTION" connection-cache-size="24"/>
	

	<rabbit:admin connection-factory="connectionFactory"/>
	
	<!-- 消息对象json转换类 -->
    <bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />
    
	<!-- Creates a org.springframework.amqp.rabbit.core.RabbitTemplate for access to the broker -->
	<rabbit:template id="rabbitTemplate" connection-factory="connectionFactory" message-converter="jsonMessageConverter"/>
	
	<rabbit:queue id="ttt_queue" name="ttt_queue" durable="true" auto-delete="false" exclusive="false" auto-declare="true"/>
	<rabbit:direct-exchange id="ttt_exchange" name="ttt_exchange" durable="true">
		<rabbit:bindings>
			<rabbit:binding queue="ttt_queue" />
		</rabbit:bindings>
	</rabbit:direct-exchange>
	
	<rabbit:listener-container connection-factory="connectionFactory" acknowledge="none" message-converter="jsonMessageConverter">
		<rabbit:listener queues="ttt_queue" ref="registrationPlanService" method="hehe" />
	</rabbit:listener-container>

<!-- ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ --> 
	<!--配置connection-factory,指定连接rabbit server参数 -->
    <rabbit:connection-factory id="connectionFactory2"
                               username="${${switch}.mq2.username}" password="${${switch}.mq2.password}" host="${${switch}.mq2.host}" port="5672"/>
    <!--通过指定下面的admin信息,当前producer中的exchange和queue会在rabbitmq服务器上自动生成 -->
    <rabbit:admin connection-factory="connectionFactory2"/>
    <!--定义rabbit template用于数据的接收和发送 -->
    <rabbit:template id="rabbitTemplate2" connection-factory="connectionFactory2"
                     message-converter="jsonMessageConverter"/>
 
	<rabbit:queue id="test_queue" name="test_queue" durable="true" auto-delete="false" exclusive="false" auto-declare="true"/>
	<rabbit:fanout-exchange id="test_exchange" name="test_exchange" durable="true" auto-delete="false" auto-declare="true">
	    <rabbit:bindings>
	        <rabbit:binding queue="test_queue" />
	    </rabbit:bindings>
	</rabbit:fanout-exchange>
	
	<rabbit:listener-container connection-factory="connectionFactory2" acknowledge="none" auto-declare="true" message-converter="jsonMessageConverter" concurrency="2">
		<rabbit:listener queues="test_queue" ref="registrationPlanService" method="haha"/>
	</rabbit:listener-container>
	<!-- ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ --> 

</beans>

3.测试

@Resource(name="rabbitTemplate")
private RabbitTemplate rabbitTemplate;

@Resource(name="rabbitTemplate2")
private RabbitTemplate rabbitTemplate2;

/***
 * <p>Title: xixi</p>  
 * <p>Description: 生产者</p>  
 * @param mqMap
 */
public void xixi(Map<String, Object> mqMap) {
	try {
		rabbitTemplate2.convertAndSend("test_exchange", null, mqMap);
		rabbitTemplate.convertAndSend("ttt_exchange", null, mqMap);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
/***
 * <p>Title: haha</p>  
 * <p>Description: 消费者(rabbitTemplate2)</p>  
 * @param mqMap
 */
public void haha(Map<String, Object> mqMap) {
	try {
		String success = mqMap.get("success").toString();
		String info = mqMap.get("info").toString();
		logger.info("rabbitTemplate2 mq test_exchange 消费成功, success={}, info={}", success, info);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
/***
 * <p>Title: hehe</p>  
 * <p>Description: 消费者(rabbitTemplate)</p>  
 * @param mqMap
 */
public void hehe(Map<String, Object> mqMap) {
	try {
		String success = mqMap.get("success").toString();
		String info = mqMap.get("info").toString();
		logger.info("rabbitTemplate mq ttt_exchange 消费成功, success={}, info={}", success, info);
	} catch (Exception e) {
		e.printStackTrace();
	}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值