秒杀总结3

package com.yf.service;

import com.yf.dao.GoodUserDao;
import com.yf.dao.GoodsDao;
import com.yf.pojo.GoodUser;
import com.yf.util.RedisPoolUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;

import java.util.List;

@Service
public class GoodUserService {
    @Autowired
    private GoodUserDao goodUserDao;

    @Autowired
    private GoodsDao goodsDao;

    public static Logger logger = Logger.getLogger(GoodUserService.class);


    public List<GoodUser> getGoods() {
        List<GoodUser> goods = goodUserDao.getAllGoodUser();
        return goods;
    }

    //保存
    public void saveGoods(GoodUser goodUser){
        goodUserDao.saveGoods(goodUser);
    }

    //1 判断条件
    public boolean isKill(String userName){
        Jedis jedis=RedisPoolUtil.getConn();
        //限制用户操作频率 10秒内
        String value=jedis.set(userName,"","NX","EX",10);
        if(!"OK".equals(value)){
            logger.warn("此用户重复请求");
            jedis.close();
            return false;
        }
        String token=jedis.lpop("token_list1");
        logger.info(userName+"拿到令牌");
        if(null==token||"".equals(token)){
            logger.info("令牌发完");
            jedis.close();
            return false;
        }
        return true;
    }

    //秒杀1.减少数量 2.插入用户记录
    public void killGood(String userName){
        if(isKill(userName)){
            GoodUser goodUser=new GoodUser();
            goodUser.setName(userName);
            goodUser.setGoodsName("phone");
            int i=goodsDao.minusNum("phone");
            if(i>0){
                goodUserDao.saveGoods(goodUser);
                logger.info(userName+" 秒杀成功");
            }
        }


    }

}

package com.yf.service;

import com.yf.pojo.GoodUser;
import com.yf.testMain;
import com.yf.util.RedisPoolUtil;
import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.concurrent.CountDownLatch;


@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = testMain.class)
public class GoodsServiceTest {
    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodUserService goodUserService;

    public static Logger logger = Logger.getLogger(GoodsServiceTest.class);

    private static final int threadNum=1000;

    private CountDownLatch cdl=new CountDownLatch(threadNum);

    @Test
    public void testGet() {
        List<GoodUser> goods=goodUserService.getGoods();
        for (GoodUser gu:goods){
            System.out.println(gu.getGoodsName());
        }
    }

    @Test
    public void testSave() {
       GoodUser goodUser=new GoodUser();
       goodUser.setName("yf");
       goodUser.setGoodsName("bike");
       goodUserService.saveGoods(goodUser);
    }

    @Test
    public void testMinus() {
        goodsService.minusNum("bike");
    }

    @Before
    public void start(){
        RedisPoolUtil.initialPool();
        Jedis jedis=RedisPoolUtil.getConn();
        for(int i=0;i<20;i++){
            jedis.lpush("token_list1",""+i);
        }
        jedis.close();
        logger.info("令牌预先生成完毕");
        logger.info("开始测试");
    }


    @Test
    public void testKill() {
        try {
            Thread[] threads=new Thread[threadNum];
            for(int i=0;i<threadNum;i++){
                logger.info("循环"+i+"次");
                Thread thread=new Thread(new userRequest("yf"+i));
                threads[i]=thread;
                thread.start();
                cdl.countDown();
            }
            for(Thread thread:threads){
                thread.join();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            RedisPoolUtil.closePool();
        }


    }

    class userRequest implements Runnable{
        String userName;
        public userRequest(String userName) {
            this.userName = userName;
        }
        @Override
        public void run() {
            try {
                cdl.await();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            goodUserService.killGood(userName);
        }
    }
}

3.令牌机制限定连接数据库的数量

String token=jedis.lpop("token_list1");
        logger.info(userName+"拿到令牌");
        if(null==token||"".equals(token)){
            logger.info("令牌发完");
            jedis.close();
            return false;
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值