Redisson
方式1
引入依赖
<dependency> <groupId>org.redisson</groupId> <artifactId>redisson</artifactId> <version>3.15.1</version> </dependency>
配置文件application.properties
自定义配置
redisson.url=redis://127.0.0.1:6379
配置类
import org.redisson.Redisson; import org.redisson.api.RedissonClient; import org.redisson.config.Config; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RedissonConfig { @Value("${redisson.url}") private String redisUrl; @Bean public RedissonClient getRedissonClient(){ Config config = new Config(); config.useSingleServer().setAddress(redisUrl); return Redisson.create(config); } }
使用
import org.redisson.api.RLock; import org.redisson.api.RedissonClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class DemoService { @Autowired private RedissonClient redissonClient; public void doSomething(){ RLock lock = redissonClient.getLock("lock_key"); try { lock.lock(); // TODO 业务逻辑 } finally { lock.unlock(); } } }
方式2:
依赖
<dependency> <groupId>org.redisson</groupId> <artifactId>redisson-spring-boot-starter</artifactId> <version>3.15.1</version> </dependency>
配置文件application.properties(这种方式完全兼容SpringBoot配置)
spring.redis.host=127.0.0.1
spring.redis.port=6379
使用
import org.redisson.api.RLock; import org.redisson.api.RedissonClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class DemoService { @Autowired private RedissonClient redissonClient; public void doSomething(){ RLock lock = redissonClient.getLock("lock_key"); try { lock.lock(); // TODO 业务逻辑 } finally { lock.unlock(); } } }
spring-integration-redis
依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-integration</artifactId> </dependency> <dependency> <groupId>org.springframework.integration</groupId> <artifactId>spring-integration-redis</artifactId> </dependency>
配置文件application.properties
spring.redis.host=127.0.0.1
spring.redis.port=6379
配置类
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.integration.redis.util.RedisLockRegistry; @Configuration public class RedisLockConfig { @Bean public RedisLockRegistry redisLockRegistry(RedisConnectionFactory redisConnectionFactory) { return new RedisLockRegistry(redisConnectionFactory, "registry_key"); } }
使用
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.integration.redis.util.RedisLockRegistry; import org.springframework.stereotype.Service; import java.util.concurrent.locks.Lock; @Service public class DemoService { @Autowired private RedisLockRegistry redisLockRegistry; public void doSomething(){ Lock lock = redisLockRegistry.obtain("lock_key"); try { lock.lock(); // TODO 业务逻辑 } finally { lock.unlock(); } } }
追加方式:两种方式测试方便合在一块,实际独立的
org.redisson redisson 3.15.1
import org.redisson.Redisson; import org.redisson.api.RedissonClient; import org.redisson.config.Config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class RedissonConfig { @Bean public RedissonClient getRedissonClient() { Config config = new Config(); config.useSingleServer().setAddress("redis://127.0.0.1:6379"); return Redisson.create(config); } }
@Slf4j @RunWith(SpringRunner.class) @SpringBootTest(classes = TaskApplication.class) public class ExceptionTest { //way of first //redisson-spring-boot-starter-3.13.6.jar @Autowired private RedissonClient redissonClient; // //way of second // //lock4j-redisson-spring-boot-starter-2.2.2.jar // @Resource // private LockTemplate lockTemplate; //way of first @Test public void redisTest() { RLock lock = redissonClient.getLock("this_is_key"); try { lock.lock(); //模拟业务 Thread.sleep(5000); } catch (Exception e) { log.warn("e:{}", e); } finally { lock.unlock(); } } // //way of second // @Test // public void secondTest(){ // LockInfo lockInfo = null; // try { // lockInfo = lockTemplate.lock("this_is_second_key", 5000L, 0); // // Thread.sleep(5000); // } catch (Exception e) { // log.warn("e:{}", e); // } // // lockTemplate.releaseLock(lockInfo); // } }
==========第二种
com.baomidou
lock4j-redisson-spring-boot-starter
2.2.2
spring: redis: host: 127.0.0.1 port: 6379
@Slf4j @RunWith(SpringRunner.class) @SpringBootTest(classes = TaskApplication.class) public class ExceptionTest { // //way of first // //redisson-spring-boot-starter-3.13.6.jar // @Autowired // private RedissonClient redissonClient; //way of second //lock4j-redisson-spring-boot-starter-2.2.2.jar @Resource private LockTemplate lockTemplate; // //way of first // @Test // public void redisTest() { // RLock lock = redissonClient.getLock("this_is_key"); // try { // lock.lock(); // //模拟业务 // Thread.sleep(5000); // } catch (Exception e) { // log.warn("e:{}", e); // } finally { // lock.unlock(); // } // } //way of second @Test public void secondTest(){ LockInfo lockInfo = null; try { lockInfo = lockTemplate.lock("this_is_second_key", 5000L, 0); Thread.sleep(5000); } catch (Exception e) { log.warn("e:{}", e); } lockTemplate.releaseLock(lockInfo); } }