Ubuntu16.04.1上搭建分散式的Redis叢集,並使用C#操作
為什麼要叢集:
通常為了,提高網站的響應速度,總是把一些經常用到的資料放到記憶體中,而不是放到資料庫中,Redis是一個很好的Cache工具,當然了還有Memcached,這裡只講Redis。在我們的電商系統中,熱點資料量往往巨大,比如單點登入、使用者瀏覽商品的資訊、使用者資訊、使用者收藏的商品資訊、短息提醒資料等等,也都用到了redis,如何使redis可以橫向可伸縮擴充套件,這需要由多臺機器協同提供服務,一臺掛掉了,另一臺馬上頂上去,即分散式的redis叢集,就對系統的效能非常重要。
Redis叢集的幾個重要特徵:
(1).Redis 叢集的分片特徵在於將鍵空間分拆了16384個槽位,每一個節點負責其中一些槽位。
(2).Redis提供一定程度的可用性,可以在某個節點宕機或者不可達的情況下繼續處理命令.
(3).Redis 叢集中不存在中心(central)節點或者代理(proxy)節點, 叢集的其中一個主要設計目標是達到線性可擴充套件性(linear scalability)。
(4).Redis叢集要想正常工作,必須要三個主節點,在我搭建的叢集環境中,三個主節點都需要有一個從節點,所以一共六個節點,通俗來講也就是需要開啟6個redis服務。
一、安裝Redis
關於如何在Linux上安裝Redis,可以參考我的這篇部落格,裡面有詳細的安裝步驟,注意在搭建Redis的叢集環境我使用的Redis版本為4.0.1《
整體結構:
二、進群環境的配置
(1)在這裡我就開啟三臺Ubuntu16.04.1,因為電腦8G的記憶體不夠用,如果你電腦記憶體大可以開啟4臺或者更多。在這三臺中我都安裝好了最新版本的Redis。
(2)叢集配置檔案的修改
第一臺配置檔案的修改:首先在usr/redis目錄下,把上面安裝好的的redis檔案放到該目錄下面,具體哪些檔案可參考下圖。
需要的檔案有:
然後通過下面的命令把上面的檔案複製到usr/redis/目錄下面:
首先在usr目錄下面建立一個redis和s2目錄:sudo mkdir redis; sudo mkdir s2
目錄建立好後,到上面圖中的目錄下面,開啟終端,執行:cp redis* /usr/redis/,上面圖中的檔案就會到usr/redis/目錄下面,最後到usr/redis/目錄下面,開啟終端,執行sudo cp redis* /usr/redis/s2/,這是該目錄下面也就會有相同的檔案了。
修改redis.conf檔案中Cluster的配置,修改如下:
首先由於許可權的問題,我們先要切換到root身份:sudo passwd root命令,先修改root的密碼,修改之後,再執行su root 接著輸入你設定的root密碼就可以切換到root身份,如下:
最後在root身份執行下圖中的命令就可修改redis.conf檔案:
這裡只貼出來修改的程式碼:(紅色標註的部分為修改部分)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # bind 127.0.0.1 //註釋掉 # Protected mode is a layer of security protection, in order to avoid that # Redis instances left open on the internet are accessed and exploited. # # When protected mode is on and if: # # 1) The server is not binding explicitly to a set of addresses using the # "bind" directive. # 2) No password is configured. # # The server only accepts connections from clients connecting from the # IPv4 and IPv6 loopback addresses 127.0.0.1 and ::1, and from Unix domain # sockets. # # By default protected mode is enabled. You should disable it only if # you are sure you want clients from other hosts to connect to Redis # even if no authentication is configured, nor a specific set of interfaces # are explicitly listed using the "bind" directive. protected-mode no //yes該為no # Accept connections on the specified port, default is 6379 (IANA #815344). # If port 0 is specified Redis will not listen on a TCP socket. port 6379 # TCP listen() backlog. # # In high requests-per-second environments you need an high backlog in order # to avoid slow clients connections issues. Note that the Linux kernel # will silently truncate it to the value of /proc/sys/net/core/somaxconn so # make sure to raise both the value of somaxconn and tcp_max_syn_backlog # in order to get the desired effect. tcp-backlog 511 # Unix socket. # # Specify the path for the Unix socket that will be used to listen for # incoming connections. There is no default, so Redis will not listen # on a unix socket when not specified. # # unixsocket /tmp/redis.sock # unixsocketperm 700 # Close the connection after a client is idle for N seconds (0 to disable) timeout 0 # TCP keepalive. # # If non-zero, use SO_KEEPALIVE to send TCP ACKs to clients in absence # of communication. This is useful for two reasons: # # 1) Detect dead peers. # 2) Take the connection alive from the point of view of network # equipment in the middle. # # On Linux, the specified value (in seconds) is the period used to send ACKs. # Note that to close the connection the double of the time is needed. # On other kernels the period depends on the kernel configuration. # # A reasonable value for this option is 300 seconds, which is the new # Redis default starting with Redis 3.2.1. tcp-keepalive 300 ################################ REDIS CLUSTER ############################### # # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # WARNING EXPERIMENTAL: Redis Cluster is considered to be stable code, however # in order to mark it as "mature" we need to wait for a non trivial percentage # of users to deploy it in production. # ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # # Normal Redis instances can't be part of a Redis Cluster; only nodes that are # started as cluster nodes can. In order to start a Redis instance as a # cluster node enable the cluster support uncommenting the following: # cluster-enabled yes # Every cluster node has a cluster configuration file. This file is not # intended to be edited by hand. It is created and updated by Redis nodes. # Every Redis Cluster node requires a different cluster configuration file. # Make sure that instances running in the same system do not have # overlapping cluster configuration file names. # cluster-config-file nodes-6379.conf # Cluster node timeout is the amount of milliseconds a node must be unreachable # for it to be considered in failure state. # Most other internal time limits are multiple of the node timeout. # # cluster-node-timeout 15000 # A slave of a failing master will avoid to start a failover if its data # looks too old. # # There is no simple way for a slave to actually have an exact measure of # its "data age", so the following two checks are performed: # # 1) If there are multiple slaves able to failover, they exchange messages # in order to try to give an advantage to the slave with the best # replication offset (more data from the master processed). # Slaves will try to get their rank by offset, and apply to the start # of the failover a delay proportional to their rank. # # 2) Every single slave computes the time of the last interaction with # its master. This can be the last ping or command received (if the master # is still in the "connected" state), or the time that elapsed since the # disconnection with the master (if the replication link is currently down). # If the last interaction is too old, the slave will not try to failover # at all. # # The point "2" can be tuned by user. Specifically a slave will not perform # the failover if, since the last interaction with the master, the time # elapsed is greater than: # # (node-timeout * slave-validity-factor) + repl-ping-slave-period # # So for example if node-timeout is 30 seconds, and the slave-validity-factor # is 10, and assuming a default repl-ping-slave-period of 10 seconds, the # slave will not try to failover if it was not able to talk with the master # for longer than 310 seconds. # # A large slave-validity-factor may allow slaves with too old data to failover # a master, while a too small value may prevent the cluster from being able to # elect a slave at all. # # For maximum availability, it is possible to set the slave-validity-factor # to a value of 0, which means, that slaves will always try to failover the # master regardless of the last time they interacted with the master. # (However they'll always try to apply a delay proportional to their # offset rank). # # Zero is the only value able to guarantee that when all the partitions heal # the cluster will always be able to continue. # # cluster-slave-validity-factor 10 # Cluster slaves are able to migrate to orphaned masters, that are masters # that are left without working slaves. This improves the cluster ability # to resist to failures as otherwise an orphaned master can't be failed over # in case of failure if it has no working slaves. # # Slaves migrate to orphaned masters only if there are still at least a # given number of other working slaves for their old master. This number # is the "migration barrier". A migration barrier of 1 means that a slave # will migrate only if there is at least 1 other working slave for its master # and so forth. It usually reflects the number of slaves you want for every # master in your cluster. # # Default is 1 (slaves migrate only if their masters remain with at least # one slave). To disable migration just set it to a very large value. # A value of 0 can be set but is useful only for debugging and dangerous # in production. # # cluster-migration-barrier 1 # By default Redis Cluster nodes stop accepting queries if they detect there # is at least an hash slot uncovered (no available node is serving it). # This way if the cluster is partially down (for example a range of hash slots # are no longer covered) all the cluster becomes, eventually, unavailable. # It automatically returns available as soon as all the slots are covered again. # # However sometimes you want the subset of the cluster which is working, # to continue to accept queries for the part of the key space that is still # covered. In order to do so, just set the cluster-require-full-coverage # option to no. # # cluster-require-full-coverage yes # In order to setup your cluster make sure to read the documentation # available at http://redis.io web site.
修改完之後 按下ESC 鍵,再按下 :wq!儲存突出。同理,s2中的redis也是這樣修改的但是,需要修改一下埠號,不能喝上面的埠號重複,s2中的埠號為6390。
使用相同的方式修改第二臺上redis的配置:只不過在該臺機器上只有一個redis服務(節點)
上面這兩臺伺服器中的三個redis服務地址分別為:192.168.0.109:6379 192.168.0.109:6390 192.168.0.111:6379,在進行第三部的時候要用到。
(3)第三臺機器的配置
上面的兩步中我們共開起了三臺主redis節點,所以在第三臺伺服器上我們也要開啟三臺從節點,結果如下:
其中s1、s2、s3中檔案和上面的一樣,即三臺從的redis節點。我們需要一個一個安裝上面的配置修改redis.conf檔案,只不過需要注意,三個節點的埠號是不一樣的。s1、s2、s3的埠號分別是:6379 6380 6381
到現在個個redis節點的配置檔案也修改好了,下面我們要分別啟動s1 s2 s3 和上面的三個主redis服務了~~~~~~
接下來,這三個服務都會生產一個這樣的檔案:
因為redis-trib.rb是ruby寫的,不信可以開啟看看。而在我們的Ubuntu16.04.1上是有ruby的環境的,但是還是強烈建議執行以下下面的指令 安裝ruby,同時還要安裝 ruby的Redis庫,不過沒關係,我們可以按照下面的步驟安裝。
- 安裝Redis-Cluster的依賴庫(可能在安裝依賴庫的時候特別慢,可以使用加速器,我就不在這裡說了,你懂的)
-
安裝Ruby
sudo apt-get install ruby
-
安裝Redis.gemsudo gem intall redis
-
耐心等待,因為很慢的(#^.^#)。安裝好之後,執行下面圖中劃的命令:
解釋:./redis-trib.rb create --replicas 1 192.168.0.109:6379 192.168.0.109:6390 192.168.0.110:6379 192.168.0.111:6379 192.168.0.111:6380 192.168.0.111:6381
這些IP都是上面redis服務的IP,注意他們之間是有空格分開的。
如果出現上面的資訊說明安裝成功了!!!
其中:192.168.0.109:6379、192.168.0.110:6379/192.168.0.111:6379是主,192.168.0.111:6380/192.168.0.109:6390、192.168.0.111:6381是從的。
(4)測試是否成功
在到192.168.0.110:6379中看看有沒有值:
看到沒,值同步過來了!!!!
三、面對Redis-ClusterC#該如何操作
step1:使用VS2017新建一個控制檯程式
step2: Install-Package StackExchange.Redis
step3:編寫程式碼:
1 using StackExchange.Redis; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace LinuxRedisCluster 9 { 10 class Program 11 { 12 static void Main(string[] args) 13 { 14 using (ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("192.168.0.109:6379,192.168.0.109:6390,192.168.0.110:6379,192.168.0.111:6379,192.168.0.111:6380,192.168.0.111:6381")) 15 { 16 17 IDatabase db = redis.GetDatabase(); 18 for (int i = 0; i < 1000; i++) 19 { 20 db.StringSet("name"+i, "guozheng"+i); 21 } 22 23 24 //var myname = db.StringGet("name"); 25 // Console.WriteLine(myname); 26 } 27 28 Console.ReadKey(); 29 } 30 } 31 }
step4:檢視結果:
四、總結:
關於redis如何叢集的詳細資訊,可以參考Redis手冊:叢集教程
也可以參考下面這些部落格:
http://www.cnblogs.com/huangxincheng/p/5615037.html
http://www.cnblogs.com/piscesLoveCc/p/5779795.html
覺得可以的話,希望點下推薦哈~你們的推薦是我的動力。
基礎篇
實戰篇
作者:郭崢
出處:http://www.cnblogs.com/runningsmallguo/
本文版權歸作者和部落格園共有,歡迎轉載,但未經作者同意必須保留此段宣告,且在文章頁面明顯位置給出原文連結。