基于haproxy的redis高可用环境搭建
一redis主从搭建
在10.21.6.55部署redis作为master
10.21.6.56部署redis作为slave
master配置:
###若为从库,应该设置为主库的密码
#masterauth"Ifeng888"
###登录需要密码认证
requirepass"Ifeng888"
###别忘了更改密码
pidfile/data/redis/6379/redis.pid
port6379
tcp-backlog511
timeout0
tcp-keepalive0
loglevelwarning
logfile/data/logs/redis/redis.log
databases16
save12001
save90010000
save300100000
stop-writes-on-bgsave-erroryes
rdbcompressionyes
rdbchecksumyes
dbfilenamedump.rdb
dir/data/redis/6379
slave-serve-stale-datayes
slave-read-onlyno
slave配置
###若为从库,应该设置为主库的密码
masterauth"Ifeng888"
###登录需要密码认证
requirepass"Ifeng888"
###别忘了更改密码
pidfile/data/redis/6379/redis.pid
port6379
tcp-backlog511
timeout0
tcp-keepalive0
loglevelwarning
logfile/data/logs/redis/redis.log
databases16
save12001
save90010000
save300100000
stop-writes-on-bgsave-erroryes
rdbcompressionyes
rdbchecksumyes
dbfilenamedump.rdb
dir/data/redis/6379
##设置主从
slaveof10.21.6.556379
slave-serve-stale-datayes
slave-read-onlyno
启动masterslave上的redis
在master上
127.0.0.1:6379>getIfeng777
"14"
127.0.0.1:6379>setIfeng77718
OK
slave上
127.0.0.1:6379>getIfeng888
(nil)
127.0.0.1:6379>getIfeng777
"14"
127.0.0.1:6379>getIfeng777
"18"
主从环境搭建成功
二、haproxy配置
1、安装haproxy
2、修改配置文件
配置文件
bind*:6380
serverredis-master_6.5510.21.6.55:6379checkport6379inter2000rise3fall3maxconn10000weight1
serverredis-slaver_6.5610.21.6.56:6379checkport6379inter2000rise3fall3maxconn10000weight1backup
haproxy配置完成
已实现:
1、redis主从配置
2、haproxy高可用实现
因为对slave配置backup,master正常工作时,redis请求全部转发到master上,当master挂掉时,请求转发到slave上。
写一段简单的java代码进行验证,redisclient配置为haproxy的IP端口为6380
当程序进行一半时,关停master
命令:redis-cli-aIfeng888shutdown
测试代码
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(); } } } }
输出结果:
testredis1
testredis2
testredis3
testredis4
testredis5
testredis6
2017-10-3110:05:30,154INFOmaincom.ifeng.auto.we_provider.cache.redis.RedisClient-Redis
redis.clients.jedis.exceptions.JedisConnectionException:Itseemslikeserverhasclosedtheconnection.
atredis.clients.util.RedisInputStream.readLine(RedisInputStream.java:91)
atredis.clients.jedis.Protocol.processStatusCodeReply(Protocol.java:80)
atredis.clients.jedis.Protocol.process(Protocol.java:69)
atredis.clients.jedis.Protocol.read(Protocol.java:122)
atredis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:172)
atredis.clients.jedis.BinaryJedis.get(BinaryJedis.java:74)
atcom.ifeng.auto.we_provider.cache.redis.RedisClient.get(RedisClient.java:571)
atcom.ifeng.auto.we_provider.main.Test.main(Test.java:16)
testredisnull
testredis7
testredis8
testredis9
testredis10
testredis11
testredis12
testredis13
三、主从redis更换密码
1、master修改密码
requirepass"Ifeng999"
2、关停master
使用更改配置前密码关停redis
3、启动master
4、slave修改密码
masterauth和requirepass都需要更改为新密码
masterauth"Ifeng999"
###登录需要密码认证
requirepass"Ifeng999"
5、slave关停
使用更改配置前密码关停redis
6、启动slave
7、修改各业务系统中redis密码配置文件,并重启项目
至此redis密码更改完成。