hystrix threadpool coresize_Hystrix断路器 - 求知若渴的蜗牛
Hystrix介紹
在微服務(wù)場景中,通常會有很多層的服務(wù)調(diào)用。如果一個(gè)底層服務(wù)出現(xiàn)問題,故障會被向上傳播給用戶。我們需要一種機(jī)制,當(dāng)?shù)讓臃?wù)不可用時(shí),可以阻斷故障的傳播。這就是斷路器的作用。他是系統(tǒng)服務(wù)穩(wěn)定性的最后一重保障。
在springcloud中斷路器組件就是Hystrix。Hystrix也是Netflix套件的一部分。他的功能是,當(dāng)對某個(gè)服務(wù)的調(diào)用在一定的時(shí)間內(nèi)(默認(rèn)10s),有超過一定次數(shù)(默認(rèn)20次)并且失敗率超過一定值(默認(rèn)50%),該服務(wù)的斷路器會打開。返回一個(gè)由開發(fā)者設(shè)定的fallback。
fallback可以是另一個(gè)由Hystrix保護(hù)的服務(wù)調(diào)用,也可以是固定的值。fallback也可以設(shè)計(jì)成鏈?zhǔn)秸{(diào)用,先執(zhí)行某些邏輯,再返回fallback。
Hystrix作用:
能夠?qū)崿F(xiàn) 服務(wù)的降級,服務(wù)的熔斷,接近實(shí)時(shí)的監(jiān)控
官網(wǎng)資料?https://github.com/Netflix/Hystrix/wiki/How-To-Use
Hystrix官宣,停更進(jìn)維?https://github.com/Netflix/Hystrix
Hystrix重要概念
服務(wù)降級
? ? ? ? 服務(wù)器忙,請稍候再試,不讓客戶端等待并立刻返回一個(gè)友好提示,fallback
哪些情況會觸發(fā)降級
? ? ? ? 程序運(yùn)行異常
? ? ? ? 超時(shí)
? ? ? ? 服務(wù)熔斷觸發(fā)服務(wù)降級
? ? ? ? 線程池/信號量打滿也會導(dǎo)致服務(wù)降級
服務(wù)熔斷
? ? ? ? 類比保險(xiǎn)絲達(dá)到最大服務(wù)訪問后,直接拒絕訪問,拉閘限電,然后調(diào)用服務(wù)降級的方法并返回友好提示
就是保險(xiǎn)絲?服務(wù)的降級->進(jìn)而熔斷->恢復(fù)調(diào)用鏈路
服務(wù)限流??
? ? ? ? 秒殺高并發(fā)等操作,嚴(yán)禁一窩蜂的過來擁擠,大家排隊(duì),一秒鐘N個(gè),有序進(jìn)行
hystrix案例
? 在上一節(jié)整合openfeign的基礎(chǔ)上整合hystrix 主要用到了這個(gè)三個(gè)服務(wù)
? 模擬故障
? 服務(wù)提供者有一個(gè)耗時(shí)的服務(wù),消費(fèi)者調(diào)用一次訪問特別慢,轉(zhuǎn)圈但是最終可以訪問
?但是不影響這個(gè)接口
?當(dāng)開啟多線程測試,訪問量為2000去訪問
再次訪問
也開始轉(zhuǎn)圈了
導(dǎo)致原因
? ? ? 8001同一層次的其他接口服務(wù)被困死,因?yàn)閠omcat線程里面的工作線程已經(jīng)被擠占完畢
? ? ? 80此時(shí)調(diào)用8001,客戶端訪問響應(yīng)緩慢,轉(zhuǎn)圈圈
上訴結(jié)論
? ? ? ?正因?yàn)橛猩鲜龉收匣虿患驯憩F(xiàn),才有我們的降級/容錯(cuò)/限流等技術(shù)誕生
? ? ? ?如何解決?解決的要求
超時(shí)導(dǎo)致服務(wù)器變慢(轉(zhuǎn)圈)?超時(shí)不再等待
? ? ? ?出錯(cuò)(宕機(jī)或程序運(yùn)行出錯(cuò))?出錯(cuò)要有兜底
解決?
? ? ? ? ?對方服務(wù)(8001)超時(shí)了,調(diào)用者(80)不能一直卡死等待,必須有服務(wù)降級
? ? ? ? ?對方服務(wù)(8001)down機(jī)了,調(diào)用者(80)不能一直卡死等待,必須有服務(wù)降級
? ? ? ? ?對方服務(wù)(8001)OK,調(diào)用者(80)自己出故障或有自我要求(自己的等待時(shí)間小于服務(wù)提供者),自己處理降級
服務(wù)降級
? ? ?pom文件加入依賴
<!--整合熔斷器--><dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
? ? ? ?降低配置??@HystrixCommand
? ? ? ?8001先從自身找問題? ?設(shè)置自身調(diào)用超時(shí)時(shí)間的峰值,峰值內(nèi)可以正常運(yùn)行,超過了需要有兜底的方法處理,作服務(wù)降級fallback8001fallback
業(yè)務(wù)類啟用
package com.shiwen.controller;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; import com.shiwen.entity.CommonResult; import com.shiwen.entity.Payment; import com.shiwen.service.PaymentService; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.*;import java.util.concurrent.TimeUnit;/*** 支付表(Payment)表控制層** @author wangjie* @since 2020-09-27 17:19:14*/@RestController @RequestMapping("payment") public class PaymentController {/*** 服務(wù)對象*/@Autowiredprivate PaymentService paymentService;@Value("${}")private String serverPort;/*** 通過主鍵查詢單條數(shù)據(jù)** @param id 主鍵* @return 單條數(shù)據(jù)*/@ApiOperation(value = "根據(jù)id查詢數(shù)據(jù)")@GetMapping("select-one/{id}")public CommonResult selectOne(@PathVariable Long id) {Payment payment = (id);return new CommonResult(200,"成功"+serverPort,payment);}/*** 增加*/@ApiOperation(value = "添加數(shù)據(jù)")@PostMapping("add")public CommonResult addPayment(@RequestBody Payment payment){boolean insert = (payment);if(insert==true){return new CommonResult(200,"插入成功人不輸"+serverPort,true);}else{return new CommonResult(500,"插入失敗"+serverPort,false);}}/*** 編寫超時(shí)程序*/@GetMapping("timeout")@HystrixCommand(fallbackMethod = "timeOutMethod",commandProperties = {@HystrixProperty(name = "",value = "3000") //三秒以內(nèi)正常})public CommonResult timeOutMethods(){try {(5);} catch (InterruptedException e) {();}return new CommonResult(200,"超時(shí)服務(wù)",serverPort);}//兜底的方法public CommonResult timeOutMethod(){return new CommonResult(200,"系統(tǒng)占時(shí)繁忙請稍后重試",serverPort);}}啟動類添加@EnableHystrix
一旦調(diào)用服務(wù)方法失敗并拋出了錯(cuò)誤信息后,會自動調(diào)用@HystrixCommand標(biāo)注好的fallbackMethod調(diào)用類中的指定方法timeOutMethod圖示
?這樣寫一個(gè)方法一個(gè)兜底的方法代碼太過臃腫,通過注解@DefaultProperties(defaultFallback = "globalTimeOutMethod")配置全局的兜底方法
@RestController @RequestMapping("/order") @DefaultProperties(defaultFallback = "globalTimeOutMethod") public class FeignController {@Autowiredprivate FeignService feignService;@GetMapping("/selete/{id}")public CommonResult getUserById(@PathVariable Long id){return feignService.selectOne(id);}@GetMapping("/timeout")//@HystrixCommand(defaultFallback = "orderTimeOutMethod",commandProperties = {@HystrixProperty(name = "",value = "1500")})@HystrixCommandpublic CommonResult getTimeOut(){return feignService.timeOutMethods();}public CommonResult orderTimeOutMethod(){return new CommonResult(200,"消費(fèi)端兜底的方法","===================");}public CommonResult globalTimeOutMethod(){return new CommonResult(200,"消費(fèi)端全局的兜底方法","=====================");} }還有一個(gè)問題是兜底的方法和業(yè)務(wù)代碼混合在一起 以下的降級實(shí)在80消費(fèi)端完成的
第一步創(chuàng)建
FeignServiceImpl類實(shí)現(xiàn)FeignService接口 編寫每一個(gè)方法對應(yīng)的兜底內(nèi)容@Component public class FeignServiceImpl implements FeignService {@Overridepublic CommonResult selectOne(Long id) {return null;}@Overridepublic CommonResult timeOutMethods() {return new CommonResult(200,"我是一接口的形式實(shí)現(xiàn)兜底方法的","====================");} }第二步?FeignService 接口指名兜底的類fallback =
@FeignClient(value = "CLOUD-PAYMENT-SERVICE",fallback = ) public interface FeignService {@GetMapping("payment/select-one/{id}")CommonResult selectOne(@PathVariable("id") Long id);@GetMapping("payment/timeout")CommonResult timeOutMethods();第三步controller調(diào)用package com.shiwen.controller;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import com.shiwen.entity.CommonResult; import com.shiwen.service.FeignService; 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.RequestMapping; import org.springframework.web.bind.annotation.RestController;/*** @author wangjie* @Title: FeignController* @Description: openfeign服務(wù)的調(diào)用* @company: 西安石文軟件有限公司* @date 2020/10/1411:13*/ @RestController @RequestMapping("/order") //@DefaultProperties(defaultFallback = "globalTimeOutMethod") public class FeignController {@Autowiredprivate FeignService feignService;@GetMapping("/selete/{id}")public CommonResult getUserById(@PathVariable Long id){return feignService.selectOne(id);}@GetMapping("/timeout")//@HystrixCommand(defaultFallback = "orderTimeOutMethod",commandProperties = {@HystrixProperty(name = "",value = "1500")})//@HystrixCommandpublic CommonResult getTimeOut(){return feignService.timeOutMethods();}public CommonResult orderTimeOutMethod(){return new CommonResult(200,"消費(fèi)端兜底的方法","===================");}public CommonResult globalTimeOutMethod(){return new CommonResult(200,"消費(fèi)端全局的兜底方法","=====================");} }
yml編寫 開啟hystrix
feign:hystrix:
enabled: true
測試依次啟動注冊中心,服務(wù)提供者,消費(fèi)者
訪問http://localhost/order/timeout 當(dāng)程序訪問超時(shí)就會走實(shí)現(xiàn)類了兜底
?服務(wù)熔斷
說白了就是程序不正常了,再這一段時(shí)間訪問并不會立馬崩掉,而是訪問失敗的次數(shù)大于設(shè)定的比率,就會跳閘,對服務(wù)實(shí)現(xiàn)降級,之后走的是兜底方法,當(dāng)程序再次恢復(fù)正常時(shí),并不會里面返回正確的數(shù)據(jù)。還是走的是兜底的方法,同樣需要在一段時(shí)間訪問成功的次數(shù)大于設(shè)定的比率,會再次跳閘。程序恢復(fù)正常(就是檢測到改節(jié)點(diǎn)微服務(wù)調(diào)用響應(yīng)正常后,恢復(fù)調(diào)用鏈路)
服務(wù)熔斷需要配置的參數(shù)
@HystrixCommand(fallbackMethod = "paymentCircuitBreaker_fallback",commandProperties = {
@HystrixProperty(name = "",value = "true"), //是否開啟斷路器
@HystrixProperty(name = "",value = "10"), //請求次數(shù)
@HystrixProperty(name = "",value = "10000"), //時(shí)間范圍
@HystrixProperty(name = "",value = "60"), //失敗率達(dá)到多少后跳閘
})
總結(jié)
以上是生活随笔為你收集整理的hystrix threadpool coresize_Hystrix断路器 - 求知若渴的蜗牛的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 0.5%手续费怎么算
- 下一篇: 华为 虚拟键盘_华为mate30 pro