日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Redis觸發(fā)的主從切換研究(redis觸發(fā)主從切換)

Redis觸發(fā)的主從切換研究

創(chuàng)新互聯(lián)建站主營望都網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都App定制開發(fā),望都h5微信平臺(tái)小程序開發(fā)搭建,望都網(wǎng)站營銷推廣歡迎望都等地區(qū)企業(yè)咨詢

Redis是一個(gè)開源的,基于內(nèi)存的數(shù)據(jù)結(jié)構(gòu)存儲(chǔ)系統(tǒng),被廣泛應(yīng)用于緩存、隊(duì)列、發(fā)布/訂閱等場景。Redis中的數(shù)據(jù)可以被持久化到磁盤中,實(shí)現(xiàn)數(shù)據(jù)的可靠性和持久性。

在Redis中,可以通過主從復(fù)制的方式實(shí)現(xiàn)數(shù)據(jù)的高可用性和可擴(kuò)展性。主從復(fù)制中,一個(gè)Redis實(shí)例扮演主節(jié)點(diǎn)的角色,負(fù)責(zé)處理寫請(qǐng)求,而其他Redis實(shí)例扮演從節(jié)點(diǎn)的角色,負(fù)責(zé)復(fù)制主節(jié)點(diǎn)中的數(shù)據(jù),并且可以處理讀請(qǐng)求。主從復(fù)制有助于提升系統(tǒng)的讀寫性能,并且可以對(duì)Redis進(jìn)行水平擴(kuò)展。

在主從復(fù)制中,一般情況下,主節(jié)點(diǎn)是穩(wěn)定的,從節(jié)點(diǎn)是動(dòng)態(tài)的。當(dāng)主節(jié)點(diǎn)發(fā)生故障或不可用時(shí),從節(jié)點(diǎn)就需要扮演主節(jié)點(diǎn)的角色,而這種自動(dòng)的主從切換是需要保證數(shù)據(jù)的一致性和正確性的。

Redis中的主從切換是基于Sentinel機(jī)制實(shí)現(xiàn)的。sentinel是一個(gè)專門的進(jìn)程,負(fù)責(zé)監(jiān)控Redis節(jié)點(diǎn)的可用性,當(dāng)主節(jié)點(diǎn)不可用時(shí),sentinel會(huì)自動(dòng)將從節(jié)點(diǎn)升級(jí)為主節(jié)點(diǎn),并且將原本的從節(jié)點(diǎn)作為新主節(jié)點(diǎn)的從節(jié)點(diǎn),以此來保證系統(tǒng)的可用性。

那么,在Redis中,當(dāng)主從切換發(fā)生時(shí)會(huì)發(fā)生什么呢?我們可以從以下幾個(gè)方面來分析:

1.判斷主節(jié)點(diǎn)是否可用。sentinel會(huì)定期檢測(cè)主節(jié)點(diǎn)的可用性,如果主節(jié)點(diǎn)不可用,則sentinel會(huì)發(fā)出切換請(qǐng)求。

“`python

class Sentinel:

def check_Master_state(self, master):

“””

檢查主節(jié)點(diǎn)的狀態(tài)

“””

try:

ping_result = master.ping()

info_result = master.info(‘replication’)

if ping_result and “role” in info_result and info_result[“role”] == “master”:

return True

except Exception as e:

self.logger.warning(“Error when checking master state: {}”.format(e))

return False


2.選舉新的主節(jié)點(diǎn)。當(dāng)sentinel收到切換請(qǐng)求后,會(huì)進(jìn)行一次選舉來決定哪個(gè)從節(jié)點(diǎn)成為新的主節(jié)點(diǎn)。sentinel會(huì)將收到切換請(qǐng)求的所有從節(jié)點(diǎn)進(jìn)行評(píng)分,并選取評(píng)分最高的節(jié)點(diǎn)作為新的主節(jié)點(diǎn)。

```python
class Sentinel:
def elect_new_master(self, sentinel_marks):
"""
選舉新的主節(jié)點(diǎn)
"""
candidates = set()
for name, marks in sentinel_marks.items():
if marks.get('s_down', 0)
candidates.add(name)
top_score, top_name = None, None
for name in candidates:
try:
info = self.sentinel_conn(name).sentinel_get_master_info(self.master_name)
except:
continue
if info is None:
continue
score = self.master_monitor.monitored_state.score(info, self.sentinels)
if top_score is None or score > top_score:
top_score, top_name = score, name
if top_name:
self.logger.info('Leader sentinel is [%s]', top_name)
return top_name

3.進(jìn)行主從切換。選舉出新的主節(jié)點(diǎn)后,sentinel會(huì)發(fā)出切換命令,執(zhí)行主從切換。此時(shí),原本的從節(jié)點(diǎn)變?yōu)樾碌闹鞴?jié)點(diǎn),而原本的主節(jié)點(diǎn)變?yōu)閺墓?jié)點(diǎn)。

“`python

class Sentinel:

def flover(self, new_master_addr):

“””

進(jìn)行主從切換

“””

self.logger.info(“Flover to {}”.format(new_master_addr))

old_master_addr, next_master_addr = self.master_addr, new_master_addr

try:

next_master = redis.StrictRedis.from_url(“redis://{}”.format(next_master_addr))

# 將主節(jié)點(diǎn)轉(zhuǎn)為從節(jié)點(diǎn)

next_master.slaveof()

# 新主節(jié)點(diǎn)已經(jīng)升級(jí)完成

self.master_monitor.set_master(new_master_addr)

self.master_addr = new_master_addr

old_master = redis.StrictRedis.from_url(“redis://{}”.format(old_master_addr))

# 使原主節(jié)點(diǎn)成為從節(jié)點(diǎn)

if self.slave_of_no_one:

old_master.slaveof()

else:

old_master.slaveof(*self.slave_of_no_one.split())

# sentinel開始新一輪監(jiān)控

self.master_monitor.reset()

except Exception as e:

self.logger.warning(“flover fl {}”.format(e))


綜上所述,Redis中的主從切換是基于sentinel機(jī)制實(shí)現(xiàn)的,當(dāng)主節(jié)點(diǎn)不可用時(shí),sentinel會(huì)自動(dòng)將從節(jié)點(diǎn)升級(jí)為主節(jié)點(diǎn),并且將原本的從節(jié)點(diǎn)作為新主節(jié)點(diǎn)的從節(jié)點(diǎn),以此來保證系統(tǒng)的可用性。通過以上代碼的分析,我們可以更深入地了解Redis的主從切換的實(shí)現(xiàn)原理,以及如何保證數(shù)據(jù)的一致性和正確性。

創(chuàng)新互聯(lián)【028-86922220】值得信賴的成都網(wǎng)站建設(shè)公司。多年持續(xù)為眾多企業(yè)提供成都網(wǎng)站建設(shè),成都品牌網(wǎng)站設(shè)計(jì),成都高端網(wǎng)站制作開發(fā),SEO優(yōu)化排名推廣服務(wù),全網(wǎng)營銷讓企業(yè)網(wǎng)站產(chǎn)生價(jià)值。


網(wǎng)頁題目:Redis觸發(fā)的主從切換研究(redis觸發(fā)主從切換)
網(wǎng)站地址:http://m.5511xx.com/article/dhdhpph.html