热门标签 | HotTags
当前位置:  开发笔记 > 后端 > 正文

详解SpringBoot配置多个RabbitMQ

闲话 好久没有写博客了,6月份毕业,因为工作原因,公司上网受限,一直没能把学到的知识点写下来,工作了半年,其实学到的东西也不少,但是现在回

闲话

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

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

最近公司新启动一个新平台的项目,需要用微服务这个这几年很火的概念来做,所以就学习了Spring Boot方面的知识,给同事展示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项目中最重要的一个配置文件


 4.0.0
 com.paas.springboot.demo
 springboot
 war
 0.0.1-SNAPSHOT
 springboot Maven Webapp
 http://maven.apache.org

 
  org.springframework.boot
  spring-boot-starter-parent
  1.4.3.RELEASE
   
 

 
  
   junit
   junit
   test
  
  
   org.springframework.boot
   spring-boot-starter-amqp
  
  
   org.springframework.boot
   spring-boot-starter-actuator
  
   
   org.springframework.boot
   spring-boot-starter-web
  
  
   org.springframework.boot
   spring-boot-starter-jdbc
  
  
   org.springframework.boot
   spring-boot-starter-test
   test
  
  
   com.jayway.jsonpath
   json-path
   test
  
  
   mysql
   mysql-connector-java
  
 

 
  springboot
  
   
    org.springframework.boot
    spring-boot-maven-plugin
   
  
 

 
  
   spring-releases
   https://repo.spring.io/libs-release
  
 
 
  
   spring-releases
   https://repo.spring.io/libs-release
  
 



运行&测试

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

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


推荐阅读
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • r2dbc配置多数据源
    R2dbc配置多数据源问题根据官网配置r2dbc连接mysql多数据源所遇到的问题pom配置可以参考官网,不过我这样配置会报错我并没有这样配置将以下内容添加到pom.xml文件d ... [详细]
  • 本文介绍了如何使用JSONObiect和Gson相关方法实现json数据与kotlin对象的相互转换。首先解释了JSON的概念和数据格式,然后详细介绍了相关API,包括JSONObject和Gson的使用方法。接着讲解了如何将json格式的字符串转换为kotlin对象或List,以及如何将kotlin对象转换为json字符串。最后提到了使用Map封装json对象的特殊情况。文章还对JSON和XML进行了比较,指出了JSON的优势和缺点。 ... [详细]
  • Activiti7流程定义开发笔记
    本文介绍了Activiti7流程定义的开发笔记,包括流程定义的概念、使用activiti-explorer和activiti-eclipse-designer进行建模的方式,以及生成流程图的方法。还介绍了流程定义部署的概念和步骤,包括将bpmn和png文件添加部署到activiti数据库中的方法,以及使用ZIP包进行部署的方式。同时还提到了activiti.cfg.xml文件的作用。 ... [详细]
  • 本文讨论了在shiro java配置中加入Shiro listener后启动失败的问题。作者引入了一系列jar包,并在web.xml中配置了相关内容,但启动后却无法正常运行。文章提供了具体引入的jar包和web.xml的配置内容,并指出可能的错误原因。该问题可能与jar包版本不兼容、web.xml配置错误等有关。 ... [详细]
  • 本文讨论了Alink回归预测的不完善问题,指出目前主要针对Python做案例,对其他语言支持不足。同时介绍了pom.xml文件的基本结构和使用方法,以及Maven的相关知识。最后,对Alink回归预测的未来发展提出了期待。 ... [详细]
  • ZSI.generate.Wsdl2PythonError: unsupported local simpleType restriction ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Java验证码——kaptcha的使用配置及样式
    本文介绍了如何使用kaptcha库来实现Java验证码的配置和样式设置,包括pom.xml的依赖配置和web.xml中servlet的配置。 ... [详细]
  • Android系统移植与调试之如何修改Android设备状态条上音量加减键在横竖屏切换的时候的显示于隐藏
    本文介绍了如何修改Android设备状态条上音量加减键在横竖屏切换时的显示与隐藏。通过修改系统文件system_bar.xml实现了该功能,并分享了解决思路和经验。 ... [详细]
  • flowable工作流 流程变量_信也科技工作流平台的技术实践
    1背景随着公司业务发展及内部业务流程诉求的增长,目前信息化系统不能够很好满足期望,主要体现如下:目前OA流程引擎无法满足企业特定业务流程需求,且移动端体 ... [详细]
  • 本文介绍了一些Java开发项目管理工具及其配置教程,包括团队协同工具worktil,版本管理工具GitLab,自动化构建工具Jenkins,项目管理工具Maven和Maven私服Nexus,以及Mybatis的安装和代码自动生成工具。提供了相关链接供读者参考。 ... [详细]
  • iOS超签签名服务器搭建及其优劣势
    本文介绍了搭建iOS超签签名服务器的原因和优势,包括不掉签、用户可以直接安装不需要信任、体验好等。同时也提到了超签的劣势,即一个证书只能安装100个,成本较高。文章还详细介绍了超签的实现原理,包括用户请求服务器安装mobileconfig文件、服务器调用苹果接口添加udid等步骤。最后,还提到了生成mobileconfig文件和导出AppleWorldwideDeveloperRelationsCertificationAuthority证书的方法。 ... [详细]
  • uniapp开发H5解决跨域问题的两种代理方法
    本文介绍了uniapp开发H5解决跨域问题的两种代理方法,分别是在manifest.json文件和vue.config.js文件中设置代理。通过设置代理根域名和配置路径别名,可以实现H5页面的跨域访问。同时还介绍了如何开启内网穿透,让外网的人可以访问到本地调试的H5页面。 ... [详细]
  • Android日历提醒软件开源项目分享及使用教程
    本文介绍了一款名为Android日历提醒软件的开源项目,作者分享了该项目的代码和使用教程,并提供了GitHub项目地址。文章详细介绍了该软件的主界面风格、日程信息的分类查看功能,以及添加日程提醒和查看详情的界面。同时,作者还提醒了读者在使用过程中可能遇到的Android6.0权限问题,并提供了解决方法。 ... [详细]
author-avatar
企鹅之神魔大陆_544
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有