spring boot 中 redisson 的使用

1、增加pom.xml

<dependency>
   <groupId>org.redisson</groupId>
   <artifactId>redisson</artifactId>
   <version>3.11.1</version>
</dependency>

2、application.properties 配置

spring.redis.port=6379
//安装redis 服务器ip
spring.redis.host= ip 
//安装redis 的密码
spring.redis.password= xxxx

3、创建RedissonConfig配置类

@Configuration
public class RedissonConfig {
    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private String port;
    @Value("${spring.redis.password}")
    private String password;

    @Bean
    public RedissonClient getRedisson() {
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://" + host + ":" + port)
                .setPassword(password)
                .setRetryInterval(5000)
                .setTimeout(10000)
                .setConnectTimeout(10000);
        //添加主从配置
        //config.useMasterSlaveServers().setMasterAddress("").setPassword("").addSlaveAddress(new String[]{"",""});
        return Redisson.create(config);
    }
}

4、引入,简单使用如下

@Autowired
private RedissonClient redissonClient;
public  String getGenerateCarPassword2() {
    String id = "OIL_CARPASSWORD_ID";
    String oil_lock = "OIL_CARPASSWORD_LOCK";
    String value = BatchUtil.getValue();
    RLock lock = redissonClient.getLock(oil_lock);
    try {
        lock.lock();
        //业务代码
        
    }catch (Exception e) {
        e.printStackTrace();
    }finally {
        lock.unlock();
    }
    return null;
}

猜你喜欢

转载自blog.csdn.net/CHL123456789/article/details/114537048