1. 程式人生 > >Redis叢集伺服器搭建

Redis叢集伺服器搭建

為什麼要有叢集

a)伺服器可能因為程式碼原因,人為原因,或者自然災害等造成伺服器損壞。資料服務就掛掉了 b)大公司都會有很多的伺服器(華東地區、華南地區、華中地區、華北地區、西北地區、西南地區、東北地區、臺港澳地區機房)

叢集的概念

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

在這裡插入圖片描述

當請求到來首先由負載均衡伺服器處理,把請求轉發到另外的一臺伺服器上。 百度的ip地址 119.75.217.109/ 61.135.169.121/ Redis叢集

分類 1)軟體層面 2)硬體層面 軟體層面:只有一臺電腦,在這臺電腦上啟動了多臺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("叢集建立成功")
}