我正在尝试使用RabbitMQ在 Spring 创建发布者和消费者应用程序。一切都运行良好,当发布者发送消息时,消费者接收并成功使用它。但如下图所示,RabbitMQ界面没有显示创建的队列和交换。
无队列
不交换
这是我写的代码:
RABBITMQ配置(发布者应用程序和消费者应用程序中相同)
package com.example.rabbitmq.springrabbitmqconsumer.configuration;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
public static final String ROUTING_A = "routing.A";
public static final String ROUTING_B = "routing.B";
//QUEUES
@Bean
Queue queueA() {
return new Queue("queue.A", false);
}
@Bean
Queue queueB() {
return new Queue("queue.B", false);
}
//Direct Exchange
@Bean
DirectExchange exchange() {
return new DirectExchange("exchange.direct");
}
//BINDINGS
@Bean
Binding bindingA(Queue queueA, DirectExchange exchange) {
return BindingBuilder.bind(queueA)
.to(exchange)
.with(ROUTING_A);
}
@Bean
Binding bindingB(Queue queueB, DirectExchange exchange) {
return BindingBuilder.bind(queueB)
.to(exchange)
.with(ROUTING_B);
}
@Bean
MessageConverter messageConverter() {
return new Jackson2JsonMessageConverter();
}
@Bean
RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setMessageConverter(messageConverter());
return rabbitTemplate;
}
}
消息模型(发布者和消费者应用程序中相同)
package com.example.rabbitmq.springrabbitmqconsumer.model;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
@Getter
@Setter
@ToString
public class Message {
private int id;
private String name;
}
消费者控制器
package com.example.rabbitmq.springrabbitmqconsumer.controller;
import com.example.rabbitmq.springrabbitmqconsumer.model.Message;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class Consumer {
@RabbitListener(queues = "queue.A")
private void receiveA(Message message) {
log.info("Message received from queueA -> {}", message);
}
@RabbitListener(queues = "queue.B")
private void receiveB(Message message) {
log.info("Message received from queueB -> {}", message);
}
}
唱针控制器
package com.example.rabbitmq.springrabbitmqproducer.contoller;
import com.example.rabbitmq.springrabbitmqproducer.model.Message;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class Producer {
private final RabbitTemplate rabbitTemplate;
private final DirectExchange exchange;
public Producer(RabbitTemplate rabbitTemplate, DirectExchange exchange) {
this.rabbitTemplate = rabbitTemplate;
this.exchange = exchange;
}
@PostMapping("/posta")
public String senda(@RequestBody Message message) {
rabbitTemplate.convertAndSend(exchange.getName(), "routing.A", message);
return "message sent successfully";
}
@PostMapping("/postb")
public String sendb(@RequestBody Message message) {
rabbitTemplate.convertAndSend(exchange.getName(), "routing.B", message);
return "message sent successfully";
}
}
属性文件(除端口外,两个应用程序中的属性相同)
server.port=8081
# rabbitmq configuration
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
这是我用来启动RabbitMQ容器的docker命令:
docker run -d --name my-rabbit -p 15672:15672 -p 5672:5672 rabbitmq:3-management
正如您所看到的,使用者接收消息并使用它
1条答案
按热度按时间pbpqsu0x1#
我刚想起来我问了这个问题,我忘了回答它,所以这里是解决方案。
我发现rabbitmq被安装在我的pc上,这引起了一个混乱,因为,Spring自动运行rabbitmq示例,就像它运行tomcat一样,所以当我运行一个rabbitmq docker映像时,Spring使用的是本地示例而不是docker映像。
我只是意识到,当我停靠我的 Spring 应用程序和我的rabbitmq管理用户界面开始注册队列。
结语
如果你有rabbitmq本地安装在你的电脑这是一些情况下,可能会发生:
1/非固定Spring应用程序:工作正常bcz spring默认使用本地rabbitmq示例,可以打开管理界面
2/dockerised spring应用程序+ rabbitmq docker图片:工作正常,bcz当u dockerise ur spring应用程序将停止使用本地示例
3/非停靠的Spring应用程序+ rabbitmq停靠器映像:奇怪的行为,spring会继续将消息推送到队列中,但当您尝试打开管理UI时,您不会看到任何已注册的队列,可能是因为这是Docker映像示例的管理UI