交换机的类型
Fanout:广播,将消息交给所有绑定到交换机的队列。
Direct:订阅,基于RoutingKey(路由key)发送给订阅了消息的队列。
Topic:通配符订阅,与Direct类似,只不过RoutingKey可以使用通配符(# (一个或多个单词)和 * (一个单词))。
Headers:头匹配,基于MQ的消息头匹配,用的较少。
准备
导入依赖:
<!--AMQP依赖,包含RabbitMQ-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!--单元测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
配置文件:
spring:
rabbitmq:
host: ****** # 你的虚拟机/服务器IP
port: 5672 # 端口
virtual-host: **** # mq虚拟主机
username: *** # 用户名
password: *** # 密码
这里用direct类型的交换机举例:
基于配置类
步骤一:配置类中创建交换机和队列的Bean,并设置绑定关系,设置routingKey为sdg
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FanoutConfiguration {
@Bean
public DirectExchange directExchange() {
return new DirectExchange("exchange01.direct");
//或者return ExchangeBuilder.directExchange("exchange01.direct").build();
}
@Bean
public Queue queue() {
return new Queue("direct.queue01");
}
@Bean
public Binding bind01(DirectExchange directExchange, Queue queue){
return BindingBuilder.bind(queue).to(directExchange).with("sdg");
}
}
步骤二:发送者发送消息
@Test
public void test5() {
String exchange = "exchange01.direct";
String message = "Hello World!";
rabbitTemplate.convertAndSend(exchange,"sdg",message);
}
步骤三:消费者消费消息
package com.itheima.consumer.mq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class SpringRabbitListener {
@RabbitListener(queues = "direct.queue01")
public void queue01Listener(String msg) {
System.out.println("收到消息: "+msg);
}
}
direct模式由于要绑定多个KEY,每一个Key都要编写一个binding,会非常麻烦,基于配置类适用与简单的情况,所以我们就可以基于注解来声明交换机、队列和绑定关系
基于注解
消费者:
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
@Component
public class SpringRabbitListener {
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "direct.queue01"),
exchange = @Exchange(name = "htsdg.direct",type = ExchangeTypes.DIRECT),
key = {"sdg","ht"}
))//默认类型为direct
public void queue01Listener(String msg) {
System.out.println("收到消息: "+msg);
}
}
再来一个topic类型的:
发送者:
@Test
public void test5() {
String exchange = "htsdg.direct";
String message = "Hello World!";
rabbitTemplate.convertAndSend(exchange,"china.qianXueSen",message);
}
消费者:
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "topic.queue1"),
exchange = @Exchange(name = "htsdg.topic", type = ExchangeTypes.TOPIC),
key = "china.#"
))
public void listenTopicQueue1(String msg){
System.out.println("消费者1接收到topic.queue1的消息:【" + msg + "】");
}
@RabbitListener(bindings = @QueueBinding(
value = @Queue(name = "topic.queue2"),
exchange = @Exchange(name = "htsdg.topic", type = ExchangeTypes.TOPIC),
key = "#.news"
))
public void listenTopicQueue2(String msg){
System.out.println("消费者2接收到topic.queue2的消息:【" + msg + "】");
}
版权归原作者 java炒饭小能手 所有, 如有侵权,请联系我们删除。