标签:stc src ddr rac 执行 pen int rgba end
基于redisson实现分布式锁
一、上官网 https://redisson.org/
二、pom文件引入依赖
<dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>3.11.2</version> </dependency>
三、如何使用,测试程序
package com.example.redissonlock; import lombok.extern.slf4j.Slf4j; import org.junit.Test; import org.junit.runner.RunWith; import org.redisson.Redisson; import org.redisson.api.RLock; import org.redisson.api.RedissonClient; import org.redisson.config.Config; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import java.util.concurrent.TimeUnit; @RunWith(SpringRunner.class) @SpringBootTest @Slf4j public class RedissonLockApplicationTests { @Test public void contextLoads() { } @Test public void testRedissonLock() { Config config = new Config(); config.useSingleServer().setAddress("redis://192.168.73.130:6379"); RedissonClient redisson = Redisson.create(config); RLock rLock = redisson.getLock("order"); try { rLock.lock(30, TimeUnit.SECONDS); log.info("我获得了锁!!!"); Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); }finally { log.info("我释放了锁!!"); rLock.unlock(); } } }
四、Controller测试
package com.example.redissonlock.controller; import lombok.extern.slf4j.Slf4j; import org.redisson.Redisson; import org.redisson.api.RLock; import org.redisson.api.RedissonClient; import org.redisson.config.Config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.concurrent.TimeUnit; @RestController @Slf4j public class RedissonLockController { @Autowired private RedissonClient redisson; @RequestMapping("redissonLock") public String redissonLock() { RLock rLock = redisson.getLock("order"); log.info("我进入了方法!!"); try { rLock.lock(30, TimeUnit.SECONDS); log.info("我获得了锁!!!"); Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); }finally { log.info("我释放了锁!!"); rLock.unlock(); } log.info("方法执行完成!!"); return "方法执行完成!!"; } }
五、 spring和springboot引入redisson
1.上官网
代码:
<dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>3.15.2</version> </dependency>
配置文件 application.properties
spring.redis.host=192.168.73.130
Api 测试
package com.example.redissonlock.controller; import lombok.extern.slf4j.Slf4j; import org.redisson.Redisson; import org.redisson.api.RLock; import org.redisson.api.RedissonClient; import org.redisson.config.Config; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.concurrent.TimeUnit; @RestController @Slf4j public class RedissonLockController { @Autowired private RedissonClient redisson; @RequestMapping("redissonLock") public String redissonLock() { RLock rLock = redisson.getLock("order"); log.info("我进入了方法!!"); try { rLock.lock(30, TimeUnit.SECONDS); log.info("我获得了锁!!!"); Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); }finally { log.info("我释放了锁!!"); rLock.unlock(); } log.info("方法执行完成!!"); return "方法执行完成!!"; } }
标签:stc src ddr rac 执行 pen int rgba end
原文地址:https://www.cnblogs.com/callbin/p/14584859.html