rabbitmq 学习(五) spring-boot-rabbit同时连接多个rabbitmq服务器收发消息

本文主要是介绍rabbitmq 学习(五) spring-boot-rabbit同时连接多个rabbitmq服务器收发消息,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

上一遍博客介绍了使用spring-boot连接单个rabbitmq服务器发送和接收消息,但是在实际的项目中我们可能需要同时连接多个不同的rabbitmq服务器发送和接收消息。今天简单介绍下如何使用spring-boot连个多个rabbitmq服务器发送和接收消息。
1.配置文件中配置2个rabbitmq连接信息(这里我在2台虚拟机上安装rabbitmq)

server:port: 8091
spring:application:name: rabbit-spring-bootrabbitmq:dev:host: 192.168.98.22port: 5672username: rootpassword: roottest:host: 192.168.98.23port: 5672username: rootpassword: root

2.创建rabbitmq连接配置类

@Configuration
public class RabbitMqConfig {// 注意这里使用了primary注解//  声明连接工厂连接开发服务器@Primary@Bean(name = "devConnectionFactory")public ConnectionFactory devConnectionFactory(@Value("${spring.rabbitmq.dev.host}") String host,@Value("${spring.rabbitmq.dev.port}") int port,@Value("${spring.rabbitmq.dev.username}") String username,@Value("${spring.rabbitmq.dev.password}") String password) {// 使用@Value直接读取配置文件中的信息CachingConnectionFactory connectionFactory = new CachingConnectionFactory();connectionFactory.setHost(host);connectionFactory.setPort(port);connectionFactory.setUsername(username);connectionFactory.setPassword(password);connectionFactory.setVirtualHost("/");connectionFactory.setPublisherConfirms(true);connectionFactory.setPublisherReturns(true);return connectionFactory;}//  声明连接工厂连接测试服务器@Bean(name = "testConnectionFactory")public ConnectionFactory testConnectionFactory(@Value("${spring.rabbitmq.test.host}") String host,@Value("${spring.rabbitmq.test.port}") int port,@Value("${spring.rabbitmq.test.username}") String username,@Value("${spring.rabbitmq.test.password}") String password) {CachingConnectionFactory connectionFactory = new CachingConnectionFactory();connectionFactory.setHost(host);connectionFactory.setPort(port);connectionFactory.setUsername(username);connectionFactory.setPassword(password);connectionFactory.setVirtualHost("/");connectionFactory.setPublisherConfirms(true);connectionFactory.setPublisherReturns(true);return connectionFactory;}//  声明开发服务器rabbitTemplate@Bean(name = "devRabbitTemplate")public RabbitTemplate devRabbitTemplate(@Qualifier("devConnectionFactory") ConnectionFactory connectionFactory) {RabbitTemplate rabbitTemplate=new RabbitTemplate(connectionFactory);return rabbitTemplate;}//  声明测试服务器连接 rabbitTemplate@Bean(name = "testRabbitTemplate")public RabbitTemplate testRabbitTemplate(@Qualifier("testConnectionFactory") ConnectionFactory connectionFactory) {RabbitTemplate rabbitTemplate=new RabbitTemplate(connectionFactory);return rabbitTemplate;}/*** 声明dev containerFactory* @param rabbitListenerContainerFactoryConfigurer* @param connectionFactory* @return*/@Bean(name = "devContainerFactory")public SimpleRabbitListenerContainerFactory devSimpleRabbitListenerContainerFactory(SimpleRabbitListenerContainerFactoryConfigurer rabbitListenerContainerFactoryConfigurer,@Qualifier("devConnectionFactory") ConnectionFactory connectionFactory) {SimpleRabbitListenerContainerFactory containerFactory=new SimpleRabbitListenerContainerFactory();rabbitListenerContainerFactoryConfigurer.configure(containerFactory,connectionFactory);return containerFactory;}/*** 声明  test containerFactory* @param rabbitListenerContainerFactoryConfigurer* @param connectionFactory* @return*/@Bean(name = "testContainerFactory")public SimpleRabbitListenerContainerFactory testSimpleRabbitListenerContainerFactory(SimpleRabbitListenerContainerFactoryConfigurer rabbitListenerContainerFactoryConfigurer,@Qualifier("testConnectionFactory") ConnectionFactory connectionFactory) {SimpleRabbitListenerContainerFactory containerFactory=new SimpleRabbitListenerContainerFactory();rabbitListenerContainerFactoryConfigurer.configure(containerFactory,connectionFactory);return containerFactory;}@Beanpublic RestTemplate restTemplate() {return new RestTemplate();}
}

3.创建消息生产者

@Component
public class RabbitMQProducer {//  指定dev服务器rabbitTemplate@Resource(name = "devRabbitTemplate")AmqpTemplate devAmqpTemplate;//  指定test服务器rabbitTemplate@Resource(name = "testRabbitTemplate")AmqpTemplate testAmqpTemplate;@Value("${open-care.rabbitmq.exchange.name}")String exchangeName;@Value("${open-care.rabbitmq.queue1-name}")String queueName;public void send(Object object) {try {Message message= MessageBuilder.withBody(JSON.toJSONString(object).getBytes("UTF-8")).setContentType(MessageProperties.CONTENT_TYPE_JSON).setContentEncoding("UTF-8").setMessageId(UUID.randomUUID().toString()).build();// 指定exchange交换器名称为fanout_exchange(fanout交换器没有路由键)devAmqpTemplate.send("fanout_exchange","",message);//  使用devAmqpTemplate将消息发送到开发服务器} catch (UnsupportedEncodingException e) {e.printStackTrace();}}public void sendMessage(Object object) {try {Message message= MessageBuilder.withBody(JSON.toJSONString(object).getBytes("UTF-8")).setContentType(MessageProperties.CONTENT_TYPE_JSON).setContentEncoding("UTF-8").setMessageId(UUID.randomUUID().toString()).build();// 指定交换器名称为fanout_exchange(fanout交换器没有路由键)testAmqpTemplate.send("fanout_exchange","",message);// 使用testAmqpTeplate将消息发送到测试服务器} catch (UnsupportedEncodingException e) {e.printStackTrace();}}
}

