redis集群配置

文章介绍了如何在Springboot应用中配置Redis集群,包括设置超时时间、数据库索引和节点地址。同时,展示了如何使用Redisson创建Redis连接工厂,以及配置RedisTemplate和ReactiveRedisConnectionFactory。配置中包含了集群模式的详细设置,如节点地址、密码等,并提供了Redis服务器的相关配置参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分片集群搭建

  1. springboot集成redis
    相关配置:
spring:
	redis:
		timeout: 3000
		database: 0
		cluster:
			node: 127.0.0.1:7000,127.0.0.1:7001,127.0.0.1:7002
		password: 123	
  1. 使用redisTemplate连接redis
    添加以下相关配置类
@Configuration
@AutoConfigureAfter(RedisAutoConfiguration.class)
public class RedissonConfig extends CachingConfigurerSupport {

    @Autowired
    private RedissonProperties RedissonProperties;

    @Bean
    public RedissonClient getRedissonClient() {
        Config config = new Config();
        if (RedissonProperties.getCluster() != null) {
            //集群模式配置
            List<String> nodes = RedissonProperties.getCluster().getNodes();

            List<String> clusterNodes = new ArrayList<>();
            for (int i = 0; i < nodes.size(); i++) {
                clusterNodes.add("redis://" + nodes.get(i));
            }
            ClusterServersConfig clusterServersConfig = config.useClusterServers()
                .addNodeAddress(clusterNodes.toArray(new String[clusterNodes.size()]));

            if (!StringUtils.isEmpty(RedissonProperties.getPassword())) {
                clusterServersConfig.setPassword(RedissonProperties.getPassword());
            }
        }
        return Redisson.create(config);
    }
    @Bean
    @ConditionalOnMissingBean(RedisConnectionFactory.class)
    public RedissonConnectionFactory redissonConnectionFactory(RedissonClient redisson) {
        return new RedissonConnectionFactory(redisson);
    }

    @Bean
    @ConditionalOnMissingBean(ReactiveRedisConnectionFactory.class)
    public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory(RedissonClient redisson) {
        return new RedissonConnectionFactory(redisson);
    }

    @Bean
    @ConditionalOnMissingBean(name = "redisTemplate")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactor) {
        RedisTemplate<String, Object> template = new RedisTemplate<String, Object>();
        template.setConnectionFactory(redisConnectionFactor);
        template.setKeySerializer(new StringRedisSerializer());
        template.setHashKeySerializer(new StringRedisSerializer());
        template.setValueSerializer(new StringRedisSerializer());
        template.setHashValueSerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }
}
@Component
@ConfigurationProperties(prefix = "spring.redis")
@Data
public class RedissonProperties {

    private RCluster cluster;
    private String password;
    @Data
    static class RCluster {
        private List<String> nodes;
    }
}
  1. redis配置项
#去掉bind绑定访问ip信息
bind 0.0.0.0

#关闭保护模式
protected-mode no

#修改对应的端口
port 7001

#启动集群模式
cluster-enabled yes

#集群节点信息文件,这里500x最好和port对应上
cluster-config-file nodes-7001.conf

#节点离线的超时时间
cluster-node-timeout 5000

#如果要设置密码需要增加如下配置:
#设置redis访问密码
requirepass xdx97

#设置集群节点间访问密码,跟上面一致
masterauth xdx97

# 修改启动进程号存储位置
pidfile /var/run/redis_5001.pid

#指定数据文件存放位置,必须要指定不同的目录位置,不然会丢失数据
dir /usr/local/redis-cluster/redis-5001

#修改为后台启动
daemonize yes

#启动AOF文件
appendonly yes

tcp-backlog 511
timeout 0
tcp-keepalive 300
supervised no
loglevel notice
logfile ""
databases 16
always-show-logo yes
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
replica-serve-stale-data yes
replica-read-only yes
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
replica-priority 100
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no
auto-aof-rewrite-percentage 100
auto-aof-rewrite-min-size 64mb
aof-load-truncated yes
aof-use-rdb-preamble yes
lua-time-limit 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-ziplist-entries 512
hash-max-ziplist-value 64
list-max-ziplist-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-ziplist-entries 128
zset-max-ziplist-value 64
hll-sparse-max-bytes 3000
stream-node-max-bytes 4096
stream-node-max-entries 100
activerehashing yes
client-output-buffer-limit normal 0 0 0
client-output-buffer-limit replica 256mb 64mb 60
client-output-buffer-limit pubsub 32mb 8mb 60
hz 10
dynamic-hz yes
aof-rewrite-incremental-fsync yes
rdb-save-incremental-fsync yes

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值