java高并发秒杀活动的各种简单实现【springBoot+mybatis+redis+mysql】

最近遇到比较多数据不一致的问题,大多数都是因为并发请求时,没及时处理的原因,故用一个比较有代表性的业务场景【活动秒杀】来模拟一下这个这种高并发所产生的问题。

众所周知,电商系统的秒杀活动是高并发的很好应用场景,这里用的demo模拟的基本框架是springBoot+mybatis+redis+mysql,搭建的过程,我这里就不提了,有需要的可以自行百度。

1.搭好的项目目录:

java高并发秒杀活动的各种简单实现【springBoot+mybatis+redis+mysql】_第1张图片

2.建了一张表(记录商品名称、本次可秒杀的库存量):

java高并发秒杀活动的各种简单实现【springBoot+mybatis+redis+mysql】_第2张图片

加了一条记录(后面每次测试都先手动把库存恢复成100才进行测试)

3.实体:

package com.mybatis.model;

public class MiaoShaGoods {
    private Integer id;

    private String goodsName;

    private Integer goodsSum;

    private Integer version;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getGoodsName() {
        return goodsName;
    }

    public void setGoodsName(String goodsName) {
        this.goodsName = goodsName == null ? null : goodsName.trim();
    }

    public Integer getGoodsSum() {
        return goodsSum;
    }

    public void setGoodsSum(Integer goodsSum) {
        this.goodsSum = goodsSum;
    }

    public Integer getVersion() {
        return version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }
}

 

一、不做任何处理的高并发秒杀实现(错误演示):

1.Controller层,模拟500个并发调用:

package com.mybatis.controller;

import com.mybatis.domain.BaseResponse;
import com.mybatis.domain.MiaoshaRequest;
import com.mybatis.service.MiaoshaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;



@Controller
@RequestMapping(value="/miaoshagoods")
public class MiaoshaController {

    @Autowired
    public MiaoshaService miaoshaService;


    @PostMapping("/miaosha_java_sql_lock")
    public @ResponseBody BaseResponse miaoshaJavaSqlLock(@RequestBody MiaoshaRequest request){

        BaseResponse response=new BaseResponse();
            for(int i=0;i<500;i++){
                Thread thread=new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //不做任何处理的秒杀实现
                        miaoshaService.miaoshaGoods(request,response);              
                    }
                });
                thread.start();
            }
            return response;
     }
}

2.Service层,每个请求进来就去数据库里查剩余的库存量,并且抢购成功后,就减1个库存:

package com.mybatis.service.Impl;

import com.mybatis.dao.MiaoShaGoodsDao;
import com.mybatis.domain.BaseResponse;
import com.mybatis.domain.MiaoshaRequest;
import com.mybatis.model.MiaoShaGoods;
import com.mybatis.service.MiaoshaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


@Service
public class MiashaServiceImpl implements MiaoshaService{

    @Autowired
    MiaoShaGoodsDao miaoShaGoodsDao;

    private Lock lock = new ReentrantLock();

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 不做任何处理的秒杀实现
     * @param request
     * @return
     */
    @Override
    public BaseResponse miaoshaGoods(MiaoshaRequest request, BaseResponse response) {
        int countSuc=0;
        MiaoShaGoods miaoShaGoods=miaoShaGoodsDao.getGoods(request.getGoodNames());
        if(miaoShaGoods.getGoodsSum()>0){
            miaoShaGoods.setGoodsSum(miaoShaGoods.getGoodsSum()-1);
            countSuc= miaoShaGoodsDao.updateMsGoods(miaoShaGoods);
        }
        if(countSuc==1){
            System.out.println("抢到iphoneX,成功!");
        }else{
            System.out.println("抢到iphoneX,失败!");
        }
        return response;
    }
}

3.dao层(mybatis的xml文件):

  

  
    update miao_sha_goods
    set goods_sum = #{goodsSum,jdbcType=INTEGER}
    where goods_name = #{goodsName,jdbcType=VARCHAR}
  

4.测试结果:

java高并发秒杀活动的各种简单实现【springBoot+mybatis+redis+mysql】_第3张图片

java高并发秒杀活动的各种简单实现【springBoot+mybatis+redis+mysql】_第4张图片

截图表明,居然有500个人抢购成功,而且库存量却只减少了12个,这是明显是错误的。

 

