仅作为Shane个人笔记
主变从(读写变Read Only)
redis-cli -p 26379
info
从图中可以看出Sentinel也是可以执行一些命令的 查阅了Redis官网的一些信息之后,Shane发现通过Sentinel命令可以得到主从服务器的信息(端口号、地址等)
SENTINEL masters
既然能够得到主节点的信息(端口号、地址),那在程序执行写的操作之前,先进行查询不就行了吗
用TP6执行生命令(RawCommand)
在TP6框架源码目录下/vendor/topthink/framework/src/think/cache/driver/ 在这个目录下有一个Redis类,是TP6提供的Redis扩展 打开之后可以发现其中的扩展并不完整(没有RawCommand),需要进一步扩展
public function rawCommand($command, $arguments){
return $this -> handler -> rawCommand($command, $arguments);
}
将代码写入保存 然后在配置文件cache里写入一个新配置用来连接Sentinel
sentinel => [
host => 127.0.0.1,
port => 26379,
type => redis,
timeout => 0,
select => 0
]
创建一个类,Shane取名SentinelWork
public function __construct()
{
$sentinelInfo = Cache::store(sentinel) -> rawCommand(SENTINEL, masters);
halt($sentinelInfo);
}
写一个__construct方法并执行刚刚扩展好的rawCommand方法 注意:没扩展则执行不了这个方法 可以看到返回的信息和在终端执行看到的是一模一样的 接着做一个配置模板
/**
* passadmin为上上上篇笔记约定的
*/
private $master = [
host => 127.0.0.1,
port => 6379,
password => passadmin,
select => 0,
timeout => 0
];
写完后在__construct方法中替换配置模板的host和port
public function __construct()
{
$sentinelInfo = Cache::store(sentinel) -> rawCommand(SENTINEL, masters);
$this -> master[host] = $sentinelInfo[0][3];
$this -> master[port] = $sentinelInfo[0][5];
}
写完后,再为自己创造的Redis扩展,扩展一个set方法
/**
* 这里先halt打印后续执行结果
*/
public function set($key, $value){
$redis = new Redis($this -> master);
halt($redis -> set($key,$value));
}
<?php
/**
*
*
* @description: オラ!オラ!オラ!オラ!
* @author: Shane
* @time: 2020/4/19 19:04
*
*/
namespace appclustercontroller;
use appBaseController;
use thinkApp;
use thinkcachedriverRedis;
use thinkfacadeCache;
class SentinelWork extends BaseController
{
private $master = [
host => 127.0.0.1,
port => 6379,
password => passadmin,
select => 0,
timeout => 0
];
public function __construct()
{
$sentinelInfo = Cache::store(sentinel) -> rawCommand(SENTINEL, masters);
$this -> master[host] = $sentinelInfo[0][3];
$this -> master[port] = $sentinelInfo[0][5];
}
public function set($key, $value){
$redis = new Redis($this -> master);
halt($redis -> set($key,$value));
}
}
注:这里可以不继承BaseController,直接将SentinelWork作为lib类库使用 完成后,创建一个测试控制器,引入SentinelWork调用其中的set方法(这里Shane输入redis:666这个键值对) 返回true执行成功,再来终端进入cli查看下是否成功 成功查询到数据,再去另一台云服务器看看 同样成功查询到数据
总结
通过上述操作,当主服务器挂掉之后,Sentinel选举出新的主服务器之后 不会导致原来的代码依旧去请求已经挂掉的服务器或是启动之后显示Read Only报错 而是直接通过Sentinel返回的新的主服务器的信息(端口号和地址)来进行写的操作 用ThinkPHP6制作后台