欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

详解Spring Boot 配置多个RabbitMQ

程序员文章站 2023-12-11 18:30:52
闲话 好久没有写博客了,6月份毕业,因为工作原因,公司上网受限,一直没能把学到的知识点写下来,工作了半年,其实学到的东西也不少,但是现在回忆起来的东西少之又少,有时甚至能...

闲话

好久没有写博客了,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的程序控制台将刚刚发送的消息打印出来

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

上一篇:

下一篇: