SpringBoot整合Redis分布式锁Redisson(单机)

环境:SpringBoot2.x

maven增加配置

 
    


org.springframework.boot
spring-boot-starter-data-redis



org.apache.commons
commons-pool2
2.4.2



org.redisson
redisson
3.6.5


io.netty
netty-all
4.1.25.Final
 

增加yml文件配置

redis:
        database: 0
        host: 127.0.0.1
        port: 6379
        password: '123456'
        jedis:
          pool:
            #最大连接数据库连接数,设 0 为没有限制
            max-active: 8
            #最大等待连接中的数量,设 0 为没有限制
            max-idle: 8
            #最大建立连接等待时间。如果超过此时间将接到异常。设为-1表示无限制。
            max-wait: -1ms
            #最小等待连接中的数量,设 0 为没有限制
            min-idle: 0  

增加RedissonConfig

import org.redisson.Redisson;
import org.redisson.config.Config;
import org.redisson.config.SingleServerConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RedissonConfig {

    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private String port;
    @Value("${spring.redis.password}")
    private String password;


    //添加redisson的bean
    @Bean
    public Redisson redisson() {
        Config config = new Config();
        //此示例是单机的,可以是主从、sentinel、集群等模式
        SingleServerConfig singleServerConfig = config.useSingleServer()
                .setAddress("redis://" + host + ":" + port);
        singleServerConfig.setPassword(password);//设置密码
        return (Redisson) Redisson.create(config);
    }
}

模拟测试控制器

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("")
public class RedisLockController {
    private static String product1Count = "product1Count";//商品1的数量key
    private static String lockKey = "testLockKey";//分布式锁的key
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private Redisson redisson;

    /**
     * 初始化设置商品数量
     *
     * @return
     */
    @RequestMapping("/setProductCount")
    public String setValue() {
        redisTemplate.opsForValue().set(product1Count, "100");
        return "success";
    }

    /**
     * 模拟秒杀抢购,并发多个请求过来,查看是否出现超卖
     *
     * @return
     */
    @RequestMapping("/spike")
    public String spike() {
        String flag = "success";
        RLock lock = redisson.getLock(lockKey);
        try {
            //lock.lockAsync(5 , TimeUnit.SECONDS);
            //lock.lock(5, TimeUnit.SECONDS); //设置60秒自动释放锁  (默认是30秒自动过期)
            Future res = lock.tryLockAsync(100, 5, TimeUnit.SECONDS);
            boolean result = res.get();
            System.out.println("result:" + result);
            if (result) {
                int stock = Integer.parseInt(redisTemplate.opsForValue().get(product1Count).toString());
                if (stock > 0) {
                    redisTemplate.opsForValue().set(product1Count, (stock - 1) + "");
                } else {
                    flag = "fail";
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock(); //释放锁
        }
        return flag;
    }
}

 

模拟秒杀抢购场景,初始化100库存,用jmeter软件工具测试,设置2秒内启动 300线程,循环请求2次,总计600请求,最后查看库存是否为负数,证明分布式锁是否锁住了库存

转载于:https://www.cnblogs.com/zengnansheng/p/11415247.html

你可能感兴趣的:(数据库,java)