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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

(七)SpringBoot+SpringCloud —— 集成断路器

發布時間:2023/12/10 javascript 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (七)SpringBoot+SpringCloud —— 集成断路器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

斷路器簡介

在一個項目中,系統可能被拆分成多個服務,例如用戶、訂單和庫存等。

這里存在這服務調用服務的情況,例如,客戶端調用訂單服務,訂單服務又調用庫存服務。

此時若庫存服務響應緩慢,會直接導致訂單服務的線程被掛起,以等待庫存申請服務的響應,在漫長的等待之后用戶會因為請求庫存失敗而得到創建訂單失敗的結果。

如果在高并發下,因這些掛起的線程在等待庫存服務的響應而未能獲得釋放,會似的后續到來的請求被阻塞,最終導致訂單服務也不可用。

在分布式架構中,斷路器模式的作用也是類似的,當某個服務單元發生故障之后,通過斷路器的故障監控,向調用方返回一個錯誤響應,而不是漫長的等待。

快速入門

首先,添加斷路器hystrix的依賴。

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-hystrix</artifactId></dependency>

接著在工程的主類,添加注解@EnableCircuitBreaker:

package cn.net.bysoft.owl.bookstore.web.console;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate;@SpringBootApplication @EnableDiscoveryClient @EnableCircuitBreaker public class OwlBookstoreWebConsoleApplication {@Bean@LoadBalancedRestTemplate restTemplate() {return new RestTemplate();}public static void main(String[] args) {SpringApplication.run(OwlBookstoreWebConsoleApplication.class, args);} }

接著,就可以使用斷路器了,可以添加@HystrixCommand注解,對調用服務的方法進行修飾:

@HystrixCommand(fallbackMethod = "findByIdFallback")public User findById(Long id) {UriComponents uriComponents = UriComponentsBuilder.fromUriString("http://SERVICE-USER/users/{id}").build().expand(id).encode();URI uri = uriComponents.toUri();return restTemplate.getForObject(uri, User.class);}public User findByIdFallback(Long id) {return null;}

fallbackMethod是服務發生異常時,回調的降級處理函數,該函數的參數和返回值要與調用函數一致。

斷路器的默認超時時間為2000毫秒。當被斷路器修飾的函數執行超過這個值,將觸發斷路器的服務降級,該參數是可以設置的。

斷路器配置

全局配置屬性:hystrix.[attr].default.

實例配置屬性:hystrix.[attr].[key].

execution配置

  • Command Properties
  • Execution
  • execution.isolation.strategy (執行的隔離策略)
  • execution.isolation.thread.timeoutInMilliseconds(執行的超時時間)
  • execution.timeout.enabled(是否啟用超時時間)
  • execution.isolation.thread.interruptOnTimeout(超時發生后是否要中斷該服務)
  • execution.isolation.thread.interruptOnCancel(執行被取消后是否要中斷該服務)
  • execution.isolation.semaphore.maxConcurrentRequests(當最大并發達到該值,后續的請求會被拒絕)
  • Fallback
  • fallback.isolation.semaphore.maxConcurrentRequests(fallback方法執行的最大并發請求數,當達到最大,后續的請求將會被拒絕并拋出異常)
  • fallback.enabled(服務降級策略是否啟用)
  • Circuit Breaker
  • circuitBreaker.enabled (斷路器開關)
  • circuitBreaker.requestVolumeThreshold (斷路器請求閾值)
  • circuitBreaker.sleepWindowInMilliseconds(斷路器休眠時間)
  • circuitBreaker.errorThresholdPercentage(斷路器錯誤請求百分比)
  • circuitBreaker.forceOpen(斷路器強制開啟)
  • circuitBreaker.forceClosed(斷路器強制關閉)
  • Metrics
  • metrics.rollingStats.timeInMilliseconds(滾動時間窗長度,毫秒級)
  • metrics.rollingStats.numBuckets(滾動時間窗統計指標信息時劃分“桶”的數量)
  • metrics.rollingPercentile.enabled(對命令執行的延遲是否使用百分位數來跟蹤和計算)
  • metrics.rollingPercentile.timeInMilliseconds(設置百分位統計的滾動窗口的持續時間)
  • metrics.rollingPercentile.numBuckets(設置百分位統計滾動窗口中使用“桶”的數量)
  • metrics.rollingPercentile.bucketSize(設置在執行過程中每個“桶”中保留的最大執行次數)
  • metrics.healthSnapshot.intervalInMilliseconds(采集健康快照的間隔等待時間)
  • Request Context
  • requestCache.enabled(是否啟用緩存)
  • requestLog.enabled(執行的時間是否打印到日志)
  • Collapser Properties
  • maxRequestsInBatch(請求合并批處理中允許的最大請求數)
  • timerDelayInMilliseconds(批處理過程中每個命令延遲的時間,毫秒級)
  • requestCache.enabled(是否開啟請求緩存)
  • Thread Pool Properties
  • coreSize(線程池大小)
  • maxQueueSize(最大隊列數量)
  • queueSizeRejectionThreshold (隊列大小拒絕閾值)
  • metrics.rollingStats.timeInMilliseconds(滾動時間窗的長度,毫秒級)
  • metrics.rollingStats.numBuckets(滾動時間窗被劃分的數量)
  • Github

    https://github.com/XuePeng87/owl-bookstore

    轉載于:https://my.oschina.net/u/2450666/blog/1499040

    總結

    以上是生活随笔為你收集整理的(七)SpringBoot+SpringCloud —— 集成断路器的全部內容,希望文章能夠幫你解決所遇到的問題。

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