Springboot初始化连接Redisson/RedissonClient

一、maven依赖

    <!-- redisson -->
    <dependency>
        <groupId>org.redisson</groupId>
        <artifactId>redisson</artifactId>
        <version>3.19.3</version>
    </dependency>

二、ClusterRedissonConfig.java配置redis哨兵模式集群
package com.sms.gateway.config;

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;

import java.util.List;

/**

  • @author

  • @description 集群Redis配置
    */
    @Configuration
    public class ClusterRedissonConfig {

    @Value("${spring.redis.sentinel.nodes}")
    private List nodes;

    @Value("${spring.redis.sentinel.master}")
    private String master;

    @Value("${spring.redis.password}")
    private String password;

    @Bean
    public RedissonClient getRedisson() {
    Config config = new Config();
    config.useSentinelServers()
    .setMasterName(master)
    //可以用"rediss://"来启用SSL连接
    .addSentinelAddress(nodes.stream().map(node -> "redis://" + node).toArray(String[]::new))
    .setPassword(password);
    return Redisson.create(config);
    }
    }

三、使用redisson
注入redissonClient对象

使用redisson获取分布式锁

posted on 2024-11-14 17:19  vello  阅读(59)  评论(0编辑  收藏  举报