分片集群搭建
- 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
- 使用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;
}
}
- 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