一、redis–cluster安装环境简述
redis–cluster集群模式: 三主三从
服务器信息:linux–centos7 , 三台服务器,每台服务器两个redis节点
redis版本:redis-7.0.4
服务器IP及redis端口:192.168.1.1:7000,192.168.1.1:7001;
192.168.1.2:7002,192.168.1.2:7003;
192.168.1.3:7004,192.168.1.3:7005
注意事项:redis集群模式只有一个库(0),没有其他库可以选择,相应的后台代码配置的时候也没有办法选择redis库,都是默认0
二、redis–cluster集群安装配置
- 安装单机redis,每台服务安装配置redis步骤都一样(以192.168.1.1为例):
1.1,下载redis-7.0.4.tar.gz安装文件,存放至目录/opt/redis/;
1.2,解压/opt/redis/目录下redis-7.0.4.tar.gz文件:tar -zxvf redis-7.0.4.tar.gz ;
1.3,进入解压好的redis目录redis-7.0.4 : cd /opt/redis/redis-7.0.4 ,
执行redis编译命令: make ;
1.4,编译成功后执行安装命令:make install /opt/redis/redis-7.0.4 ,
安装成功后服务器的单机redis就配置完成了 。 - 安装完成单机redis后,进入cluster集群配置步骤,每台服务配置步骤一样(以192.168.1.1 为例):
2.1,进入/var/log/目录创建文件夹redis,后续redis配置文件需用到该目录,若不提前创建好启动redis会报错:
cd /var/log/
mkdir redis
2.2,进入redis目录,创建cluster目录,及cluster下创建7000,7001目录:
cd /opt/redis/redis-7.0.4
mkdir -p cluster/{7000,7001}
2.3,复制redis.conf配置文件至7000和70001目录下,
cp /opt/redis/redis-7.0.4/redis.conf /opt/redis/redis-7.0.4/cluster/7000/7000.conf
cp /opt/redis/redis-7.0.4/redis.conf /opt/redis/redis-7.0.4/cluster/7001/7001.conf
2.4,修改7000.conf和7001.conf配置文件配置(以7000.conf为例修改):
bind 192.168.1.1 127.0.0.1 # 192.168.1.1为本机服务器IP地址,后边127.0.0.1不用修改
protected-mode no # 关闭保护模式,允许其他主机通过IP访问连接redis服务
port 7000 # 端口号,需修改为对应的端口号
tcp-backlog 511
timeout 0
tcp-keepalive 300
daemonize yes # 打开后台运行模式,启动redis后台执行
supervised no
pidfile /var/log/redis/redis_7000.pid # 指定pidfile文件存放目录,需提前创建好/var/log/redis目录,若没有目录启动redis时会报错,pidfile名称也需要根据端口号做修改
loglevel notice
logfile "/var/log/redis/redis-server.log" # 指定redis日志文件存放目录,需提前创建好/var/log/redis目录,若没有目录启动redis时会报错
databases 16
always-show-logo no
set-proc-title yes
proc-title-template "{title} {listen-addr} {server-mode}"
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir ./
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
acllog-max-len 128
lazyfree-lazy-eviction no
lazyfree-lazy-expire no
lazyfree-lazy-server-del no
replica-lazy-flush no
lazyfree-lazy-user-del no
lazyfree-lazy-user-flush no
oom-score-adj no
oom-score-adj-values 0 200 800
disable-thp yes
appendonly yes
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
lua-time-limit 5000
注意:若需要配置密码,所有节点的密码都必须一致,masterauth也要加
requirepass "myredis" # redis服务访问密码,若不需密码注释这个配置即可
masterauth "myredis" # master 节点登录密码,若不需密码注释这个配置即可
cluster-enabled yes
cluster-config-file nodes-7000.conf
cluster-node-timeout 5000
slowlog-log-slower-than 10000
slowlog-max-len 128
latency-monitor-threshold 0
notify-keyspace-events ""
hash-max-listpack-entries 512
hash-max-listpack-value 64
list-max-listpack-size -2
list-compress-depth 0
set-max-intset-entries 512
zset-max-listpack-entries 128
zset-max-listpack-value 64
hll-sparse-max-bytes 3000
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
aof-rewrite-incremental-fsync yes
jemalloc-bg-thread yes
2.4,创建启动脚本,每台服务器步骤一致,按如下配置即可(以192.168.1.1为例,创建redis7000和redis7001):
vi /etc/init.d/redis7000
配置文件内容如下:
#!/bin/sh
REDISPORT=7000 #更改全局变量端口
EXEC=/opt/redis/redis-7.0.4/bin/redis-server
CLIEXEC=/opt/redis/redis-7.0.4/bin/redis-cli
PIDFILE=/var/log/redis/redis_${REDISPORT}.pid
CONF="/opt/redis/redis-7.0.4/cluster/${REDISPORT}/${REDISPORT}.conf"
case "$1" in
start)
if [ -f $PIDFILE ]
then
echo "$PIDFILE exists, process is already running or crashed"
else
echo "Starting Redis server..."
$EXEC $CONF
fi
;;
stop)
if [ ! -f $PIDFILE ]
then
echo "$PIDFILE does not exist, process is not running"
else
PID=$(cat $PIDFILE)
echo "Stopping ..."
$CLIEXEC -p $REDISPORT shutdown
while [ -x /proc/${PID} ]
do
echo "Waiting for Redis to shutdown ..."
sleep 1
done
echo "Redis stopped"
fi
;;
*)
echo "Please use start or stop as first argument"
;;
esac
-
三台服务器配置好之后,启动所有的redis服务(以192.168.1.1为例):
bash /etc/init.d/redis7000 start , 启动端口为7000的redis服务
bash /etc/init.d/redis7001 start, 启动端口为7001的reids服务
注:停止相应的redis服务使用命令 bash /etc/init.d/redis7000 stop -
确定所有的redis服务启动成功后,创建redis-cluster集群,在目录/opt/redis/redis7.0.4/下执行命令(以192.168.1.1为例):
./bin/redis-cli –cluster create 192.168.1.1:7000 192.168.1.2:7002 192.168.1.3:7004 192.168.1.1:7001 192.168.1.2:7003 192.168.1.3:7005 –cluster-replicas 1 -a myredis注: –cluster-replicas 1 表示创建自动创建并给每个 master 节点分配一个 slave 节点;
前三个IP:PORT为master节点 ,三主三从模式;
-a myredis 表示验证密码。 -
进入集群,查看集群信息,进入/opt/redis/redis7.0.4/下执行命令(以192.168.1.1为例):
./bin/redis-cli -h 192.168.1.1 -p 7000 -a myredis -c # -c 表示集群支持,支持自动重定向集群(cluster):
CLUSTER INFO # 查看集群信息
CLUSTER NODES # 查看集群所有节点信息CLUSTER MEET IP PORT 将 ip 和 port 所指定的节点添加到集群当中,让它成为集群的一份子
CLUSTER FORGET <node_id> 从集群中移除 node_id 指定的节点
三、redis-cluster如何重新创建
1,关闭所有已启动的redis节点
2,删除集群相关文件
删除每个节点下的appendonlydir 、dump.rdb和nodes.conf文件
3,启动所有Redis节点
4,重新创建cluster集群
./bin/redis-cli –cluster create 192.168.1.1:7000 192.168.1.2:7002 192.168.1.3:7004 192.168.1.1:7001 192.168.1.2:7003 192.168.1.3:7005 –cluster-replicas 1 -a myredis
若是创建失败,提示:
则进入所有的节点,清空数据,然后重新创建cluster集群,参考博客:
https://blog.csdn.net/Alexander_yun/article/details/81506339
四、SpringBoot + redis-cluster集群项目配置
<!--redis相关依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
- yml 配置文件:
spring:
redis:
cluster:
# 集群节点
nodes: 192.168.1.1:7000,192.168.1.1:7001,192.168.1.2:7002,192.168.1.2:7003,192.168.1.3:7004,192.168.1.3:7005
# 最大重定向次数
max-redirects: 5
# 密码
password: myredis
lettuce:
pool:
min-idle: 0
max-active: 8
max-wait: -1
max-idle: 8
enabled: true
- RedisConfig配置文件:
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
/**
* redis配置
* 集群版 Redis缓存配置类
*/
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport
{
@Bean
@SuppressWarnings(value = { "unchecked", "rawtypes" })
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory connectionFactory)
{
RedisTemplate<Object, Object> template = new RedisTemplate<>();
template.setConnectionFactory(connectionFactory);
FastJson2JsonRedisSerializer serializer = new FastJson2JsonRedisSerializer(Object.class);
// 使用StringRedisSerializer来序列化和反序列化redis的key值
template.setKeySerializer(new StringRedisSerializer());
template.setValueSerializer(serializer);
// Hash的key也采用StringRedisSerializer的序列化方式
template.setHashKeySerializer(new StringRedisSerializer());
template.setHashValueSerializer(serializer);
template.afterPropertiesSet();
return template;
}
}
原文地址:https://blog.csdn.net/qq_32512453/article/details/128057577
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_45926.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!