一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - 詳解Spring Boot 配置多個(gè)RabbitMQ

詳解Spring Boot 配置多個(gè)RabbitMQ

2020-11-23 13:031CSH1 Java教程

本篇文章主要介紹了Spring Boot 配置多個(gè)RabbitMQ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

閑話

好久沒(méi)有寫博客了,6月份畢業(yè),因?yàn)楣ぷ髟颍旧暇W(wǎng)受限,一直沒(méi)能把學(xué)到的知識(shí)點(diǎn)寫下來(lái),工作了半年,其實(shí)學(xué)到的東西也不少,但是現(xiàn)在回憶起來(lái)的東西少之又少,有時(shí)甚至能在同個(gè)問(wèn)題中踩了幾次,越來(lái)越覺(jué)得及時(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)造

詳解Spring Boot 配置多個(gè)RabbitMQ

關(guān)注點(diǎn)在紅框的代碼。。。

代碼

下面就把項(xiàng)目的代碼展示下來(lái)

application.properties

配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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

程序入口

?
1
2
3
4
5
6
7
8
9
10
11
12
13
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配置類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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ù)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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中的消費(fèi)者,接收second RabbitMQ中的隊(duì)列hello2的數(shù)據(jù)

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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中的隊(duì)列hello1和hello2

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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中的隊(duì)列hello1和hello2

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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

測(cè)試類,調(diào)用Sender發(fā)送消息

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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項(xiàng)目中最重要的一個(gè)配置文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<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>

運(yùn)行&測(cè)試

通過(guò)運(yùn)行HelloApplication.Java,將程序中的Receiver啟動(dòng)一直監(jiān)控著隊(duì)列,然后通過(guò)運(yùn)行TestDemo01.java中的測(cè)試案例,發(fā)送消息到隊(duì)列中,這時(shí)可以發(fā)現(xiàn)運(yùn)行HelloApplication的程序控制臺(tái)將剛剛發(fā)送的消息打印出來(lái)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:https://1csh1.github.io/2017/01/19/Spring%20Boot%20%E9%85%8D%E7%BD%AE%E5%A4%9A%E4%B8%AARabbitMQ/

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚洲国产在线视频精品 | 国产福利在线观看第二区 | 国产在线视频欧美亚综合 | 国内精品久久久久影院中国 | 日本人泡妞xxxxxx69 | 国产免费美女视频 | 日本妇人成熟免费不卡片 | 国产精品秒播无毒不卡 | 91制片厂制作果冻传媒破解 | 操一操影院| 沟厕okn系列在线播放 | 国产66 | 欧美高清在线不卡免费观看 | 色戒 完整版 | 亚洲精品久久久992KVTV | 午夜大片在线观看 | 午夜片神马影院福利 | 午夜亚洲精品久久久久久 | 国产精品亚欧美一区二区三区 | 久久国产精品高清一区二区三区 | 茄子视频懂你更多apl | 亚洲色图.com | 青草视频网站在线观看 | 欧美1| 精品高潮呻吟99AV无码 | 午夜国产精品福利在线观看 | 精品久久伦理中文字幕 | 韩国久播影院理论片不卡影院 | 奇米影视久久777中文字幕 | 国产亚洲欧美成人久久片 | 性做久久久久久久久老女人 | 欧美黑人成人免费全部 | 亚洲精品123区在线观看 | 日韩无砖2021特黄 | 久久中文字幕亚洲精品最新 | 亚州人成网在线播放 | 亚洲天堂999| 国产91成人精品亚洲精品 | 天堂成人在线观看 | 欧美成人精品福利在线视频 | 大伊人青草狠狠久久 |