基于Spring+SpringMVC+MyBatis实现高并发秒杀APIM

  1. 云栖社区>
  2. 博客>
  3. 正文

基于Spring+SpringMVC+MyBatis实现高并发秒杀APIM

daleychao 2017-01-02 11:12:00 浏览1235
展开阅读全文

基于Spring+SpringMVC+MyBatis实现高并发秒杀API

项目地址:https://github.com/DaleyChao/SecondKill 项目下载链接:https://github.com/DaleyChao/SecondKill/archive/master.zip

一、项目概述

一、为什么使用SSM框架

1.互联网公司常用框架
2.框架易于使用和轻量级
3.低代码倾入性
4.成熟的社区和用户群

二、相关技术

MySQL:1.表设计2.SQL技巧3.事务和行级锁
MyBatis:1.DAO层的设计和开发2.MyBatis合理使用3.MyBatis和Spring的整合和使用
Spring:1.Spring IOC整合Service2.声明式事务
SpringMVC:1.Restful接口设计和使用技巧2.框架运作流程3.Controller开发技巧
前端:1.交互设计2.Bootstrap3.JQuery
高并发:1.高并发点和高并发分析2.优化思路并实现

三、项目效果








二、建立项目

一、使用Maven建立项目

1.使用maven建立项目:mvn archetype:generate -DgroupId=org.seckill -DartifactId=seckill -DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false -DarchetypeCatalog=internal
也可以使用mvn archetype:generate命令根据提示建立项目。
2.使用Eclipse导入项目
3.修改web.xml文件schema,使用servlet4.0版本,默认2.3版本EL表达式不工作。
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
	version="3.1" metadata-complete="true">
	<!--修改servlet版本为3.1 -->
</web-app>
4.补全目录
5.补全pom.xml,导入相关依赖

二、相关依赖

部分说明:
1.Java日志:slf4j,log4j,logback,common-logging等等(commons-logging 和 slf4j 都是日志规范/接口,区别是slf4j在加载时寻找接口的实现,而 commons-logging 在运行时寻找接口的实现
slf4j是规范/接口,log4j,logback,common-logging是日志实现
这里使用slf4j+logback
2.c3p0数据库连接池
pom.xml
<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>org.seckill</groupId>
	<artifactId>seckill</artifactId>
	<packaging>war</packaging>
	<version>1.0.0SNAPSHOT</version>
	<name>seckill Maven Webapp</name>
	<url>http://maven.apache.org</url>
	<dependencies>
		<!-- https://mvnrepository.com/artifact/junit/junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>

		<!-- 日志 -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.12</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-core</artifactId>
			<version>1.1.1</version>
		</dependency>
		<!-- 实现slf4j接口并整合 -->
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.1.1</version>
		</dependency>
		<!-- 数据库相关的依赖 -->
		<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.40</version>
			<scope>runtime</scope>
		</dependency>

		<!-- 数据库链接池 -->
		<dependency>
			<groupId>c3p0</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.1.2</version>
		</dependency>
		<!-- Dao层 -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.1</version>
		</dependency>
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.0</version>
		</dependency>
		<!-- Servlet web 相关依赖 -->
		<!-- https://mvnrepository.com/artifact/taglibs/standard -->
		<dependency>
			<groupId>taglibs</groupId>
			<artifactId>standard</artifactId>
			<version>1.1.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/jstl/jstl -->
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
			<version>2.8.5</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.8.5</version>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>4.0.0-b01</version>
			<scope>provided</scope>
		</dependency>
		<!-- Spring -->
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.3.4.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.3.4.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-beans -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>4.3.4.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>4.3.4.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>4.3.4.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-web -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>4.3.4.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>4.3.4.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>4.3.4.RELEASE</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
		<dependency>
			<groupId>redis.clients</groupId>
			<artifactId>jedis</artifactId>
			<version>2.8.1</version>
		</dependency>
		<!-- protostuff序列化依赖 -->
		<!-- https://mvnrepository.com/artifact/io.protostuff/protostuff-core -->
		<dependency>
			<groupId>io.protostuff</groupId>
			<artifactId>protostuff-core</artifactId>
			<version>1.5.2</version>
		</dependency>

		<!-- https://mvnrepository.com/artifact/io.protostuff/protostuff-runtime -->
		<dependency>
			<groupId>io.protostuff</groupId>
			<artifactId>protostuff-runtime</artifactId>
			<version>1.5.2</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
		<dependency>
			<groupId>commons-collections</groupId>
			<artifactId>commons-collections</artifactId>
			<version>3.2.2</version>
		</dependency>

	</dependencies>
	<build>
		<finalName>seckill</finalName>
	</build>
</project>

MySQL等关系型数据库提供的事务机制是目前可靠的数据落地方案

三、秒杀功能

1.秒杀接口暴露
2.执行秒杀
3.相关的查询


四、项目阶段

一、DAO层设计和编码
1.数据库表设计 2.DAO的接口 3.MyBatis实现DAO
二、Service设计编码
1.Service层设计以及编码实现 2.通过Spring管理Service,通过声明式事务标注事务操作(通过Spring声明式事务简化事务控制)
三、web设计
1.RESTful接口设计 2.前端交互
四、优化项目

三、详细设计

一、数据库设计

--数据库初始化脚本
CREATE DATABASE seckill;

--使用数据库
use seckill;
--创建秒杀库存表
CREATE TABLE seckill1(
`seckill_id` bigint NOT NULL AUTO_INCREMENT COMMENT '商品库存id',
`name` varchar(120) NOT NULL COMMENT '商品名称',
`number` int NOT NULL COMMENT '库存数量',
`start_time` timestamp NOT NULL COMMENT '秒杀开启时间',
`end_time` timestamp NOT NULL COMMENT '秒杀结束时间',
`create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
PRIMARY KEY (seckill_id),
KEY idx_start_time(start_time),
KEY idx_end_time(end_time),
KEY idx_create_time(create_time)
)ENGINE=InnoDB AUTO_INCREMENT=1000  DEFAULT CHARSET=utf8 COMMENT="秒杀库存表";

--初始化数据
insert into seckill(name,number,start_time,end_time) values
('1000元秒杀iphone6s',100,'2016-12-19 00:00:00','2016-12-20 00:00:00'),
('100元秒杀iphone6',200,'2016-12-19 00:00:00','2016-12-20 00:00:00'),
('200元秒杀iphone5',300,'2016-12-19 00:00:00','2016-12-20 00:00:00'),
('10元秒杀iphone4',400,'2016-12-19 00:00:00','2016-12-20 00:00:00');

--秒杀成功明细表
--用户登录认证相关的信息
CREATE TABLE success_kill(
`seckill_id` bigint NOT NULL COMMENT '秒杀商品id',
`user_phone` bigint NOT NULL COMMENT '用户手机号',
`state` tinyint NOT NULL DEFAULT -1 COMMENT '创建标识:-1 无效  0 成功 1:已付款 2:已发货',
`create_time` timestamp NOT NULL COMMENT '创建时间',
PRIMARY KEY (seckill_id,user_phone),
KEY idx_create_time(create_time)
)ENGINE=InnoDB  DEFAULT CHARSET=utf8 COMMENT="秒杀成功明细表";

--连接数据库控制台
mysql -uroot -p


未完……

网友评论

登录后评论
0/500
评论