文章前言
众所周知,当遇到比较多数据不一致的问题时,大多数都是因为并发请求时,没及时处理的原因,提一个电商平台比较经常出现得高并发场景限时秒杀活动,他们是怎么来防止超卖呢?如何实现高并发秒杀呢?。
本文模拟了高并发秒杀,并且防止了超卖,也模拟了纯数据库秒杀超卖得场景,本次模拟demo得框架技术为:SpringBoot+Mysql+Redis+RabbitMQ+tkmybatis
数据库表结构:
一个为库存表,一个为订单表,本人使用得是mysql8.0。
完整得项目工具展示
Jmeter :
redisManager :
RabbitMQ :
编写代码
1.首先新建Springboot项目
2.可以先不勾选需要得jar包,项目初始化好之后,使用maven导入项目需要得jar包
pom.xml :
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.projectlombok
lombok
true
org.springframework.boot
spring-boot-starter-data-jpa
org.springframework.boot
spring-boot-devtools
runtime
true
mysql
mysql-connector-java
runtime
org.apache.commons
commons-lang3
3.8.1
org.springframework.boot
spring-boot-starter-amqp
io.jsonwebtoken
jjwt
0.7.0
org.springframework.boot
spring-boot-starter-data-redis
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.0
org.springframework
spring-tx
tk.mybatis
mapper-spring-boot-starter
2.0.3-beta1
tk.mybatis
mapper
4.0.0
3.配置application.properties
spring.devtools.restart.enabled=false
##配置数据库连接
spring.datasource.username=root
spring.datasource.password=root
server.port=8443
spring.datasource.url=jdbc:mysql://localhost:3306/ktoa?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&allowMultiQueries=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
##配置rabbitmq连接
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
##配置连接redis --都记得打开服务
spring.redis.host=localhost
spring.redis.port=6379
spring.redis.jedis.pool.max-active=1024
spring.redis.jedis.pool.max-wait=-1s
spring.redis.jedis.pool.max-idle=200
spring.redis.password=123456
这时可以启动一下springboot项目是否能够正常启动,如没问题可以继续往下编写!!
4.新建pojo包,添加实体类
Order.java:
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
@Data
@Table(name = "t_order")
public class Order implements Serializable {
private static final long serialVersionUID = -8867272732777764701L;
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "order_name")
private String order_name;
@Column(name = "order_user")
private String order_user;
}
Stock.java:
import lombok.Data;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
@Table(name = "stock")
@Data
public class Stock implements Serializable {
private static final long serialVersionUID = 2451194410162873075L;
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "stock")
private Long stock;
}
因为本次数据库操作方面使用了tkmybatis框架,所以实体类我们需要用到JPA的注解,来实现映射关系!!
5.配置tkmybatis得接口
新建名为base得包,在base下面新建service得接口