由于秒杀的并发量太大,所以仅仅使用缓存是不够的,还需要用到RabbitMQ。

这里推荐一款用于分库分表的中间件:mycat

解决超卖的问题(看第五章节):

秒杀接口优化:

实操:

然后把下载好的文件上传到服务器上:

验证一下:

OK,到这一步,erlang安装好了。

启动rabbitMQ:

关闭:

或者:

配置环境变量:

在末尾加上:

使环境变量失效:

SpringBoot集成RabbitMQ:
添加依赖:

application.properties中配置:

#rabbitmq
spring.rabbitmq.host=10.110.3.62
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.virtual-host=/
#\u6D88\u8D39\u8005\u6570\u91CF
spring.rabbitmq.listener.simple.concurrency= 10
spring.rabbitmq.listener.simple.max-concurrency= 10
#\u6D88\u8D39\u8005\u6BCF\u6B21\u4ECE\u961F\u5217\u83B7\u53D6\u7684\u6D88\u606F\u6570\u91CF
spring.rabbitmq.listener.simple.prefetch= 1
#\u6D88\u8D39\u8005\u81EA\u52A8\u542F\u52A8
spring.rabbitmq.listener.simple.auto-startup=true
#\u6D88\u8D39\u5931\u8D25\uFF0C\u81EA\u52A8\u91CD\u65B0\u5165\u961F
spring.rabbitmq.listener.simple.default-requeue-rejected= true
#\u542F\u7528\u53D1\u9001\u91CD\u8BD5
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

另外如果想用guest登录远程的rabbitMQ,必须设置如下:

代码部分:

MQConfig:

package com.imooc.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();
} }

MQSender:

package com.imooc.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.imooc.miaosha.redis.RedisService; @Service
public class MQSender { private static Logger log = LoggerFactory.getLogger(MQSender.class); @Autowired
AmqpTemplate amqpTemplate ; public void sendMiaoshaMessage(MiaoshaMessage mm) {
String msg = RedisService.beanToString(mm);
log.info("send message:"+msg);
amqpTemplate.convertAndSend(MQConfig.MIAOSHA_QUEUE, msg);
} // public void send(Object message) {
// String msg = RedisService.beanToString(message);
// 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);
// } }

MQReceiver:

package com.imooc.miaosha.rabbitmq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.imooc.miaosha.domain.MiaoshaOrder;
import com.imooc.miaosha.domain.MiaoshaUser;
import com.imooc.miaosha.redis.RedisService;
import com.imooc.miaosha.service.GoodsService;
import com.imooc.miaosha.service.MiaoshaService;
import com.imooc.miaosha.service.OrderService;
import com.imooc.miaosha.vo.GoodsVo; @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)
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));
// }
// }