二、数据库乐观锁处理的高并发秒杀实现:

1.Controller层,模拟500个并发调用:

package com.mybatis.controller;

import com.mybatis.domain.BaseResponse;
import com.mybatis.domain.MiaoshaRequest;
import com.mybatis.service.MiaoshaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@Controller
@RequestMapping(value="/miaoshagoods")
public class MiaoshaController {

    @Autowired
    public MiaoshaService miaoshaService;

    @PostMapping("/miaosha_java_sql_lock")
    public @ResponseBody BaseResponse miaoshaJavaSqlLock(@RequestBody MiaoshaRequest request){

        BaseResponse response=new BaseResponse();
        for(int i=0;i<500;i++){
            Thread thread=new Thread(new Runnable() {
                @Override
                public void run() {

                    //不做任何处理的秒杀实现
                    //miaoshaService.miaoshaGoods(request,response);
                    //数据库乐观锁秒杀
                    miaoshaService.miaoshaGoods_sql_optimistic_lock(request,response);

                }
            });
            thread.start();
        }
        return response;
    }
}

2.Service层,每个请求进来就去数据库里查剩余的库存量,并且抢购成功后,就减1个库存:

package com.mybatis.service.Impl;

import com.mybatis.dao.MiaoShaGoodsDao;
import com.mybatis.domain.BaseResponse;
import com.mybatis.domain.MiaoshaRequest;
import com.mybatis.model.MiaoShaGoods;
import com.mybatis.service.MiaoshaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


@Service
public class MiashaServiceImpl implements MiaoshaService{

    @Autowired
    MiaoShaGoodsDao miaoShaGoodsDao;

    private Lock lock = new ReentrantLock();

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 不做任何处理的秒杀实现
     * @param request
     * @return
     */
    @Override
    public BaseResponse miaoshaGoods(MiaoshaRequest request, BaseResponse response) {
        int countSuc=0;
        MiaoShaGoods miaoShaGoods=miaoShaGoodsDao.getGoods(request.getGoodNames());
        if(miaoShaGoods.getGoodsSum()>0){
            miaoShaGoods.setGoodsSum(miaoShaGoods.getGoodsSum()-1);
            countSuc= miaoShaGoodsDao.updateMsGoods(miaoShaGoods);
        }
        if(countSuc==1){
            System.out.println("抢到iphoneX,成功!");
        }else{
            System.out.println("抢到iphoneX,失败!");
        }
        return response;
    }
    /**
     * 数据库乐观锁实现秒杀
     * @param request
     * @return
     */
    @Override
    public BaseResponse miaoshaGoods_sql_optimistic_lock(MiaoshaRequest request,BaseResponse response) {
        int countSuc=0;
        MiaoShaGoods miaoShaGoods=miaoShaGoodsDao.getGoods_lgs(request.getGoodNames());
        if(miaoShaGoods.getGoodsSum()>0){
            miaoShaGoods.setGoodsSum(miaoShaGoods.getGoodsSum()-1);
            countSuc= miaoShaGoodsDao.updateMsGoods_lgs(miaoShaGoods);
        }
        if(countSuc==1){
            System.out.println("抢到iphoneX,成功!");

            //做出相应的逻辑(记录抢购成功的用户名什么的....)

        }else{
            System.out.println("抢到iphoneX,失败!");

            //重试或者返回友好的提示什么的....
        }
        return response;
    }
}

3.dao层(mybatis的xml文件)[在SQL层面改为数据库乐观锁]: 



  
    update miao_sha_goods
    set goods_sum = #{goodsSum,jdbcType=INTEGER},version=version+1
    where goods_name = #{goodsName,jdbcType=VARCHAR} and version = #{version,jdbcType=VARCHAR}
  

4.测试结果:

java高并发秒杀活动的各种简单实现【springBoot+mybatis+redis+mysql】_第5张图片

截图表明,总共有500个人抢,有29个人抢购成功,而且库存量减少了29个,这保证了库存的正确性。但却会有抢购不成功的请求,需要我们后续去处理。

 

三、数据库悲观锁处理的高并发秒杀实现:

1.Controller层,模拟500个并发调用:

package com.mybatis.controller;

import com.mybatis.domain.BaseResponse;
import com.mybatis.domain.MiaoshaRequest;
import com.mybatis.service.MiaoshaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@Controller
@RequestMapping(value="/miaoshagoods")
public class MiaoshaController {

