一般提及到Redis的分布式锁我们更多的使用的是Redisson的分布式锁,Redis的官方也是建议我们这样去做的。Redisson点我可以直接跳转到Redisson的官方文档。
1、引入Maven依赖
<!-- https://mvnrepository.com/artifact/org.redisson/redisson -->
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.12.2</version>
</dependency>
2、配置redis信息
单机模式
spring: redis: port: 6379 host: 127.0.0.1 password: ****** database: 0 timeout: 2000
集成模式:
spring: redis: cluster: nodes: "192.168.1.11:7000,192.168.1.12:7000,192.168.1.12:7001" password: ****** lettuce: pool: max-active: 1500 max-wait: 5000 max-idle: 500 min-idle: 100 shutdown-timeout: 1000 timeout: 60000
3、增加一个RedisConfigProperties用于读取配置文件信息
单机模式
/** * redisson 配置类 * Created on 2018/6/19 */ @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; @Bean public RedissonClient getRedisson(){ Config config = new Config(); config.useSingleServer().setAddress("redis://" + host + ":" + port).setPassword(password); //添加主从配置 // config.useMasterSlaveServers().setMasterAddress("").setPassword("").addSlaveAddress(new String[]{"",""}); return Redisson.create(config); } }
集成模式
@Component @ConfigurationProperties(prefix = "spring.redis") public class RedisConfigProperties { private String password; private cluster cluster; public static class cluster { private List<String> nodes; public List<String> getNodes() { return nodes; } public void setNodes(List<String> nodes) { this.nodes = nodes; } } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public RedisConfigProperties.cluster getCluster() { return cluster; } public void setCluster(RedisConfigProperties.cluster cluster) { this.cluster = cluster; } }
@Configuration public class RedissonConfig { @Autowired private RedisConfigProperties redisConfigProperties; @Bean public RedissonClient redissonClient() { //redisson版本是3.5,集群的ip前面要加上“redis://”,不然会报错,3.2版本可不加 List<String> clusterNodes = new ArrayList<>(); for (int i = 0; i < redisConfigProperties.getCluster().getNodes().size(); i++) { clusterNodes.add("redis://" + redisConfigProperties.getCluster().getNodes().get(i)); } Config config = new Config(); // 添加集群地址 ClusterServersConfig clusterServersConfig = config.useClusterServers().addNodeAddress(clusterNodes.toArray(new String[clusterNodes.size()])); // 设置密码 clusterServersConfig.setPassword(redisConfigProperties.getPassword()); RedissonClient redissonClient = Redisson.create(config); return redissonClient; } }
4、测试调用
@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<Boolean> 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; } }
其实Redisson还有其他好多的方法来解决现在互联网中的好多问题,大家如果想了解更多的东西,可以去Redisson官网。
Redisson官网:https://redisson.org/
参考:
https://www.cnblogs.com/milicool/p/9201271.html
https://www.cnblogs.com/zengnansheng/p/11426996.html