参考地址: https://blog.youkuaiyun.com/qq_35387940/article/details/100514134
应用消息中间件第一反应就是:异步,解耦,削峰 主要的场景就是秒杀,抢购,邮件发送,消息发送,日志收集
这篇主要的就是讲述:rabbitmq 在rabbitmq中几个重要的组成部分,消息生产者,交换机,队列,消息消费者 者四个部分组成
其中消费分发有以下几种模式:direct fanout topic 这里面还需要区分生产者到交换机是需要指定routyKey,excahnge-queue这是需要指定bindkey的
其中direct模式 routyKey与bindKey必须相同
fanout主要是指:消费者订阅了交换机,都会将消息发送给所有的消费者,
topic是与direct模式相同的,其中需要注意就是* 代表一个单词 #代表任意一个单词
队列Q1 绑定键为 .TT. 队列Q2绑定键为 TT.# 如果一条消息携带的路由键为 A.TT.B,那么队列Q1将会收到; 如果一条消息携带的路由键为TT.AA.BB,那么队列Q2将会收到
sprignboot整合rabbitmq实现消息的发送与消费:
流程:pom文件中的直接导入amqp依赖包,对直连或者是topic模式,或者是fanout模式配置好:
代码如下:这里是指消息的生产者:
<!--amqp协议的依赖包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
application.yml中的代码如下:
#具体的是配置rabbitmq信息
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
直连配置类中的代码展示如下:
@SpringBootConfiguration
public class RabbitDirectConfig {
/**
* 直连的队列名称
* @return
*/
@Bean
public Queue testDirectQueue(){
/**
* dureable:是否持久化,默认是false, 持久化队列: 会被存储在磁盘上,当消息代理重启时仍然存在
* exclusive: 默认也是false,只能被当前创建的连接使用,而且当连接关闭后队列会被关闭
* autoDelete:是否自动删除,当没有生产者或者消费者使用队列,该队列会自动删除
* return new Queue("testDirectQueue", true, true, fasle,)
*/
return new Queue("TestDirectQueue", true);
}
/**
* 直连交换机
* @return
*/
@Bean
DirectExchange testDirectExchange(){
return new DirectExchange("testDirectExchange", true, false);
}
/**
* 指定直连的队列与交换机,并且指定routyKey为:testDirectRouting
* @return
*/
@Bean
Binding bindingDirect(){
return BindingBuilder.bind(testDirectQueue()).to(testDirectExchange()).with("testDirectRouting");
}
/**
* 重新创建一个直连的交换机
* @return
*/
@Bean
DirectExchange lonelyDirectExchange(){
return new DirectExchange("lonelyDirectExchange");
}
}
----------Topic模式的配置类----------------------------------------------
@SpringBootConfiguration
public class RabbitTopicConfig {
public final static String man = "topic.man";
public final static String woman = "topic.women";
/**
* 创建一个新的队列
* @return
*/
@Bean
public Queue firstQueue(){
return new Queue(RabbitTopicConfig.man);
}
/**
* 创建另一个队列
* @return
*/
@Bean
public Queue secondQueue(){
return new Queue(RabbitTopicConfig.woman);
}
/**
* 创建一个topic模式的交换机
* @return
*/
@Bean
TopicExchange exchange(){
return new TopicExchange("topicExchange");
}
/**
* 队列与交换机之间进行绑定
* @return
*/
@Bean
Binding bindingExchangeMessage(){
return BindingBuilder.bind(firstQueue()).to(exchange()).with(man);
}
/**
* 将第二个队列与交换机进行绑定
* @return
*/
@Bean
Binding bindingExchangeMessage2(){
return BindingBuilder.bind(secondQueue()).to(exchange()).with("topic.#");
}
---------------------------------------Fanout----------------------------------------------------------
@SpringBootConfiguration
public class FanOutConfig {
/**
* 第一个队列
* @return
*/
@Bean
public Queue queueA(){
return new Queue("fanout.a");
}
/**
* 创建第二个队列
* @return
*/
@Bean
public Queue queueB(){
return new Queue("fanout.B");
}
/**
* 创建第三个队列
* @return
*/
@Bean
public Queue queueC(){
return new Queue("fanout.C");
}
/**
* fanout类型的交换机
* @return
*/
@Bean
FanoutExchange fanoutExchange(){
return new FanoutExchange("fanoutExchange");
}
/**
* 将队列与交换进行绑定
* @return
*/
@Bean
Binding bindingExchange(){
return BindingBuilder.bind(queueA()).to(fanoutExchange());
}
@Bean
Binding bindingExchangeB(){
return BindingBuilder.bind(queueB()).to(fanoutExchange());
}
@Bean
Binding bindingExchangeC(){
return BindingBuilder.bind(queueC()).to(fanoutExchange());
}
二:这是发送消息的地方:
@RestController
@Api(produces = "rabbitmq发送消息的地方")
public class RabbitController {
//这里直接使用rabbitmq中的模板类来发送消息的
@Autowired
RabbitTemplate rabbitTemplate;
@RequestMapping(value = "sendMessage", method = RequestMethod.GET)
public String sendDirectMessage(){
//现在发送一个map的消息过去
String messageId = UUID.randomUUID().toString();
String messageData = "test message, hello";
//将lcoaldateTime转换为string类型的
String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
Map<String, Object> map = new HashMap<>();
map.put("messageId", messageId);
map.put("messageData", messageData);
map.put("createTime", format);
rabbitTemplate.convertAndSend("testDirectExchange", "testDirectRouting", map);
return "ok";
}
@RequestMapping(value = "sendMessageTopic", method = RequestMethod.GET)
public String sendTopicMessage(){
//现在发送一个map的消息过去
String messageId = UUID.randomUUID().toString();
String messageData = "test topic message, hello";
String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
Map<String, Object> map = new HashMap<>();
map.put("messageId", messageId);
map.put("messageData", messageData);
map.put("createTime", format);
rabbitTemplate.convertAndSend("topicExchange", "topic.man", map);
return "o";
}
@RequestMapping(value = "sendMessageTopic2", method = RequestMethod.GET)
public String sendTopicMessage2(){
//现在发送一个map的消息过去
String messageId = UUID.randomUUID().toString();
String messageData = "test topic message2, hello";
String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
Map<String, Object> map = new HashMap<>();
map.put("messageId", messageId);
map.put("messageData", messageData);
map.put("createTime", format);
rabbitTemplate.convertAndSend("topicExchange", "topic.women", map);
return "ok";
}
@RequestMapping(value = "sendFanoutMessage", method = RequestMethod.GET)
public String sendFanout(){
//现在发送一个map的消息过去
String messageId = UUID.randomUUID().toString();
String messageData = "test topic message2, hello";
String format = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
Map<String, Object> map = new HashMap<>();
map.put("messageId", messageId);
map.put("messageData", messageData);
map.put("createTime", format);
rabbitTemplate.convertAndSend("fanoutExchange", null, map);
return "ok";
}
消费者:步骤先是导入pom文件,application.yaml配置好属性, 监听类需要写好:
<!--amqp协议的依赖包-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
--------------------------applciation中的配置类--------------------
rabbitmq:
host: 127.0.0.1
port: 5672
username: guest
password: guest
-----------------------------监听类中代码展示类-------------------------------@Component
@RabbitListener(queues = "topic.women")
@Slf4j
public class TopicTatolListener {
@RabbitHandler
public void process(Map test){
log.info("#接收到信息为toal:{}", test.toString());
}
}