首页
导航
博客
电子书
算法
众创
代码
随贴
关于我们
您好,欢迎来到码863代码分享网! 请
[登录]
/
[注册]
搜 索
标题:
*
140
字
TAG标签:
(用空格隔开)
30
字
恢复历史版本:
请选择分类
html
python
javascript
php
sql
c
c++
c#
java
plain
所有人可见
仅自己可见
编辑器:UEditor
编辑器:TinyMCE
编辑器:Editor.md
HTML转MD
HTML转MD2
<blockquote><p><a href="https://www.jianshu.com/p/ece59725d717" _src="https://www.jianshu.com/p/ece59725d717">https://www.jianshu.com/p/ece59725d717</a> </p><p><br/></p><p>目前我们用到的 PHP 的 Redis 扩展 主要有2个,第一个是最常用的 phpredis, 它是用c写的php的高效扩展:https://github.com/phpredis/phpredis,还有1个是predis, 它是用php代码写的,也用的蛮多的:https://github.com/nrk/predis。</p><p><br/></p><p>始化连接到redis cluster服务器:</p><p>$obj_cluster = new RedisCluster(NULL, [‘127.0.0.1:7000', '127.0.0.1:7001', '127.0.0.1:7002']);</p><p>var_dump($obj_cluster);</p><p>第一个参数传NULL 别问我,我也不知道为啥。反正文档没找到,这篇也没看懂。</p><p>第二个参数是我们需要连接的redis cluster的master服务器列表。我这有3个master,就填3个。</p><p>打印结果如下:</p><p>class RedisCluster#5 (0) {}</p><p>一个RedisCluster 类资源。表示redis 已经连接成功了。</p><p>那么,我们就可以实用之前redis的方法来尝试了:</p><p>$obj_cluster->set('name11', '1111');</p><p>$obj_cluster->set('name22', '2222');</p><p>$obj_cluster->set('name33', '333');</p><p>$name1 = $obj_cluster->get('name11');</p><p>$name2 = $obj_cluster->get('name22');</p><p>$name3 = $obj_cluster->get('name33');</p><p>var_dump($name1, $name2, $name3);die;</p><p>结果如下:</p><p>string(4) "1111"</p><p>string(4) "2222"</p><p>string(3) "333"</p><p>我们连接客户端看下:</p><p>redis-cli -c -p 7004</p><p>127.0.0.1:7004> get name11</p><p>-> Redirected to slot [11311] located at 127.0.0.1:7002</p><p>"1111"</p><p>127.0.0.1:7002> get name22</p><p>-> Redirected to slot [2335] located at 127.0.0.1:7000</p><p>"2222"</p><p>127.0.0.1:7000> get name33</p><p>-> Redirected to slot [10767] located at 127.0.0.1:7001</p><p>"333"</p><p>127.0.0.1:7001></p><p>客户端是有跳转的,而php的扩展phpredis直接就给出结果了,这点很赞。</p><p>phpredis的使用</p><p>我们继续看这个蛋疼的文档,它还提供了一种连接方式:</p><p>// Connect and specify timeout and read_timeout</p><p>$obj_cluster = new RedisCluster(</p><p>NULL, Array("127.0.0.1:7000", "127.0.0.1:7001", 1.5, 1.5);</p><p>);</p><p>后面加入了timeout和read_timeout功能。就是加到master列表的后面。</p><p>timeout表示连接redis的最长时间,这里设为1.5秒,表示超过1.5秒要是还没连接成功就返回false 。</p><p>read_timeout表示连接redis成功后,读取一个key的超时时间,有时候读取一个key 可能value比较大,读取需要很长时间,这里设置1.5秒,表示要是过了1.5秒还没读取到数据就返回false。</p><p><br/><br/><br/></p><p><br/></p><p><br/></p><p><a href="https://www.php.cn/php-weizijiaocheng-391168.html" _src="https://www.php.cn/php-weizijiaocheng-391168.html">https://www.php.cn/php-weizijiaocheng-391168.html</a> </p><p style="margin-top: 15px; margin-bottom: 15px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); color: rgb(61, 70, 77); box-sizing: border-box; border-radius: 8px; font-family: " helvetica="" pingfang="" white-space:="" background-color:="">一、搭建redis集群 <br style="margin: 0px; padding: 0px;"/>1.概念解释 <br style="margin: 0px; padding: 0px;"/>redis 3.0之后版本支持redis-cluster集群,Redis-Cluster采用无中心结构,每个节点保存数据和整个集群状态,每个节点都和其他所有节点连接。其redis-cluster架构图如下: <br style="margin: 0px; padding: 0px;"/><img src="/ueditor/php/upload/image/20191207/1575721961656705.jpg" alt="这里写图片描述" title="" style="margin: 0px; padding: 0px; display: inline-block; vertical-align: middle; border: none; max-width: 760px;"/><br style="margin: 0px; padding: 0px;"/>其结构特点:</p><p class="line number1 index0 alt2" style="margin-top: 15px; margin-bottom: 15px; padding-top: 0px; padding-bottom: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); line-height: 1.5em; border-radius: 8px; color: rgb(61, 70, 77); box-sizing: border-box; padding-right: 1em !important; padding-left: 1em !important; border-left: 3px solid rgb(67, 90, 95) !important; white-space: pre-wrap !important;"><code class="php plain" style="padding: 2px 4px; font-size: 13px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px; font-family: Monaco, Menlo, Consolas, " courier="" margin:="" 0px="" background-image:="" none="" background-position:="" initial="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" border:="" bottom:="" auto="" float:="" left:="" line-height:="" 1.5em="" outline:="" overflow:="" visible="" position:="" static="" right:="" top:="" vertical-align:="" baseline="" width:="" box-sizing:="" content-box="" min-height:="">1、所有的redis节点彼此互联(PING-PONG机制),内部使用二进制协议优化传输速度和带宽。</code></p><p class="line number2 index1 alt1" style="margin-top: 15px; margin-bottom: 15px; padding-top: 0px; padding-bottom: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); line-height: 1.5em; border-radius: 8px; color: rgb(61, 70, 77); box-sizing: border-box; padding-right: 1em !important; padding-left: 1em !important; border-left: 3px solid rgb(67, 90, 95) !important; white-space: pre-wrap !important;"><code class="php plain" style="padding: 2px 4px; font-size: 13px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px; font-family: Monaco, Menlo, Consolas, " courier="" margin:="" 0px="" background-image:="" none="" background-position:="" initial="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" border:="" bottom:="" auto="" float:="" left:="" line-height:="" 1.5em="" outline:="" overflow:="" visible="" position:="" static="" right:="" top:="" vertical-align:="" baseline="" width:="" box-sizing:="" content-box="" min-height:="">2、节点的fail是通过集群中超过半数的节点检测失效时才生效。</code></p><p class="line number3 index2 alt2" style="margin-top: 15px; margin-bottom: 15px; padding-top: 0px; padding-bottom: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); line-height: 1.5em; border-radius: 8px; color: rgb(61, 70, 77); box-sizing: border-box; padding-right: 1em !important; padding-left: 1em !important; border-left: 3px solid rgb(67, 90, 95) !important; white-space: pre-wrap !important;"><code class="php plain" style="padding: 2px 4px; font-size: 13px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px; font-family: Monaco, Menlo, Consolas, " courier="" margin:="" 0px="" background-image:="" none="" background-position:="" initial="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" border:="" bottom:="" auto="" float:="" left:="" line-height:="" 1.5em="" outline:="" overflow:="" visible="" position:="" static="" right:="" top:="" vertical-align:="" baseline="" width:="" box-sizing:="" content-box="" min-height:="">3、客户端与redis节点直连,不需要中间proxy层.客户端不需要连接集群所有节点,连接集群中任何一个可用节点即可。</code></p><p class="line number4 index3 alt1" style="margin-top: 15px; margin-bottom: 15px; padding-top: 0px; padding-bottom: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); line-height: 1.5em; border-radius: 8px; color: rgb(61, 70, 77); box-sizing: border-box; padding-right: 1em !important; padding-left: 1em !important; border-left: 3px solid rgb(67, 90, 95) !important; white-space: pre-wrap !important;"><code class="php plain" style="padding: 2px 4px; font-size: 13px; color: rgb(199, 37, 78); background-color: rgb(249, 242, 244); border-radius: 4px; font-family: Monaco, Menlo, Consolas, " courier="" margin:="" 0px="" background-image:="" none="" background-position:="" initial="" background-size:="" background-repeat:="" background-attachment:="" background-origin:="" background-clip:="" border:="" bottom:="" auto="" float:="" left:="" line-height:="" 1.5em="" outline:="" overflow:="" visible="" position:="" static="" right:="" top:="" vertical-align:="" baseline="" width:="" box-sizing:="" content-box="" min-height:="">4、redis-cluster把所有的物理节点映射到[0-16383]slot上(不一定是平均分配),cluster 负责维护node<->slot<->value。<!-----><!-----><!-----><!-----><!-----><!-----></code></p><p class="line number5 index4 alt2" style="margin-top: 15px; margin-bottom: 15px; padding-top: 0px; padding-bottom: 0px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); line-height: 1.5em; border-radius: 8px; color: rgb(61, 70, 77); box-sizing: border-box; padding-right: 1em !important; padding-left: 1em !important; border-left: 3px solid rgb(67, 90, 95) !important; white-space: pre-wrap !important;"> <span style="background-color: rgb(249, 242, 244); color: rgb(199, 37, 78); font-family: Monaco, Menlo, Consolas, " courier="" font-size:="">5、Redis集群预分好16384个桶,当需要在 Redis 集群中放置一个 key-value 时,根据 CRC16(key) mod 16384的值,决定将一个key放到哪个桶中。</span></p><pre class="prettyprinttoolbar:false;toolbar:false;" style="margin-top: 0px; margin-bottom: 0px; padding: 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; overflow-wrap: break-word; background-color: rgb(216, 214, 214); font-size: 14px;"> 现在我们是三个主节点分别是:A, B, C 三个节点,它们可以是一台机器上的三个端口,也可以是三台不同的服务器。那么,采用哈希槽 (hash slot)的方式来分配16384个slot 的话,它们三个节点分别承担的slot 区间是: 节点A覆盖0-5460; 节点B覆盖5461-10922; 节点C覆盖10923-16383.</pre><p style="margin-top: 15px; margin-bottom: 15px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); color: rgb(61, 70, 77); box-sizing: border-box; border-radius: 8px; font-family: " helvetica="" pingfang="" white-space:="" background-color:="">看不懂,,,,</p><pre class="prettyprinttoolbar:false;toolbar:false;" style="margin-top: 0px; margin-bottom: 0px; padding: 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; overflow-wrap: break-word; background-color: rgb(216, 214, 214); font-size: 14px;"> 获取数据: 如果存入一个值,按照redis cluster哈希槽的算法: CRC16('key')384 = 6782。 那么就会把这个key 的存储分配到 B 上了。同样,当我连接(A,B,C)任何一个节点想获取'key'这个key时,也会这样的算法,然后内部跳转到B节点上获取数据</pre><p style="margin-top: 15px; margin-bottom: 15px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); color: rgb(61, 70, 77); box-sizing: border-box; border-radius: 8px; font-family: " helvetica="" pingfang="" white-space:="" background-color:="">2、Redis Cluster主从模式</p><pre class="prettyprinttoolbar:false;toolbar:false;" style="margin-top: 0px; margin-bottom: 0px; padding: 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; overflow-wrap: break-word; background-color: rgb(216, 214, 214); font-size: 14px;">redis cluster 为了保证数据的高可用性,加入了主从模式,一个主节点对应一个或多个从节点,主节点提供数据存取,从节点则是从主节点拉取数据备份,当这个主节点挂掉后,就会有这个从节点选取一个来充当主节点,从而保证集群不会挂掉。并且如果之前的旧主节点恢复正常时。</pre><pre class="prettyprinttoolbar:false;toolbar:false;" style="margin-top: 0px; margin-bottom: 0px; padding: 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; overflow-wrap: break-word; background-color: rgb(216, 214, 214); font-size: 14px;"> 上面那个例子里, 集群有ABC三个主节点, 如果这3个节点都没有加入从节点,如果B挂掉了,我们就无法访问整个集群了。A和C的slot也无法访问。 所以我们在集群建立的时候,一定要为每个主节点都添加了从节点, 比如像这样, 集群包含主节点A、B、C, 以及从节点A1、B1、C1, 那么即使B挂掉系统也可以继续正确工作。 B1节点替代了B节点,所以Redis集群将会选择B1节点作为新的主节点,集群将会继续正确地提供服务。 当B重新开启后,它就会变成B1的从节点。 不过需要注意,如果节点B和B1同时挂了,Redis集群就无法继续正确地提供服务了。</pre><p style="margin-top: 15px; margin-bottom: 15px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); color: rgb(61, 70, 77); box-sizing: border-box; border-radius: 8px; font-family: " helvetica="" pingfang="" white-space:="" background-color:="">3.redis集群的搭建</p><pre class="prettyprinttoolbar:false;toolbar:false;" style="margin-top: 0px; margin-bottom: 0px; padding: 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; overflow-wrap: break-word; background-color: rgb(216, 214, 214); font-size: 14px;">集群中至少应该有奇数个节点,所以至少有三个节点,每个节点至少有一个备份节点,所以下面使用6节点(主节点、备份节点由redis-cluster集群确定)。</pre><p style="margin-top: 15px; margin-bottom: 15px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); color: rgb(61, 70, 77); box-sizing: border-box; border-radius: 8px; font-family: " helvetica="" pingfang="" white-space:="" background-color:="">嗯,所有最少要6个redis服务。 <br style="margin: 0px; padding: 0px;"/>然后启动这6个redis服务,在本机上,但要记得启动前要进行修改配置。</p><pre class="prettyprinttoolbar:false;toolbar:false;" style="margin-top: 0px; margin-bottom: 0px; padding: 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; overflow-wrap: break-word; background-color: rgb(216, 214, 214); font-size: 14px;">port 7000 //端口7000,7002,7003 bind 10.93.84.53 //默认ip为127.0.0.1 需要改为其他节点机器可访问的ip 否则创建集群时无法访问对应的端口,无法创建集群daemonize yes //redis后台运行pidfile ./redis_7000.pid //pidfile文件对应7000,7001,7002cluster-enabled yes //开启集群 把注释#去掉cluster-config-file nodes.conf //集群的配置 配置文件首次启动自动生成 7000,7001,7002cluster-node-timeout 15000 //请求超时 默认15秒,可自行设置appendonly yes //aof日志开启 有需要就开启,它会每次写操作都记录一条日志 </pre><p style="margin-top: 15px; margin-bottom: 15px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); color: rgb(61, 70, 77); box-sizing: border-box; border-radius: 8px; font-family: " helvetica="" pingfang="" white-space:="" background-color:="">然后把redis源码中的src目录中的redis-trib.rb拷到当前路径中来。 <br style="margin: 0px; padding: 0px;"/>安装ruby环境</p><pre class="prettyprinttoolbar:false;toolbar:false;" style="margin-top: 0px; margin-bottom: 0px; padding: 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; overflow-wrap: break-word; background-color: rgb(216, 214, 214); font-size: 14px;">yum install ruby yum install rubygems gem install redis-3.2.2.gem</pre><p style="margin-top: 15px; margin-bottom: 15px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); color: rgb(61, 70, 77); box-sizing: border-box; border-radius: 8px; font-family: " helvetica="" pingfang="" white-space:="" background-color:="">查的一下本机6个redis的启动情况</p><pre class="prettyprinttoolbar:false;toolbar:false;" style="margin-top: 0px; margin-bottom: 0px; padding: 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; overflow-wrap: break-word; background-color: rgb(216, 214, 214); font-size: 14px;">ps -ef | grep redis</pre><p style="margin-top: 15px; margin-bottom: 15px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); color: rgb(61, 70, 77); box-sizing: border-box; border-radius: 8px; font-family: " helvetica="" pingfang="" white-space:="" background-color:="">最后我们使用redis-trib.rb来创建redis集群。使用create命令 –replicas 1 参数表示为每个主节点创建一个从节点,一对两个,第一个为主第二个为从。</p><pre class="prettyprinttoolbar:false;toolbar:false;" style="margin-top: 0px; margin-bottom: 0px; padding: 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; overflow-wrap: break-word; background-color: rgb(216, 214, 214); font-size: 14px;">./redis-trib.rb create --replicas 1 127.0.0.1:7001 127.0.0.1:7002 127.0.0.1:7003 127.0.0.1:7004 127.0.0.1:7005 127.0.0.1:7006</pre><p style="margin-top: 15px; margin-bottom: 15px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); color: rgb(61, 70, 77); box-sizing: border-box; border-radius: 8px; font-family: " helvetica="" pingfang="" white-space:="" background-color:="">使用redis-cli进行测试,要加得加-c参数,以集群方式进行连接。</p><pre class="prettyprinttoolbar:false;toolbar:false;" style="margin-top: 0px; margin-bottom: 0px; padding: 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; overflow-wrap: break-word; background-color: rgb(216, 214, 214); font-size: 14px;">redis-cli -c</pre><p style="margin-top: 15px; margin-bottom: 15px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); color: rgb(61, 70, 77); box-sizing: border-box; border-radius: 8px; font-family: " helvetica="" pingfang="" white-space:="" background-color:="">要进得加-c参数,代表连接的是集群方式。 <br style="margin: 0px; padding: 0px;"/>至此,redis集群搭建成功。 <br style="margin: 0px; padding: 0px;"/>但是我这里只是要本机测试的6个端口,真实的话最少都是要两台机器的。我们怎么做安全性呢? <br style="margin: 0px; padding: 0px;"/>1.修改redis.conf把bind选项改成当前的局域网ip <br style="margin: 0px; padding: 0px;"/>2.配置防火墙只允许另一台的redis服务器进行访问,当然也要允许应用服务器的ip进行访问吧。。。不然程序都连不上redis服务,玩毛啊。。。。</p><pre class="prettyprinttoolbar:false;toolbar:false;" style="margin-top: 0px; margin-bottom: 0px; padding: 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; overflow-wrap: break-word; background-color: rgb(216, 214, 214); font-size: 14px;">//先关掉6379(redis服务使用的端口)iptables -I INPUT -p tcp --dport 6379 -j DROP //允许192.168.1.0这个机器进行访问本机的6379端口iptables -I INPUT -s 192.168.1.0 -p tcp --dport 6379 -j ACCEPT</pre><p style="margin-top: 15px; margin-bottom: 15px; padding: 0px 0px 0px 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); color: rgb(61, 70, 77); box-sizing: border-box; border-radius: 8px; font-family: " helvetica="" pingfang="" white-space:="" background-color:="">二、php中连接到redis集群 <br style="margin: 0px; padding: 0px;"/>首先使用php –ri redis来查看redis扩展版本。要在3.0版本之上。 <br style="margin: 0px; padding: 0px;"/>参考 <br style="margin: 0px; padding: 0px;"/>第二个参数只要集群中的一个节点就行了,并不需要全填上</p><pre class="prettyprinttoolbar:false;toolbar:false;" style="margin-top: 0px; margin-bottom: 0px; padding: 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); white-space: pre-wrap; overflow-wrap: break-word; background-color: rgb(216, 214, 214); font-size: 14px;">$obj_cluster = new RedisCluster(NULL, ['127.0.0.1:6380']);echo $obj_cluster->get('name1');</pre></blockquote><p><br/><br/><br/></p><pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(28, 31, 33); font-size: 14px; background-color: rgb(248, 250, 252);">phpredis和Predis性能对比 https://www.imooc.com/article/271976 使用phpredis操作集群性能和单机相差一倍左右, predis和phpredis差距有点大, 在实际编程中可以按需要去选择</pre><pre style="margin-top: 0px; margin-bottom: 0px; padding: 0px; color: rgb(28, 31, 33); font-size: 14px; background-color: rgb(248, 250, 252);"><br/></pre><p><span style="color: rgb(28, 31, 33); font-family: " pingfang="" microsoft="" helvetica="" font-size:="" background-color:=""></span></p><audio controls="controls" style="display: none;"></audio>
CopyRight 2002~2023 精通2100网 联系邮箱:qqtxt@163.com
版权所有:精通2100网
湘ICP备2023018646号-1
MYSQl共执行 4 个查询,用时 0.002194881439209 秒,PHP脚本用时 0.005004 秒,占用内存 0.667 MB,Gzip 已启用