天天看點

Redis叢集服務搭建

為什麼要有叢集

a)伺服器可能因為代碼原因,人為原因,或者自然災害等造成伺服器損壞。資料服務就挂掉了

b)大公司都會有很多的伺服器(華東地區、華南地區、華中地區、華北地區、西北地區、西南地區、東北地區、台港澳地區機房)

叢集的概念

叢集是一組互相獨立的、通過高速網絡互聯的計算機,它們構成了一個組,并以單一系統的模式加以管理。一個客戶與叢集互相作用時,叢集像是一個獨立的伺服器。叢集配置是用于提高可用性和可縮放性。

Redis叢集服務搭建

當請求到來首先由負載均衡伺服器處理,把請求轉發到另外的一台伺服器上。

百度的ip位址 119.75.217.109/

61.135.169.121/

Redis叢集

分類

1)軟體層面

2)硬體層面

軟體層面:隻有一台電腦,在這台電腦上啟動了多台redis服務

Redis叢集服務搭建

硬體層面:存在多台實體電腦,每台電腦都啟動了一個redis或者多個redis服務

Redis叢集服務搭建

參考閱讀

Redis搭建叢集http://www.cnblogs.com/wuxl360/p/5920330.html

go語言redis-cluster開源用戶端https://github.com/gitstliu/go-redis-cluster

配置機器1

1)在示範中,192.168.110.37為目前ubuntu機器的ip

2)在192.168.110.37上進⼊Desktop⽬錄,建立conf⽬錄

3)在conf⽬錄下建立⽂件7000.conf,編輯内容如下

在這裡插入代碼片port 7000
bind 192.168.110.37
daemonize yes
pidfile 7000.pid
cluster-enabled yes
cluster-config-file 7000_node.conf
cluster-node-timeout 15000
appendonly yese
           

在conf⽬錄下建立⽂件7001.conf,編輯内容如下

port 7001
bind 192.168.110.37
daemonize yes
pidfile 7001.pid
cluster-enabled yes
cluster-config-file 7001_node.conf
cluster-node-timeout 15000
appendonly yes
           

在conf⽬錄下建立⽂件7002.conf,編輯内容如下

port 7002
bind 192.168.110.37
daemonize yes
pidfile 7002.pid
cluster-enabled yes
cluster-config-file 7002_node.conf
cluster-node-timeout 15000
appendonly yes
           

總結:這三個檔案的配置差別隻有port、pidfile、cluster-config-file三項

使用配置檔案啟動redis服務

redis-server 7000.conf
redis-server 7001.conf
redis-server 7002.conf
           

配置機器2

1)在示範中,192.168.110.38為目前ubuntu機器的ip

2)在192.168.110.38上進⼊Desktop⽬錄,建立conf⽬錄

3)在conf⽬錄下建立⽂件7003.conf,編輯内容如下

port 7003
bind 192.168.110.38
daemonize yes
pidfile 7003.pid
cluster-enabled yes
cluster-config-file 7003_node.conf
cluster-node-timeout 15000
appendonly yes
           

在conf⽬錄下建立⽂件7004.conf,編輯内容如下

port 7004
bind 192.168.110.38
daemonize yes
pidfile 7004.pid
cluster-enabled yes
cluster-config-file 7004_node.conf
cluster-node-timeout 15000
appendonly yes
           

在conf⽬錄下建立⽂件7005.conf,編輯内容如下

port 7005
bind 192.168.110.38
daemonize yes
pidfile 7005.pid
cluster-enabled yes
cluster-config-file 7005_node.conf
cluster-node-timeout 15000
appendonly yes
           

總結:這三個檔案的配置差別隻有port、pidfile、cluster-config-file三項

使用配置檔案啟動redis服務

redis-server 7003.conf
redis-server 7004.conf
redis-server 7005.conf

           

建叢集

1)redis的安裝包中包含了redis-trib.rb,⽤于建立叢集 //ruby

2)接下來的操作在192.168.110.37機器上進⾏

3)将指令複制,這樣可以在任何⽬錄下調⽤此指令

sudo cp /usr/share/doc/redis-tools/examples/redis-trib.rb /usr/local/bin/
           

4)安裝ruby環境,因為redis-trib.rb是⽤ruby開發的

sudo apt-get install ruby
           

5)在提示資訊處輸⼊y,然後回⻋繼續安裝

運⾏如下指令建立叢集

redis-trib.rb create --replicas 1 192.168.110.37:7000 192.168.110.37:7001 192.168.110.37:7002 192.168.110.38:7003 192.168.110.38:7004 192.168.110.38:7005
           

執⾏上⾯這個指令在某些機器上可能會報錯,主要原因是由于安裝的 ruby 不是最 新版本

天朝的防⽕牆導緻⽆法下載下傳最新版本,是以需要設定 gem 的源

解決辦法如下:

//先檢視⾃⼰的 gem 源是什麼位址
gem source -l // 如果是https://rubygems.org/ 就需要更換
// 更換指令為
gem sources --add https://gems.ruby-china.com --remove https://rubygems.org/
// 通過 gem 安裝 redis 的相關依賴
sudo gem install redis
// 然後重新執⾏指令
redis-trib.rb create --replicas 1 192.168.110.37:7000 192.168.110.37:7001 192.168.110.37:7002 192.168.110.38:7003 192.168.110.38:7004 192.168.110.38:7005
           

提示完成,叢集搭建成功

注意點

1)Redis 叢集會把資料存在⼀個 master 節點,然後在這個 master 和其對應的salve 之間進⾏資料同步。當讀取資料時,也根據⼀緻性雜湊演算法到對應的 master 節 點擷取資料。隻有當⼀個master 挂掉之後,才會啟動⼀個對應的 salve 節點,充 當 master

2)需要注意的是:必須要3個或以上的主節點,否則在建立叢集時會失敗,并且當存 活的主節點數⼩于總節點數的⼀半時,整個叢集就⽆法提供服務了

go語言redis-cluster開源用戶端

安裝:

go get github.com/gitstliu/go-redis-cluster
           

示例代碼:

func (this*ClusterController)Get(){
	cluster, _ := redis.NewCluster(
		&redis.Options{
			StartNodes: []string{"192.168.110.37:7000", "192.168.110.37:7001", "192.168.110.37:7002","192.168.110.38:7003","192.168.110.38:7004","192.168.110.38:7005"},
			ConnTimeout: 50 * time.Millisecond,
			ReadTimeout: 50 * time.Millisecond,
			WriteTimeout: 50 * time.Millisecond,
			KeepAlive: 16,
			AliveTime: 60 * time.Second,
		})
	cluster.Do("set","name","itheima")

	name,_ := redis.String(cluster.Do("get","name"))
	beego.Info(name)
	this.Ctx.WriteString("叢集建立成功")
}