在10.21.6.55部署redis作为master
10.21.6.56部署redis作为slave
master配置:
引用
daemonize yes
###若为从库,应该设置为主库的密码
#masterauth "Ifeng888"
###登录需要密码认证
requirepass "Ifeng888"
###别忘了更改密码
pidfile /data/redis/6379/redis.pid
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel warning
logfile /data/logs/redis/redis.log
databases 16
save 1200 1
save 900 10000
save 300 100000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data/redis/6379
slave-serve-stale-data yes
slave-read-only no
###若为从库,应该设置为主库的密码
#masterauth "Ifeng888"
###登录需要密码认证
requirepass "Ifeng888"
###别忘了更改密码
pidfile /data/redis/6379/redis.pid
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel warning
logfile /data/logs/redis/redis.log
databases 16
save 1200 1
save 900 10000
save 300 100000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data/redis/6379
slave-serve-stale-data yes
slave-read-only no
slave配置
引用
daemonize yes
###若为从库,应该设置为主库的密码
masterauth "Ifeng888"
###登录需要密码认证
requirepass "Ifeng888"
###别忘了更改密码
pidfile /data/redis/6379/redis.pid
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel warning
logfile /data/logs/redis/redis.log
databases 16
save 1200 1
save 900 10000
save 300 100000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data/redis/6379
##设置主从
slaveof 10.21.6.55 6379
slave-serve-stale-data yes
slave-read-only no
###若为从库,应该设置为主库的密码
masterauth "Ifeng888"
###登录需要密码认证
requirepass "Ifeng888"
###别忘了更改密码
pidfile /data/redis/6379/redis.pid
port 6379
tcp-backlog 511
timeout 0
tcp-keepalive 0
loglevel warning
logfile /data/logs/redis/redis.log
databases 16
save 1200 1
save 900 10000
save 300 100000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
dbfilename dump.rdb
dir /data/redis/6379
##设置主从
slaveof 10.21.6.55 6379
slave-serve-stale-data yes
slave-read-only no
启动master slave上的redis
在master上
引用
redis-cli -a Ifeng888
127.0.0.1:6379> get Ifeng777
"14"
127.0.0.1:6379> set Ifeng777 18
OK
127.0.0.1:6379> get Ifeng777
"14"
127.0.0.1:6379> set Ifeng777 18
OK
slave 上
引用
redis-cli -a Ifeng888
127.0.0.1:6379> get Ifeng888
(nil)
127.0.0.1:6379> get Ifeng777
"14"
127.0.0.1:6379> get Ifeng777
"18"
127.0.0.1:6379> get Ifeng888
(nil)
127.0.0.1:6379> get Ifeng777
"14"
127.0.0.1:6379> get Ifeng777
"18"
主从环境搭建成功
二、haproxy配置
1、安装haproxy
引用
yum install haproxy
2、修改配置文件
引用
vim /etc/haproxy/haproxy.cfg
配置文件
引用
listen redis_cluster:6380 #运行的端口及主机名
bind *:6380
server redis-master_6.55 10.21.6.55:6379 check port 6379 inter 2000 rise 3 fall 3 maxconn 10000 weight 1
server redis-slaver_6.56 10.21.6.56:6379 check port 6379 inter 2000 rise 3 fall 3 maxconn 10000 weight 1 backup
bind *:6380
server redis-master_6.55 10.21.6.55:6379 check port 6379 inter 2000 rise 3 fall 3 maxconn 10000 weight 1
server redis-slaver_6.56 10.21.6.56:6379 check port 6379 inter 2000 rise 3 fall 3 maxconn 10000 weight 1 backup
haproxy配置完成
已实现:
1、redis主从配置
2、haproxy高可用实现
因为对slave配置backup,master正常工作时,redis请求全部转发到master上,当master挂掉时,请求转发到slave上。
写一段简单的java代码进行验证,redisclient配置为haproxy 的 IP 端口为6380
当程序进行一半时,关停master
命令:redis-cli -a Ifeng888 shutdown
测试代码
public class Test { public static void main(String[] args){ String key = "Ifengtest1"; RedisClient.set(key,0); while (true){ Integer rv= RedisClient.get(key,Integer.class); System.out.println("test redis "+rv); if (rv!=null){ rv++; RedisClient.set(key,rv); } try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
输出结果:
引用
test redis 0
test redis 1
test redis 2
test redis 3
test redis 4
test redis 5
test redis 6
2017-10-31 10:05:30,154 INFO main com.ifeng.auto.we_provider.cache.redis.RedisClient - Redis
redis.clients.jedis.exceptions.JedisConnectionException: It seems like server has closed the connection.
at redis.clients.util.RedisInputStream.readLine(RedisInputStream.java:91)
at redis.clients.jedis.Protocol.processStatusCodeReply(Protocol.java:80)
at redis.clients.jedis.Protocol.process(Protocol.java:69)
at redis.clients.jedis.Protocol.read(Protocol.java:122)
at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:172)
at redis.clients.jedis.BinaryJedis.get(BinaryJedis.java:74)
at com.ifeng.auto.we_provider.cache.redis.RedisClient.get(RedisClient.java:571)
at com.ifeng.auto.we_provider.main.Test.main(Test.java:16)
test redis null
test redis 7
test redis 8
test redis 9
test redis 10
test redis 11
test redis 12
test redis 13
test redis 1
test redis 2
test redis 3
test redis 4
test redis 5
test redis 6
2017-10-31 10:05:30,154 INFO main com.ifeng.auto.we_provider.cache.redis.RedisClient - Redis
redis.clients.jedis.exceptions.JedisConnectionException: It seems like server has closed the connection.
at redis.clients.util.RedisInputStream.readLine(RedisInputStream.java:91)
at redis.clients.jedis.Protocol.processStatusCodeReply(Protocol.java:80)
at redis.clients.jedis.Protocol.process(Protocol.java:69)
at redis.clients.jedis.Protocol.read(Protocol.java:122)
at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:172)
at redis.clients.jedis.BinaryJedis.get(BinaryJedis.java:74)
at com.ifeng.auto.we_provider.cache.redis.RedisClient.get(RedisClient.java:571)
at com.ifeng.auto.we_provider.main.Test.main(Test.java:16)
test redis null
test redis 7
test redis 8
test redis 9
test redis 10
test redis 11
test redis 12
test redis 13
三、主从redis更换密码
1、master修改密码
引用
###登录需要密码认证
requirepass "Ifeng999"
requirepass "Ifeng999"
2、关停master
使用更改配置前密码关停redis
引用
redis-cli -a Ifeng888 shutdown
3、启动master
引用
service redis start
4、slave修改密码
masterauth 和requirepass 都需要更改为新密码
引用
###若为从库,应该设置为主库的密码
masterauth "Ifeng999"
###登录需要密码认证
requirepass "Ifeng999"
masterauth "Ifeng999"
###登录需要密码认证
requirepass "Ifeng999"
5、slave关停
使用更改配置前密码关停redis
引用
redis-cli -a Ifeng888 shutdown
6、启动slave
引用
service redis start
7、修改各业务系统中redis密码配置文件,并重启项目
至此 redis密码更改完成。