1 秒杀系统模拟基础实现,使用DB实现
本文根据动脑学院的一节类似的课程,改编实现。分别使用DB和redis来完成。
隔离的解释
业务隔离:将秒杀业务独立出来,尽量不与其他业务关联,以减少对其他业务的依赖性。譬如秒杀业务只保留用户id,商品id,数量等重要属性,通过中间件发送给业务系统,完成后续的处理。
系统隔离:将秒杀业务单独部署,以减少对其他业务服务器的压力。
数据隔离:由于秒杀对DB的压力很大,将DB单独部署,不与其他业务DB放一起,避免对DB的压力。
本篇讲使用DB完成秒杀系统。下一篇使用redis完成持久层。
一 初始化项目
以Springboot,mysql,jpa为技术方案。
新建Springboot项目,pom如下
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.tianyalei</groupId> <artifactId>common</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>common</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <optional>true</optional><!-- optional=true,依赖不会传递,该项目依赖devtools;之后依赖myboot项目的项目如果想要使用devtools,需要重新引入 --> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.18</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
javaBean
package com.tianyalei.model;
import javax.persistence.*;
/**
* Created by wuwf on 17/7/5.
*/
@Entity
public class GoodInfo {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
//数量
private int amount;
//商品编码
@Column(unique = true)
private String code;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
dao层,注意一下sql语句,where条件中的amount - count >= 0是关键,该语句能严格保证不超卖。
package com.tianyalei.repository;
import com.tianyalei.model.GoodInfo;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
/**
* Created by admin on 17/7/5.
*/
public interface GoodInfoRepository extends CrudRepository<GoodInfo, Integer> {
@Query("update GoodInfo set amount = amount - ?2 where code = ?1 and amount - ?2 >= 0")
@Modifying
int updateAmount(String code, int count);
}
service接口
package com.tianyalei.service;
import com.tianyalei.model.GoodInfo;
/**
* Created by wuwf on 17/7/5.
*/
public interface GoodInfoService {
void add(GoodInfo goodInfo);
void delete(GoodInfo goodInfo);
int update(String code, int count);
}
Service实现类
package com.tianyalei.service;
import com.tianyalei.model.GoodInfo;
import com.tianyalei.repository.GoodInfoRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
* Created by wuwf on 17/7/5.
*/
@Service("db")
public class GoodInfoDbService implements GoodInfoService {
@Autowired
private GoodInfoRepository goodInfoRepository;
@Transactional
public int update(String code, int count) {
return goodInfoRepository.updateAmount(code, count);
}
public void add(GoodInfo goodInfo) {
goodInfoRepository.save(goodInfo);
}
public void delete(GoodInfo goodInfo) {
goodInfoRepository.deleteAll();
}
}
yml配置文件
spring:
jpa:
database: mysql
show-sql: true
hibernate:
ddl-auto: update
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test
username: root
password:
redis:
host: localhost
port: 6379
password:
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: 10000
profiles:
active: dev
server:
port: 8080
以上即是基本配置。
二 模拟并发访问抢购
package com.tianyalei;
import com.tianyalei.model.GoodInfo;
import com.tianyalei.service.GoodInfoService;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
/**
* Created by wuwf on 17/7/5.
*/
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyTest {
@Resource(name = "db")
private GoodInfoService service;
private String goodCode = "iphone7";
/**
* 机器总数量
*/
private int goodAmount = 100;
/**
* 并发量
*/
private int threadNum = 200;
//销售量
private int goodSale = 0;
//买成功的数量
private int accountNum = 0;
//买成功的人的ID集合
private List<Integer> successUsers = new ArrayList<>();
private GoodInfo goodInfo;
/*当创建 CountDownLatch 对象时,对象使用构造函数的参数来初始化内部计数器。每次调用 countDown() 方法,
CountDownLatch 对象内部计数器减一。当内部计数器达到0时, CountDownLatch 对象唤醒全部使用 await() 方法睡眠的线程们。*/
private CountDownLatch countDownLatch = new CountDownLatch(threadNum);
@Test
public void contextLoads() {
for (int i = 0; i < threadNum; i++) {
new Thread(new UserRequest(goodCode, 7, i)).start();
countDownLatch.countDown();
}
//让主线程等待200个线程执行完,休息2秒,不休息的话200条线程还没执行完,就打印了
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("-----------购买成功的用户数量----------为" + accountNum);
System.out.println("-----------销售量--------------------为" + goodSale);
System.out.println("-----------剩余数量------------------为" + (goodAmount - goodSale));
System.out.println(successUsers);
}
private class UserRequest implements Runnable {
private String code;
private int buyCount;
private int userId;
public UserRequest(String code, int buyCount, int userId) {
this.code = code;
this.buyCount = buyCount;
this.userId = userId;
}
@Override
public void run() {
try {
//让线程等待,等200个线程创建完一起执行
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
//如果更新数据库成功,也就代表购买成功了
if (service.update(code, buyCount) > 0) {
//对service加锁,因为很多线程在访问同一个service对象,不加锁将导致购买成功的人数少于预期,且数量不对,可自行测试
synchronized (service) {
//销售量
goodSale += buyCount;
accountNum++;
//收录购买成功的人
successUsers.add(userId);
}
}
}
}
@Before
public void add() {
goodInfo = new GoodInfo();
goodInfo.setCode(goodCode);
goodInfo.setAmount(goodAmount);
service.add(goodInfo);
}
@After
public void delete() {
service.delete(goodInfo);
}
}
注意,由于是模拟并发,需要保证200个线程同时启动去访问数据库,所以使用了CountDownLatch类,在调用UserRequest线程的start方法后,会先进入await状态,等待200个线程创建完毕后,一起执行。
注意,由于是多线程操作service,必然导致数据不同步,所以需要对service加synchronize锁,来保证service的update方法能够正确执行。如果不加,可以自行测试,会导致少卖。
运行该测试类,看打印的结果。
可以多次运行,并修改每个人的购买数量、总商品数量、线程数,看看结果是否正确。
如修改为每人购买8个
mysql支持的并发访问量有限,倘若并发量较小,可以采用上面的update的sql就能控制住,倘若量大,可以考虑使用nosql。
下一篇讲一下redis模拟的方式。
1 秒杀系统模拟基础实现,使用DB实现的更多相关文章
- 2 秒杀系统模拟基础实现,使用Redis实现
这一篇,我们来使用redis进行数据存储. 新建一个redis的service实现类 package com.tianyalei.service; import com.tianyalei.model ...
- 谈一下关于CQRS架构如何实现高性能
CQRS架构简介 前不久,看到博客园一位园友写了一篇文章,其中的观点是,要想高性能,需要尽量:避开网络开销(IO),避开海量数据,避开资源争夺.对于这3点,我觉得很有道理.所以也想谈一下,CQRS架构 ...
- CQRS架构如何实现高性能
CQRS架构如何实现高性能 CQRS架构简介 前不久,看到博客园一位园友写了一篇文章,其中的观点是,要想高性能,需要尽量:避开网络开销(IO),避开海量数据,避开资源争夺.对于这3点,我觉得很有道理. ...
- MongoDB(NoSQL) 非关系型数据库
目录 简单了解 mongoDB 简单使用mongoDB 简单了解 mongoDB # NoSQL 泛指非关系型的数据库 NoSQL(NoSQL = Not Only SQL ),意即"不仅仅 ...
- mongodb 性能篇
一. 索引及其优化 索引的概述 数据库的索引好比是一本书前面的目录,能加快数据查询的速度. 适当的地方增加索引,不合理的地方删除次优索引,能优化性能较差的应用. 索引的操作 基础索引:db.ken. ...
- MongoDB常用命令
本文整理了一年多以来我常用的MongoDB操作,涉及mongo-shell.pymongo,既有运维层面也有应用层面,内容有浅有深,这也就是我从零到熟练的历程. MongoDB的使用之前也分享过一篇, ...
- MongoDB使用小结:一些常用操作分享
本文整理了一年多以来我常用的MongoDB操作,涉及mongo-shell.pymongo,既有运维层面也有应用层面,内容有浅有深,这也就是我从零到熟练的历程. MongoDB的使用之前也分享过一篇, ...
- VC++ ADO相关
<VC对ADO的操作> ADO概述: ADO是Microsoft为最新和最强大的数据访问范例 OLE DB 而设计的,是一个便于使用的应用程序层接口. ADO 使您能够编写应用程序以通过 ...
- [MongoDB]Mongodb攻略
-------------------------------------------------------------------------------------------- [基础] 1. ...
随机推荐
- 错误:未启用当前数据库的SQL Server Service Broker,因此查询通知不受支持。如果希望使用通知,请为此数据库启用 Service Broker。
解决方法: 打开SQL Server,新建查询: ALTER DATABASE 数据库名 SET NEW_BROKER WITH ROLLBACK IMMEDIATE;ALTER DATABASE 数 ...
- JavaScript判断对象 是什么类型的.
// 这种方法不起作用 if (x == undefined) { // 作某些操作 } // 这个方法同样不起作用- if (typeof(x) == undefined) { // 作某些 ...
- NFS 安装、管理
NFS简介 NFS允许一个系统在网络上与他人共享目录和文件.通过使用NFS,用户和程序可以像访问本地文件一样访问远程系统上的文件. 安装NFS 服务端安装 NFS安装包:nfs-utils-lib.i ...
- jQuery/CSS3 3D焦点图动画
在线演示 本地下载
- MapReduce:实现文档倒序排序,且字符串拼接+年+月+日
写出MapReduce程序完成以下功能. input1: -- a -- b -- c -- d -- a -- b -- c -- c input2: -- b -- a -- b -- d -- ...
- [mongodb] MMAPv1 Storage Engine
MMAPv1 是mongodb 在3.2以前默认的存储引擎,在3.2 之后默认的存储引擎为WiredTiger,MMAPv1存储引擎基于内存映射文件,它擅长高容量的插入,读取和更新. Journal ...
- CentOS 7 安装各个桌面版本
http://unix.stackexchange.com/questions/181503/how-to-install-desktop-environments-on-centos-7 92dow ...
- jvm-垃圾收集器与内存分配策略
垃圾收集器与内存分配策略 参考: https://my.oschina.net/hosee/blog/644085 http://www.cnblogs.com/zhguang/p/Java-JVM- ...
- [TJOI2010]打扫房间
题目描述 学校新建了一批宿舍,值日生小A要把所有的空房间都打扫一遍.这些宿舍的布局很奇怪,整个建筑物里所有的房间组成一个N * M的矩阵,每个房间的东南西北四面墙上都有一个门通向隔壁房间.另外有些房间 ...
- js 前台ajax验证马克一下
function check_form(){ var email=$("#email").val(); var code=$("#code").val(); v ...