创建消息生产者微服务
rabbitmq-provider微服务
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.2</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>rabbitmq-provider</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>rabbitmq-provider</name>
<description>rabbitmq-provider</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.12.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
server:
port: 8021
spring:
application:
#项目名称
name: rabbitmq-provider
#配置rabbitmq服务器
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
virtual-host: /
直连交换机
DirectRabbitConfig
package com.example.rabbitmqprovider.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author 吴伟伟
* @description DirectRabbitConfig
* @date 2023/6/27 16:10
*/
@Configuration
public class DirectRabbitConfig {
//队列起名:TestDirectQueue
@Bean
public Queue TestDirectQueue(){
/*
durable:是否持久化,默认false。持久化队列:会被存储在磁盘上,当消息代理重启时仍然存在。 暂存队列:当前连接有效
exclusive:默认false,只能被当前创建的连接使用,当连接关闭后队列即被删除;
autoDelete:默认false, 是否自动删除,当没有生产者或者消费者使用此队列,该队列会自动删除
*/
//new Queue("TestDirectQueue",true,true,true);
return new Queue("TestDirectQueue",true);
}
//Direct交换机 起名:TestDirectExchange
@Bean
public DirectExchange TestDirectExchange(){
return new DirectExchange("TestDirectExchange",true,false);
}
//绑定 将队列和交换机绑定,并设置用于匹配键 : TestDirectRouting
@Bean
public Binding bindingDirect(){
//BindingBuilder.bind(队列).to(交换机).with(路由键routingKey);
return BindingBuilder.bind(TestDirectQueue()).to(TestDirectExchange()).with("TestDirectRouting");
}
@Bean
public DirectExchange lonelyDirectExchange(){
return new DirectExchange("lonelyDirectExchange");
}
}
SendMessageController
package com.example.rabbitmqprovider.controller;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.UUID;
/**
* @author 吴伟伟
* @description SendMessageController
* @date 2023/6/27 16:27
*/
@RestController
@RequestMapping("/mq")
public class SendMessageController {
@Autowired
private RabbitTemplate rabbitTemplate; //使用RabbitTemplate,提供了接收,发送等方法
@RequestMapping("/sendDirectMessage")
public String sendDirectMessage(){
String messageId = String.valueOf(UUID.randomUUID());
String messageData="test message , hello";
String createTime= LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
HashMap<String, Object> map = new HashMap<>();
map.put("messageId",messageId);
map.put("messageData",messageData);
map.put("createTime",createTime);
//将消息携带绑定键值:TestDirectRouting 发送到交换机TestDirectExchange
rabbitTemplate.convertAndSend("TestDirectExchange","TestDirectRouting",map);// 交换机 路由 消息
return "ok";
}
}
主类启动
使用postman调一下接口
看一下rabbitmq管理界面,有一条未被消费的消息
接下来消费者微服务
pom.xml同上
application.yml
server:
port: 8022
spring:
application:
#项目名称
name: rabbitmq-consumer
#配置rabbitmq服务器
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
virtual-host: /
监听消息队列
package com.example.rabbitmqconsumer.receiver;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* @author 吴伟伟
* @description DirectReciver
* @date 2023/6/27 17:29
*/
@Component
@RabbitListener(queues = "TestDirectQueue") //监听队列TestDirectQueue
public class DirectReciver {
@RabbitHandler
public void process(Map testMessage){
System.out.println("DirectReciver消费者收到消息:"+testMessage.toString());
}
}
启动服务后,发现消息被消费了
如果多个消费者监听同一个队列会发现
会发现,是轮询方式进行消息消费
主题交换机----Topic Exchange
生产者
TopicRabbitConfig
package com.example.rabbitmqprovider.config;
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;
/**
* @author 吴伟伟
* @description TopicRabbitConfig
* @date 2023/6/28 14:24
*/
@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(MAN);
}
@Bean
public Queue SecondQueue(){
return new Queue(WOMAN);
}
//创建主题交换机
@Bean
public TopicExchange exchange(){
return new TopicExchange("TopicExchange");
}
//将firstQueue和topicExchange绑定,而且绑定的键值为topic.man
//这样只要是消息携带的路由键是topic.man,才会分发到该队列
@Bean
public Binding bindingExchangeMessage(){
return BindingBuilder.bind(firstQueue()).to(exchange()).with(MAN);
}
//将SecondQueue和topicExchange绑定,而且绑定的键值为topic.#
//这样只要是消息携带的路由键是topic.开头,都会分发到该队列
@Bean
public Binding bindingExchangeMessage2(){
return BindingBuilder.bind(SecondQueue()).to(exchange()).with("topic.#");
}
}
@RequestMapping("/sendTopicMessage1")
public String sendTopicMessage1(){
String messageId = String.valueOf(UUID.randomUUID());
String messageData=" message:man";
String createTime= LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
HashMap<String, Object> map = new HashMap<>();
map.put("messageId",messageId);
map.put("messageData",messageData);
map.put("createTime",createTime);
rabbitTemplate.convertAndSend("TopicExchange","topic.man",map);// 交换机 路由 消息
return "ok";
}
@RequestMapping("/sendTopicMessage2")
public String sendTopicMessage2(){
String messageId = String.valueOf(UUID.randomUUID());
String messageData="message:woman";
String createTime= LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
HashMap<String, Object> map = new HashMap<>();
map.put("messageId",messageId);
map.put("messageData",messageData);
map.put("createTime",createTime);
rabbitTemplate.convertAndSend("TopicExchange","topic.woman",map);// 交换机 路由 消息
return "ok";
}
消费者
TopicManReciver
package com.example.rabbitmqconsumer.receiver;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
@RabbitListener(queues = "topic.man") //监听队列
public class TopicManReciver {
@RabbitHandler
public void process(Map testMessage){
System.out.println("TopicManReciver消费者收到消息:"+testMessage.toString());
}
}
TopicWomanReciver
package com.example.rabbitmqconsumer.receiver;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
@RabbitListener(queues = "topic.woman") //监听队列
public class TopicWomanReciver {
@RabbitHandler
public void process(Map testMessage){
System.out.println("TopicWomanReciver消费者收到消息:"+testMessage.toString());
}
}
启动生产者和消费者,使用postman发请求
http://127.0.0.1:8021/mq/sendTopicMessage1
http://127.0.0.1:8021/mq/sendTopicMessage2
http://127.0.0.1:8021/mq/sendDirectMessage