一、官网地址
二、整合配置
redisson 有些版本配置可能出现bug,开始用的3.13.6版本,哨兵配置一直报错
redisson 有些版本配置可能出现bug,开始用的3.13.6版本,哨兵配置一直报错
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
2)spring中的Bean配置
mport 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; import java.io.IOException; /** * @Classname RedssonConfig * @Description TODO * @Author Jack * Date 2020/9/16 20:47 * Version 1.0 */ @Configuration public class RedissonConfig { @Bean(name = "redissonClient",destroyMethod = "shutdown") public RedissonClient redissonClient() throws IOException { Config config = new Config(); config.useSentinelServers() .setMasterName("mymaster") //可以用"rediss://"来启用SSL连接 .addSentinelAddress("redis://192.168.112.131:26379" ) .addSentinelAddress("redis://192.168.112.131:26380") .addSentinelAddress("redis://192.168.112.131:26381"); RedissonClient redisson = Redisson.create(config); return redisson; } }
三、测试代码
@Autowired @Qualifier("redissonClient") private RedissonClient redissonClient; @Test public void test5() throws InterruptedException { RLock lock = redissonClient.getLock("hello_redisson_lock"); if(lock.tryLock( )) { System.out.println( "加锁成功"); } lock.unlock(); }