3.订阅者会轮流收到信息
receiver01 message = direct
receiver02 message = direct
receiver01 message = direct
receiver02 message = direct
receiver01 message = direct
receiver02 message = direct
复制代码
2.2 topic - 主题交换器
2.2.1 消息发送者
声明topic交换器
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class BlogPublisherConfig {
@Bean
public Exchange blogTopicExchange() {
return ExchangeBuilder.topicExchange(“exchange.topic.springboot.blog”).build();
}
}
复制代码
声明controller
@RequestMapping(“/topic”)
public Object topic(String routingKey, String message) {
rabbitTemplate.convertAndSend(“exchange.topic.springboot.blog”, routingKey, message);
return routingKey + " : " + message;
}
复制代码
2.2.2 消息接收者
声明交换器、三个队列、队列的绑定
-
*:匹配一个串
-
#:匹配一个或者多个串
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.Resource;
@Configuration
public class BlogSubscriberConfig {
/**
- 主题交换器
*/
@Bean
public TopicExchange blogTopicExchange() {
return ExchangeBuilder.topicExchange(“exchange.topic.springboot.blog”).build();
}
@Bean
public Queue blogJavaQueue() {
return QueueBuilder.durable(“queue.topic.springboot.blog.java”).build();
}
@Bean
public Queue blogMqQueue() {
return QueueBuilder.durable(“queue.topic.springboot.blog.mq”).build();
}
@Bean
public Queue blogAllQueue() {
return QueueBuilder.durable(“queue.topic.springboot.blog.all”).build();
}
@Bean
@Resource
public Binding blogJavaBinding(TopicExchange blogTopicExchange, Queue blogJavaQueue) {
return BindingBuilder.bind(blogJavaQueue).to(blogTopicExchange).with(“springboot.blog.java.routing.key”);
}
@Bean
@Resource
public Binding blogMqBinding(TopicExchange blogTopicExchange, Queue blogMqQueue) {
return BindingBuilder.bind(blogMqQueue).to(blogTopicExchange).with(“springboot.blog.mq.routing.key”);
}
@Bean
@Resource
public Binding blogAllBinding(TopicExchange blogTopicExchange, Queue blogAllQueue) {
// #: 匹配一个或者多个 *:匹配一个
return BindingBuilder.bind(blogAllQueue).to(blogTopicExchange).with(“springboot.blog.#.routing.key”);
}
}
复制代码
监听队列
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;
@Service
public class BlogService {
/**
- topic监听
*/
@RabbitListener(queues = “queue.topic.springboot.blog.java”)
public void blogJavaListener(String message) {
System.out.println("blogJavaListener message = " + message);
}
@RabbitListener(queues = “queue.topic.springboot.blog.mq”)
public void blogMqListener(String message) {
System.out.println("blogMqListener message = " + message);
}
@RabbitListener(queues = “queue.topic.springboot.blog.all”)
public void blogAllaListener(String message) {
System.out.println("blogAllListener message = " + message);
}
}
复制代码
2.2.3 消息发布订阅
- 发布者发送消息
-
http://localhost:8071/topic?routingKey=springboot.blog.java.routing.key&message=hello
-
http://localhost:8071/topic?routingKey=springboot.blog.mq.routing.key&message=hello
- 订阅者收到消息
-
全匹配和模糊匹配
-
全匹配无论是哪个都会被匹配上
blogJavaListener message = hello
blogAllListener message = hello
blogAllListener message = hello</