    @Autowired
    public MiaoshaService miaoshaService;

    @PostMapping("/miaosha_java_sql_lock")
    public @ResponseBody BaseResponse miaoshaJavaSqlLock(@RequestBody MiaoshaRequest request){

        BaseResponse response=new BaseResponse();
        for(int i=0;i<500;i++){
            Thread thread=new Thread(new Runnable() {
                @Override
                public void run() {

                    //不做任何处理的秒杀实现
                    //miaoshaService.miaoshaGoods(request,response);
                    //数据库乐观锁秒杀
                    //miaoshaService.miaoshaGoods_sql_optimistic_lock(request,response);
                    //数据库悲观锁秒杀
                    miaoshaService.miaoshaGoods_sql_pessimistic_lock(request,response);

                }
            });
            thread.start();
        }
        return response;
    }
}

2.Service层,每个请求进来就去数据库里查剩余的库存量,并且抢购成功后,就减1个库存(查询和更新必须在同一个事务):

package com.mybatis.service.Impl;

import com.mybatis.dao.MiaoShaGoodsDao;
import com.mybatis.domain.BaseResponse;
import com.mybatis.domain.MiaoshaRequest;
import com.mybatis.model.MiaoShaGoods;
import com.mybatis.service.MiaoshaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


@Service
public class MiashaServiceImpl implements MiaoshaService{

    @Autowired
    MiaoShaGoodsDao miaoShaGoodsDao;

    private Lock lock = new ReentrantLock();

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 不做任何处理的秒杀实现
     * @param request
     * @return
     */
    @Override
    public BaseResponse miaoshaGoods(MiaoshaRequest request, BaseResponse response) {
        int countSuc=0;
        MiaoShaGoods miaoShaGoods=miaoShaGoodsDao.getGoods(request.getGoodNames());
        if(miaoShaGoods.getGoodsSum()>0){
            miaoShaGoods.setGoodsSum(miaoShaGoods.getGoodsSum()-1);
            countSuc= miaoShaGoodsDao.updateMsGoods(miaoShaGoods);
        }
        if(countSuc==1){
            System.out.println("抢到iphoneX,成功!");
        }else{
            System.out.println("抢到iphoneX,失败!");
        }
        return response;
    }
    /**
     * 数据库乐观锁实现秒杀
     * @param request
     * @return
     */
    @Override
    public BaseResponse miaoshaGoods_sql_optimistic_lock(MiaoshaRequest request,BaseResponse response) {
        int countSuc=0;
        MiaoShaGoods miaoShaGoods=miaoShaGoodsDao.getGoods_lgs(request.getGoodNames());
        if(miaoShaGoods.getGoodsSum()>0){
            miaoShaGoods.setGoodsSum(miaoShaGoods.getGoodsSum()-1);
            countSuc= miaoShaGoodsDao.updateMsGoods_lgs(miaoShaGoods);
        }
        if(countSuc==1){
            System.out.println("抢到iphoneX,成功!");

            //做出相应的逻辑(记录抢购成功的用户名什么的....)

        }else{
            System.out.println("抢到iphoneX,失败!");

            //重试或者返回友好的提示什么的....
        }
        return response;
    }
    /**
     * 数据库悲观锁实现秒杀
     * @param request
     * @return
     */
    @Override
    @Transactional
    public BaseResponse miaoshaGoods_sql_pessimistic_lock(MiaoshaRequest request,BaseResponse response) {
        int countSuc=0;
        MiaoShaGoods miaoShaGoods=miaoShaGoodsDao.getGoods_bgs(request.getGoodNames());
        if(miaoShaGoods.getGoodsSum()>0){
            miaoShaGoods.setGoodsSum(miaoShaGoods.getGoodsSum()-1);
            countSuc= miaoShaGoodsDao.updateMsGoods_bgs(miaoShaGoods);
        }
        if(countSuc==1){
            System.out.println("抢到iphoneX,成功!");
        }else{
            System.out.println("抢到iphoneX,失败!");
        }
        return response;
    }
}

 3.dao层(mybatis的xml文件)[在SQL层面改为数据库悲观锁]: 

  

  
    update miao_sha_goods
    set goods_sum = #{goodsSum,jdbcType=INTEGER}
    where goods_name = #{goodsName,jdbcType=VARCHAR}
  

