Redisson
方式1 引入依赖
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.15.1</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
配置文件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); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
使用
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(); } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
方式2: 依赖
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson-spring-boot-starter</artifactId>
<version>3.15.1</version>
</dependency>
- 1
- 2
- 3
- 4
- 5
配置文件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(); } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
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>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
配置文件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");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
使用
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(); } } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
追加方式:两种方式测试方便合在一块,实际独立的
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); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
@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); // } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
==========第二种
com.baomidou
lock4j-redisson-spring-boot-starter 2.2.2
spring:
redis:
host: 127.0.0.1
port: 6379
- 1
- 2
- 3
- 4
@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); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
参考:http://t.zoukankan.com/LUA123-p-14516530.html
本文使用 文章同步助手 同步
网友评论