RabbitMQ 罕用的三种 Exchange Type:fanout、direct、topic。
- fanout: 把所有发送到该 Exchange 的音讯投递到所有与它绑定的队列中。
- direct: 把音讯投递到那些 binding key 与 routing key 齐全匹配的队列中。
- topic: 将音讯路由到 binding key 与 routing key 模式匹配的队列中。
这里基于 springboot 整合 音讯队列,测试这三种 Exchange。
- 启动 RabbitMQ
双击运行 rabbitmq-server.bat
- SpringBoot 整合 RabbitMQ——Direct 模式(默认模式)
创立 springboot web 我的项目——发送者 springboot-sender
追加测试和 rabbitmq 所需的依赖
<!-- 增加 springboot 对 amqp 的反对 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
<!-- 增加测试包 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.9.RELEASE</version>
</dependency>
批改配置文件 application.yml 或 application.properties:
server:
port: 7001
spring:
application:
name: spirngboot-sender
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
发送的信息能够是 根本数据类型 也能够是 对象,这里创立一个用户对象
public class User implements Serializable{
private String username;
private String password;
public String getUsername() {return username;}
public void setUsername(String username) {this.username = username;}
public String getPassword() {return password;}
public void setPassword(String password) {this.password = password;}
}
创立一个配置类:SenderConfiguration.java
一个名为 queue1 的 队列
@Configuration
public class SenderConfiguration {
@Bean
public Queue directQueue() {return new Queue("queue1");
}
}
创立一个发送信息类:SenderService.java
发送 user 对象给 queue1 队列
@Component
public class SenderService {
@Autowired
private AmqpTemplate template;
public void sendUser() {User user=new User();
user.setUsername("张三");
user.setPassword("123456");
template.convertAndSend("queue1",user);
}
}
创立一个测试类:TestRabbitMQ.java
@SpringBootTest(classes=SpringbootSenderApplication.class)
@RunWith(SpringJUnit4ClassRunner.class)
public class TestRabbitMQ {
@Autowired
private SenderService senderService;
@Test
public void testRabbit() {senderService.sendUser();
}
}
运行 testRabbit 办法:
创立 springboot web 我的项目——接收者 springboot-receiver
批改配置文件 application.yml 或 application.properties:
server:
port: 7002
spring:
application:
name: spirngboot-receiver
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
增加接管类:ReceiverService.java
@Component
public class ReceiverService {@RabbitListener(queues="queue1")
public void receiveUser(User user) {System.out.println("username:"+user.getUsername()+"password:"+user.getPassword());
}
}
运行启动类:SpringbootApplication.java,后果:
信息胜利被接管。
SpringBoot 整合 RabbitMQ——Topic 模式(含糊匹配)
步骤与 Direct 差不多。
发送者:
批改配置类 SenderConfiguration.java:
创立两个队列 topic1,topic2,创立一个 topic 交换器,绑定交换机和队列以及绑定规定
@Test
public void testRabbit() {senderService.sendUser();
}@Bean(name="topic1")
public Queue topicQueue1() {return new Queue("topic1");
}
@Bean(name="topic2")
public Queue topicQueue2() {return new Queue("topic2");
}
@Bean
public TopicExchange exchange() {
// 创立一个 topic 交换器
return new TopicExchange("topicExchange");
}
@Bean
Binding bindingExchangeTopic1(@Qualifier("topic1") Queue queueMessage, TopicExchange exchange) {
// 设置 topic1 绑定规定
return BindingBuilder.bind(queueMessage).to(exchange).with("topic.queue");
}
@Bean
Binding bindingExchangeTopic2(@Qualifier("topic2") Queue queueMessages, TopicExchange exchange) {
// 设置 topic2 绑定规定 * 示意一个词,# 示意零个或多个词
return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");
}
批改发送类 SenderService.java:
User user=new User();
user.setUsername("张三");
user.setPassword("123456");
// 发送给 topicExchange 的交换机
template.convertAndSend("topicExchange","topic.queue",user);
template.convertAndSend("topicExchange","topic.anyting",user);
运行 testRabbit 办法:
胜利播送进来两条信息
接收者:
批改接管类 ReceiverService.java:
@RabbitListener(queues="fanout1")
public void receiveFanout1(User user) {System.out.println("队列:fanout1 username:"+user.getUsername()+"password:"+user.getPassword());
}
@RabbitListener(queues="fanout2")
public void receiveFanout2(User user) {System.out.println("队列:fanout2 username:"+user.getUsername()+"password:"+user.getPassword());
}
运行启动类,后果:
音讯胜利被发送接管