4.测试结果:

java高并发秒杀活动的各种简单实现【springBoot+mybatis+redis+mysql】_第6张图片

 截图表明,总共有500个人抢,有100个人抢购成功,而且库存量减少了100个,这保证了库存的正确性。

 

四、java线程同步锁处理的高并发秒杀实现:

1.Controller层,模拟500个并发调用:

package com.mybatis.controller;

import com.mybatis.domain.BaseResponse;
import com.mybatis.domain.MiaoshaRequest;
import com.mybatis.service.MiaoshaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@Controller
@RequestMapping(value="/miaoshagoods")
public class MiaoshaController {

    @Autowired
    public MiaoshaService miaoshaService;

    @PostMapping("/miaosha_java_sql_lock")
    public @ResponseBody BaseResponse miaoshaJavaSqlLock(@RequestBody MiaoshaRequest request){

        BaseResponse response=new BaseResponse();
        for(int i=0;i<500;i++){
            Thread thread=new Thread(new Runnable() {
                @Override
                public void run() {

                    //不做任何处理的秒杀实现
                    //miaoshaService.miaoshaGoods(request,response);
                    //数据库乐观锁秒杀
                    //miaoshaService.miaoshaGoods_sql_optimistic_lock(request,response);
                    //数据库悲观锁秒杀
                    //miaoshaService.miaoshaGoods_sql_pessimistic_lock(request,response);
                    //java线程同步锁秒杀
                    miaoshaService.miaoshaGoods_java_synchronized_lock(request,response);

                }
            });
            thread.start();
        }
        return response;
    }
}

2.Service层,每个请求进来就去数据库里查剩余的库存量,并且抢购成功后,就减1个库存:

package com.mybatis.service.Impl;

import com.mybatis.dao.MiaoShaGoodsDao;
import com.mybatis.domain.BaseResponse;
import com.mybatis.domain.MiaoshaRequest;
import com.mybatis.model.MiaoShaGoods;
import com.mybatis.service.MiaoshaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


@Service
public class MiashaServiceImpl implements MiaoshaService{

    @Autowired
    MiaoShaGoodsDao miaoShaGoodsDao;

    private Lock lock = new ReentrantLock();

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 不做任何处理的秒杀实现
     * @param request
     * @return
     */
    @Override
    public BaseResponse miaoshaGoods(MiaoshaRequest request, BaseResponse response) {
        int countSuc=0;
        MiaoShaGoods miaoShaGoods=miaoShaGoodsDao.getGoods(request.getGoodNames());
        if(miaoShaGoods.getGoodsSum()>0){
            miaoShaGoods.setGoodsSum(miaoShaGoods.getGoodsSum()-1);
            countSuc= miaoShaGoodsDao.updateMsGoods(miaoShaGoods);
        }
        if(countSuc==1){
            System.out.println("抢到iphoneX,成功!");
        }else{
            System.out.println("抢到iphoneX,失败!");
        }
        return response;
    }
    /**
     * 数据库乐观锁实现秒杀
     * @param request
     * @return
     */
    @Override
    public BaseResponse miaoshaGoods_sql_optimistic_lock(MiaoshaRequest request,BaseResponse response) {
        int countSuc=0;
        MiaoShaGoods miaoShaGoods=miaoShaGoodsDao.getGoods_lgs(request.getGoodNames());
        if(miaoShaGoods.getGoodsSum()>0){
            miaoShaGoods.setGoodsSum(miaoShaGoods.getGoodsSum()-1);
            countSuc= miaoShaGoodsDao.updateMsGoods_lgs(miaoShaGoods);
        }
        if(countSuc==1){
            System.out.println("抢到iphoneX,成功!");

            //做出相应的逻辑(记录抢购成功的用户名什么的....)

        }else{
            System.out.println("抢到iphoneX,失败!");

            //重试或者返回友好的提示什么的....
        }
        return response;
    }
    /**
     * 数据库悲观锁实现秒杀
     * @param request
     * @return
     */
    @Override
    @Transactional
    public BaseResponse miaoshaGoods_sql_pessimistic_lock(MiaoshaRequest request,BaseResponse response) {
        int countSuc=0;
        MiaoShaGoods miaoShaGoods=miaoShaGoodsDao.getGoods_bgs(request.getGoodNames());
        if(miaoShaGoods.getGoodsSum()>0){
            miaoShaGoods.setGoodsSum(miaoShaGoods.getGoodsSum()-1);
            countSuc= miaoShaGoodsDao.updateMsGoods_bgs(miaoShaGoods);
        }
        if(countSuc==1){
            System.out.println("抢到iphoneX,成功!");
        }else{
            System.out.println("抢到iphoneX,失败!");
        }
        return response;
    }
    /**
     * java同步锁实现秒杀
     * @param request
     * @return
     */
    @Override
    public BaseResponse miaoshaGoods_java_synchronized_lock(MiaoshaRequest request,BaseResponse response) {
        synchronized(this){
            int countSuc=0;
            MiaoShaGoods miaoShaGoods=miaoShaGoodsDao.getGoods(request.getGoodNames());
            if(miaoShaGoods.getGoodsSum()>0){
                miaoShaGoods.setGoodsSum(miaoShaGoods.getGoodsSum()-1);
                countSuc= miaoShaGoodsDao.updateMsGoods(miaoShaGoods);
            }
            if(countSuc==1){
                System.out.println("抢到iphoneX,成功!");
            }else{
                System.out.println("抢到iphoneX,失败!");
            }
        }
        return response;
    }
}

