【黑马程序员】redis令牌机制实现秒杀

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

黑马程序员成都中心编著【黑马程序员】redis令牌机制实现秒杀一、前言1.秒杀介绍秒杀是电商系统非常常见的功能模块,是商家进行相关促销推广的常用方式。主要特点是商品库存有限,抢购时间有限。那么在系统设计之初就应该考虑在数量和时间有限的情况下导致的一个高并发以及高并发所带来的库存超卖的问题。秒杀需要解决的问题:1)库存超卖解决方案:1)悲观锁:synchronize、Lock2)乐观锁:数据库乐观锁版本号控制2)高并发情况下系统压力以及用户体验解决方案:redis本教程采用:redis中list类型达到令牌机制完成秒杀。用户抢redis中的令牌,抢到令牌的用户才能进行支付,支付成功之后可以生成订单,如果一定时间之内没有支付那么就由定时任务来归还令牌2.开发介绍1)开发工具:IntelliJIDEA2017.3.52)JDK版本:1.7+3)数据库:mysql5.7、Redis4)技术:Spring、SpringDataRedis、mybatis二、环境搭建1.数据库表创建/*商品表*/CREATETABLE`goods`(`goods_id`int(11)NOTNULLAUTO_INCREMENT,`num`int(11)DEFAULTNULL,`goods_name`varchar(50)DEFAULTNULL,PRIMARYKEY(`goods_id`))ENGINE=InnoDBAUTO_INCREMENT=2DEFAULTCHARSET=utf8;insertinto`goods`(`goods_id`,`num`,`goods_name`)values(1,100,'iphoneX');/*订单表*/CREATETABLE`orders`(`order_id`int(11)NOTNULLAUTO_INCREMENT,`good_id`int(11)DEFAULTNULL,`user`varchar(50)DEFAULTNULL,PRIMARYKEY(`order_id`)黑马程序员成都中心编著)ENGINE=InnoDBAUTO_INCREMENT=1163DEFAULTCHARSET=utf8;2.redis安装(略)3.创建mavne项目,打包方式jar,pom.xml如下propertiesproject.build.sourceEncodingUTF-8/project.build.sourceEncodingjunit.version4.12/junit.versionspring.version4.2.4.RELEASE/spring.versionpagehelper.version4.0.0/pagehelper.versionmybatis.version3.2.8/mybatis.versionmybatis.spring.version1.2.2/mybatis.spring.versionmybatis.paginator.version1.2.15/mybatis.paginator.versionmysql.version5.1.32/mysql.versiondruid.version1.0.9/druid.version/propertiesdependencies!--Spring--dependencygroupIdorg.springframework/groupIdartifactIdspring-context/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-beans/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-webmvc/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-jdbc/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-aspects/artifactIdversion${spring.version}/version黑马程序员成都中心编著/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-jms/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-context-support/artifactIdversion${spring.version}/version/dependencydependencygroupIdorg.springframework/groupIdartifactIdspring-test/artifactIdversion${spring.version}/version/dependencydependencygroupIdjunit/groupIdartifactIdjunit/artifactIdversion4.9/version/dependencydependencygroupIdcom.alibaba/groupIdartifactIdfastjson/artifactIdversion1.2.28/version/dependencydependencygroupIdjavassist/groupIdartifactIdjavassist/artifactIdversion3.11.0.GA/version/dependencydependencygroupIdcommons-codec/groupIdartifactIdcommons-codec/artifactIdversion1.10/version/dependencydependencygroupIdcom.github.pagehelper/groupIdartifactIdpagehelper/artifactIdversion${pagehelper.version}/version/dependency!--Mybatis--黑马程序员成都中心编著dependencygroupIdorg.mybatis/groupIdartifactIdmybatis/artifactIdversion${mybatis.version}/version/dependencydependencygroupIdorg.mybatis/groupIdartifactIdmybatis-spring/artifactIdversion${mybatis.spring.version}/version/dependencydependencygroupIdcom.github.miemiedev/groupIdartifactIdmybatis-paginator/artifactIdversion${mybatis.paginator.version}/version/dependency!--MySql--dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdversion${mysql.version}/version/dependency!--连接池--dependencygroupIdcom.alibaba/groupIdartifactIddruid/artifactIdversion${druid.version}/version/dependencydependencygroupIdredis.clients/groupIdartifactIdjedis/artifactIdversion2.8.1/version/dependencydependencygroupIdorg.springframework.data/groupIdartifactIdspring-data-redis/artifactIdversion1.7.2.RELEASE/version/dependencydependencygroupIddom4j/groupIdartifactIddom4j/artifactIdversion1.6.1/version/dependencydependency黑马程序员成都中心编著groupIdxml-apis/groupIdartifactIdxml-apis/artifactIdversion1.4.01/version/dependency/dependencies4.数据访问层利用mybatis逆向工程生成POJO,以及mapper接口和mapper映射文件。该部分自行操作mybatis核心配置文件SqlMapConfig.xml?xmlversion=1.0encoding=UTF-8?!DOCTYPEconfigurationPUBLIC-//mybatis.org//DTDConfig3.0//EN!--com.github.pagehelper为PageHelper类所在包名--plugininterceptor=com.github.pagehelper.PageHelper!--设置数据库类型Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库--propertyname=dialectvalue=mysql//plugin/plugins/configuration数据访问db.propertiesjdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost:3306/miaosha?characterEncoding=utf-8jdbc.username=rootjdbc.password=rootredis配置属性文件redis-config.propertiesproper

1 / 15
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功