Spring+Redisson实现分布式锁
1、在pom文件引入相关依赖
<spring.version>4.1.5.RELEASE</spring.version> <!--redisson--> <dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>2.10.4</version> </dependency> <!--redis--> <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-redis</artifactId> <version>1.7.1.RELEASE</version> </dependency> |
2、在项目的application.properties增加redis服务配置
redis.host=192.168.29.94 redis.port=6379 redis.password= redis.timeout=100000 redis.maxActive=1000 redis.maxIdle=200 redis.maxWait=2000 redis.poolTestOnBorrow=true redis.database=1 |
3、创建RedissonConfig类
@Component public class RedissonConfig { @Value("${redis.host}") private String host; @Value("${redis.port}") private String port; @Value("${redis.password}") private String password; @Bean public RedissonClient getRedisson(){ Config config = new Config(); config.useSingleServer().setAddress("redis://"+host+":"+port).setPassword(password).setTimeout(10000); config.useSingleServer().setConnectionMinimumIdleSize(10); //添加主从配置 return Redisson.create(config); } } |
4创建一个测试类RedissonTest
@Controller public class RedissonTest { @Autowired private RedissonConfig redissonConfig; @Autowired private JedisUtil jedisUtil; private static final String LOCK_CACHE_KEY="LOCK_DATA_"; private static final String NO_LOCK_CACHE_KEY="NO_LOCK_DATA_"; private static final String LOCK_ID="LOCK_8888"; @ResponseBody @RequestMapping(value = { "getLockData" }, method = { RequestMethod.GET }) public void getUserInfo(String user_id,String method,HttpServletResponse response){ RLock lock = redissonConfig.getRedisson().getLock(LOCK_ID); //加锁 lock.lock(); String cacheV= jedisUtil.get(LOCK_CACHE_KEY); System.out.println(Thread.currentThread().getName()+" :分布式锁:"+cacheV); if(StringUtils.isNotBlank(cacheV)){ String value = ""+(Integer.parseInt(cacheV)-1); jedisUtil.set(LOCK_CACHE_KEY, value, 50000) ; } //释放锁 lock.unlock(); } @ResponseBody @RequestMapping(value = { "getNoLockData" }, method = { RequestMethod.GET }) public void test1(String user_id,String method,HttpServletResponse response){ String cacheV= jedisUtil.get(NO_LOCK_CACHE_KEY); System.out.println(Thread.currentThread().getName()+" :无锁:"+cacheV); if(StringUtils.isNotBlank(cacheV)){ String value = ""+(Integer.parseInt(cacheV)-1); jedisUtil.set(NO_LOCK_CACHE_KEY, value, 50000) ; } } @ResponseBody @RequestMapping(value = { "initCacheValue" }, method = { RequestMethod.GET }) public void initCacheValue(String user_id,String method,HttpServletResponse response){ jedisUtil.set(LOCK_CACHE_KEY, "50", 50000) ; jedisUtil.set(NO_LOCK_CACHE_KEY, "50", 50000) ; System.out.println(":::: "+jedisUtil.get(LOCK_CACHE_KEY)); } |
5、启动服务用户AB工具验证
①在浏览器执行http://192.168.1.19:8080/initCacheValue初始化缓存中的值
②用ab测试工具执行:
ab -n 20 -c 10 http://192.168.1.19:8080/getLockData 结果如下:
qtp1196706114-49 :分布式锁:50 qtp1196706114-60 :分布式锁:49 qtp1196706114-64 :分布式锁:48 qtp1196706114-59 :分布式锁:47 qtp1196706114-66 :分布式锁:46 qtp1196706114-61 :分布式锁:45 qtp1196706114-48 :分布式锁:44 qtp1196706114-63 :分布式锁:43 qtp1196706114-62 :分布式锁:42 qtp1196706114-65 :分布式锁:41 qtp1196706114-61 :分布式锁:40 qtp1196706114-49 :分布式锁:39 qtp1196706114-48 :分布式锁:38 qtp1196706114-190 :分布式锁:37 qtp1196706114-60 :分布式锁:36 qtp1196706114-62 :分布式锁:35 qtp1196706114-59 :分布式锁:34 qtp1196706114-63 :分布式锁:33 qtp1196706114-66 :分布式锁:32 qtp1196706114-64 :分布式锁:31 |
ab -n 20 -c 10 http://192.168.1.19:8080/getNoLockData 结果如下:
qtp1196706114-66 :无锁:50 qtp1196706114-62 :无锁:49 qtp1196706114-60 :无锁:49 qtp1196706114-65 :无锁:49 qtp1196706114-63 :无锁:48 qtp1196706114-59 :无锁:48 qtp1196706114-66 :无锁:48 qtp1196706114-60 :无锁:47 qtp1196706114-190 :无锁:47 qtp1196706114-64 :无锁:47 qtp1196706114-48 :无锁:47 qtp1196706114-62 :无锁:46 qtp1196706114-63 :无锁:46 qtp1196706114-65 :无锁:46 qtp1196706114-59 :无锁:45 qtp1196706114-48 :无锁:44 qtp1196706114-60 :无锁:44 qtp1196706114-399 :无锁:44 qtp1196706114-63 :无锁:43 qtp1196706114-400 :无锁:43 |