3.dao层(mybatis的xml文件):

  

  
    update miao_sha_goods
    set goods_sum = #{goodsSum,jdbcType=INTEGER}
    where goods_name = #{goodsName,jdbcType=VARCHAR}
  

4.测试结果:

java高并发秒杀活动的各种简单实现【springBoot+mybatis+redis+mysql】_第7张图片

截图表明,总共有500个人抢,有100个人抢购成功,而且库存量减少了100个,这保证了库存的正确性。

 

五、java线程可重入锁处理的高并发秒杀实现:

1.Controller层,模拟500个并发调用:

package com.mybatis.controller;

import com.mybatis.domain.BaseResponse;
import com.mybatis.domain.MiaoshaRequest;
import com.mybatis.service.MiaoshaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


@Controller
@RequestMapping(value="/miaoshagoods")
public class MiaoshaController {

    @Autowired
    public MiaoshaService miaoshaService;

    @PostMapping("/miaosha_java_sql_lock")
    public @ResponseBody BaseResponse miaoshaJavaSqlLock(@RequestBody MiaoshaRequest request){

        BaseResponse response=new BaseResponse();
        for(int i=0;i<500;i++){
            Thread thread=new Thread(new Runnable() {
                @Override
                public void run() {

                    //不做任何处理的秒杀实现
                    //miaoshaService.miaoshaGoods(request,response);
                    //数据库乐观锁秒杀
                    //miaoshaService.miaoshaGoods_sql_optimistic_lock(request,response);
                    //数据库悲观锁秒杀
                    //miaoshaService.miaoshaGoods_sql_pessimistic_lock(request,response);
                    //java线程同步锁秒杀
                  //miaoshaService.miaoshaGoods_java_synchronized_lock(request,response);
                    //java线程可重入锁秒杀
                    miaoshaService.miaoshaGoods_java_reentrant_lock(request,response);

                }
            });
            thread.start();
        }
        return response;
    }
}

2.Service层,每个请求进来就去数据库里查剩余的库存量,并且抢购成功后,就减1个库存: 

package com.mybatis.service.Impl;

import com.mybatis.dao.MiaoShaGoodsDao;
import com.mybatis.domain.BaseResponse;
import com.mybatis.domain.MiaoshaRequest;
import com.mybatis.model.MiaoShaGoods;
import com.mybatis.service.MiaoshaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


@Service
public class MiashaServiceImpl implements MiaoshaService{

    @Autowired
    MiaoShaGoodsDao miaoShaGoodsDao;

