server:192.168.0.117
redis:3.2.3
os: centos 7
安装目录默认: /usr/local PREFIX=/usr/local
一主一从,2个哨兵
一、解压安装包
1. cd /usr/loca/src
2. 复制下载好的 redis-3.2.3.tar.gz到 /usr/loca/src
3. tar -xvzf redis-3.2.3.tar.gz -C /usr/local/
4. cd /usr/local/redis-3.2.3
5. make MALLOC=libc install
二、分别 配置2个redis 和 两个 sentinel配置(这里都安装在一台机器上,实际至少应分别安装在2台机器上)
1. cd /usr/local/redis-3.2.3
2. cp redis.conf redis-7501.conf (主)
3. cp redis.conf redis-7502.conf (备)
4. cp sentinel.conf sentinel-26378.conf
5. cp sentinel.conf sentinel-26379.conf
6. 创建数据和日志存放目录()
mkdir -pv /data/redis/logs
三、修改配置内容如下
1. redis-7501.conf
daemonize yes
port 7501
bind 192.168.0.117
protected-mode no
pidfile /var/run/redis_7501.pid
logfile "/data/redis/logs/redis-7501.log"
dir /data/redis/
dbfilename dump-7501.rdb
appendfilename "appendonly-7501.aof"
2. redis-7502.conf
同上,修改所有7502->7502,然后再加上
slaveof 192.168.0.117 7501
3. sentinel-26378.conf
port 26378
protected-mode no
pidfile /var/run/sentinel_26378.pid
sentinel monitor mymaster 192.168.0.117 7501 2
sentinel down-after-milliseconds mymaster 5000
sentinel parallel-syncs mymaster 1
dir "/data/redis/"
logfile "/data/redis/logs/sentinel_26378.log"
daemonize yes
4. sentinel-26379.conf
port 26379
protected-mode no
pidfile /var/run/sentinel_26379.pid
sentinel monitor mymaster 192.168.0.117 7501 2 (注意这句定义了master的名字,后面2个配置必须在它后面)
sentinel down-after-milliseconds mymaster 5000 (5秒master没有响应就认为主观下线默认是30s)
sentinel parallel-syncs mymaster 1
dir "/data/redis/"
logfile "/data/redis/logs/sentinel_26379.log"
daemonize yes
四、启动
1. cd /usr/local/redis-3.2.3
2. redis-server redis-7501.conf &
3. redis-server redis-7502.conf &
4. redis-sentinel sentinel-26378.conf &
5. redis-sentinel sentinel-26379.conf &
五、检查服务配置
1. 登录哨兵redis
redis-cli -h 192.168.0.117 -p 26378
2. 查看主服务信息
SENTINEL masters
3. 查看从服务信息
SENTINEL slaves mymaster
4. 写入master一条测试key
redis-cli -h 192.168.0.117 -p 7501
set test test
5. 从服务器get测试key
redis-cli -h 192.168.0.117 -p 7502
get test
六、主从切换测试
1. kill 掉7501的进程
2. 登录哨兵redis查看主服务器是否变化
七、配置redis服务自启动
这里只演示一个redis服务和一个哨兵服务(实际应该是2太机器上,都有这么一组)
1. vi /usr/lib/systemd/system/redis7501.service
[Unit]
Description=redis - High performance k-v cache
Documentation=http://redis.io/documentation
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/redis_7501.pid
ExecStart=/usr/local/bin/redis-server /usr/local/redis-3.2.3/redis-7501.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
2. 添加开机自启动
systemctl enable redis7501.service
3. vi /usr/lib/systemd/system/sentinel26378.service
Unit]
Description=redis-sentinel - High performance k-v cache's sentinel module
Documentation=http://redis.io/documentation
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/sentinel_26379.pid
ExecStart=/usr/local/bin/redis-sentinel /usr/local/redis-3.2.3/sentinel-26378.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true
[Install]
WantedBy=multi-user.target
4. 添加开机自启动
systemctl enable sentinel26378.service
八、客户端连接spring配置修改及测试
1. Spring Jedis 连接配置
<bean id="sentinelConfiguration"
class="org.springframework.data.redis.connection.RedisSentinelConfiguration">
<property name="master">
<bean class="org.springframework.data.redis.connection.RedisNode">
<property name="name" value="mymaster"></property>
</bean>
</property>
<property name="sentinels">
<set>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.0.117"></constructor-arg>
<constructor-arg name="port" value="26378"></constructor-arg>
</bean>
<bean class="org.springframework.data.redis.connection.RedisNode">
<constructor-arg name="host" value="192.168.0.117"></constructor-arg>
<constructor-arg name="port" value="26379"></constructor-arg>
</bean>
</set>
</property>
</bean>
<bean id="jedisConnectionFactory"
class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
<constructor-arg name="sentinelConfig" ref="sentinelConfiguration"></constructor-arg>
<constructor-arg name="poolConfig" ref="poolConfig"></constructor-arg>
</bean>
<bean id="redisTemplate" class="org.springframework.data.redis.core.StringRedisTemplate">
<property name="connectionFactory" ref="jedisConnectionFactory" />
<!--<property name="keySerializer">
<bean class="com.kingsum.bigdata.collector.cache.redis.RedisKeySerializer" />
</property>
<property name="valueSerializer">
<bean
class="com.kingsum.bigdata.collector.cache.redis.RedisValueSerializer" />
</property>-->
</bean>
<bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig"
lazy-init="true">
<property name="maxIdle" value="${redis.maxIdle}" />
<property name="maxTotal" value="${redis.maxActive}" />
<property name="maxWaitMillis" value="${redis.maxWait}" />
</bean>
2. 测试连接性
3. kill 掉当前的master 7502 可以看到,连接自动切换到7501,但是中间大概有间隔10s左右,会仍然访问7501。 (责任编辑:最模板) |