Redis主从集群+哨兵部署与Springboot项目配置

75 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第17天,点击查看活动详情

系统调优

集群每台机器中,系统内核参数修改:

/etc/sysctl.conf 中加入,并运行 sysctl -p 使生效:

vm.overcommit_memory = 1
# 以下示情况调整
net.ipv4.tcp_max_syn_backlog = 2048
net.core.somaxconn = 2048

/etc/rc.local 中加入:

echo never > /sys/kernel/mm/transparent_hugepage/enabled

核对/proc/sys/fs/file-max,可通过/etc/sysctl.conf中增加fs.file-max配置

/etc/security/limits.conf 中加入(据说系统init脚本或者daemons运行时这里无法生效,可在启动脚本中加ulimit -n 10032),重新登陆后生效:

# 这里设置了所有用户,可以指定Redis启动的用户,设置值可根据需要另行调节
* - nofile 20032

防火墙端口开启Redis访问权限

firewall-cmd --permanent --add-port=6379/tcp
firewall-cmd --reload

防火墙端口开启哨兵访问权限

firewall-cmd --permanent --add-port=26379/tcp
firewall-cmd --reload

Redis参数调整与启动

机器分布:

192.168.1.45 redis-master
192.168.1.46 redis-slaver1
192.168.1.47 redis-slaver2

master&slaver 参数调整redis.conf:

  • bind 0.0.0.0
  • tcp-backlog 2047
  • daemonize yes
  • logfile /opt/redis-logs/redis_6379.log
  • dbfilename dump_6379.rdb
  • dir /opt/redis-dump
  • rename-command CONFIG abcdefg
  • maxmemory 2gb
  • maxmemory-policy volatile-lfu
  • 各个Redis节点必须设置一致,防止故障转移后,客户端连接失败:requirepass 123456

master 额外参数:

  • 最后一行加入:include /opt/configs/redis-master.conf

slaver 额外参数:

  • 最后一行加入:include /opt/configs/redis-slaver.conf

redis-master.conf:

  • repl-backlog-size 256mb

redis-slaver.conf:

  • slaveof 192.168.xx.xxx 6379
  • masterauth 123456

启动redis:/opt/redis-4.0.14/src/redis-server /opt/configs/redis.conf &

哨兵配置调整与启动

机器分布:

192.168.xx.xx
192.168.xx.xx
192.168.xx.xx

sentinel.conf 修改:

  • bind 0.0.0.0
  • protected-mode yes
  • sentinel monitor master1 192.168.xx.xx 6379 2
  • sentinel auth-pass master1 123456
  • sentinel down-after-milliseconds master1 30000
  • sentinel parallel-syncs master1 1
  • sentinel failover-timeout master1 180000
  • — 暂不配置— : sentinel notification-script master1 通知脚本文件位置

启动哨兵:nohup /opt/redis-4.0.14/src/redis-sentinel /opt/configs/sentinel.conf > /opt/redis-logs/sentinel_26379.log &

Springboot项目集成哨兵

示例为springboot 2.x

maven依赖:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-redis</artifactId>
    <version>${spring boot version}</version>
</dependency>
<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>${jedis version}</version>
</dependency>

配置文件配置(详见 org.springframework.boot.autoconfigure.data.redis.RedisProperties):

# 哨兵地址列表
spring.redis.sentinel.nodes=192.168.xx.xx:26379,192.168.xx.xx:26379,192.168.xx.xx:26379
# 哨兵中配置的集群名称
spring.redis.sentinel.master=master1
# 如果有密码则设置密码
spring.redis.password=123456
spring.redis.database=2
#最大连接数
spring.redis.jedis.pool.max-active=50
#最大等待连接数
spring.redis.jedis.pool.max-idle=10
#最小等待连接数
spring.redis.jedis.pool.min-idle=5
#最大等待毫秒数
spring.redis.jedis.pool.max-wait=10000
#超时时间
spring.redis.jedis.pool.timeout=1000

代码中可通过依赖注入使用以下任一:

@Autowired
private RedisTemplate redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;

欢迎 补充,在 .net 环境下如何配置