    private Lock lock = new ReentrantLock();

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 不做任何处理的秒杀实现
     * @param request
     * @return
     */
    @Override
    public BaseResponse miaoshaGoods(MiaoshaRequest request, BaseResponse response) {
        int countSuc=0;
        MiaoShaGoods miaoShaGoods=miaoShaGoodsDao.getGoods(request.getGoodNames());
        if(miaoShaGoods.getGoodsSum()>0){
            miaoShaGoods.setGoodsSum(miaoShaGoods.getGoodsSum()-1);
            countSuc= miaoShaGoodsDao.updateMsGoods(miaoShaGoods);
        }
        if(countSuc==1){
            System.out.println("抢到iphoneX,成功!");
        }else{
            System.out.println("抢到iphoneX,失败!");
        }
        return response;
    }
    /**
     * 数据库乐观锁实现秒杀
     * @param request
     * @return
     */
    @Override
    public BaseResponse miaoshaGoods_sql_optimistic_lock(MiaoshaRequest request,BaseResponse response) {
        int countSuc=0;
        MiaoShaGoods miaoShaGoods=miaoShaGoodsDao.getGoods_lgs(request.getGoodNames());
        if(miaoShaGoods.getGoodsSum()>0){
            miaoShaGoods.setGoodsSum(miaoShaGoods.getGoodsSum()-1);
            countSuc= miaoShaGoodsDao.updateMsGoods_lgs(miaoShaGoods);
        }
        if(countSuc==1){
            System.out.println("抢到iphoneX,成功!");

            //做出相应的逻辑(记录抢购成功的用户名什么的....)

        }else{
            System.out.println("抢到iphoneX,失败!");

            //重试或者返回友好的提示什么的....
        }
        return response;
    }
    /**
     * 数据库悲观锁实现秒杀
     * @param request
     * @return
     */
    @Override
    @Transactional
    public BaseResponse miaoshaGoods_sql_pessimistic_lock(MiaoshaRequest request,BaseResponse response) {
        int countSuc=0;
        MiaoShaGoods miaoShaGoods=miaoShaGoodsDao.getGoods_bgs(request.getGoodNames());
        if(miaoShaGoods.getGoodsSum()>0){
            miaoShaGoods.setGoodsSum(miaoShaGoods.getGoodsSum()-1);
            countSuc= miaoShaGoodsDao.updateMsGoods_bgs(miaoShaGoods);
        }
        if(countSuc==1){
            System.out.println("抢到iphoneX,成功!");
        }else{
            System.out.println("抢到iphoneX,失败!");
        }
        return response;
    }
    /**
     * java同步锁实现秒杀
     * @param request
     * @return
     */
    @Override
    public BaseResponse miaoshaGoods_java_synchronized_lock(MiaoshaRequest request,BaseResponse response) {
        synchronized(this){
            int countSuc=0;
            MiaoShaGoods miaoShaGoods=miaoShaGoodsDao.getGoods(request.getGoodNames());
            if(miaoShaGoods.getGoodsSum()>0){
                miaoShaGoods.setGoodsSum(miaoShaGoods.getGoodsSum()-1);
                countSuc= miaoShaGoodsDao.updateMsGoods(miaoShaGoods);
            }
            if(countSuc==1){
                System.out.println("抢到iphoneX,成功!");
            }else{
                System.out.println("抢到iphoneX,失败!");
            }
        }
        return response;
    }
    /**
     * java可重入锁实现秒杀
     * @param request
     * @return
     */
    @Override
    public BaseResponse miaoshaGoods_java_reentrant_lock(MiaoshaRequest request,BaseResponse response) {

        lock.lock();
        int countSuc=0;
        MiaoShaGoods miaoShaGoods=miaoShaGoodsDao.getGoods(request.getGoodNames());
        if(miaoShaGoods.getGoodsSum()>0){
            miaoShaGoods.setGoodsSum(miaoShaGoods.getGoodsSum()-1);
            countSuc= miaoShaGoodsDao.updateMsGoods(miaoShaGoods);
        }
        if(countSuc==1){
            System.out.println(request.getGoodNames()+"抢到iphoneX,成功!");
        }else{
            System.out.println("抢到iphoneX,失败!");
        }
        lock.unlock();
        return response;
    }
}

3.dao层(mybatis的xml文件): 

  

  
    update miao_sha_goods
    set goods_sum = #{goodsSum,jdbcType=INTEGER}
    where goods_name = #{goodsName,jdbcType=VARCHAR}
  

4.测试结果:

java高并发秒杀活动的各种简单实现【springBoot+mybatis+redis+mysql】_第8张图片

 截图表明,总共有500个人抢,有100个人抢购成功,而且库存量减少了100个,这保证了库存的正确性。

 

六、redis单线程处理的高并发秒杀实现(推荐):

1.Controller层,模拟500个并发调用:

package com.mybatis.controller;

