此项目根据rabbitmq-tutorials.git项目而来,算是实际操作一把。
准备工作
- 安装并启动rabbitmq,这里有安装过程:
https://editor.csdn.net/md/?articleId=104569766 - git拉取rabbitmq-tutorials项目,地址:
https://github.com/rabbitmq/rabbitmq-tutorials.git - idea导入git项目的spring-amqp项目
开始使用
进入idea打开的项目,执行命令:
mvn clean package
然后开启2个terminal,以下是6组命令,等会要一对对执行:
# 前5个先启动sender 再启动receiver (其实顺序没关系)
java -jar target/rabbitmq-tutorials.jar --spring.profiles.active=hello-world,receiver
java -jar target/rabbitmq-tutorials.jar --spring.profiles.active=hello-world,sender
java -jar target/rabbitmq-tutorials.jar --spring.profiles.active=work-queues,receiver
java -jar target/rabbitmq-tutorials.jar --spring.profiles.active=work-queues,sender
java -jar target/rabbitmq-tutorials.jar --spring.profiles.active=pub-sub,receiver
java -jar target/rabbitmq-tutorials.jar --spring.profiles.active=pub-sub,sender
java -jar target/rabbitmq-tutorials.jar --spring.profiles.active=routing,receiver
java -jar target/rabbitmq-tutorials.jar --spring.profiles.active=routing,sender
java -jar target/rabbitmq-tutorials.jar --spring.profiles.active=topics,receiver
java -jar target/rabbitmq-tutorials.jar --spring.profiles.active=topics,sender
## 先启动server 再启动client
java -jar target/rabbitmq-tutorials.jar --spring.profiles.active=rpc,client
java -jar target/rabbitmq-tutorials.jar --spring.profiles.active=rpc,server
tut1
直接发送和接收,不会丢失
发布:
this.template.convertAndSend(queue.getName(), message);
接收:
@RabbitListener和@RabbitHandler共同实现了队列的接收
@RabbitListener(queues = "hello")
public class Tut1Receiver {
@RabbitHandler
public void receive(String in) {
System.out.println(" [x] Received '" + in + "'");
}
}
tut2
队列模式
tut3
发布订阅模式
tut4
路由模式
tut5
主题模式
tut6
远程调用
application-remote.yml配置文件:
spring:
rabbitmq:
host: rabbitserver
username: tutorial
password: tutorial