做网站需要企业/网站文章优化技巧
文章目录
- 一、下载安装包
- 二、解压并安装
- 三、软连接
- 四、主从配置
- 五、部署Sentinel节点
- 六、springboot 与 redis sentinel整合
- 七、使用lettuce与 redis sentinel整合,手动创建连接
- 八、使用redisson与 redis sentinel整合,手动创建连接
一、下载安装包
去redis官网下载最新的consul版本,地址:
redis官方下载地址
不需要下载到自己的客户机上,直接在服务器里面用wget下载更快
拷贝下载链接
cd /home/download
wget http://download.redis.io/releases/redis-5.0.0.tar.gz
二、解压并安装
tar xzf redis-5.0.0.tar.gz
cd redis-5.0.0
make PREFIX=/usr/local/redis install
三、软连接
ln -s /usr/local/redis/bin/redis-cli /usr/local/bin/redis-cli
ln -s /usr/local/redis/bin/redis-sentinel /usr/local/bin/redis-sentinel
ln -s /usr/local/redis/bin/redis-server /usr/local/bin/redis-server
四、主从配置
- 1、修改配置文件
cd redis-5.0.0
cp redis.conf /etc/redis/redis_6001.conf
cp redis.conf /etc/redis/redis_6002.conf
cp redis.conf /etc/redis/redis_6003.conf
6001端口作为主服务
cd /etc/redis/
# 需要改动的配置
protected-mode no
port 6001
daemonize yes
logfile "6001.log"
dbfilename "dump-6001.rdb"
dir "/var/redis/data"
requirepass "123456"
masterauth "123456"
如果想用redis客户端工具远程连接,必须要protected-mode设为no
从节点在主节点的基础上改端口,并加上配置:
slaveof 127.0.0.1 6001
- 2、启动:
redis-server redis-6001.conf
redis-server redis-6002.conf
redis-server redis-6003.conf
- 3、测试:
redis-cli -h 127.0.0.1 -p 6001 -a 123456 ping
redis-cli -h 127.0.0.1 -p 6002 -a 123456 ping
redis-cli -h 127.0.0.1 -p 6003 -a 123456 ping
- 4、确认主从关系:
主节点视角
redis-cli -h 127.0.0.1 -p 6001 -a 123456 INFO replication
# Replication
role:master
connected_slaves:2
slave0:ip=127.0.0.1,port=6002,state=online,offset=85,lag=0
slave1:ip=127.0.0.1,port=6003,state=online,offset=85,lag=0
......
从主节点视角
redis-cli -h 127.0.0.1 -p 6002 -a 123456 INFO replication
# Replication
role:slave
master_host:127.0.0.1
master_port:6001
master_link_status:up
......
五、部署Sentinel节点
- 1、配置文件
cd /home/download/redis/redis-5.0.0
cp sentinel.conf /etc/redis/sentinel_17001.conf
cp sentinel.conf /etc/redis/sentinel_17002.conf
cp sentinel.conf /etc/redis/sentinel_17003.conf
3个Sentinel节点的部署方法是相同的(端口不同),以17001为例
protected-mode no
port 17001
daemonize yes
logfile "17001.log"
dir "/var/redis/data"
# 当前Sentinel节点监控 127.0.0.1:6001 这个主节点
# 2代表判断主节点失败至少需要2个Sentinel节点节点同意
# mymaster是主节点的别名
sentinel monitor mymaster 127.0.0.1 6001 2
sentinel auth-pass mymaster 123456
- 2、启动
redis-sentinel sentinel-17001.conf
redis-sentinel sentinel-17002.conf
redis-sentinel sentinel-17003.conf
- 3、测试
redis-cli -h 127.0.0.1 -p 17001 INFO Sentinel# Sentinel
sentinel_masters:1
sentinel_tilt:0
sentinel_running_scripts:0
sentinel_scripts_queue_length:0
sentinel_simulate_failure_flags:0
master0:name=mymaster,status=ok,address=35.234.13.166:6001,slaves=2,sentinels=3
redis-cli -h 127.0.0.1 -p 17001
127.0.0.1:17001> sentinel masters 1) "name"2) "mymaster"3) "ip"4) "127.0.0.1"5) "port"6) "6001"7) "runid"8) "e8f4febb12cbb2f2c42ccc732db7d6abef8181ac"9) "flags"10) "master"11) "link-pending-commands"12) "0"13) "link-refcount"14) "1"......127.0.0.1:17001> sentinel slaves mymaster
#略
- 4、拓补图如下:
六、springboot 与 redis sentinel整合
springboot2.0 默认用的redis连接是 lettuce
配置如下:
#redis
spring.redis.sentinel.nodes=ip:17001,ip:17002,ip:17003
spring.redis.sentinel.master=mymaster
spring.redis.password=123456
接下来springboot 会自动用lettuce连接redis,从而生成RedisTemplate来操作redis
七、使用lettuce与 redis sentinel整合,手动创建连接
直接上代码:
@Configuration
public class RedisConfig {@Bean(name = "lettucePool")public GenericObjectPool lettucePool(RedisProperties redisProperties) {RedisURI redisURI = null;if (redisProperties.getSentinel() != null) {RedisURI.Builder builder = RedisURI.builder();builder.withPassword(redisProperties.getPassword());builder.withDatabase(12);RedisProperties.Sentinel sentinel = redisProperties.getSentinel();builder.withSentinelMasterId(sentinel.getMaster());List<String> nodes = sentinel.getNodes();nodes.forEach(node -> {builder.withSentinel(HostAndPort.parse(node).getHostText(), HostAndPort.parse(node).getPort());});redisURI = builder.build();} else {redisURI = RedisURI.create(redisProperties.getHost(), redisProperties.getPort());redisURI.setDatabase(12);redisURI.setPassword(redisProperties.getPassword());}RedisClient client = RedisClient.create(redisURI);GenericObjectPoolConfig config = new GenericObjectPoolConfig();config.setJmxEnabled(false);GenericObjectPool<StatefulRedisConnection> lettucePool = ConnectionPoolSupport.createGenericObjectPool(() -> client.connect(), config);return lettucePool;}
八、使用redisson与 redis sentinel整合,手动创建连接
直接上代码:
@Configuration
public class RedisConfig {@Beanpublic RedissonClient redisson(RedisProperties redisProperties) {Config config = new Config();if (redisProperties.getSentinel() != null) { //sentinelSentinelServersConfig sentinelServersConfig = config.useSentinelServers();sentinelServersConfig.setMasterName(redisProperties.getSentinel().getMaster());List<String> nodes = redisProperties.getSentinel().getNodes();String[] strings = new String[nodes.size()];String schema = redisProperties.isSsl() ? "rediss://" : "redis://";for (int i = 0; i < nodes.size(); i++) {strings[i] = schema + nodes.get(i);}sentinelServersConfig.addSentinelAddress(strings);sentinelServersConfig.setDatabase(0);if (redisProperties.getPassword() != null) {sentinelServersConfig.setPassword(redisProperties.getPassword());}} else { //single serverSingleServerConfig singleServerConfig = config.useSingleServer();String schema = redisProperties.isSsl() ? "rediss://" : "redis://";singleServerConfig.setAddress(schema + redisProperties.getHost() + ":" + redisProperties.getPort());singleServerConfig.setDatabase(0);if (redisProperties.getPassword() != null) {singleServerConfig.setPassword(redisProperties.getPassword());}}return Redisson.create(config);}
大功告成!