好久沒有寫博客了,6月份畢業(yè),因?yàn)楣ぷ髟?,公司上網(wǎng)受限,一直沒能把學(xué)到的知識(shí)點(diǎn)寫下來(lái),工作了半年,其實(shí)學(xué)到的東西也不少,但是現(xiàn)在回憶起來(lái)的東西少之又少,有時(shí)甚至能在同個(gè)問(wèn)題中踩了幾次,越來(lái)越覺得及時(shí)記錄一下學(xué)到的東西很重要。
好了,閑話少說(shuō),寫下這段時(shí)間學(xué)習(xí)的東西,先記錄一下用spring Boot配置多個(gè)RabbitMQ的情況。。。
最近公司新啟動(dòng)一個(gè)新平臺(tái)的項(xiàng)目,需要用微服務(wù)這個(gè)這幾年很火的概念來(lái)做,所以就學(xué)習(xí)了Spring Boot方面的知識(shí),給同事展示Spring Boot的一些小事例的時(shí)候,同事提出了可不可以配置多個(gè)RabbitMQ?下面就是在Spring Boot配置多個(gè)RabbitMQ的例子。是自己摸索搭建的,也不知道對(duì)不對(duì),有其他好的實(shí)現(xiàn)方法的網(wǎng)友可以互相交流一下。
項(xiàng)目代碼構(gòu)造
關(guān)注點(diǎn)在紅框的代碼。。。
代碼
下面就把項(xiàng)目的代碼展示下來(lái)
application.properties
配置文件
spring.application.name=rabbitmq-hello # RabbitMQ spring.rabbitmq.first.host=node9 spring.rabbitmq.first.port=5670 spring.rabbitmq.first.username=guest spring.rabbitmq.first.password=guest spring.rabbitmq.second.host=localhost spring.rabbitmq.second.port=5672 spring.rabbitmq.second.username=guest spring.rabbitmq.second.password=guest # MySQL spring.datasource.url = jdbc:mysql://localhost:3306/cloudtest spring.datasource.username = root spring.datasource.password = root spring.datasource.driverClassName = com.mysql.jdbc.Driver
HelloApplication.java
程序入口
package com.paas.springboot.demo01; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class HelloApplication { public static void main(String[] args) { SpringApplication.run(HelloApplication.class, args); } }
RabbitConfig.java
RabbitMQ配置類
package com.paas.springboot.demo01; import org.springframework.amqp.core.Queue; import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory; import org.springframework.amqp.rabbit.connection.CachingConnectionFactory; import org.springframework.amqp.rabbit.connection.ConnectionFactory; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; @Configuration public class RabbitConfig { @Bean(name="firstConnectionFactory") @Primary public ConnectionFactory firstConnectionFactory( @Value("${spring.rabbitmq.first.host}") String host, @Value("${spring.rabbitmq.first.port}") int port, @Value("${spring.rabbitmq.first.username}") String username, @Value("${spring.rabbitmq.first.password}") String password ){ CachingConnectionFactory connectionFactory = new CachingConnectionFactory(); connectionFactory.setHost(host); connectionFactory.setPort(port); connectionFactory.setUsername(username); connectionFactory.setPassword(password); return connectionFactory; } @Bean(name="secondConnectionFactory") public ConnectionFactory secondConnectionFactory( @Value("${spring.rabbitmq.second.host}") String host, @Value("${spring.rabbitmq.second.port}") int port, @Value("${spring.rabbitmq.second.username}") String username, @Value("${spring.rabbitmq.second.password}") String password ){ CachingConnectionFactory connectionFactory = new CachingConnectionFactory(); connectionFactory.setHost(host); connectionFactory.setPort(port); connectionFactory.setUsername(username); connectionFactory.setPassword(password); return connectionFactory; } @Bean(name="firstRabbitTemplate") @Primary public RabbitTemplate firstRabbitTemplate( @Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory ){ RabbitTemplate firstRabbitTemplate = new RabbitTemplate(connectionFactory); return firstRabbitTemplate; } @Bean(name="secondRabbitTemplate") public RabbitTemplate secondRabbitTemplate( @Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory ){ RabbitTemplate secondRabbitTemplate = new RabbitTemplate(connectionFactory); return secondRabbitTemplate; } @Bean(name="firstFactory") public SimpleRabbitListenerContainerFactory firstFactory( SimpleRabbitListenerContainerFactoryConfigurer configurer, @Qualifier("firstConnectionFactory") ConnectionFactory connectionFactory ) { SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); configurer.configure(factory, connectionFactory); return factory; } @Bean(name="secondFactory") public SimpleRabbitListenerContainerFactory secondFactory( SimpleRabbitListenerContainerFactoryConfigurer configurer, @Qualifier("secondConnectionFactory") ConnectionFactory connectionFactory ) { SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory(); configurer.configure(factory, connectionFactory); return factory; } @Bean public Queue firstQueue() { System.out.println("configuration firstQueue ........................"); return new Queue("hello1"); } @Bean public Object secondQueue() { System.out.println("configuration secondQueue ........................"); return new Queue("hello2"); } }
Receiver.java
RabbitMQ中的消費(fèi)者,接收f(shuō)irst RabbitMQ中的隊(duì)列hello1的數(shù)據(jù)