- 准备工作
安装在安装完rabbitMq后,输入http://ip:15672/ ,是可以看到一个简单后台管理界面的。 - RabbitMq简介
常用的交换机有以下三种,因为消费者是从队列获取信息的,队列是绑定交换机的(一般),所以对应的消息推送/接收模式也会有以下几种:
1)Direct Exchange
直连型交换机,根据消息携带的路由键将消息投递给对应队列。
大致流程,有一个队列绑定到一个直连交换机上,同时赋予一个路由键 routing key 。然后当一个消息携带着路由值为X,这个消息通过生产者发送给交换机时,交换机就会根据这个路由值X去寻找绑定值也是X的队列。
2)Fanout Exchange
扇型交换机,这个交换机没有路由键概念,就算你绑了路由键也是无视的。 这个交换机在接收到消息后,会直接转发到绑定到它上面的所有队列。
3)Topic Exchange
主题交换机,这个交换机其实跟直连交换机流程差不多,但是它的特点就是在它的路由键和绑定键之间是有规则的。
简单地介绍下规则:
- (星号) 用来表示一个单词 (必须出现的)
(井号) 用来表示任意数量(零个或多个)单词
通配的绑定键是跟队列进行绑定的,举个小例子
队列Q1 绑定键为 .TT. 队列Q2绑定键为 TT.#
如果一条消息携带的路由键为 A.TT.B,那么队列Q1将会收到;
如果一条消息携带的路由键为TT.AA.BB,那么队列Q2将会收到
3. 添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
- yml配置文件
spring:
rabbitmq:
username: guest
port: 5672
password: guest
host: localhost
#虚拟host 可以不设置,使用server默认host
virtual-host: JCcccHost
5.开启基于注解的RabbitMQ模式
/**
- 自动配置
- 1、RabbitAutoConfiguration
- 2、有自动配置了连接工厂ConnectionFactory;
- 3、RabbitProperties 封装了 RabbitMQ的配置
- 4、 RabbitTemplate :给RabbitMQ发送和接受消息;
- 5、 AmqpAdmin : RabbitMQ系统管理功能组件;
AmqpAdmin:创建和删除 Queue,Exchange,Binding
- 6、@EnableRabbit + @RabbitListener 监听消息队列的内容
*/
@EnableRabbit //开启基于注解的RabbitMQ模式
@SpringBootApplication
public class Springboot02AmqpApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot02AmqpApplication.class, args);
}
}
6.如何创建交换机,队列,路由键,以及绑定
@Autowired
AmqpAdmin amqpAdmin;
@Test
public void createExchange(){
//创建 直型交换机
amqpAdmin.declareExchange(new DirectExchange("amqpadmin.exchange"));
//创建 队列
amqpAdmin.declareQueue(new Queue("amqpadmin.queue",true));
//根据路由键将队列绑到交换机上
amqpAdmin.declareBinding(new Binding("amqpadmin.queue",Binding.DestinationType.QUEUE,"amqpadmin.exchange","amqp.haha",null));
}
7.如何发送消息(并且序列化消息为json格式)到队列上去
```yaml
@Configuration
public class MyAMQPConfig {
@Bean
public MessageConverter messageConverter(){
return new Jackson2JsonMessageConverter();
}
}
@Autowired
RabbitTemplate rabbitTemplate;
@Test
public void contextLoads() {
Map<String,Object> map = new HashMap<>();
map.put("msg","这是第一个消息");
map.put("data", Arrays.asList("helloworld",123,true));
//对象被默认序列化以后发送出去
rabbitTemplate.convertAndSend("amqpadmin.exchange","amqp.haha",new Book("西游记2","吴承恩"));
}
8.如何监听发送消息的队列
@RabbitListener(queues = "amqpadmin.queue")
public void receive02(Message message){
System.out.println(message.getBody());
System.out.println(message.getMessageProperties());
}
9. (样例对三种不同的交换机进行测试)irect exchange(直连型交换机),
1)创建直型交换机配置DirectRabbitConfig.java:
```yaml
@Configuration
public class DirectRabbitConfig {
//队列 起名TestDirectQueue
@Bean
public Queue TestDirectQueue() {
return new Queue("TestDirectQueue", true); //true 是否持久
}
//Direct交换机 起名:TestDirectExchange
@Bean
DirectExchange TestDirectExchange() {
return new DirectExchange("TestDirectExchange");
}
//绑定 将队列和交换机绑定, 并设置用于(路由key)匹配键:TestDirectRouting
@Bean
Binding bindingDirect() {
Return BindingBuilder.bind(TestDirectQueue()).to(TestDirectExchange()).with("TestDirectRouting");
}
}
2)写个简单的接口进行消息推送
@Autowired
RabbitTemplate rabbitTemplate;
public String sendDirectMessage() {
String messageId = String.valueOf(UUID.randomUUID());
String messageData = "test message, hello!";
Map<String,Object> map=new HashMap<>();
map.put("messageId",messageId);
map.put("messageData",messageData);
//将消息携带绑定键值:TestDirectRouting 发送到交换机TestDirectExchange
rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", map);
return "ok";
}
}
3)消费者进行消息监听
@Component
@RabbitListener(queues = "TestDirectQueue") //监听的队列名称 TestDirectQueue
public class DirectReceiver {
@RabbitHandler
public void process(Map testMessage) {
System.out.println("DirectReceiver消费者收到消息 : " + testMessage.toString());
}
}
- 主题交换机
1)创建主题交换机TopicRabbitConfig.class类
@Configuration
public class TopicRabbitConfig {
//绑定键(路由建)
public final static String man = "topic.man";
public final static String woman = "topic.woman";
@Bean
public Queue firstQueue() {
return new Queue(TopicRabbitConfig.man);
}
@Bean
public Queue secondQueue() {
return new Queue(TopicRabbitConfig.woman);
}
@Bean
TopicExchange exchange() {
return new TopicExchange("topicExchange");
}
//将firstQueue和topicExchange绑定,而且绑定的键值为topic.man
//这样只要是消息携带的路由键是topic.man,才会分发到该队列
@Bean
Binding bindingExchangeMessage() {
return BindingBuilder.bind(firstQueue()).to(exchange()).with(man);
}
//将secondQueue和topicExchange绑定,而且绑定的键值为用上通配路由键规则topic.#
// 这样只要是消息携带的路由键是以topic.开头,都会分发到该队列
@Bean
Binding bindingExchangeMessage2() {
return BindingBuilder.bind(secondQueue()).to(exchange()).with("topic.#");
}
2)创建消费者 TopicWomanReceiver.class和 TopicManReceiver.class两个类
@Component
@RabbitListener(queues = "topic.man")
public class TopicManReceiver {
@RabbitHandler
public void process(Map testMessage) {
System.out.println("TopicManReceiver消费者收到消息 : " + testMessage.toString());
}
}
@Component
@RabbitListener(queues = "topic.woman")
public class TopicWomanReceiver {
@RabbitHandler
public void process(Map testMessage) {
System.out.println("TopicWomanReceiver消费者收到消息 : " + testMessage.toString());
}
}
3)测试主题交换机
//主题交换机 测试
@GetMapping("/sendTopicMessage")
public String sendTopicMessage() {
String messageId = String.valueOf(UUID.randomUUID());
String messageData = "message: M A N ";
String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Map<String, Object> manMap = new HashMap<>();
manMap.put("messageId", messageId);
manMap.put("messageData", messageData);
manMap.put("createTime", createTime);
rabbitTemplate.convertAndSend("topicExchange", "topic.man", manMap);
Map<String, Object> womanMap = new HashMap<>();
womanMap.put("messageId", messageId);
womanMap.put("messageData", messageData);
womanMap.put("createTime", createTime);
rabbitTemplate.convertAndSend("topicExchange", "topic.woman", womanMap);
return "ok";
}
- 扇形交换机
1)创建扇形交换机配置类FanoutRabbitConfig.java
@Configuration
public class FanoutRabbitConfig {
/**
* 创建2个队列 :fanout.A fanout.B
* 将三个队列都绑定在交换机 fanoutExchange 上
* 因为是扇型交换机, 路由键无需配置,配置也不起作用
*/
@Bean
public Queue queueA() {
return new Queue("fanout.A");
}
@Bean
public Queue queueB() {
return new Queue("fanout.B");
}
@Bean
FanoutExchange fanoutExchange() {
return new FanoutExchange("fanoutExchange");
}
@Bean
Binding bindingExchangeA() {
return BindingBuilder.bind(queueA()).to(fanoutExchange());
}
@Bean
Binding bindingExchangeB() {
return BindingBuilder.bind(queueB()).to(fanoutExchange());
}
}
2)先建消费者FanoutReceiverA.java 和B类一样
```yaml
@Bean
@Component
@RabbitListener(queues = "fanout.A")
public class FanoutReceiverA {
@RabbitHandler
public void process(Map testMessage) {
System.out.println("FanoutReceiverA消费者收到消息 : " +testMessage.toString());
}
}
@Bean
@Component
@RabbitListener(queues = “fanout.B”)
public class FanoutReceiverA {
@RabbitHandler
public void process(Map testMessage) {
System.out.println("FanoutReceiverB消费者收到消息 : " +testMessage.toString());
}
}
3)测试
```yaml
@GetMapping("/sendFanoutMessage")
public String sendFanoutMessage() {
String messageId = String.valueOf(UUID.randomUUID());
String messageData = "message: testFanoutMessage ";
String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
Map<String, Object> map = new HashMap<>();
map.put("messageId", messageId);
map.put("messageData", messageData);
map.put("createTime", createTime);
rabbitTemplate.convertAndSend("fanoutExchange", null, map);
return "ok";
}