真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

詳解SpringBoot配置多個RabbitMQ

閑話

創(chuàng)新互聯(lián)專注于即墨企業(yè)網(wǎng)站建設,成都響應式網(wǎng)站建設,購物商城網(wǎng)站建設。即墨網(wǎng)站建設公司,為即墨等地區(qū)提供建站服務。全流程定制制作,專業(yè)設計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務

好久沒有寫博客了,6月份畢業(yè),因為工作原因,公司上網(wǎng)受限,一直沒能把學到的知識點寫下來,工作了半年,其實學到的東西也不少,但是現(xiàn)在回憶起來的東西少之又少,有時甚至能在同個問題中踩了幾次,越來越覺得及時記錄一下學到的東西很重要。

好了,閑話少說,寫下這段時間學習的東西,先記錄一下用spring Boot配置多個RabbitMQ的情況。。。

最近公司新啟動一個新平臺的項目,需要用微服務這個這幾年很火的概念來做,所以就學習了Spring Boot方面的知識,給同事展示Spring Boot的一些小事例的時候,同事提出了可不可以配置多個RabbitMQ?下面就是在Spring Boot配置多個RabbitMQ的例子。是自己摸索搭建的,也不知道對不對,有其他好的實現(xiàn)方法的網(wǎng)友可以互相交流一下。

項目代碼構造

詳解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的數(shù)據(jù)

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的數(shù)據(jù)

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中的生產(chǎn)者,發(fā)送消息到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中的生產(chǎn)者,發(fā)送消息到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發(fā)送消息

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啟動一直監(jiān)控著隊列,然后通過運行TestDemo01.java中的測試案例,發(fā)送消息到隊列中,這時可以發(fā)現(xiàn)運行HelloApplication的程序控制臺將剛剛發(fā)送的消息打印出來

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。


本文題目:詳解SpringBoot配置多個RabbitMQ
文章網(wǎng)址:http://weahome.cn/article/gesdjs.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部