import com.mybatis.domain.BaseResponse;
import com.mybatis.domain.MiaoshaRequest;
import com.mybatis.service.MiaoshaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;



@Controller
@RequestMapping(value="/miaoshagoods")
public class MiaoshaController {

    @Autowired
    public MiaoshaService miaoshaService;

    @Autowired
    private RedisTemplate redisTemplate;


    @PostMapping("/miaosha_java_sql_lock")
    public @ResponseBody BaseResponse miaoshaJavaSqlLock(@RequestBody MiaoshaRequest request){

        BaseResponse response=new BaseResponse();
        for(int i=0;i<500;i++){
            Thread thread=new Thread(new Runnable() {
                @Override
                public void run() {

                    //不做任何处理的秒杀实现
                    //miaoshaService.miaoshaGoods(request,response);
                    //数据库乐观锁秒杀
                    //miaoshaService.miaoshaGoods_sql_optimistic_lock(request,response);
                    //数据库悲观锁秒杀
                    //miaoshaService.miaoshaGoods_sql_pessimistic_lock(request,response);
                    //java线程同步锁秒杀
                    //miaoshaService.miaoshaGoods_java_synchronized_lock(request,response);
                    //java线程可重入锁秒杀
                    miaoshaService.miaoshaGoods_java_reentrant_lock(request,response);
                }
            });
            thread.start();
        }
        return response;
    }

   @PostMapping("/miaosha_redis_lock")
    public @ResponseBody BaseResponse miaoshaRedisLock(@RequestBody MiaoshaRequest request){
        BaseResponse response=new BaseResponse();
        //初始化商品数量
        Integer goodsSum=miaoshaService.getGoodsSum(request);
        redisTemplate.opsForValue().set(request.getGoodNames()+":goodsSum",goodsSum+"");
        System.out.println("总共的库存量:"+goodsSum);

        for(int i=0;i<500;i++){
            Thread thread=new Thread(new Runnable() {
                @Override
                public void run() {
                    miaoshaService.miaoshaGoods_redis(request,response);
                }
            });
            thread.start();
        }
        return response;
    }
}

2.Service层,先把参与秒杀的总库存量写入redis里,然后再利用redis的增量方法去递减:

package com.mybatis.service.Impl;

import com.mybatis.dao.MiaoShaGoodsDao;
import com.mybatis.domain.BaseResponse;
import com.mybatis.domain.MiaoshaRequest;
import com.mybatis.model.MiaoShaGoods;
import com.mybatis.service.MiaoshaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


@Service
public class MiashaServiceImpl implements MiaoshaService{

    @Autowired
    MiaoShaGoodsDao miaoShaGoodsDao;

