-
rabbitTemplate实现自amqpTemplate接口,需引入spring-boot-starter-amqp依赖
<
dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
- 配置application.yml文件,我这边是配置两个mq,分别为:mq_a,mq_b
###服务启动端口号
server:
port: 8088
spring:
rabbitmq:
mq_a:
host: 15.201.x.01
port: 5672
username: admin
password: admin
mq_b:
host: 15.201.x.02
port: 60060
username: user
password: user
- 分别配置RabbitTemplate
package com.thg.config;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Configuration
public class MqFlagByAConfig {
@Value("${spring.rabbitmq.mq_a.host}")
private String host;
@Value("${spring.rabbitmq.mq_a.port}")
private int port;
@Value("${spring.rabbitmq.mq_a.username}")
private String username;
@Value("${spring.rabbitmq.mq_a.password}")
private String password;
@Bean("mqFlagByAConfigConnectionFactory")
@Primary
public ConnectionFactory mqFlagByAConfigConnectionFactory(){
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
return connectionFactory;
@Bean(name="mqaRabbitTemplate")
public RabbitTemplate mqaRabbitTemplate(@Qualifier("mqFlagByAConfigConnectionFactory") ConnectionFactory connectionFactory){
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
return rabbitTemplate;
package com.thg.config;
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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
@Configuration
public class MqFlagByBConfig {
@Value("${spring.rabbitmq.mq_b.host}")
private String host;
@Value("${spring.rabbitmq.mq_b.port}")
private int port;
@Value("${spring.rabbitmq.mq_b.username}")
private String username;
@Value("${spring.rabbitmq.mq_b.password}")
private String password;
@Bean("mqFlagByBConfigConnectionFactory")
public ConnectionFactory mqFlagByBConfigConnectionFactory(){
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(host);
connectionFactory.setPort(port);
connectionFactory.setUsername(username);
connectionFactory.setPassword(password);
return connectionFactory;
@Bean(name="mqbRabbitTemplate")
public RabbitTemplate mqbRabbitTemplate(@Qualifier("mqFlagByBConfigConnectionFactory") ConnectionFactory connectionFactory){
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
return rabbitTemplate;
- 使用时注入自己需要的template模板,可以写一个类似这样得工具类,这样方便管理调用。
package com.thg.mq;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class SendMessage {
@Resource(name = "mqaRabbitTemplate")
private RabbitTemplate mqaRabbitTemplate;
@Resource(name = "mqbRabbitTemplate")
private RabbitTemplate mqbRabbitTemplate;
private final static String exChange_police_action ="police_action";
public void sendMessage(String message){
this.mqaRabbitTemplate.convertAndSend(
exChange_police_action,"test","A---->"+message);
this.mqbRabbitTemplate.convertAndSend("test","B---->"+message);
- 可以写个junit测试一下,我这边使用controller调用得
package com.thg.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@RestController
public class TestController {
@Autowired
private SendMessage sendMQMessage;
@RequestMapping(value = "/testMq", method = RequestMethod.POST)
public String testMq() {
sendMQMessage.sendMessage("测试测试测试!!!");
rabbitTemplate实现自amqpTemplate接口,需引入spring-boot-starter-amqp依赖<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-amqp</artifactId></dependency>配置application.yml文件###服.
文章目录一、认识 RabbitMQ二、安装 RabbitMQ三、SpringBoot 整合 RabbitMQ1. 加入依赖2. 配置 application.properties
一、认识 RabbitMQ
RabbitMQ 简介以 AMQP 协议:
(1)RabbitMQ 是开源的消息代理和队列服务器,用来通过普通协议在完全不同的应用之间共享数据,RabbitMQ 底层是用了 Erlang 语言来编写的,并且 RabbitMQ 是基于 AMQP 协议的.
(2)RabbitMQ 不仅仅可以使用 java
信道。消息的读写操作在信道中进行,客户端可以建立多个信道,每个信道代表一个会话。
Message
消息。应用程序和MQ服务之间传送的数据,消息可以非常简单,也可以很复杂。有Properties和body组成。Properties为外包装,可以对消息进行修饰,比如消息的优先级、延迟等高级特性;body就是消息体内容。
Exchange
交换器,一个MQ服务器可以有一个或多个交换器。应用程序通过交换器,按照一定路由规则将消
本文章总体简介,会采用Docker下载安装RabbitMQ,详细介绍RabbitMQ优缺点及其相关技术知识点,SpringBoot作为整合学习的基础架构。看完本片文章,你会了解消息队列RabbitMQ,你会使用消息队列RabbitMQ。
二、消息队列
当第一次出现在我脑中这个词语时,我会问自己什么时消息队列?我做的项目为什么没有消息队列?我做的项目可以用消息队列么?如果可以用消息队列这要怎么使用消息队列?消息队列能解决项目什么问题?等等问题会出现在我的脑海…,然而我将会在面文章中对自己慢慢
一个spring boot项目配置一个rabbit mq很常见,如何配置两个以及两个以上的mq?本篇文章将结合代码说明如何配置两个rabbit mq(talk is cheap, show me the code)。
2.项目结构
<groupId>cn.honorzhang</groupId>
<artifactId>my-springboot-rabbitmq</artifactId>
<version>1.0.0</.
RabbitMQ介绍与安装
1.RabbitMQ的介绍
RabbitMQ是实现了高级消息队列协议(AMQP)的开源消息代理软件(也称为消息的中间件)。AMQP(Advanced Message Queuing Protocol)高级消息队列协议:客户端向MQ发送的消息协议是AMQP协议。JMS(Java Message Server)一样,都是一种消息规范,相比而言可定是AMQP高级一些。
2.MQ的功能
1)异步处理:把用户的请求发送给消息中间件后,消息中间件会先进行局部响应,
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</depend
Spring Boot可以很容易地配置多个RabbitMQ实例。以下是配置多个RabbitMQ实例的步骤:
1. 在application.properties文件中添加多个RabbitMQ实例的配置信息。例如:
spring.rabbitmq.host=host1
spring.rabbitmq.port=5672
spring.rabbitmq.username=user1
spring.rabbitmq.password=password1
spring.rabbitmq.second.host=host2
spring.rabbitmq.second.port=5672
spring.rabbitmq.second.username=user2
spring.rabbitmq.second.password=password2
2. 创建多个RabbitTemplate和ConnectionFactory bean。例如:
@Configuration
public class RabbitMQConfig {
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(env.getProperty("spring.rabbitmq.host"));
connectionFactory.setPort(env.getProperty("spring.rabbitmq.port", Integer.class));
connectionFactory.setUsername(env.getProperty("spring.rabbitmq.username"));
connectionFactory.setPassword(env.getProperty("spring.rabbitmq.password"));
return connectionFactory;
@Bean
public RabbitTemplate rabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(connectionFactory());
return template;
@Bean(name = "secondConnectionFactory")
public ConnectionFactory secondConnectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
connectionFactory.setHost(env.getProperty("spring.rabbitmq.second.host"));
connectionFactory.setPort(env.getProperty("spring.rabbitmq.second.port", Integer.class));
connectionFactory.setUsername(env.getProperty("spring.rabbitmq.second.username"));
connectionFactory.setPassword(env.getProperty("spring.rabbitmq.second.password"));
return connectionFactory;
@Bean(name = "secondRabbitTemplate")
public RabbitTemplate secondRabbitTemplate() {
RabbitTemplate template = new RabbitTemplate(secondConnectionFactory());
return template;
3. 在需要使用RabbitMQ的地方,注入对应的RabbitTemplate或ConnectionFactory bean即可。例如:
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
@Qualifier("secondRabbitTemplate")
private RabbitTemplate secondRabbitTemplate;
使用以上步骤,就可以在Spring Boot应用中配置多个RabbitMQ实例了。
### 回答2:
在Spring Boot中配置多个RabbitMQ会让应用程序变得更加灵活,这意味着应用可以与多个RabbitMQ实例连接,并能够发送和接收消息。
Spring Boot通过在配置文件(application.properties或application.yml)中定义多个RabbitMQ实例来实现多个RabbitMQ的配置。下面是在同一应用程序中配置两个RabbitMQ实例的示例:
在application.yml文件中添加以下内容:
spring.rabbitmq.host=server1
spring.rabbitmq.port=5672
spring.rabbitmq.username=user1
spring.rabbitmq.password=password1
spring.rabbitmq.virtual-host=/vhost1
spring.rabbitmq.second.host=server2
spring.rabbitmq.second.port=5672
spring.rabbitmq.second.username=user2
spring.rabbitmq.second.password=password2
spring.rabbitmq.second.virtual-host=/vhost2
在上面的示例中,我们定义了两个RabbitMQ实例,一个用于服务器1,另一个用于服务器2。对于每个实例,我们指定了名称,主机名,端口,用户名,密码和虚拟主机。
为了使用以上配置,我们需要通过@Resource注解或@Autowired注解在Java类中定义一个RabbitTemplate bean。同时在需要连接第二个RabbitMQ实例的地方指定使用哪个RabbitMQ实例即可。例如:
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
@Qualifier("secondRabbitTemplate")
private RabbitTemplate secondRabbitTemplate;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
@Override
public void run(String... args) throws Exception {
rabbitTemplate.convertAndSend("queue1", "message1");
secondRabbitTemplate.convertAndSend("queue2", "message2");
在上面的代码中,我们注入了两个不同的RabbitTemplate实例,分别用于发送到不同的RabbitMQ实例。我们可以使用这两个实例来发送消息到不同的队列。在这个例子中,我们把“message1”发送到“queue1”,把“message2” 发送到“queue2”。
总之,配置多个RabbitMQ实例可以让Spring Boot应用程序与多个RabbitMQ连接,并实现更复杂的异构应用程序。我们只需要简单地在配置文件中添加多个实例,并通过Java类中的@Resource或@Autowired注解即可使用。
### 回答3:
Spring Boot 是一种流行的 Java 框架,用于快速构建基于 Spring 框架的应用程序。RabbitMQ 是一个流行的开源消息队列,常用于构建分布式系统。Spring Boot 提供了对 RabbitMQ 的支持,并允许用户配置多个 RabbitMQ。
配置多个 RabbitMQ,可以通过在 application.properties 或 application.yml 文件中添加多个 RabbitMQ 的配置来实现。如下所示:
application.yml:
spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest
virtual-host: /
host: localhost
port: 5673
username: guest
password: guest
virtual-host: /
application.properties:
spring.rabbitmq.1.host=localhost
spring.rabbitmq.1.port=5672
spring.rabbitmq.1.username=guest
spring.rabbitmq.1.password=guest
spring.rabbitmq.1.virtual-host=/
spring.rabbitmq.2.host=localhost
spring.rabbitmq.2.port=5673
spring.rabbitmq.2.username=guest
spring.rabbitmq.2.password=guest
spring.rabbitmq.2.virtual-host=/
其中,`1` 和 `2` 表示 RabbitMQ 的标识符。我们可以根据需要添加更多的标识符。在我们的应用程序中,我们可以使用以下方式注入多个RabbitMQ:
@Bean
@Primary
public ConnectionFactory connectionFactory1() {
CachingConnectionFactory connectionFactory =
new CachingConnectionFactory("localhost", 5672);
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
return connectionFactory;
@Bean
public RabbitTemplate rabbitTemplate1(ConnectionFactory connectionFactory1) {
return new RabbitTemplate(connectionFactory1);
@Bean
public SimpleMessageListenerContainer messageListenerContainer1(
ConnectionFactory connectionFactory1) {
SimpleMessageListenerContainer container =
new SimpleMessageListenerContainer(connectionFactory1);
container.setQueueNames("queue1");
container.setDefaultRequeueRejected(false);
container.setMessageListener(messageListenerAdapter1());
return container;
@Bean
public MessageListenerAdapter messageListenerAdapter1() {
return new MessageListenerAdapter(new MyMessageListener());
@Bean
public ConnectionFactory connectionFactory2() {
CachingConnectionFactory connectionFactory =
new CachingConnectionFactory("localhost", 5673);
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
return connectionFactory;
@Bean
public RabbitTemplate rabbitTemplate2(ConnectionFactory connectionFactory2) {
return new RabbitTemplate(connectionFactory2);
@Bean
public SimpleMessageListenerContainer messageListenerContainer2(
ConnectionFactory connectionFactory2) {
SimpleMessageListenerContainer container =
new SimpleMessageListenerContainer(connectionFactory2);
container.setQueueNames("queue2");
container.setDefaultRequeueRejected(false);
container.setMessageListener(messageListenerAdapter2());
return container;
@Bean
public MessageListenerAdapter messageListenerAdapter2() {
return new MessageListenerAdapter(new MyMessageListener());
这样,在我们的应用程序中就可以使用`rabbitTemplate1`和`rabbitTemplate2`来发送消息,使用 `messageListenerContainer1`和`messageListenerContainer2`监听队列消息。
在 Spring Boot 配置多个 RabbitMQ 的过程中,我们需要在 application.properties 或 application.yml 中添加多个配置,注入多个 ConnectionFactory,RabbitTemplate 和 SimpleMessageListenerContainer 来进行发送和监听消息。同时,我们还需要根据实际情况调整配置,确保消息传递的正确性和可靠性。