RabbitMQ
消息中间件的一种,使用docker方式下载RabbitMQ的镜像,网址访问的端口号默认是15672,程序访问的端口号默认是5672。
一、相关基础概念
生产者和消费者这里就不做说明。
1、Virtual Host:一个虚拟主机是由一组交换机、队列和绑定构成的,RabbitMQ是很多个虚拟主机构成的,一个虚拟主机相当于是一个mini的RabbitMQ,用户只能在虚拟主机的粒度上做权限控制。
2、Exchange:交换机,生产者生产的消息首先发送给交换机,由交换机决定发送到哪个队列,生产者不直接与队列打交到。交换机是需要和队列进行绑定的,如果没有绑定队列,那么该交换机收到消息以后会自动丢弃。
3、Binding:绑定routing key,由交换机到队列有一定的规则。
direct:直连,交换机、绑定routing key与队列是一对一关系,发送消息的时候,知道交换机和routing key就能知道需要发送到哪个队列。
fanout:广播,生产者只要将消息发送给了该交换机,该交换机会自动将消息转发给与它绑定的所有的队列。
topic:定制,绑定的routing key是一个规则,交换机会发送消息给满足这个规则的队列。例如routing key是“bafan.*”,绑定的队列是"bafan.queue1",那么生产者在发送消息的时候,routing key设置为“bafan.1”会发送消息给"bafan.queue1“这个队列,routing key设置为“bafan.2”也会发送消息给"bafan.queue1“这个队列。
4、queue:队列,接收消息,一般来说选用直连的方式routing key与queue的名称设置相同,消费者直接监听队列就好。
二、消息格式
在实际开发中,放入的消息值一般是json字符串,无论发送还是接收都用String类型。如果发送的消息是Map,接收是String会报错。
三、spring boot简单实现
maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
配置文件
//默认是localhost
spring.rabbitmq.host=192.168.0.86
//默认是5672
spring.rabbitmq.port=5672
//默认是guest
spring.rabbitmq.username=admin
//默认是guest
spring.rabbitmq.password=123456
如果RabbitMQ安装在了本地,那么配置文件中就不需要配置了
交换机、绑定、队列配置
@Configuration
public class Direct {
@Bean
public Queue queue1() {
return new Queue(QueueName.queue1);
}
@Bean
public DirectExchange directExchange1() {
return new DirectExchange("directExchange1");
}
@Bean
public Binding bingingDirectExchange1Message(Queue queue1, DirectExchange directExchange1) {
return BindingBuilder.bind(queue1).to(directExchange1).with(QueueName.queue1);
}
}
@Configuration
public class Fanout {
@Bean
public Queue queue2() {
return new Queue(QueueName.queue2);
}
@Bean
public Queue queue3() {
return new Queue(QueueName.queue3);
}
@Bean
public Queue queue4() {
return new Queue(QueueName.queue4);
}
@Bean
public FanoutExchange fanoutExchange1() {
return new FanoutExchange("fanoutExchange1");
}
@Bean
public Binding bingingFanoutExchange2Message(Queue queue2, FanoutExchange fanoutExchange1) {
return BindingBuilder.bind(queue2).to(fanoutExchange1);
}
@Bean
public Binding bingingFanoutExchange3Message(Queue queue3, FanoutExchange fanoutExchange1) {
return BindingBuilder.bind(queue3).to(fanoutExchange1);
}
@Bean
public Binding bingingFanoutExchange4Message(Queue queue4, FanoutExchange fanoutExchange1) {
return BindingBuilder.bind(queue4).to(fanoutExchange1);
}
}
@Configuration
public class Topic {
@Bean
public Queue queue5() {
return new Queue(QueueName.queue5);
}
@Bean
public Queue queue6() {
return new Queue(QueueName.queue6);
}
@Bean
public Queue queue7() {
return new Queue(QueueName.queue7);
}
@Bean
public TopicExchange topicExchange1() {
return new TopicExchange("topicExchange1");
}
@Bean
public TopicExchange topicExchange2() {
return new TopicExchange("topicExchange2");
}
@Bean
public Binding bingingTopicExchange1Message(Queue queue5, TopicExchange topicExchange1) {
return BindingBuilder.bind(queue5).to(topicExchange1).with("bafan.*");
}
@Bean
public Binding bingingTopicExchange2Message(Queue queue6, TopicExchange topicExchange1) {
return BindingBuilder.bind(queue6).to(topicExchange1).with("bafan.*");
}
@Bean
public Binding bingingTopicExchange4Message(Queue queue7, TopicExchange topicExchange1) {
return BindingBuilder.bind(queue7).to(topicExchange1).with("bafan.*");
}
@Bean
public Binding bingingTopicExchange5Message(Queue queue5, TopicExchange topicExchange2) {
return BindingBuilder.bind(queue5).to(topicExchange2).with(QueueName.queue5);
}
}
除此之外,需要使用@EnableRabbit开启RabbitMQ
消息测试(直连方式)
发送消息
@Test
public void test1() {
rabbitTemplate.convertAndSend("directExchange1", QueueName.queue1, "hello");
}
接收消息
//需要使用@RabbitListener和@RabbitHandler注解
@Service
@RabbitListener(queues = QueueName.queue1)
public class Receive1 {
@RabbitHandler
public void process(String message) {
System.out.println("receive1..." + message);
}
}