標籤:redis資料庫、緩衝和訊息中介軟體
一.Redis簡介
Redis 是一個開源(BSD許可)的,記憶體中的資料結構儲存系統,它可以用作資料庫、緩衝和訊息中介軟體。 它支援多種類型的資料結構,如 字串(strings), 散列(hashes), 列表(lists), 集合(sets), 有序集合(sorted sets) 與範圍查詢, bitmaps, hyperloglogs 和 地理空間(geospatial) 索引半徑查詢。 Redis 內建了 複製(replication),LUA指令碼(Lua scripting), LRU驅動事件(LRU eviction),事務(transactions) 和不同層級的 磁碟持久化(persistence), 並通過 Redis哨兵(Sentinel)和自動 分區(Cluster)提供高可用性(high availability)
二.安裝redis
650) this.width=650;" src="https://s2.51cto.com/oss/201710/19/066a9c742baab6ecd8930669b51fdaf5.png" style="float:none;" title="Screenshot from 2017-10-18 15-37-09.png" alt="066a9c742baab6ecd8930669b51fdaf5.png" />
650) this.width=650;" src="https://s2.51cto.com/oss/201710/19/04afe8aeb24129b4a9db914167cf91c6.png" style="float:none;" title="Screenshot from 2017-10-18 15-38-17.png" alt="04afe8aeb24129b4a9db914167cf91c6.png" />
2.啟動redis
650) this.width=650;" src="https://s2.51cto.com/oss/201710/19/1cefc06f2a794c9758673e07b78ec7c9.png" style="float:none;" title="Screenshot from 2017-10-18 15-39-39.png" alt="1cefc06f2a794c9758673e07b78ec7c9.png" />
三.安裝redis
bind 全網段
650) this.width=650;" src="https://s4.51cto.com/oss/201710/19/af39ed6923296ea51ed3853f1653a29a.png" style="float:none;" title="Screenshot from 2017-10-18 15-52-22.png" alt="af39ed6923296ea51ed3853f1653a29a.png" />
650) this.width=650;" src="https://s3.51cto.com/oss/201710/19/e9b48eb65e7ebf8f3abe2ef1333f124e.png" style="float:none;" title="Screenshot from 2017-10-18 15-53-20.png" alt="e9b48eb65e7ebf8f3abe2ef1333f124e.png" />
進入redis,建立資料
資料是記憶體快照,儲存到/var/lib/redis,格式為rdb
650) this.width=650;" src="https://s5.51cto.com/oss/201710/19/e76d402e664f8223a1f8f7d75d1289eb.png" style="float:none;" title="Screenshot from 2017-10-18 15-54-57.png" alt="e76d402e664f8223a1f8f7d75d1289eb.png" />、
四.在server2和server3安裝redis,測試資料同步
650) this.width=650;" src="https://s5.51cto.com/oss/201710/19/9afe20fb0a1a3d16c48e7d9ee98d7cd4.png" style="float:none;" title="Screenshot from 2017-10-18 15-55-45.png" alt="9afe20fb0a1a3d16c48e7d9ee98d7cd4.png" />
650) this.width=650;" src="https://s1.51cto.com/oss/201710/19/fc8a694465cb641b97828a0966c27bc3.png" style="float:none;" title="Screenshot from 2017-10-18 15-57-39.png" alt="fc8a694465cb641b97828a0966c27bc3.png" />
650) this.width=650;" src="https://s3.51cto.com/oss/201710/19/7d2b3b93b71c377241e9cc3766a13b57.png" style="float:none;" title="Screenshot from 2017-10-18 16-05-55.png" alt="7d2b3b93b71c377241e9cc3766a13b57.png" />
可以查看到server2和server3資料同步
650) this.width=650;" src="https://s3.51cto.com/oss/201710/19/760eed070aa82c4532d49cd1e36264ce.png" style="float:none;" title="Screenshot from 2017-10-18 16-06-54.png" alt="760eed070aa82c4532d49cd1e36264ce.png" />
650) this.width=650;" src="https://s3.51cto.com/oss/201710/19/459fca762d7d3e87eac7693a52dbcb69.png" style="float:none;" title="Screenshot from 2017-10-18 16-07-00.png" alt="459fca762d7d3e87eac7693a52dbcb69.png" />
650) this.width=650;" src="https://s2.51cto.com/oss/201710/19/96a0c4671db30d9ec7b5cd6cb7c37aa3.png" style="float:none;" title="Screenshot from 2017-10-18 16-10-14.png" alt="96a0c4671db30d9ec7b5cd6cb7c37aa3.png" />
650) this.width=650;" src="https://s2.51cto.com/oss/201710/19/ddbce8363e7052790ac21e3f10462f1b.png" style="float:none;" title="Screenshot from 2017-10-18 16-10-51.png" alt="ddbce8363e7052790ac21e3f10462f1b.png" />
五.Redis的哨兵
Redis的哨兵(sentinel)系統用於管理多個 Redis伺服器,該系統執行以下三個任務:
· 監控(Monitoring):哨兵(sentinel)會不斷地檢查你的Master和Slave是否運作正常。
· 提醒(Notification):當被監控的某個 Redis出現問題時,哨兵(sentinel)可以通過 API 向管理員或者其他應用程式發送通知。
· 自動故障遷移(Automatic failover):當一個Master不能正常工作時,哨兵(sentinel)會開始一次自動故障遷移操作,它會將失效Master的其中一個Slave升級為新的Master,並讓失效Master的其他Slave改為複製新的Master;當用戶端試圖串連失效的Master時,叢集也會向用戶端返回新Master的地址,使得叢集可以使用Master代替失效Master。
1.在server1配置sentinel哨兵檔案
650) this.width=650;" src="https://s4.51cto.com/oss/201710/19/4408b75cf0fafa655eed77d2b8ad3ad2.png" style="float:none;" title="Screenshot from 2017-10-18 16-47-57.png" alt="4408b75cf0fafa655eed77d2b8ad3ad2.png" />
server1為主,server2和server3為從
650) this.width=650;" src="https://s1.51cto.com/oss/201710/19/55de8a236048f4c083312ade200951a8.png" style="float:none;" title="Screenshot from 2017-10-18 17-07-30.png" alt="55de8a236048f4c083312ade200951a8.png" />
2.將sentinel.conf檔案傳給server2和server3
650) this.width=650;" src="https://s4.51cto.com/oss/201710/19/3d3c5d7e453fc9c80926dfe0a3c1067b.png" style="float:none;" title="Screenshot from 2017-10-18 16-48-08.png" alt="3d3c5d7e453fc9c80926dfe0a3c1067b.png" />
650) this.width=650;" src="https://s4.51cto.com/oss/201710/19/990a49998a60b20d146f47cf32dc37f9.png" style="float:none;" title="Screenshot from 2017-10-18 16-48-40.png" alt="990a49998a60b20d146f47cf32dc37f9.png" />
3.server1、server2、server3啟動哨兵
650) this.width=650;" src="https://s2.51cto.com/oss/201710/19/876b5922b689320bc8e0eb9471a8872c.png" style="float:none;" title="Screenshot from 2017-10-18 16-49-26.png" alt="876b5922b689320bc8e0eb9471a8872c.png" />
650) this.width=650;" src="https://s1.51cto.com/oss/201710/19/cad7071c51393eebee9fd8a53e0ce920.png" style="float:none;" title="Screenshot from 2017-10-18 16-49-32.png" alt="cad7071c51393eebee9fd8a53e0ce920.png" />
650) this.width=650;" src="https://s1.51cto.com/oss/201710/19/40f407aeed6fd357864b8fdcd26789f0.png" style="float:none;" title="Screenshot from 2017-10-18 16-49-56.png" alt="40f407aeed6fd357864b8fdcd26789f0.png" />
650) this.width=650;" src="https://s1.51cto.com/oss/201710/19/12b9b54c9ac52c6e886cc775c7930e60.png" style="float:none;" title="Screenshot from 2017-10-18 16-50-14.png" alt="12b9b54c9ac52c6e886cc775c7930e60.png" />
4.將server1 主節點shutdown,30s後,在server3上查看master切換到server2
650) this.width=650;" src="https://s1.51cto.com/oss/201710/19/fbb1c8728fc8fec73a267ba8225493e1.png" style="float:none;" title="Screenshot from 2017-10-18 17-25-43.png" alt="fbb1c8728fc8fec73a267ba8225493e1.png" />
650) this.width=650;" src="https://s3.51cto.com/oss/201710/19/af5f606bfaaedad82b8ef1a642273af3.png" style="float:none;" title="Screenshot from 2017-10-18 17-26-14.png" alt="af5f606bfaaedad82b8ef1a642273af3.png" />
650) this.width=650;" src="https://s4.51cto.com/oss/201710/19/20fafaa79c1bfa260377b8c8e7d945ec.png" style="float:none;" title="Screenshot from 2017-10-18 17-26-41.png" alt="20fafaa79c1bfa260377b8c8e7d945ec.png" />
4.重新啟動,將server1的sentinel.conf檔案裡的主修改為server2
650) this.width=650;" src="https://s3.51cto.com/oss/201710/19/e02c5fb860458147d4d78cae8f1468ea.png" style="float:none;" title="Screenshot from 2017-10-18 17-32-37.png" alt="e02c5fb860458147d4d78cae8f1468ea.png" />
650) this.width=650;" src="https://s3.51cto.com/oss/201710/19/df3ce058825c0a1035dc52e759bb3425.png" style="float:none;" title="Screenshot from 2017-10-18 17-32-45.png" alt="df3ce058825c0a1035dc52e759bb3425.png" />
650) this.width=650;" src="https://s3.51cto.com/oss/201710/19/d06ae23eb10fd17e43dc29e62cf0a8d1.png" style="float:none;" title="Screenshot from 2017-10-18 17-33-42.png" alt="d06ae23eb10fd17e43dc29e62cf0a8d1.png" />
Linux的企業-Redis資料庫、緩衝和訊息中介軟體