hystrix熔断和降级的区别_Ribbon+Hystrix断路器实现微服务的降级和熔断
生活随笔
收集整理的這篇文章主要介紹了
hystrix熔断和降级的区别_Ribbon+Hystrix断路器实现微服务的降级和熔断
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
微服務宕機時,Ribbon無法實現轉發請求,因此引入Hystrix.
Hystrix短路器的核心功能:
1. 創建SpringBoot項目,添加依賴:
2. 添加Hystrix依賴:
org.springframework.cloud spring-cloud-starter-netflix-hystrix3. 添加自己項目的common工具類依賴:
4. application.yml文件中進行相關配置:
5.主啟動類上加注解 @EnableCircuitBreaker和@EnableDiscoveryClient:
這三個注解可以用@SpringCloudApplication一個代替
6. 創建RibbonController類,在該類中編寫springMVC的controller方法,此外指定對應的降級方法,并在controller方法上通過注解標明指定的降級方法:
package com.tedu.sp7.controller;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PathVariable;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RequestBody;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.client.RestTemplate;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;import com.tedu.sp01.pojo.Item;import com.tedu.sp01.pojo.Order;import com.tedu.sp01.pojo.User;import com.tedu.web.util.JsonResult;@RestControllerpublic class RibbonController { @Autowired private RestTemplate rt; @GetMapping("/item-service/{orderId}") @HystrixCommand(fallbackMethod = "getItemsFB") //指定降級方法的方法名 public JsonResult> getItems(@PathVariable String orderId) { return rt.getForObject("http://item-service/{1}", JsonResult.class, orderId); } @PostMapping("/item-service/decreaseNumber") @HystrixCommand(fallbackMethod = "decreaseNumberFB") public JsonResult decreaseNumber(@RequestBody List items) { return rt.postForObject("http://item-service/decreaseNumber", items, JsonResult.class); } / @GetMapping("/user-service/{userId}") @HystrixCommand(fallbackMethod = "getUserFB") public JsonResult getUser(@PathVariable Integer userId) { return rt.getForObject("http://user-service/{1}", JsonResult.class, userId); } @GetMapping("/user-service/{userId}/score") @HystrixCommand(fallbackMethod = "addScoreFB") public JsonResult addScore(@PathVariable Integer userId, Integer score) { return rt.getForObject("http://user-service/{1}/score?score={2}", JsonResult.class, userId, score); } / @GetMapping("/order-service/{orderId}") @HystrixCommand(fallbackMethod = "getOrderFB") public JsonResult getOrder(@PathVariable String orderId) { return rt.getForObject("http://order-service/{1}", JsonResult.class, orderId); } @GetMapping("/order-service") @HystrixCommand(fallbackMethod = "addOrderFB") public JsonResult addOrder() { return rt.getForObject("http://order-service/", JsonResult.class); } / //降級方法的參數和返回值,需要和原始方法一致,方法名任意 public JsonResult> getItemsFB(String orderId) { return JsonResult.err("獲取訂單商品列表失敗"); } public JsonResult decreaseNumberFB(List items) { return JsonResult.err("更新商品庫存失敗"); } public JsonResult getUserFB(Integer userId) { return JsonResult.err("獲取用戶信息失敗"); } public JsonResult addScoreFB(Integer userId, Integer score) { return JsonResult.err("增加用戶積分失敗"); } public JsonResult getOrderFB(String orderId) { return JsonResult.err("獲取訂單失敗"); } public JsonResult addOrderFB() { return JsonResult.err("添加訂單失敗"); }}Hystrix常用配置:
- hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds
請求超時時間,超時后觸發失敗降級 - hystrix.command.default.circuitBreaker.requestVolumeThreshold
10秒內請求數量,默認20,如果沒有達到該數量,即使請求全部失敗,也不會觸發斷路器打開 - hystrix.command.default.circuitBreaker.errorThresholdPercentage
失敗請求百分比,達到該比例則觸發斷路器打開 - hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds
斷路器打開多長時間后,再次允許嘗試訪問(半開),仍失敗則繼續保持打開狀態,如成功訪問則關閉斷路器,默認 5000
總結
以上是生活随笔為你收集整理的hystrix熔断和降级的区别_Ribbon+Hystrix断路器实现微服务的降级和熔断的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python最适合做什么生意赚钱投资小_
- 下一篇: arduino黑线循迹小车程序_循迹小车