发布时间:2023-09-23 13:00
目录
安装集群
需要文件
安装
上传文件解压
编译
准备配置文件
安装ruby和启动各个redis节点
创建集群
集群查看
集群检查
集群添加节点
集群的节点关闭
springboot集成
yml配置
pom文件
redisTemplate
redis-3.3.5.gem
redis-4.0.10.tar.gz
上传文件redis-3.3.5.gem、 redis-4.0.10.tar.gz 到/opt目录
cd /opt
tar -zxvf redis-4.0.10.tar.gz
cd redis-4.0.10
make && make install PREFIX=/opt/redis-cluster 这样相关的数据配置文件就都在这个目录,要不然有些会在/root/ 下,如下面这些
mkdir ../redis-cluster
cp /opt/redis-4.0.10/redis.conf /opt/redis-cluster
复制6份redis.conf 修改为redis7000-7005.conf,内容修改以7000为例如下
修改项 |
注意事项 |
---|---|
appendonly yes | |
bind 本机IP地址 | |
cluster-config-file /opt/redis-cluster/nodes-7000.conf | 执行启动命令这个文件默认是在当前目录生成,使用绝对路径可以防止下次重启执行命令在当前路径下找不到,不能自动加入到集群 |
cluster-enabled yes | |
daemonize yes | |
dbfilename dump7000.rdb | 相对路径,在哪个目录执行启动命令就在那个目录生成,所以统一在/opt/redis-cluster 这个目录执行启动命令 |
pidfile /var/run/redis_7000.pid | |
port 7000 |
yum install -y ruby
gem install redis-3.3.5.gem
/opt/redis-4.0.10/src/redis-server /opt/redis-cluster/redis7000.conf > /opt/redis-cluster/redis-7000.out 2>&1 &
/opt/redis-4.0.10/src/redis-server /opt/redis-cluster/redis7001.conf > /opt/redis-cluster/redis-7001.out 2>&1 &
/opt/redis-4.0.10/src/redis-server /opt/redis-cluster/redis7002.conf > /opt/redis-cluster/redis-7002.out 2>&1 &
/opt/redis-4.0.10/src/redis-server /opt/redis-cluster/redis7003.conf > /opt/redis-cluster/redis-7003.out 2>&1 &
/opt/redis-4.0.10/src/redis-server /opt/redis-cluster/redis7004.conf > /opt/redis-cluster/redis-7004.out 2>&1 &
/opt/redis-4.0.10/src/redis-server /opt/redis-cluster/redis7005.conf > /opt/redis-cluster/redis-7005.out 2>&1 &
/opt/redis-4.0.10/src/redis-trib.rb create --replicas 1 192.168.25.62:7000 192.168.25.62:7001 192.168.27.79:7002 192.168.27.79:7003 192.168.27.106:7004 192.168.27.106:7005
/opt/redis-4.0.10/src/redis-trib.rb info 192.168.25.62:7000
/opt/redis-4.0.10/src/redis-trib.rb check 192.168.25.62:7000
/opt/redis-4.0.10/src/redis-trib.rb add-node --slave 192.168.27.79:7003 192.168.25.62:7000(已存在的节点)
/opt/redis-4.0.10/src/redis-cli -h 192.168.25.62 -p 7001 shutdown
注意:在上次执行命令的路径执行,或者在nodes-7000.conf文件的路径执行
spring:
redis:
# 地址
# host: 192.168.27.106
# 端口,默认为6379
# port: 7004
# 数据库索引
# database: 0
# 密码
password:
# 连接超时时间
timeout: 10s
lettuce:
pool:
# 连接池中的最小空闲连接
min-idle: 0
# 连接池中的最大空闲连接
max-idle: 8
# 连接池的最大数据库连接数
max-active: 8
# #连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: -1ms
cluster:
max-redirects: 3
nodes:
- 192.168.25.62:7000
- 192.168.25.62:7001
- 192.168.27.79:7002
- 192.168.27.79:7003
- 192.168.27.106:7004
- 192.168.27.106:7005
org.springframework.boot
spring-boot-starter-data-redis
org.apache.commons
commons-pool2
这里设置了所有的key的默认失效时间(3小时)和序列化方式
或者直接使用spring提供的注解@Cacheable("hello:info")在方法上
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.time.Duration;
@Configuration
@EnableCaching
public class RedisConfig {
private static final StringRedisSerializer STRING_SERIALIZER = new StringRedisSerializer();
private static final GenericJackson2JsonRedisSerializer JACKSON__SERIALIZER = new GenericJackson2JsonRedisSerializer();
@Bean
public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
//设置缓存过期时间
RedisCacheConfiguration redisCacheCfg = RedisCacheConfiguration.defaultCacheConfig()
.entryTtl(Duration.ofHours(3))
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(STRING_SERIALIZER))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(JACKSON__SERIALIZER));
return RedisCacheManager.builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
.cacheDefaults(redisCacheCfg)
.build();
}
}