- 一、适用场景
- 二、前提条件
- 三、执行流程
- 四、实现方法
- 1.依赖包
- 2.application配置文件
- 3.创建一个接收消息的对象
- 4.创建三个消息队列绑定到一个发布订阅交换机上
- 5.创建三个队列的消费者
- 6.controller中创建一个生产者
- 五、测试结果
一、适用场景
高并发场景下,多个耗时业务顺序执行会浪费大量时间,多线程又会导致过高的cpu占用,较好的方式是采用消息队列。
二、前提条件
需要先部署一个RabbitMQ,可以参考这个教程
https://blog.csdn.net/weixin_43721000/article/details/124587795
三、执行流程
四、实现方法 1.依赖包
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-amqpartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>1.2.9version>
dependency>
2.application配置文件
server:
port: 8080
spring:
rabbitmq:
host: 192.168.0.231 # RabbitMQ主机ip
port: 5672 # RabbitMQ端口
username: admin # 用户名
password: 123456 # 密码
virtual-host: my_vhost # 虚拟主机名【用于隔离不同用户创建的消息队列】
3.创建一个接收消息的对象
package com.cxstar.bean;
import lombok.Data;
@Data
public class MsgBean {
private String searchKey;
private Integer curPage;
// fastjson的json转对象的方法需要一个无参构造
public MsgBean() {}
public MsgBean(String searchKey, Integer curPage) {
this.searchKey = searchKey;
this.curPage = curPage;
}
}
4.创建三个消息队列绑定到一个发布订阅交换机上
package com.cxstar.config;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class FanoutExchangeConfig {
// 新建发布订阅交换机,并将队列A B C绑定到该交换机
// 命名 ------------------------------------------------------------------------
// 交换机名
public static final String FANOUT_EXCHANGE = "fanout_exchange";
// 队列名
public static final String FANOUT_EXCHANGE_QUEUE_A = "fanout_exchange_queue_a";
public static final String FANOUT_EXCHANGE_QUEUE_B = "fanout_exchange_queue_b";
public static final String FANOUT_EXCHANGE_QUEUE_C = "fanout_exchange_queue_c";
// -----------------------------------------------------------------------------
// 创建 ------------------------------------------------------------------------
// 创建交换机
@Bean
FanoutExchange fanoutExchange() {
return new FanoutExchange(FANOUT_EXCHANGE);
}
// 创建队列
@Bean
public Queue queueA(){
return new Queue(FANOUT_EXCHANGE_QUEUE_A);
}
@Bean
public Queue queueB(){
return new Queue(FANOUT_EXCHANGE_QUEUE_B);
}
@Bean
public Queue queueC(){
return new Queue(FANOUT_EXCHANGE_QUEUE_C);
}
// -----------------------------------------------------------------------------
// 将三个队列绑定到交换机上【队列订阅交换机】 ------------------------------------------------------------------
@Bean
Binding bindingExchangeA() {
return BindingBuilder.bind(queueA()).to(fanoutExchange());
}
@Bean
Binding bindingExchangeB() {
return BindingBuilder.bind(queueB()).to(fanoutExchange());
}
@Bean
Binding bindingExchangeC() {
return BindingBuilder.bind(queueC()).to(fanoutExchange());
}
// ---------------------------------------------------------------------------------------
}
5.创建三个队列的消费者
package com.cxstar.consumer;
import com.alibaba.fastjson.JSONObject;
import com.cxstar.bean.MsgBean;
import com.cxstar.config.FanoutExchangeConfig;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import java.io.IOException;
@Slf4j
@Component
public class Consumer {
// 消费者A
@RabbitListener(queues = FanoutExchangeConfig.FANOUT_EXCHANGE_QUEUE_A)
public void receiverQueueA(String msg, Channel channel, Message message) throws IOException {
// 消息转对象
MsgBean msgBean = JSONObject.toJavaObject(JSONObject.parseObject(msg), MsgBean.class);
// 消费
log.info("receiverQueueA 消费对象:"+msgBean.toString());
}
// 消费者B
@RabbitListener(queues = FanoutExchangeConfig.FANOUT_EXCHANGE_QUEUE_B)
public void receiverQueueB(String msg, Channel channel, Message message) throws IOException {
// 消息转对象
MsgBean msgBean = JSONObject.toJavaObject(JSONObject.parseObject(msg), MsgBean.class);
// 消费
log.info("receiverQueueB 消费对象:"+msgBean.toString());
}
// 消费者C
@RabbitListener(queues = FanoutExchangeConfig.FANOUT_EXCHANGE_QUEUE_C)
public void receiverQueueC(String msg, Channel channel, Message message) throws IOException {
// 消息转对象
MsgBean msgBean = JSONObject.toJavaObject(JSONObject.parseObject(msg), MsgBean.class);
// 消费
log.info("receiverQueueC 消费对象:"+msgBean.toString());
}
}
6.controller中创建一个生产者
package com.cxstar.controller;
import com.alibaba.fastjson.JSONObject;
import com.cxstar.bean.MsgBean;
import com.cxstar.config.FanoutExchangeConfig;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
public class Controller {
@Resource
private RabbitTemplate rabbitTemplate;
@GetMapping("/send_msg")
public void sendExchange(){
// 创建消息
MsgBean msgBean = new MsgBean();
msgBean.setSearchKey("python");
msgBean.setCurPage(1);
String msgBeanJsonString = JSONObject.toJSONString(msgBean);
// 生产者发布消息到交换机
rabbitTemplate.convertAndSend(FanoutExchangeConfig.FANOUT_EXCHANGE, null, msgBeanJsonString);
}
}
五、测试结果
启动 SpringBoot
访问 127.0.0.1:8080/send_msg
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)