Spring cloud 之熔斷機制
{"type":"doc","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"作者:Damon","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"博客:","attrs":{}},{"type":"link","attrs":{"href":"http://www.damon8.cn","title":"","type":null},"content":[{"type":"text","text":"http://www.damon8.cn","attrs":{}}]}]},{"type":"blockquote","content":[{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"程序猿Damon | 微服務 | 容器化 | 自動化","attrs":{}}]}],"attrs":{}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"前面講過","attrs":{}},{"type":"link","attrs":{"href":"https://xie.infoq.cn/article/a98e4ecd0d90810dbeba34b58","title":null,"type":null},"content":[{"type":"text","text":"Spring cloud 之多種方式限流","attrs":{}}],"marks":[{"type":"color","attrs":{"color":"#1e6bb8","name":"user"}},{"type":"strong"}]},{"type":"text","text":"處理請求頻繁的壓力。大家都知道,多個微服務之間調用的時候,假設微服務 A 調用微服務 B 和微服務 C,微服務 B 和微服務 C 有調用其他的微服務,這就是所謂的 扇出,若扇出的鏈路上某個微服務的請求時間過長或者不可用,對微服務 A 的調用就會佔用越來越多的時間以及更多資源,進而引起系統雪崩,即”雪崩效應”。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個時候,需要一個機制來保證當某個微服務出現異常時(請求反應慢或宕機),其整個流程還是闊以友好滴進行下去。即向調用方返回一個符合預期的、可處理的備選響應(FallBack),而不是長時間的等待或者拋出調用方無法處理的異常,這樣就可以保證調用方的線程不會被長時間、無厘頭滴佔用,從而避免了故障在分佈式系統中的蔓延,乃至雪崩。我們把這個機制,或者這種處理方式叫作“熔斷器”。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"熔斷機制是應對雪崩效應的一種微服務鏈路保護機制,當整個鏈路的某個微服務異常時,會進行服務的降級,進而熔斷該節點微服務的調用,快速返回“合理”的響應信息。當檢測到該節點微服務正常後恢復調用鏈路,在Spring cloud 框架機制通過 Hystrix 實現,Hystrix 會監控微服務見調用的狀況,當失敗的調用到一個閾值,默認是5秒內20次調用失敗就會啓動熔斷機制,熔斷機制的註解是 @HystrixCommand。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最近研究了一下 Spring cloud 的熔斷機制,特分享一些代碼,以及實戰中的坑。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null}},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"在Spring cloud 中,假設有幾個微服務:用戶管理服務、訂單服務、鑑權中心、物流服務等。這時,訂單服務中,某個接口請求用戶管理服務,這個時候如果需要熔斷機制,該怎麼處理呢?","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"首先,訂單服務引入依賴:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"\n\n org.springframework.cloud \n spring-cloud-starter-netflix-ribbon \n \n\n \n org.springframework.cloud \n spring-cloud-starter-netflix-hystrix \n \n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這個時候,訂單服務啓動類中需要引用熔斷註解 @EnableCircuitBreaker,使其生效:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"/**\n * @author Damon\n * @date 2020年1月13日 下午3:23:06\n *\n */\n\n@EnableOAuth2Sso\n@Configuration\n@EnableAutoConfiguration\n@ComponentScan(basePackages = {\"com.damon\"})\n@EnableDiscoveryClient\n@EnableCircuitBreaker\npublic class OrderApp {\n\n public static void main(String[] args) {\n SpringApplication.run(OrderApp.class, args);\n }\n\n}\n","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"這裏,不要忘記註解 @EnableDiscoveryClient 來相互暴露服務。","attrs":{}}]},{"type":"paragraph","attrs":{"indent":0,"number":0,"align":null,"origin":null},"content":[{"type":"text","text":"最後需要在調用用戶管理服務的函數中,加入註解 @HystrixCommand:","attrs":{}}]},{"type":"codeblock","attrs":{"lang":null},"content":[{"type":"text","text":"@HystrixCommand(fallbackMethod = \"admin_service_fallBack\", commandProperties = {\n @HystrixProperty(name = \"execution.isolation.thread.timeoutInMilliseconds\", value = \"5000\") })//隔離策略:execution.isolation.strategy =SEMAPHORE or THREAD(不配置默認)\n @Override\n public Response
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.