文章目录
rabbitmq
优化秒杀接口,目标:2000并发 QPS 4000
思路:要减少数据库的访问,没必要每次都查询数据库
(1)系统初始化,把商品库存数量加载到redis
(2)收到请求,首先看redis的标志,如果已经结束,直接返回,否则进入(3)
(3)入队缓冲,直接返回,并不是返回成功,而是返回排队中,客户端不能直接提示秒杀成功,而是启动定时器,过一段时间再去查是否成功
(4)出队,修改库存,修改结束标志
安装RabbitMQ
网上有很多教程这里就不赘述了
SpringBoot集成RabbitMQ
1.添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
2.application.properties添加配置
#rabbitmq
spring.rabbitmq.host=192.168.56.101
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.virtual-host=/
#消费者数量
spring.rabbitmq.listener.simple.concurrency= 10
spring.rabbitmq.listener.simple.max-concurrency= 10
#消费者每次从队列获取的消息数量
spring.rabbitmq.listener.simple.prefetch= 1
#消费者自动启动
spring.rabbitmq.listener.simple.auto-startup=true
#消费失败,自动重新入队
spring.rabbitmq.listener.simple.default-requeue-rejected= true
#启用发送重试
spring.rabbitmq.template.retry.enabled=true
spring.rabbitmq.template.retry.initial-interval=1000
spring.rabbitmq.template.retry.max-attempts=3
spring.rabbitmq.template.retry.max-interval=10000
spring.rabbitmq.template.retry.multiplier=1.0
spring.rabbitmq.host=192.168.56.101
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.virtual-host=/
#
spring.rabbitmq.listener.simple.concurrency= 10
spring.rabbitmq.listener.simple.max-concurrency= 10
#
spring.rabbitmq.listener.simple.prefetch= 1
#
spring.rabbitmq.listener.simple.auto-startup=true
#
spring.rabbitmq.listener.simple.default-requeue-rejected= true
#
spring.rabbitmq.template.retry.enabled=true
spring.rabbitmq.template.retry.initial-interval=1000
spring.rabbitmq.template.retry.max-attempts=3
spring.rabbitmq.template.retry.max-interval=10000
spring.rabbitmq.template.retry.multiplier=1.0
3. MQSender创建发送者
package com.pro.miaosha.rabbitmq;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.pro.miaosha.redis.RedisService;
@Service
public class MQSender {
private static Logger log = LoggerFactory.getLogger(MQSender.class);
@Autowired
AmqpTemplate amqpTemplate ;//操作queue的工具类
public void send(Object message) {
String msg = RedisService.beanToString(message);//把bean => string再放到消息队列中
log.info("send message:"+msg);
amqpTemplate.convertAndSend(MQConfig.QUEUE, msg);
}
//
// public void sendTopic(Object message) {
// String msg = RedisService.beanToString(message);
// log.info("send topic message:"+msg);
// amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE, "topic.key1", msg+"1");
// amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE, "topic.key2", msg+"2");
// }
//
// public void sendFanout(Object message) {
// String msg = RedisService.beanToString(message);
// log.info("send fanout message:"+msg);
// amqpTemplate.convertAndSend(MQConfig.FANOUT_EXCHANGE, "", msg);
// }
//
// public void sendHeader(Object message) {
// String msg = RedisService.beanToString(message);
// log.info("send fanout message:"+msg);
// MessageProperties properties = new MessageProperties();
// properties.setHeader("header1", "value1");
// properties.setHeader("header2", "value2");
// Message obj = new Message(msg.getBytes(), properties);
// amqpTemplate.convertAndSend(MQConfig.HEADERS_EXCHANGE, "", obj);
// }
}
4.MQReceiver:创建消费者
@Service
public class MQReceiver {
private static Logger log = LoggerFactory.getLogger(MQReceiver.class);
@Autowired
RedisService redisService;
@Autowired
GoodsService goodsService;
@Autowired
OrderService orderService;
@Autowired
MiaoshaService miaoshaService;
@RabbitListener(queues=MQConfig.MIAOSHA_QUEUE)//监听的是哪一个queue
public void receive(String message) {
log.info("receive message:"+message);
MiaoshaMessage mm = RedisService. stringToBean(message, MiaoshaMessage.class);
MiaoshaUser user = mm.getUser();
long goodsId = mm.getGoodsId();
GoodsVo goods = goodsService.getGoodsVoByGoodsId(goodsId);
int stock = goods.getStockCount();
if(stock <= 0) {
return;
}
//判断是否已经秒杀到了
MiaoshaOrder order = orderService.getMiaoshaOrderByUserIdGoodsId(user.getId(), goodsId);
if(order != null) {
return;
}
//减库存 下订单 写入秒杀订单
miaoshaService.miaosha(user, goods);
}
// @RabbitListener(queues=MQConfig.QUEUE)
// public void receive(String message) {
// log.info("receive message:"+message);
// }
//
// @RabbitListener(queues=MQConfig.TOPIC_QUEUE1)
// public void receiveTopic1(String message) {
// log.info(" topic queue1 message:"+message);
// }
//
// @RabbitListener(queues=MQConfig.TOPIC_QUEUE2)
// public void receiveTopic2(String message) {
// log.info(" topic queue2 message:"+message);
// }
//
// @RabbitListener(queues=MQConfig.HEADER_QUEUE)
// public void receiveHeaderQueue(byte[] message) {
// log.info(" header queue message:"+new String(message));
// }
//
}
package com.pro.miaosha.rabbitmq;
import java.util.HashMap;
import java.util.Map;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.HeadersExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class MQConfig {
public static final String MIAOSHA_QUEUE = "miaosha.queue";//消息队列
public static final String QUEUE = "queue";
public static final String TOPIC_QUEUE1 = "topic.queue1";
public static final String TOPIC_QUEUE2 = "topic.queue2";
public static final String HEADER_QUEUE = "header.queue";
public static final String TOPIC_EXCHANGE = "topicExchage";
public static final String FANOUT_EXCHANGE = "fanoutxchage";
public static final String HEADERS_EXCHANGE = "headersExchage";
/**
* Direct模式 交换机Exchange
* */
@Bean
public Queue queue() {
return new Queue(QUEUE, true);//名称+是否初始化
}
/**
* Topic模式 交换机Exchange
* */
@Bean
public Queue topicQueue1() {
return new Queue(TOPIC_QUEUE1, true);
}
@Bean
public Queue topicQueue2() {
return new Queue(TOPIC_QUEUE2, true);
}
@Bean
public TopicExchange topicExchage(){
return new TopicExchange(TOPIC_EXCHANGE);
}
@Bean
public Binding topicBinding1() {
return BindingBuilder.bind(topicQueue1()).to(topicExchage()).with("topic.key1");
}
@Bean
public Binding topicBinding2() {
return BindingBuilder.bind(topicQueue2()).to(topicExchage()).with("topic.#");
}
/**
* Fanout模式 交换机Exchange
* */
@Bean
public FanoutExchange fanoutExchage(){
return new FanoutExchange(FANOUT_EXCHANGE);
}
@Bean
public Binding FanoutBinding1() {
return BindingBuilder.bind(topicQueue1()).to(fanoutExchage());
}
@Bean
public Binding FanoutBinding2() {
return BindingBuilder.bind(topicQueue2()).to(fanoutExchage());
}
/**
* Header模式 交换机Exchange
* */
@Bean
public HeadersExchange headersExchage(){
return new HeadersExchange(HEADERS_EXCHANGE);
}
@Bean
public Queue headerQueue1() {
return new Queue(HEADER_QUEUE, true);
}
@Bean
public Binding headerBinding() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("header1", "value1");
map.put("header2", "value2");
return BindingBuilder.bind(headerQueue1()).to(headersExchage()).whereAll(map).match();
}
}
5.修改配置
为了让guest用户能远程连接,修改rabbitmq的配置 /usr/local/rabbitmq/etc/rabbitmq/rabbitmq.config
添加:[{rabbit, [{loopback_users, []}]}].
重新启动
http://www.rabbitmq.com/access-control.html
四种交换机模式
需要四个文件:
首先在MQConfig里面定义这些string
public static final String QUEUE = "queue";
public static final String TOPIC_QUEUE1 = "topic.queue1";
public static final String TOPIC_QUEUE2 = "topic.queue2";
public static final String TOPIC_EXCHANGE = "topicExchange";
public static final String ROUTING_KEY1 = "top.key1";
public static final String ROUTING_KEY2 = "top.#";
1.Direct模式
//receicer
@RabbitListener(queues=MQConfig.QUEUE)
public void receive(String message) {
log.info("receive message:"+message);
}
//sender
@Autowired
AmqpTemplate amqpTemplate ;
public void send(Object message) {
String msg = RedisService.beanToString(message);
log.info("send message:"+msg);
amqpTemplate.convertAndSend(MQConfig.QUEUE, msg);
}
//config
@Configuration
public class MQConfig {
public static final String QUEUE = "queue";
@Bean
public Queue queue() {
return new Queue(QUEUE, true);
}
}
@RequestMapping("/mq")
@ResponseBody
public Result<String> mq(){
sender.send("helloiloveu");
return Result.success("helloworld");
}
2.Topic 模式
//config
@Bean
public Queue topicQueue1(){
return new Queue(TOPIC_QUEUE1,ture);
}
@Bean
public Queue topicQueue2(){
return new Queue(TOPIC_QUEUE2,ture);
}
@Bean
public TopicExchange topicExchange(){
return new TopicExchange(TOPIC_EXCHANGE);
}
@Bean
public Binding topicBinding1(){
return BindingBuilder.bind(topicQueue1().to(topicExchange)).with(topic.key1)
}
@Bean
public Binding topicBinding2(){
return BindingBuilder.bind(topicQueue2().to(topicExchange)).with(topic.#)
}
//receiver
@RabbitListener(queues=MQConfig.TOPIC_QUEUE1)
public void receiveTopic1(String message) {
log.info(" topic queue1 message:"+message);
}
@RabbitListener(queues=MQConfig.TOPIC_QUEUE2)
public void receiveTopic2(String message) {
log.info(" topic queue2 message:"+message);
}
//sender
public void sendTopic(Object message) {
String msg = RedisService.beanToString(message);
log.info("send topic message:"+msg);
amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE, "topic.key1", msg+"1");
amqpTemplate.convertAndSend(MQConfig.TOPIC_EXCHANGE, "topic.key2", msg+"2");
}
3.fanout模式(广播)
//sender
@RequestMapping("/fanout")
@ResponseBody
public Result<String> mqFanout(){
sender.sendFanout("valentine");
return Result.success("helloworld");
}
//litsener
@RabbitListener(queues=MQConfig.TOPIC_QUEUE1)
public void receiveTopic1(String message) {
log.info(" topic queue1 message:"+message);
}
@RabbitListener(queues=MQConfig.TOPIC_QUEUE2)
public void receiveTopic2(String message) {
log.info(" topic queue2 message:"+message);
}
@RabbitListener(queues=MQConfig.HEADER_QUEUE)
public void receiveHeaderQueue(byte[] message) {
log.info(" header queue message:"+new String(message));
}
//config
@Bean
public FanoutExchange fanoutExchage(){
return new FanoutExchange(FANOUT_EXCHANGE);
}
@Bean
public Binding FanoutBinding1() {//这个没有key
return BindingBuilder.bind(topicQueue1()).to(fanoutExchage());
}
@Bean
public Binding FanoutBinding2() {
return BindingBuilder.bind(topicQueue2()).to(fanoutExchage());
}
@RequestMapping("/fanout")
@ResponseBody
public Result<String> mqFanout(){
sender.sendFanout("valentine");
return Result.success("helloworld");
}
4.Header 模式
//demo
@RequestMapping("/header")
@ResponseBody
public Result<String> mqHeader(){
sender.sendHeader("valentine");
return Result.success("helloworld");
}
//config
@Bean
public HeadersExchange headersExchage(){
return new HeadersExchange(HEADERS_EXCHANGE);
}
@Bean
public Queue headerQueue1() {
return new Queue(HEADER_QUEUE, true);
}
@Bean
public Binding headerBinding() {
Map<String, Object> map = new HashMap<String, Object>();
map.put("header1", "value1");
map.put("header2", "value2");
return BindingBuilder.bind(headerQueue1()).to(headersExchage()).whereAll(map).match();
}
//listener
@RabbitListener(queues=MQConfig.HEADER_QUEUE)
public void receiveHeaderQueue(byte[] message) {
log.info(" header queue message:"+new String(message));
}
//
public void sendHeader(Object message) {
String msg = RedisService.beanToString(message);
log.info("send fanout message:"+msg);
MessageProperties properties = new MessageProperties();
properties.setHeader("header1", "value1");
properties.setHeader("header2", "value2");
Message obj = new Message(msg.getBytes(), properties);
amqpTemplate.convertAndSend(MQConfig.HEADERS_EXCHANGE, "", obj);
}
秒杀接口优化思路
思路:减少数据库访问
1.系统初始化,把商品库存数量加载到Redis
2.收到请求, Redis预减库存,库存不足,直接返回,否则进入3
3.请求入队,立即返回排队中
4.请求出队,生成订单,减少库存
5.客户端轮询,是否秒杀成功
//MiaoshaMessage
package com.pro.miaosha.rabbitmq;
import com.pro.miaosha.domain.MiaoshaUser;
public class MiaoshaMessage {
private MiaoshaUser user;
private long goodsId;
public MiaoshaUser getUser() {
return user;
}
public void setUser(MiaoshaUser user) {
this.user = user;
}
public long getGoodsId() {
return goodsId;
}
public void setGoodsId(long goodsId) {
this.goodsId = goodsId;
}
}
//receiver
@RabbitListener(queues = MQConfig.MIAOSHA_QUEUE)
public void receive(String message) {
log.info("receive message:"+message);
MiaoshaMessage mm = RedisService.stringToBean(message, MiaoshaMessage.class);
MiaoshaUser user = mm.getUser();
long goodsIn = mm.getGoodsId();
}