在上一章当中,我们完成了环境的搭建。从这章开始,我们一步一步来实现秒杀业务的代码编写。
整个秒杀的流程如下:
查询商品,如果商品不存在,当然直接秒杀失败如果商品存在,则要查询商品的库存并进行预扣提交订单,提醒用户秒杀成功并付款付款成功,秒杀成功入库并结束付款失败,秒杀库存+1,秒杀失败 在编码的过程中,需要注意后面的流程可能会对前面的流程有影响,比如付款失败后,会对库存增加操作。这与第2步的预扣是环环相扣的。首先创建两个表,脚本如下
-- ---------------------------- -- Table structure for goods -- ---------------------------- DROP TABLE IF EXISTS `goods`; CREATE TABLE `goods` ( `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'Id', `goodsname` varchar(45) NOT NULL COMMENT '商品名称', `count` int NOT NULL COMMENT '商品数量', PRIMARY KEY (`id`) ) ENGINE = InnoDB AUTO_INCREMENT = 4 DEFAULT CHARSET = utf8 COMMENT ='商品信息表'; -- ---------------------------- -- Records of goods -- ---------------------------- INSERT INTO `goods` VALUES ('1', '华为手机', '1000'); INSERT INTO `goods` VALUES ('2', '华为手机', '1000'); DROP TABLE IF EXISTS `goods_user`; CREATE TABLE `goods_user` ( `goodsId` int(11) NOT NULL COMMENT '商品编号', `userId` varchar(45) NOT NULL COMMENT '用户编号', key (`goodsId`, `userId`) ) ENGINE = InnoDB DEFAULT CHARSET = utf8 COMMENT ='商品用户秒杀结果表';需要注意实现java.io.Serializable接口,因为后面需要进行缓存操作,可能涉及到序列化与反序列化。
创建mapper接口和xml文件 package com.example.kill.mapper; import com.example.kill.entity.Goods; import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Param; import java.util.List; @Mapper public interface GoodsMapper { Goods selectByPrimaryKey(Integer id); int updateSecKill(@Param("id") Integer id); } package com.example.kill.mapper; import com.example.kill.entity.GoodsUser; import org.apache.ibatis.annotations.Mapper; @Mapper public interface GoodsUserMapper { GoodsUser findByUserId(String userId); } <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.kill.mapper.GoodsMapper"> <resultMap id="BaseResultMap" type="com.example.kill.entity.Goods"> <!-- WARNING - @mbg.generated This element is automatically generated by MyBatis Generator, do not modify. --> <id column="id" jdbcType="INTEGER" property="id"/> <result column="goodsname" jdbcType="VARCHAR" property="goodsname"/> <result column="count" jdbcType="INTEGER" property="count"/> </resultMap> <select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap"> select id, goodsname, count from goods where id = #{id,jdbcType=INTEGER} </select> <update id="updateSecKill"> update goods SET count = count - 1 where id = #{id,jdbcType=INTEGER} and count > 0 </update> </mapper> <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.example.kill.mapper.GoodsUserMapper"> <resultMap id="BaseResultMap" type="com.example.kill.entity.GoodsUser"> <!-- WARNING - @mbg.generated This element is automatically generated by MyBatis Generator, do not modify. --> <id column="goodsId" jdbcType="INTEGER" property="goodsId"/> <result column="userId" jdbcType="VARCHAR" property="userId"/> </resultMap> <select id="findByUserId" resultMap="BaseResultMap"> select goodsId, userId from goods_user where userId = #{userId,jdbcType=VARCHAR} </select> </mapper> 业务层接口以及实现 暂时提供两个接口一个是查询商品,一个是秒杀商品 package com.example.kill.service; import com.example.kill.entity.Goods; public interface KillService { Goods detail(Integer goodsId); boolean secKillByDb(Integer goodsId, String sessionUserId); }实现类
package com.example.kill.service.impl; import com.example.kill.entity.Goods; import com.example.kill.entity.GoodsUser; import com.example.kill.mapper.GoodsMapper; import com.example.kill.mapper.GoodsUserMapper; @Service public class KillServiceImpl implements KillService { private static final Logger logger = LoggerFactory.getLogger(KillService.class); @Autowired private GoodsMapper goodsMapper; @Autowired private GoodsUserMapper goodsUserMapper; @Override public Goods detail(Integer goodsId) { Goods goods = null; String killGoodsId = KillConstants.KILLGOOD_DETAIL + goodsId; return goods; } @Override public boolean secKillByDb(Integer goodsId, String userId) { return true; } } 控制层在正常项目中需要考虑秒杀时间的问题,此处简化处理。用户需要从登陆的session信息中获取到,这里直接随机取uuid值。秒杀的方法中首先查询商品,如果商品不存在,就会抛出异常。如果商品存在,则执行秒杀服务。
package com.example.kill.controller; import com.example.kill.constant.HttpResponseBody; import com.example.kill.entity.Goods; import com.example.kill.service.KillService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.UUID; @RestController public class KillController { private static final Logger logger = LoggerFactory.getLogger(KillController.class); @Autowired private KillService killService; @RequestMapping("kill/killByDb/{goodsId}") public HttpResponseBody killByDb(@PathVariable("goodsId") Integer goodsId) { Goods goods = killService.detail(goodsId); if (goods == null) { return HttpResponseBody.failResponse("产品不存在"); } // if (goods.getBegainTime().getTime() > System.currentTimeMillis()){ // return HttpResponseBody.failResponse("抢购还未开始"); // } // if (goods.getEndTime().getTime() < System.currentTimeMillis()){ // return HttpResponseBody.failResponse("抢购已结束"); // } if (!killService.secKillByDb(goodsId, getSessionUserId())) { return HttpResponseBody.failResponse("抢购失败"); } return HttpResponseBody.successResponse("ok", goods); } private String getSessionUserId() { return UUID.randomUUID().toString(); } } 一些公共类和配置类 package com.example.kill.constant; import java.io.Serializable; public class HttpResponseBody<E> implements Serializable { /** * Comment for <code>serialVersionUID</code> */ private static final long serialVersionUID = -5406928126690333502L; private String code; private String msg; private E data; public HttpResponseBody() { } public HttpResponseBody(String code, String msg, E data) { this.code = code; this.msg = msg; this.data = data; } public HttpResponseBody(String code, String msg) { this.code = code; this.msg = msg; } public static HttpResponseBody successResponse(String message) { return new HttpResponseBody("秒杀成功", message); } public static <T> HttpResponseBody<T> successResponse(String message, T singleData) { return new HttpResponseBody<>("秒杀成功", message, singleData); } public static HttpResponseBody failResponse(String message) { return new HttpResponseBody("秒杀失败", message); } public String getCode() { return code; } public void setCode(String code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } public E getData() { return data; } public void setData(E data) { this.data = data; } } package com.example.kill.config; import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.StringRedisSerializer; @Configuration public class RedisConfig { @Bean public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(factory); //使用jdk的序列化 template.setValueSerializer(new FastJsonRedisSerializer<>(Object.class)); //使用StringRedisSerializer来序列化和反序列化redis的key值 template.setKeySerializer(new StringRedisSerializer()); template.afterPropertiesSet(); return template; } }首先需要完成的就是商品的查询操作
这个是最简单的实现。
在上面的测试中,吞吐量相对于之前的测试还算可以。但是这并不能说明直接请求数据库层是可以的,我们测试时使用的线程为1000,如果数量继续加大,所有的查询都打到数据库,可能就立刻导致数据库的崩溃。服务层核心设计思想, 尽量把大量的请求不要瞬时落到数据库层,读请求, cache 来扛, memcached, redis 单机扛 10W+应该没问题。
使用redis缓存能有效提高服务的抗压能力。但有几点需要注意:
考虑以上的问题和解决方案,此次实现缓存的失效时间为2天(避免缓存击穿问题),另外如果数据库也不包含数据,会在redis缓存中保存一个null的字符串(防止缓存穿透问题)。
@Override public Goods detail(Integer goodsId) { Goods goods = null; String killGoodsId = KillConstants.KILLGOOD_DETAIL + goodsId; Object killGoodCache = redisTemplate.opsForValue().get(killGoodsId); if (null != killGoodCache) { logger.info(Thread.currentThread().getName() + "---redis缓存中得到的数据---------"); return JSONObject.parseObject(killGoodCache.toString(), Goods.class); } synchronized (KillServiceImpl.class) { // 1、 从缓存里面拿 killGoodCache = redisTemplate.opsForValue().get(killGoodsId); if (null != killGoodCache) { logger.info(Thread.currentThread().getName() + "---redis缓存中得到的数据---------"); return JSONObject.parseObject(killGoodCache.toString(), Goods.class); } // 2、 去数据库里面查询数据 goods = goodsMapper.selectByPrimaryKey(goodsId); if (null != goods) { redisTemplate.opsForValue().set(killGoodsId, goods, 2, TimeUnit.DAYS); } else { //防止缓存穿透 缓存时间一定要短 ,空数据没有必要占用redis内存 redisTemplate.opsForValue().set(killGoodsId, "null", 5, TimeUnit.MINUTES); } return goods; }该处使用的url网络请求的数据。
在查询秒杀商品详情的时候,我们首先是把秒杀商品做缓存预热加载到了redis中了,但是我们忽略了一个问题,当流量达到一定量的时候,比如流量超过了10W+的并发时,这时候redis也是扛不住的,这时候就需要考虑使用本地缓存了,而且使用本地缓存也可以加快程序响应速度,毕竟如果详情信息从redis中拿的话,还是需要占用网络带宽的,其速度不可能比使用本地缓存更快。此处我们使用ehcache作为本地缓存。
import net.sf.ehcache.Cache; import net.sf.ehcache.CacheManager; import net.sf.ehcache.Element; @Autowired private GoodsMapper goodsMapper; @Autowired private RedisTemplate redisTemplate; @Autowired private GoodsUserMapper goodsUserMapper; @Autowired private CacheManager cacheManager; @Override public Goods detail(Integer goodsId) { Goods goods = null; String killGoodsId = KillConstants.KILLGOOD_DETAIL + goodsId; Cache killgoodsLocalCache = cacheManager.getCache("killgoodDetail"); if (null != killgoodsLocalCache.get(killGoodsId)) { logger.info(Thread.currentThread().getName() + "--------ehcache缓存里面的到数据-------"); goods = (Goods) killgoodsLocalCache.get(killGoodsId).getObjectValue(); return goods; } Object killGoodCache = redisTemplate.opsForValue().get(killGoodsId); if (null != killGoodCache) { logger.info(Thread.currentThread().getName() + "---redis缓存中得到的数据---------"); return JSONObject.parseObject(killGoodCache.toString(), Goods.class); } synchronized (KillServiceImpl.class) { if (null != killgoodsLocalCache.get(killGoodsId)) { logger.info(Thread.currentThread().getName() + "--------ehcache缓存里面的到数据-------"); goods = (Goods) killgoodsLocalCache.get(killGoodsId).getObjectValue(); return goods; } killGoodCache = redisTemplate.opsForValue().get(killGoodsId); if (null != killGoodCache) { logger.info(Thread.currentThread().getName() + "---redis缓存中得到的数据---------"); return JSONObject.parseObject(killGoodCache.toString(), Goods.class); } goods = goodsMapper.selectByPrimaryKey(goodsId); if (null != goods) { killgoodsLocalCache.putIfAbsent(new Element(killGoodsId, goods)); redisTemplate.opsForValue().set(killGoodsId, goods, 2, TimeUnit.DAYS); } else { //防止缓存穿透 缓存时间一定要短 ,空数据没有必要占用redis内存 redisTemplate.opsForValue().set(killGoodsId, "null", 5, TimeUnit.MINUTES); } } return goods; }先查找本地缓存,如果有直接返回,再找redis,如果有直接返回,再去查询数据库,并把结果缓存到本地缓存和redis缓存,这里需要注意,本地缓存中缓存的数据量一定不要大,而且ehcache中缓存的数据的失效时间一定要短于redis中的数据的有效时间,目的是为了减少GC的压力。
另外 ehcache后续还有堆外模式, 而且是一种大堆的堆外模式, 这块有兴趣的同学可以去了解一下, 这样很多缓存数据都可以存储到 ehcache 中, 可以大大提高程序的响应速度。 其实有没有发现, 这里 ehcache 的使用是不是跟布隆过滤器有点像, 都是为了减少对 redis 的访问压力。 而且我们可以把原本会落到 redis 的请求量分散到了各个应用服务器了, 而应用服务器的数量肯定是要多余 redis 服务器的, 这样就把压力均摊到了各个应用服务器了。
相关ehcache的配置:
package com.example.kill.config; import net.sf.ehcache.CacheManager; import org.springframework.cache.ehcache.EhCacheManagerFactoryBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Primary; import org.springframework.core.io.ClassPathResource; @Configuration public class EhcacheConfig { @Primary @Bean(name = "ehCacheManager") public CacheManager cacheManager(EhCacheManagerFactoryBean bean) { CacheManager cm = bean.getObject(); return cm; } @Bean public EhCacheManagerFactoryBean ehCacheManagerFactoryBean() { EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean(); cacheManagerFactoryBean.setConfigLocation(new ClassPathResource("ehcache.xml")); cacheManagerFactoryBean.setShared(true); // 设置完属性后,cacheManagerFactoryBean会执行afterProertiesSet()方法, // 所以不能在这里直接执行cacheManagerFactoryBean.getObject(),直接执行的话,因为在afterPropertiesSet()方法之前执行,所以:getObject()会得到null值 return cacheManagerFactoryBean; } }src/main/resources/ehcache.xml
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd" updateCheck="false"> <diskStore path = "/home/cache"/> <!-- eternal="false"意味着该缓存会死亡 maxElementsInMemory="900"缓存的最大数目 overflowToDisk="false" 内存不足时,是否启用磁盘缓存,如果为true则表示启动磁盘来存储,如果为false则表示不启动磁盘 diskPersistent="false" timeToIdleSeconds="0" 当缓存的内容闲置多少时间销毁 timeToLiveSeconds="60" 当缓存存活多少时间销毁(单位是秒,如果我们想设置2分钟的缓存存活时间,那么这个值我们需要设置120) memoryStoreEvictionPolicy="LRU" /> 自动销毁策略 --> <!-- name:缓存名称。 maxElementsInMemory:缓存最大个数。 eternal:对象是否永久有效,一但设置了,timeout将不起作用。 timeToIdleSeconds:设置对象在失效前的允许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大。 timeToLiveSeconds:设置对象在失效前允许存活时间(单位:秒)。最大时间介于创建时间和失效时间之间。仅当eternal=false对象不是永久有效时使用,默认是0.,也就是对象存活时间无穷大。 overflowToDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。 diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区。 maxElementsOnDisk:硬盘最大缓存个数。 diskPersistent:是否缓存虚拟机重启期数据 Whether the disk store persists between restarts of the Virtual Machine. The default value is false. diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。 memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)。 clearOnFlush:内存数量最大时是否清除。 --> <defaultCache eternal="false" maxElementsInMemory="900" overflowToDisk="false" diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="60" memoryStoreEvictionPolicy="LRU" /> <!-- 这里的 users 缓存空间是为了下面的 demo 做准备 --> <cache name="killgoodDetail" eternal="false" maxElementsInMemory="5000" overflowToDisk="true" diskPersistent="false" timeToIdleSeconds="0" timeToLiveSeconds="86400" memoryStoreEvictionPolicy="LRU" /> </ehcache> <!--<diskStore>==========当内存缓存中对象数量超过maxElementsInMemory时,将缓存对象写到磁盘缓存中(需对象实现序列化接口) --> <!--<diskStore path="">==用来配置磁盘缓存使用的物理路径,Ehcache磁盘缓存使用的文件后缀名是*.data和*.index --> <!--name=================缓存名称,cache的唯一标识(ehcache会把这个cache放到HashMap里) --> <!--maxElementsOnDisk====磁盘缓存中最多可以存放的元素数量,0表示无穷大 --> <!--maxElementsInMemory==内存缓存中最多可以存放的元素数量,若放入Cache中的元素超过这个数值,则有以下两种情况 --> <!--1)若overflowToDisk=true,则会将Cache中多出的元素放入磁盘文件中 --> <!--2)若overflowToDisk=false,则根据memoryStoreEvictionPolicy策略替换Cache中原有的元素 --> <!--eternal==============缓存中对象是否永久有效,即是否永驻内存,true时将忽略timeToIdleSeconds和timeToLiveSeconds --> <!--timeToIdleSeconds====缓存数据在失效前的允许闲置时间(单位:秒),仅当eternal=false时使用,默认值是0表示可闲置时间无穷大,此为可选属性 --> <!--即访问这个cache中元素的最大间隔时间,若超过这个时间没有访问此Cache中的某个元素,那么此元素将被从Cache中清除 --> <!--timeToLiveSeconds====缓存数据在失效前的允许存活时间(单位:秒),仅当eternal=false时使用,默认值是0表示可存活时间无穷大 --> <!--即Cache中的某元素从创建到清楚的生存时间,也就是说从创建开始计时,当超过这个时间时,此元素将从Cache中清除 --> <!--overflowToDisk=======内存不足时,是否启用磁盘缓存(即内存中对象数量达到maxElementsInMemory时,Ehcache会将对象写到磁盘中) --> <!--会根据标签中path值查找对应的属性值,写入磁盘的文件会放在path文件夹下,文件的名称是cache的名称,后缀名是data --> <!--diskPersistent=======是否持久化磁盘缓存,当这个属性的值为true时,系统在初始化时会在磁盘中查找文件名为cache名称,后缀名为index的文件 --> <!--这个文件中存放了已经持久化在磁盘中的cache的index,找到后会把cache加载到内存 --> <!--要想把cache真正持久化到磁盘,写程序时注意执行net.sf.ehcache.Cache.put(Element element)后要调用flush()方法 --> <!--diskExpiryThreadIntervalSeconds==磁盘缓存的清理线程运行间隔,默认是120秒 --> <!--diskSpoolBufferSizeMB============设置DiskStore(磁盘缓存)的缓存区大小,默认是30MB --> <!--memoryStoreEvictionPolicy========内存存储与释放策略,即达到maxElementsInMemory限制时,Ehcache会根据指定策略清理内存 --> <!--共有三种策略,分别为LRU(Least Recently Used 最近最少使用)、LFU(Less Frequently Used最不常用的)、FIFO(first in first out先进先出) -->本章通过一个简单的实例介绍了秒杀过程中的一些问题以及redis作为二级缓存、ehcache作为一级本地缓存来应对高并发。在实际过程中,问题只会更多。比如:
秒杀和抢购收到了“海量”的请求, 实际上里面的水分是很大的。 不少用户, 为了“抢“到商品,会使用“刷票工具”等类型的辅助工具, 帮助他们发送尽可能多的请求到服务器。 还有一部分高级用户, 制作强大的自动请求脚本。 这种做法的理由也很简单, 就是在参与秒杀和抢购的请求中, 自己的请求数目占比越多, 成功的概率越高
同一账号发送多次请求 这种加锁就可以解决, redis 锁, 如果其中一个请求获取锁成功就允许秒杀, 其他都拒绝多个账号发送多次请求 黄牛党, 僵尸号在秒杀开始的时候疯狂请求可以做 ip 限制, 如果某个 ip 请求频率过高, 可以弹验证码或者实名验证, 其实就是判断是否是活人。多个账号, 不同 ip 这种就比较复杂了, 典型的就是高级黑客手里掌握了 N 多肉鸡服务器, 那么 ip 就不一样了这种请求就跟普通用户请求基本上类似了这种必须要做业务限制, 提高秒杀的参与门槛, 比如只有星级用户、 账号等级等等业务手段来做限制。 而僵尸账号往往账号等级和活跃度都不高