水利部精神文明建設(shè)指導(dǎo)委員會(huì)網(wǎng)站免費(fèi)關(guān)鍵詞挖掘網(wǎng)站
在軟件架構(gòu)領(lǐng)域的中文文檔、書籍中,經(jīng)??梢钥吹健皬椥浴边@個(gè)專業(yè)術(shù)語,但在不同的語境下含義可能會(huì)不同。
在英語中,elastic 和 resilient 兩個(gè)單詞都可以翻譯為“彈性的”,但是它們?cè)谲浖軜?gòu)中代表的含義卻完全不同,需要避免混淆。
Elastic
Elastic 代表的“彈性”強(qiáng)調(diào)的是可伸縮性。
在《Designing Data-Intensive Applications》一書中,對(duì) elastic 的定義:
Some systems are elastic, meaning that they can automatically add computing resources when they detect a load increase, whereas other systems are scaled manually (a human analyzes the capacity and decides to add more machines to the system). An elastic system can be useful if load is highly unpredictable, but manually scaled systems are simpler and may have fewer operational surprises.
翻譯:某些系統(tǒng)具有彈性,意味著它檢測(cè)到負(fù)載增加時(shí),可以自動(dòng)化地增加計(jì)算資源。而非彈性的系統(tǒng)則需要手動(dòng)擴(kuò)展(人工分析性能并決定向系統(tǒng)中添加更多的機(jī)器)。當(dāng)系統(tǒng)負(fù)載很難預(yù)測(cè)時(shí),彈性系統(tǒng)會(huì)非常有用,而手動(dòng)擴(kuò)展的系統(tǒng)更加簡(jiǎn)單,并且可以減少操作上的意外。
Elastic 表示系統(tǒng)可以根據(jù)負(fù)載情況和相關(guān)策略自動(dòng)調(diào)整計(jì)算資源,所以也稱為 Auto Scaling。例如一個(gè)電商應(yīng)用會(huì)在大促時(shí)有更大的負(fù)載量,則需要自動(dòng)添加更多的服務(wù)器等資源保障系統(tǒng)正常提供服務(wù),而在平時(shí)負(fù)載量小的時(shí)候,則自動(dòng)減少資源來控制成本。當(dāng)我們看到“彈性伸縮”這個(gè)術(shù)語時(shí),要知道這里的“彈性”代表的意思就是 elastic。
例如,Kubernetes 提供了 HorizontalPodAutoscaler,支持 Pod 水平自動(dòng)擴(kuò)縮容。阿里云等商業(yè)云平臺(tái)也都提供了類似的彈性伸縮服務(wù)(Elastic Scaling Service),可根據(jù)負(fù)載情況和策略自動(dòng)調(diào)整計(jì)算能力(即實(shí)例數(shù)量)。
Resilient
Elastic 代表的“彈性”強(qiáng)調(diào)的是復(fù)原能力。
在《Designing Data-Intensive Applications》一書中,對(duì) resilient 的定義:
The things that can go wrong are called faults, and systems that anticipate faults and can cope with them are called fault-tolerant or resilient.
翻譯:可能出錯(cuò)的事情被稱為故障,系統(tǒng)能夠預(yù)測(cè)并應(yīng)對(duì)故障的能力稱為容錯(cuò)或彈性。
Resilient 表示系統(tǒng)有容錯(cuò)和故障恢復(fù)能力,從而系統(tǒng)具有可靠性。
例如,Java 著名的開源庫 Hystrix 的介紹是這樣的:
Hystrix is a latency and fault tolerance library designed to isolate points of access to remote systems, services and 3rd party libraries, stop cascading failure and enable resilience in complex distributed systems where failure is inevitable.
這里的 resilience 當(dāng)然指的不是彈性伸縮能力,而是容錯(cuò)能力。
除 Hystrix 外,其它開源的容錯(cuò)庫:
- Resilience4j: Resilience4j is a fault tolerance library designed for Java8 and functional programming.
- Sentinel: A powerful flow control component enabling reliability, resilience and monitoring for microservices.
- Polly: Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, and Fallback in a fluent and thread-safe manner.
- go-resiliency: Resiliency patterns for golang.
- Semian: Resiliency toolkit for Ruby for failing fast.
實(shí)現(xiàn) resilient 的策略通常包括斷路器(Circuit Breaker)、限流器(Rate Limiter)、重試(Retry)、艙壁(Bulkhead)等,更多可參考:https://github.com/App-vNext/Polly#resilience-policies 。
下面簡(jiǎn)單介紹常用的幾種容錯(cuò)的策略:
- 重試(Retry):很多錯(cuò)誤是短暫的并且可以自動(dòng)恢復(fù)的,對(duì)這種問題采用重試策略。
- 斷路器(Circuit Breaker):類似于電路或股市中的“熔斷”概念,當(dāng)系統(tǒng)發(fā)生嚴(yán)重故障(大量超時(shí)或失敗)時(shí),為了避免后續(xù)持續(xù)不斷的請(qǐng)求導(dǎo)致故障系統(tǒng)過載,超時(shí)導(dǎo)致網(wǎng)絡(luò)、線程資源占用,最終產(chǎn)生雪崩,而在一段時(shí)間內(nèi)直接 fail fast(快速失敗,即直接返回錯(cuò)誤而不再去請(qǐng)求故障的系統(tǒng)模塊)。
- 艙壁(Bulkhead):《泰坦尼克號(hào)》電影中有一段對(duì)船體的描述:船體包含 16 個(gè)相互隔離的水密艙,即使有 4 個(gè)水密艙受損進(jìn)水也能保證船漂浮在海面上。架構(gòu)設(shè)計(jì)中艙壁模式參考的就是這種方式,將資源進(jìn)行隔離,例如可以為調(diào)用多個(gè)服務(wù)的消費(fèi)者分配每個(gè)服務(wù)獨(dú)立的連接池,從而保證一種故障只會(huì)影響到其對(duì)應(yīng)的資源,而不會(huì)造成級(jí)聯(lián)故障。
- 限流器(Rate Limiter):通過限流算法(如令牌桶算法、漏桶算法),限制在特定時(shí)間段內(nèi)的執(zhí)行次數(shù)、數(shù)據(jù)量等指標(biāo),從而防止系統(tǒng)過載。