日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hystrix熔断和降级的区别_Ribbon+Hystrix断路器实现微服务的降级和熔断

發布時間:2024/9/30 编程问答 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hystrix熔断和降级的区别_Ribbon+Hystrix断路器实现微服务的降级和熔断 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

微服務宕機時,Ribbon無法實現轉發請求,因此引入Hystrix.

Hystrix短路器的核心功能:

  • 降級: 當后臺微服務不可用或訪問超時時,則轉向執行降級代碼,或返回錯誤信息,或返回緩存數據;
  • 熔斷: 默認配置下,后臺微服務10秒內收到的請求達到20個,并且有一半的請求(50%)出現請求失敗降級的情況,則Hystrix打開斷路器(斷路器默認關閉closed),表示后臺微服務不可用,讓所有請求執行降級代碼;當斷路器打開5秒后轉為半開閉狀態,該狀態表示當有請求到達時,會嘗試向后臺微服務轉發。如果請求成功,則關閉短路器,表示所有的請求都可請求到達后臺微服務;若仍請求失敗,則短路器仍保持打開狀態。
  • 1. 創建SpringBoot項目,添加依賴:

    2. 添加Hystrix依賴:

    org.springframework.cloud spring-cloud-starter-netflix-hystrix

    3. 添加自己項目的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断路器实现微服务的降级和熔断的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。