当前位置:主页 > java教程 > 详解Spring Boot 配置多个RabbitMQ

Spring Boot 如何配置多个RabbitMQ

发布:2020-02-05 17:23:14 129


给寻找编程代码教程的朋友们精选了Java相关的编程文章,网友瞿玉轩根据主题投稿了本篇教程内容,涉及到Spring、Boot、RabbitMQ、详解Spring Boot 配置多个RabbitMQ相关内容,已被434网友关注,涉猎到的知识点内容可以在下方电子书获得。

详解Spring Boot 配置多个RabbitMQ

闲话

好久没有写博客了,6月份毕业,因为工作原因,公司上网受限,一直没能把学到的知识点写下来,工作了半年,其实学到的东西也不少,但是现在回忆起来的东西少之又少,有时甚至能在同个问题中踩了几次,越来越觉得及时记录一下学到的东西很重要。

好了,闲话少说,写下这段时间学习的东西,先记录一下用spring Boot配置多个RabbitMQ的情况。。。

最近公司新启动一个新平台的项目,需要用微服务这个这几年很火的概念来做,所以就学习了Spring Boot方面的知识,给同事展示Spring Boot的一些小事例的时候,同事提出了可不可以配置多个RabbitMQ?下面就是在Spring Boot配置多个RabbitMQ的例子。是自己摸索搭建的,也不知道对不对,有其他好的实现方法的网友可以互相交流一下。

项目代码构造

详解Spring Boot 配置多个RabbitMQ

关注点在红框的代码。。。

代码

下面就把项目的代码展示下来

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中的消费者,接收first RabbitMQ中的队列hello1的数据

package com.paas.springboot.demo01;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "hello1", containerFactory="firstFactory")
public class Receiver {

 @RabbitHandler
 public void process(String hello) {
  System.out.println("Receiver : " + hello);
 }

}

Receiver2.java

RabbitMQ中的消费者,接收second RabbitMQ中的队列hello2的数据

package com.paas.springboot.demo01;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(queues = "hello2", containerFactory="secondFactory" )
public class Receiver2 {

 @RabbitHandler
 public void process(String hello) {
  System.out.println("Receiver : " + hello);
 }

}

Sender.java

RabbitMQ中的生产者,发送消息到first RabbitMQ中的队列hello1和hello2

package com.paas.springboot.demo01;

import java.util.Date;
import javax.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

@Component
public class Sender {

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

 public void send1() {
  String context = "hello1 " + new Date();
  System.out.println("Sender : " + context);
  this.firstRabbitTemplate.convertAndSend("hello1", context);
 }

 public void send2() {
  String context = "hello2 " + new Date();
  System.out.println("Sender : " + context);
  this.firstRabbitTemplate.convertAndSend("hello2", context);
 }

}

Sender2.java

RabbitMQ中的生产者,发送消息到second RabbitMQ中的队列hello1和hello2

package com.paas.springboot.demo01;

import java.util.Date;
import javax.annotation.Resource;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

@Component
public class Sender {

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

 public void send1() {
  String context = "hello1 " + new Date();
  System.out.println("Sender : " + context);
  this.firstRabbitTemplate.convertAndSend("hello1", context);
 }

 public void send2() {
  String context = "hello2 " + new Date();
  System.out.println("Sender : " + context);
  this.firstRabbitTemplate.convertAndSend("hello2", context);
 }

}

TestDemo01.java

测试类,调用Sender发送消息

package com.paas.springboot.demo01;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = HelloApplication.class)
public class TestDemo01 {

 @Autowired
 private Sender sender;

 @Autowired
 private Sender2 sender2;

 @Test
 public void hello() throws Exception {
  sender.send1();
  sender.send2();
 }

 @Test
 public void hello2() throws Exception {
  sender2.send1();
  sender2.send2();
 }
}

pom.xml

Maven项目中最重要的一个配置文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>com.paas.springboot.demo</groupId>
 <artifactId>springboot</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>springboot Maven Webapp</name>
 <url>http://maven.apache.org</url>

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>1.4.3.RELEASE</version>
  <relativePath /> <!-- lookup parent from repository -->
 </parent>

 <dependencies>
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-amqp</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
   <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-jdbc</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>com.jayway.jsonpath</groupId>
   <artifactId>json-path</artifactId>
   <scope>test</scope>
  </dependency>
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
  </dependency>
 </dependencies>

 <build>
  <finalName>springboot</finalName>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
  </plugins>
 </build>

 <repositories>
  <repository>
   <id>spring-releases</id>
   <url>https://repo.spring.io/libs-release</url>
  </repository>
 </repositories>
 <pluginRepositories>
  <pluginRepository>
   <id>spring-releases</id>
   <url>https://repo.spring.io/libs-release</url>
  </pluginRepository>
 </pluginRepositories>

</project>

运行&测试

通过运行HelloApplication.Java,将程序中的Receiver启动一直监控着队列,然后通过运行TestDemo01.java中的测试案例,发送消息到队列中,这时可以发现运行HelloApplication的程序控制台将刚刚发送的消息打印出来

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持码农之家。


参考资料

相关文章

  • 详解bootstrap-Treeview实现级联勾选

    发布:2020-03-12

    这篇文章主要为大家详细介绍了bootstrap-Treeview实现级联勾选,具有一定的参考价值,感兴趣的小伙伴们可以参考一下


  • 分享Bootstrap fileinput文件上传预览插件使用方法

    发布:2020-02-14

    这篇文章主要为大家详细介绍了Bootstrap fileinput文件上传预览插件的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下


  • Spring Cloud Zuul重试机制知识点总结

    发布:2020-01-30

    本篇文章主要介绍了详解Spring Cloud Zuul重试机制探秘,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧


  • Springboot项目全局异常统一处理案例代码

    发布:2023-04-22

    最近在做项目时需要对异常进行全局统一处理,主要是一些分类入库以及记录日志等,因为项目是基于Springboot的,所以去网络上找了一些博客文档,然后再结合项目本身的一些特殊需求做了些许改造,现在记录下来便于以后查看


  • Spring Data JPA实现审计功能过程详解

    发布:2023-03-25

    Spring Data JPA为跟踪持久性层的变化提供了很好的支持。通过使用审核,我们可以存储或记录有关实体更改的信息,例如谁创建或更改了实体以及何时进行更改


  • springboot普通类中如何获取session问题

    发布:2023-03-06

    这篇文章主要介绍了springboot普通类中如何获取session问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教


  • Spring ApplicationContext接口功能详细介绍

    发布:2023-04-11

    ApplicationContext是Spring应用程序中的中央接口,由于继承了多个组件,使得ApplicationContext拥有了许多Spring的核心功能,如获取bean组件,注册监听事件,加载资源文件等


  • Spring AOP的概念与实现过程详解

    发布:2023-03-28

    AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,可通过运行期动态代理实现程序功能的统一维护的一种技术。AOP是 Spring框架中的一个重要内容


网友讨论