    private Lock lock = new ReentrantLock();

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 不做任何处理的秒杀实现
     * @param request
     * @return
     */
    @Override
    public BaseResponse miaoshaGoods(MiaoshaRequest request, BaseResponse response) {
        int countSuc=0;
        MiaoShaGoods miaoShaGoods=miaoShaGoodsDao.getGoods(request.getGoodNames());
        if(miaoShaGoods.getGoodsSum()>0){
            miaoShaGoods.setGoodsSum(miaoShaGoods.getGoodsSum()-1);
            countSuc= miaoShaGoodsDao.updateMsGoods(miaoShaGoods);
        }
        if(countSuc==1){
            System.out.println("抢到iphoneX,成功!");
        }else{
            System.out.println("抢到iphoneX,失败!");
        }
        return response;
    }
    /**
     * 数据库乐观锁实现秒杀
     * @param request
     * @return
     */
    @Override
    public BaseResponse miaoshaGoods_sql_optimistic_lock(MiaoshaRequest request,BaseResponse response) {
        int countSuc=0;
        MiaoShaGoods miaoShaGoods=miaoShaGoodsDao.getGoods_lgs(request.getGoodNames());
        if(miaoShaGoods.getGoodsSum()>0){
            miaoShaGoods.setGoodsSum(miaoShaGoods.getGoodsSum()-1);
            countSuc= miaoShaGoodsDao.updateMsGoods_lgs(miaoShaGoods);
        }
        if(countSuc==1){
            System.out.println("抢到iphoneX,成功!");

            //做出相应的逻辑(记录抢购成功的用户名什么的....)

        }else{
            System.out.println("抢到iphoneX,失败!");

            //重试或者返回友好的提示什么的....
        }
        return response;
    }
    /**
     * 数据库悲观锁实现秒杀
     * @param request
     * @return
     */
    @Override
    @Transactional
    public BaseResponse miaoshaGoods_sql_pessimistic_lock(MiaoshaRequest request,BaseResponse response) {
        int countSuc=0;
        MiaoShaGoods miaoShaGoods=miaoShaGoodsDao.getGoods_bgs(request.getGoodNames());
        if(miaoShaGoods.getGoodsSum()>0){
            miaoShaGoods.setGoodsSum(miaoShaGoods.getGoodsSum()-1);
            countSuc= miaoShaGoodsDao.updateMsGoods_bgs(miaoShaGoods);
        }
        if(countSuc==1){
            System.out.println("抢到iphoneX,成功!");
        }else{
            System.out.println("抢到iphoneX,失败!");
        }
        return response;
    }
    /**
     * java同步锁实现秒杀
     * @param request
     * @return
     */
    @Override
    public BaseResponse miaoshaGoods_java_synchronized_lock(MiaoshaRequest request,BaseResponse response) {
        synchronized(this){
            int countSuc=0;
            MiaoShaGoods miaoShaGoods=miaoShaGoodsDao.getGoods(request.getGoodNames());
            if(miaoShaGoods.getGoodsSum()>0){
                miaoShaGoods.setGoodsSum(miaoShaGoods.getGoodsSum()-1);
                countSuc= miaoShaGoodsDao.updateMsGoods(miaoShaGoods);
            }
            if(countSuc==1){
                System.out.println("抢到iphoneX,成功!");
            }else{
                System.out.println("抢到iphoneX,失败!");
            }
        }
        return response;
    }
    /**
     * java可重入锁实现秒杀
     * @param request
     * @return
     */
    @Override
    public BaseResponse miaoshaGoods_java_reentrant_lock(MiaoshaRequest request,BaseResponse response) {

        lock.lock();
        int countSuc=0;
        MiaoShaGoods miaoShaGoods=miaoShaGoodsDao.getGoods(request.getGoodNames());
        if(miaoShaGoods.getGoodsSum()>0){
            miaoShaGoods.setGoodsSum(miaoShaGoods.getGoodsSum()-1);
            countSuc= miaoShaGoodsDao.updateMsGoods(miaoShaGoods);
        }
        if(countSuc==1){
            System.out.println(request.getGoodNames()+"抢到iphoneX,成功!");
        }else{
            System.out.println("抢到iphoneX,失败!");
        }
        lock.unlock();
        return response;
    }

    @Override
    public Integer getGoodsSum(MiaoshaRequest request) {
        MiaoShaGoods miaoShaGoods=miaoShaGoodsDao.getGoods(request.getGoodNames());
        return miaoShaGoods.getGoodsSum();
    }
    /**
     * redis实现秒杀
     * @param response
     * @return
     */
    @Override
    public BaseResponse miaoshaGoods_redis(MiaoshaRequest request,BaseResponse response) {

        //增量计算剩余库存(利用redis的单线程特性)
        double goodsSurplusSum=redisTemplate.opsForValue().increment(request.getGoodNames()+":goodsSum",-1);
        if(goodsSurplusSum>=0){
            System.out.println("抢到iphoneX,成功!还剩下:"+goodsSurplusSum);
        }else {
            System.out.println("抢到iphoneX,失败!");
        }
        return response;
    }
}

3.测试结果:

java高并发秒杀活动的各种简单实现【springBoot+mybatis+redis+mysql】_第9张图片

java高并发秒杀活动的各种简单实现【springBoot+mybatis+redis+mysql】_第10张图片java高并发秒杀活动的各种简单实现【springBoot+mybatis+redis+mysql】_第11张图片

 java高并发秒杀活动的各种简单实现【springBoot+mybatis+redis+mysql】_第12张图片

截图表明,总共有500个人抢,有100个人抢购成功,而且库存量减少了100个,这保证了库存的正确性,抢后redis显示是-400个,说明有500个人进来了,有400个人抢不到。

 

综上所述,要控制库存量,一般要用锁机制。但是一般加锁的话会比较影响性能(只能用于单服务),推荐大家使用redis自带的线程安全属性来实现(可实现分布式锁)。

如有错漏,望各位指出!

 

你可能感兴趣的:(高并发处理)