2.4redis哨兵高可用架构详解_攀登者m的博客-爱代码爱编程
sentinel哨兵是特殊的redis服务,不提供读写服务,主要用来监控redis实例节点.
哨兵架构下client端第一次从哨兵找出redis的主节点,后续就直接访问redis的主节点,不会每次都通过sentinel代理访问redis的主节点,当redis的主节点发生变化,哨兵会第一时间感知到,并且将新的redis主节点通知给client端(这里面redis的client端一般都实现了订阅功能,订阅sentinel发布的节点变动消息)
哨兵架构搭建
1、复制一份sentinelconf文件
cp sentinelconfsentinel-26379.conf
2、将相关配置修改为如下值: port 26379 daemonize yes
pidfile "/var/run/redis-sentinel-26379.pid"
loefile "26379.log"
dir "/usr/local/redis-5.0.3/data"
#sentinel monitor <master-name><ip><redis-port><quorum>
#quorum是一个数字,指明当有多少个sentinel认为一个master失效时(值一般为:sentinel总数/2+1),master才算真正失效
sentinel monitor mymaster 192.168.0.60 6379 2
3、启动sentinel哨兵实例
src/redis-sentinel sentinel-26379.conf
4、查看sentinel的info信息 src/redis-cli -p 26379127.0.0.1:26379>info
可以看到Sentinel的info里已经识别出了redis的主从
5、可以自己再配置两个sentinel,端口26380和26381,注意上述配置文件里的对应数字都要修改
info命令查看redis服务运行信息,分为9部分,分别如下:
1)Server 服务器运行的环境参数
2)Clients 客户端相关信息
3)Memory 服务器运行内存统计数据
4)Persistence 持久化信息
5)Stats 通用统计数据
6)Renlication 主从复制相关信息
7)CPU CPU使用情况
8)Cluster 集群信息
9)KeySpace 键值对统计数量信息
连接样例代码
public class JedisSentinelTest {
public static void main(string[] args) throws IOException {
JedisPoolConfig config=new JedisPoolConfig();
config.setMaxTotal(20);
config.setMaxIdle(10):
config.setMinIdle(5);
string masterName ="mymaster".
Set<String> sentinels=new Hashset<String>();
sentinels.add(new HostAndPort("192.168.0.60",26379)tostring());
sentinels.add(new HostAndPort("192.168.0.60" 26380)tostring());
sentinels.add(new HostAndPort("192.168.0.60"26381tostring));
//JedisSentinelPool其实本质跟JedisPool类似,都是与redis主节点建立的连接池
//JedisSentinelPool并不是说与sentinel建立的连接池,而是通过sentinel发现redis主节点并与其建立连接
JedisSentinelPool jedisSentinelPool=new JedisSentinelPool(masterName, sentinels, config, 3000,null);
Jedis jedis = null;
try {
jedis=jedisSentinelPool.getResource);
System.out.println jedis.set("sentinel","zhuge")); System.out.println(iedis.get("sentinel"));
} catch(Exception e){
e.printStackTrace();
} finally{
//注意这里不是关闭连接,在JedisPool模式下,Jedis 会被归还给资源池
if(jedis != null){
jedis.close();
}
}
Springboot 使用步骤
1)引入相关依赖
<dependency>
<groupId>org.springframework.boot</groupid>
<artifactId>spring-boot-starter-data-redis</artifactid>
</dependency>
<dependency>
<groupId>org.apache.commons</groupid>
<artifactid>commons-pool2</artifactid>
</dependency>
SpringBoot配置
server:
port:8080
spring:
redis:
database:
timeout: 3000
sentinel: #哨兵模式
master: mymaster #主服务器所在集群名称
nodes: 192.168.0.60:26379 192.168.0.60:26380 192.168.0.60:26381
lettuce:
pool:
max-idle: 50
min-idle: 10
max-active: 100
max-wait: 1000
stringRedisTemnlate.opsForValue().set(“zhuge”+1, 1+“”):
使用详情
StringRedisTemplate与RedisTemplate详解
spring封装了RedisTemplate对象来进行对redis的各种操作,它支持所有的redis原生的api。在RedisTemplate中提供了几个常用的技口方法的使用,分别是:
1 private ValueOperations<K,Y> valueOps;
2 private HashOperationshashops:
3 private ListOperations listops;
4 private SetOperations<K,V> setops;
5 private ZsetOperationszSetOps
RedisTemplate中定义了对5种数据结构操作
1 redisTemplate.opsForValue);//操作字符串
2 redisTemplate.opsForHash();//操作hash
3 redisTemplate.opsForList();//操作list
4 redisTemplate.opsForSet();//操作set
5 redisTemplate.opsForZset();//操作有序set
StringRedisTemplate继承自RedisTemplate,也一样拥有上面这些操作。
StringRedisTemplate默认采用的是String的序列化策略,保存的key和value都是采用此策略序列化保存的