文章目录
- [一. 引入依赖](#一. 引入依赖)
- [二. 添加配置](#二. 添加配置)
- [三. Work Queue(工作队列模式)](#三. Work Queue(工作队列模式))
- [四. Publish/Subscribe(发布订阅模式)](#四. Publish/Subscribe(发布订阅模式))
- [五. Routing(路由模式)](#五. Routing(路由模式))
- [六. Topics(通配符模式)](#六. Topics(通配符模式))
一. 引入依赖
创建spring项目
或者直接引入依赖
xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit-test</artifactId>
<scope>test</scope>
</dependency>
二. 添加配置
yml
#配置RabbitMQ的基本信息
spring:
rabbitmq:
host: 110.41.51.65
port: 5672 #默认为5672
username: study
password: study
virtual-host: bite #默认值为 /
# 或者:
#amqp://username:password@Ip:port/virtual-host
spring:
rabbitmq:
addresses: amqp://admin:admin@139.9.84.204:5673/good
三. Work Queue(工作队列模式)
声明队列
java
import com.example.demo.constants.Constants;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class RabbitMQConfig {
//声明队列
@Bean("workQueue")
public Queue workQueue(){
return QueueBuilder.durable(Constants.WORK_QUEUE).build();
}
}
生产者
发送消息
java
import com.example.demo.constants.Constants;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/producer")
public class ProducerController {
@Autowired
private RabbitTemplate rabbitTemplate;
@RequestMapping("/work")
public String work(){
for(int i = 0 ; i < 10; i++){
//使用内置交换机发送消息
rabbitTemplate.convertAndSend("", Constants.WORK_QUEUE, "hello spring amqp: work....");
}
return "发送成功";
}
}
消费者
java
import com.example.demo.constants.Constants;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class WorkListener {
@RabbitListener(queues = Constants.WORK_QUEUE)
public void listenerQueue1(Message message){
System.out.println("listener 1[" + Constants.WORK_QUEUE + "]收到消息: " + message);
}
@RabbitListener(queues = Constants.WORK_QUEUE)
public void listenerQueue2(Message message){
System.out.println("listener 2[" + Constants.WORK_QUEUE + "]收到消息: " + message);
}
}
四. Publish/Subscribe(发布订阅模式)
声明队列和交换机
java
//发布订阅模式
//声明队列
@Bean("fanoutQueue1")
public Queue fanoutQueue1(){
return QueueBuilder.durable(Constants.FANOUT_QUEUE1).build();
}
@Bean("fanoutQueue2")
public Queue fanoutQueue2(){
return QueueBuilder.durable(Constants.FANOUT_QUEUE2).build();
}
//声明交换机
@Bean("fanoutExchange")
public FanoutExchange fanoutExchange(){
return ExchangeBuilder
.fanoutExchange(Constants.FANOUT_EXCHANGE)
.durable(true)
.build();
}
//队列和交换机绑定
@Bean
public Binding fanoutBinding1(@Qualifier("fanoutExchange") FanoutExchange exchange,
@Qualifier("fanoutQueue1") Queue queue){
return BindingBuilder.bind(queue).to(exchange);
}
@Bean
public Binding fanoutBinding2(@Qualifier("fanoutExchange") FanoutExchange exchange,
@Qualifier("fanoutQueue2") Queue queue){
return BindingBuilder.bind(queue).to(exchange);
}
生产者
java
@RequestMapping("/fanout")
public String fanoutProduct() {
rabbitTemplate.convertAndSend(Constants.FANOUT_EXCHANGE, "", "hello spring boot: fanout....");
return "发送成功";
}
消费者
java
@Component
public class FanoutListener {
@RabbitListener(queues = Constants.FANOUT_QUEUE1)
public void listenerQueue1(Message message) {
System.out.println("listener 1[" + Constants.FANOUT_QUEUE1 + "]收到消息: " + message);
}
@RabbitListener(queues = Constants.FANOUT_QUEUE2)
public void listenerQueue2(Message message) {
System.out.println("listener 1[" + Constants.FANOUT_QUEUE2 + "]收到消息: " + message);
}
}
五. Routing(路由模式)
声明队列和交换机
java
//路由模式
@Bean("directQueue1")
public Queue directQueue1(){
return QueueBuilder.durable(Constants.DIRECT_QUEUE1).build();
}
@Bean("directQueue2")
public Queue directQueue2(){
return QueueBuilder.durable(Constants.DIRECT_QUEUE2).build();
}
@Bean("directExchange")
public DirectExchange directExchange(){
return ExchangeBuilder.directExchange(Constants.DIRECT_EXCHANGE).durable(true).build();
}
@Bean
public Binding directBinding1(@Qualifier("directExchange")DirectExchange exchange,
@Qualifier("directQueue1") Queue queue){
return BindingBuilder.bind(queue).to(exchange).with("aaa");
}
@Bean
public Binding directBinding2(@Qualifier("directExchange")DirectExchange exchange,
@Qualifier("directQueue2") Queue queue){
return BindingBuilder.bind(queue).to(exchange).with("bbb");
}
@Bean
public Binding directBinding3(@Qualifier("directExchange")DirectExchange exchange,
@Qualifier("directQueue2") Queue queue){
return BindingBuilder.bind(queue).to(exchange).with("ccc");
}
生产者
java
@RequestMapping("/direct")
public String directProduct(String routingKey){
rabbitTemplate.convertAndSend(Constants.DIRECT_EXCHANGE, routingKey, "hello spring boot: direct " + routingKey);
return "发送成功";
}
消费者
java
@Component
public class DirectListener {
@RabbitListener(queues = Constants.DIRECT_QUEUE1)
public void listenerQueue1(Message message) {
System.out.println("listener 1[" + Constants.DIRECT_QUEUE1 + "]收到消息: " + message);
}
@RabbitListener(queues = Constants.DIRECT_QUEUE2)
public void listenerQueue2(Message message) {
System.out.println("listener 2[" + Constants.DIRECT_QUEUE2 + "]收到消息: " + message);
}
}
六. Topics(通配符模式)
声明队列和交换机
java
//通配符模式
@Bean("topicQueue1")
public Queue topicQueue1(){
return QueueBuilder.durable(Constants.TOPIC_QUEUE1).build();
}
@Bean("topicQueue2")
public Queue topicQueue2(){
return QueueBuilder.durable(Constants.TOPIC_QUEUE2).build();
}
@Bean("topicExchange")
public TopicExchange topicExchange(){
return ExchangeBuilder.topicExchange(Constants.TOPIC_EXCHANGE).durable(true).build();
}
@Bean
public Binding topicBinding1(@Qualifier("topicExchange") TopicExchange exchange,
@Qualifier("topicQueue1") Queue queue){
return BindingBuilder.bind(queue).to(exchange).with("*.aaa");
}
@Bean
public Binding topicBinding2(@Qualifier("topicExchange") TopicExchange exchange,
@Qualifier("topicQueue2") Queue queue){
return BindingBuilder.bind(queue).to(exchange).with("bbb.*");
}
@Bean
public Binding topicBinding3(@Qualifier("topicExchange") TopicExchange exchange,
@Qualifier("topicQueue2") Queue queue){
return BindingBuilder.bind(queue).to(exchange).with("bbb.#");
}
生产者
java
@RequestMapping("/topic")
public String topicProduct(String routingKey){
rabbitTemplate.convertAndSend(Constants.TOPIC_EXCHANGE, routingKey, "hello spring boot: topic " + routingKey);
return "发送成功";
}
消费者
java
@Component
public class TopicListener {
@RabbitListener(queues = Constants.TOPIC_QUEUE1)
public void listenerQueue1(Message message) {
System.out.println("listener 1[" + Constants.TOPIC_QUEUE1 + "]收到消息: " + message);
}
@RabbitListener(queues = Constants.TOPIC_QUEUE2)
public void listenerQueue2(Message message) {
System.out.println("listener 2[" + Constants.TOPIC_QUEUE2 + "]收到消息: " + message);
}
}