依赖
<!--SpringAMQP起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
yml配置文件
自行修改
spring:
rabbitmq:
host: 192.168.220.130 # 主机名
port: 5672 # 端口
virtual-host: / # 虚拟主机
username: user # 用户名
password: 123456 # 密码
SpringRabbitListener
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class SpringRabbitListener {
@RabbitListener(queues = "demo.queue")
public void listenDemoQueue(String message) {
System.out.println("接收消息:" + message);
}
}
测试
启动项目