集成RabbitMQ做秒杀的更多相关文章

  1. SpringBoot集成rabbitmq(一)

    前言 Rabbitmq是一个开源的消息代理软件,是AMQP协议的实现.核心作用就是创建消息队列,异步发送和接收消息.通常用来在高并发中处理削峰填谷.延迟处理.解耦系统之间的强耦合.处理秒杀订单.  入 ...

  2. springboot集成rabbitmq(实战)

    RabbitMQ简介RabbitMQ使用Erlang语言开发的开源消息队列系统,基于AMQP协议来实现(AMQP的主要特征是面向消息.队列.路由.可靠性.安全).支持多种客户端,如:Python.Ru ...

  3. Java SpringBoot集成RabbitMq实战和总结

    目录 交换器.队列.绑定的声明 关于消息序列化 同一个队列多消费类型 注解将消息和消息头注入消费者方法 关于消费者确认 关于发送者确认模式 消费消息.死信队列和RetryTemplate RPC模式的 ...

  4. Spring boot集成RabbitMQ(山东数漫江湖)

    RabbitMQ简介 RabbitMQ是一个在AMQP基础上完整的,可复用的企业消息系统 MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过读写出 ...

  5. spring集成rabbitMq(非springboot)

    首先 , pom文件需要加入spring集成rabbitMq的依赖: <dependency> <groupId>org.springframework.amqp</gr ...

  6. Spring Boot实战三:集成RabbitMQ,实现消息确认

    Spring Boot集成RabbitMQ相比于Spring集成RabbitMQ简单很多,有兴趣了解Spring集成RabbitMQ的同学可以看我之前的<RabbitMQ学习笔记>系列的博 ...

  7. go-micro集成RabbitMQ实战和原理

    在go-micro中异步消息的收发是通过Broker这个组件来完成的,底层实现有RabbitMQ.Kafka.Redis等等很多种方式,这篇文章主要介绍go-micro使用RabbitMQ收发数据的方 ...

  8. rabbitMQ第五篇:Spring集成RabbitMQ

    前面几篇讲解了如何使用rabbitMq,这一篇主要讲解spring集成rabbitmq. 首先引入配置文件org.springframework.amqp,如下 <dependency> ...

  9. RabbitMQ第四篇:Spring集成RabbitMQ

    前面几篇讲解了如何使用rabbitMq,这一篇主要讲解spring集成rabbitmq. 首先引入配置文件org.springframework.amqp,如下 <dependency> ...

随机推荐

  1. Mac下安装和卸载MySQL(含配置)

     安装 首先需要下载 MySQL Community Server 下载地址:https://dev.mysql.com/downloads/mysql/ 进入MySQL的下载界面(https://d ...

  2. open-falcon之transfer

    功能 负责数据转发,接受agent上报的数据,然后使用一致性hash规则对数据进行分片,最后将分片后的数据分别转发至judge,graph 对接收到的数据进行合法性校验.规整 针对每个后端实例维护一个 ...

  3. 【转载】.NET 开发者必备的工具箱

    本文作者Spencer是一名专注于ASP.NET和C#的程序员,他列举了平时工作.在家所使用的大部分开发工具,其中大部分工具都是集中于开发,当然也有一些其它用途的,比如图片处理.文件压缩等. 如果你是 ...

  4. python下安装Scikit-learn

    安装SK-Learn需要依赖的Python安装包有: Python (>= 2.6), NumPy (>= 1.3), SciPy (>= 0.7), 下载python的各种包的地址 ...

  5. 【cs229-Lecture7】支持向量机(SVM)

    SVM不错的学习资料: 百度网盘链接: http://pan.baidu.com/s/1hqw0Rnm 密码: asec blog:http://www.blogjava.net/zhenandaci ...

  6. css笔记 - 张鑫旭css课程笔记之 relative 篇

    relative地址 relative 对 absolute的限制作用 限制left/top/right/bottom(方位值)定位 限制描述:absolute设置了方位值时,这些方位值是相对于pos ...

  7. jenkins定时任务未生效解决

    近期在配置jenkins定时任务时,发现未生效,并没有按时触发任务 解决思路: 1.先查看下我们的定时任务有没有选择正确,如下说明: Poll SCM:定时检查源码变更,如果有更新就checkout最 ...

  8. Unity3D 记第二次面试

    2014-03-10 忍不住投递了几份简历大概有20个,总共收到面试电话2个,十分之一.一个是11号下午4点面试另一个是12号下午3点面试(后来没去至于原因下面有)12号没去,为什么?因为招聘要求“精 ...

  9. Docker学习计划三:Dockerfile 使用

    我们使用 Dockerfile 定义镜像,依赖镜像来运行容器,因此 Dockerfile 是镜像和容器的关键,Dockerfile 可以非常容易的定义镜像内容 首先通过一张图来了解 Docker 镜像 ...

  10. 【笔记】javascript权威指南-第二章-词法结构

    词法结构 //本书是指:javascript权威指南    //以下内容摘记时间为:2013.7.28   字符集 UTF-8和UTF-16的区别?Unicode和UTF是什么关系?Unicode转义 ...