RabbitMQ
导入依赖
<!-- 集成RabbitMQ -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
配置相关信息
RabbitMQ的端口号是什么?
5672 :这是rabbitMQ的端口号;
15672 :这是那个RabbitMQ的web页面的端口号;
spring.application.name=spirng-boot-rabbitmq
spring.rabbitmq.host=192.168.xx.xx ##主机ip
spring.rabbitmq.port=5672
spring.rabbitmq.virtual-host=testHA
spring.rabbitmq.username=admin
spring.rabbitmq.password=123456
spring:
rabbitmq:
username: zolvces
password: zolvces
addresses: 192.168.39.128:5672
cache:
connection:
#Cache connection mode, with default connections and multiple channels
mode: channel
#Multiple connections, multiple channels
# mode: connection
# rabbitmq
spring:
rabbitmq:
host: 192.168.253.128
port: 5672
virtual-host: /
username: test
password: 123456
发送消息
@Autowired
private RabbitTemplate rabbitTemplate;
@Test
public void testSimpleQueue() {
// 队列名称
String queueName = "ThisKey";
// 消息
String message = "Hello, Spring AMQP!";
// 发送消息
rabbitTemplate.convertAndSend(queueName, message);
}
rabbitTemplate.convertAndSend(key, message);
接收消息
@Component
public class SpringRabbitMQListener {
@RabbitListener(queues = "simple.queue")
public void listenSimpleQueueMsg(String msg){
System.out.println(msg);
}
}
注意:此处消息被消费后,对应的ThisKey
中的消息就消失了。
RabbitMQ-基础使用(Spring AMQP) - 简书 (jianshu.com)
标签:spring,中间件,5672,rabbitmq,host,RabbitMQ,端口号 From: https://www.cnblogs.com/maomao777/p/16923390.html