4.创建消息消费者

@Component
public class RabbitMQConsumer {@RabbitHandler@RabbitListener(queues = "q.test1",containerFactory = "devContainerFactory")//  指定队列名称和容器工厂,接收开发服务器队列q.test1中的消息public void receive(Object object) {Message message=(Message)object;byte bytes[]=null;if (message != null) {bytes=message.getBody();}try {String msg=new String(bytes,"UTF-8");System.out.println("192.168.98.22 q.test1------------------------"+msg);} catch (UnsupportedEncodingException e) {e.printStackTrace();}}@RabbitHandler@RabbitListener(queues = "q.test2",containerFactory = "devContainerFactory")//  指定队列名称和容器工厂,接收开发服务器队列q.test2中的消息public void receiveQueue1(Object object) {Message message=(Message)object;byte bytes[]=null;if (message != null) {bytes=message.getBody();}try {String msg=new String(bytes,"UTF-8");System.out.println("192.168.98.22 q.test2------------------------"+msg);} catch (UnsupportedEncodingException e) {e.printStackTrace();}}@RabbitHandler@RabbitListener(queues = "q.test3",containerFactory = "devContainerFactory")//  指定队列名称和容器工厂,接收开发服务器队列q.test3中的消息public void receiveQueue2(Object object) {Message message=(Message)object;byte bytes[]=null;if (message != null) {bytes=message.getBody();}try {String msg=new String(bytes,"UTF-8");System.out.println("192.168.98.22 q.test3------------------------"+msg);} catch (UnsupportedEncodingException e) {e.printStackTrace();}}@RabbitHandler@RabbitListener(queues = "q.test1",containerFactory = "testContainerFactory")//  指定队列名称和容器工厂,接收测试服务器队列q.test1中的消息public void receive23message(Object object) {Message message=(Message)object;byte bytes[]=null;if (message != null) {bytes=message.getBody();}try {String msg=new String(bytes,"UTF-8");System.out.println("192.168.98.23 q.test1------------------------"+msg);} catch (UnsupportedEncodingException e) {e.printStackTrace();}}@RabbitHandler@RabbitListener(queues = "q.fanout1",containerFactory = "testContainerFactory")//  指定队列名称和容器工厂,接收测试服务器队列q.fanout1中的消息public void receiveFanoutQueue(Object object) {Message message=(Message)object;byte bytes[]=null;if (message != null) {bytes=message.getBody();}try {String msg=new String(bytes,"UTF-8");System.out.println("192.168.98.23 q.fanout1------------------------"+msg);} catch (UnsupportedEncodingException e) {e.printStackTrace();}}
}

这篇关于rabbitmq 学习(五) spring-boot-rabbit同时连接多个rabbitmq服务器收发消息的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/809814

相关文章

MySQL中的交叉连接、自然连接和内连接查询详解

《MySQL中的交叉连接、自然连接和内连接查询详解》:本文主要介绍MySQL中的交叉连接、自然连接和内连接查询,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、引入二、交php叉连接(cross join)三、自然连接(naturalandroid join)四

Spring Boot项目部署命令java -jar的各种参数及作用详解

《SpringBoot项目部署命令java-jar的各种参数及作用详解》:本文主要介绍SpringBoot项目部署命令java-jar的各种参数及作用的相关资料,包括设置内存大小、垃圾回收... 目录前言一、基础命令结构二、常见的 Java 命令参数1. 设置内存大小2. 配置垃圾回收器3. 配置线程栈大小

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件

解决SpringBoot启动报错:Failed to load property source from location 'classpath:/application.yml'

《解决SpringBoot启动报错:Failedtoloadpropertysourcefromlocationclasspath:/application.yml问题》这篇文章主要介绍... 目录在启动SpringBoot项目时报如下错误原因可能是1.yml中语法错误2.yml文件格式是GBK总结在启动S

Spring中配置ContextLoaderListener方式

《Spring中配置ContextLoaderListener方式》:本文主要介绍Spring中配置ContextLoaderListener方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录Spring中配置ContextLoaderLishttp://www.chinasem.cntene

java实现延迟/超时/定时问题

《java实现延迟/超时/定时问题》:本文主要介绍java实现延迟/超时/定时问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java实现延迟/超时/定时java 每间隔5秒执行一次,一共执行5次然后结束scheduleAtFixedRate 和 schedu

Java Optional避免空指针异常的实现

《JavaOptional避免空指针异常的实现》空指针异常一直是困扰开发者的常见问题之一,本文主要介绍了JavaOptional避免空指针异常的实现,帮助开发者编写更健壮、可读性更高的代码,减少因... 目录一、Optional 概述二、Optional 的创建三、Optional 的常用方法四、Optio

python连接本地SQL server详细图文教程

《python连接本地SQLserver详细图文教程》在数据分析领域,经常需要从数据库中获取数据进行分析和处理,下面:本文主要介绍python连接本地SQLserver的相关资料,文中通过代码... 目录一.设置本地账号1.新建用户2.开启双重验证3,开启TCP/IP本地服务二js.python连接实例1.

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.