亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

一文讀懂Spring Cloud-Hystrix

瀏覽:5日期:2023-07-21 08:11:32
Hystrix概述

Hystrix:斷路器,容錯管理工具,旨在通過熔斷機制控制服務(wù)和第三方庫的節(jié)點,從而對延遲和故障提供更強大的容錯能力。

hystrix可以實現(xiàn)降級和熔斷:

降級

調(diào)用遠程服務(wù)失敗(宕機、500錯、超時),可以降級執(zhí)行當前服務(wù)中的一段代碼,向客戶端返回結(jié)果

快速失敗

熔斷

當訪問量過大,出現(xiàn)大量失敗,可以做過熱保護,斷開遠程服務(wù)不再調(diào)用

限流

防止故障傳播、雪崩效應(yīng)

一文讀懂Spring Cloud-Hystrix

在微服務(wù)系統(tǒng)中,服務(wù)之間進行依賴,避免有調(diào)用其中服務(wù)失敗,而引起其他服務(wù)大范圍宕機,造成雪崩效應(yīng),hystrix熔斷可在滿足熔斷條件(默認10秒20次以上請求,同時50%失敗)后執(zhí)行降級。快速斷開故障服務(wù),保護其他服務(wù)不受影響。

降級

一文讀懂Spring Cloud-Hystrix

第一步:sp06添加hystrix依賴

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

第二步:主程序添加 @EnableCircuitBreaker 啟用 hystrix 斷路器

一文讀懂Spring Cloud-Hystrix

package cn.tedu.sp06;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;import org.springframework.cloud.client.loadbalancer.LoadBalanced;import org.springframework.context.annotation.Bean;import org.springframework.http.client.SimpleClientHttpRequestFactory;import org.springframework.web.client.RestTemplate;@EnableCircuitBreaker@SpringBootApplicationpublic class Sp06RibbonApplication { public static void main(String[] args) { SpringApplication.run(Sp06RibbonApplication.class, args); } /** * 創(chuàng)建RestTemplate實例 * 放入spring容器 * @LoadBalanced-對RestTemplate進行增強,封裝RestTemplate,添加負載均衡功能 */ @LoadBalanced @Bean public RestTemplate restTemplate(){ //設(shè)置調(diào)用超時時間,超時后認為調(diào)用失敗 SimpleClientHttpRequestFactory f = new SimpleClientHttpRequestFactory(); f.setConnectTimeout(1000);//建立連接等待時間 f.setReadTimeout(1000);//連接建立后,發(fā)送請求后,等待接收響應(yīng)的時間 return new RestTemplate(f); }}

第三步:RibbonController 中添加降級方法

為每個方法添加降級方法,例如 getItems() 添加降級方法 getItemsFB() 添加 @HystrixCommand 注解,指定降級方法名

package cn.tedu.sp06.controller;import cn.tedu.sp01.pojo.Item;import cn.tedu.sp01.pojo.Order;import cn.tedu.sp01.pojo.User;import cn.tedu.web.util.JsonResult;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.*;import org.springframework.web.client.RestTemplate;import java.util.List;@RestController@Slf4jpublic class RibbonController { @Autowired private RestTemplate rt; @GetMapping('/item-service/{orderId}') @HystrixCommand(fallbackMethod = 'getItemsFB') //指定降級方法的方法名 public JsonResult<List<Item>> getItems(@PathVariable String orderId){ return rt.getForObject('http://item-service/{1}', JsonResult.class,orderId); } @PostMapping('/item-service/decreaseNumber') @HystrixCommand(fallbackMethod = 'decreaseNumberFB') //指定降級方法的方法名 public JsonResult<?> decreaseNumber(@PathVariable List<Item> items){ return rt.postForObject('http://item-service/decreaseNumber',items, JsonResult.class); } @GetMapping('/user-service/{userId}') @HystrixCommand(fallbackMethod = 'getUserFB') //指定降級方法的方法名 public JsonResult<User> 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<Order> 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); } //降級方法的參數(shù)和返回值,需要和原始方法一致,方法名任意 public JsonResult<List<Item>> getItemsFB(String orderId) { return JsonResult.err('獲取訂單商品列表失敗'); } public JsonResult decreaseNumberFB(List<Item> items) { return JsonResult.err('更新商品庫存失敗'); } public JsonResult<User> getUserFB(Integer userId) { return JsonResult.err('獲取用戶信息失敗'); } public JsonResult addScoreFB(Integer userId, Integer score) { return JsonResult.err('增加用戶積分失敗'); } public JsonResult<Order> getOrderFB(String orderId) { return JsonResult.err('獲取訂單失敗'); } public JsonResult addOrderFB() { return JsonResult.err('添加訂單失敗'); }}

第四步:啟動eureka、item和hystrix服務(wù)器進行測試

http://localhost:3001/item-service/35

一文讀懂Spring Cloud-Hystrix

hystrix超時設(shè)置:

超時時間設(shè)置應(yīng)該超過ribbon重試時間,否則重試失效。

hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds

hystrix等待超時后, 會執(zhí)行降級代碼, 快速向客戶端返回降級結(jié)果, 默認超時時間是1000毫秒。

可在yml中設(shè)置超時時間:

hystrix: command: default: execution: isolation: thread: timeoutInMilliseconds: 6000熔斷

主程序添加 @EnableCircuitBreaker 啟用 hystrix 斷路器,熔斷自動打開。

一文讀懂Spring Cloud-Hystrix

Hystrix故障監(jiān)控-Hystrix Dashboard斷路器儀表盤

Hystrix使用springboot提供的actuator健康管理,監(jiān)控各個端點。

一文讀懂Spring Cloud-Hystrix

actuator中的hystrix.stream可以監(jiān)控hystrix斷路器各端點日志。

一文讀懂Spring Cloud-Hystrix

第一步:sp06的pom中添加actuator依賴

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> <version>2.4.0</version></dependency>

第二步:在yml中配置健康監(jiān)控的內(nèi)容

# '*'暴露所有監(jiān)控端點management: endpoints: web: exposure: include: '*'

第三步:測試actuator健康監(jiān)控

http://localhost:3001/actuator/ 搭建Hystrix Dashboard儀表盤:

儀表盤項目是一個完全獨立的項目。

第一步:創(chuàng)建springboot項目sp08-htstrix-dashboard

一文讀懂Spring Cloud-Hystrix

第二步:添加hystrix dashboard依賴

一文讀懂Spring Cloud-Hystrix

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

第三步:主程序上添加@EnableHystrixDashboard注解

package cn.tedu.sp08;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;@EnableHystrixDashboard@SpringBootApplicationpublic class Sp08HystrixDashboardApplication { public static void main(String[] args) { SpringApplication.run(Sp08HystrixDashboardApplication.class, args); }}

第四步:配置允許給那些服務(wù)器開啟權(quán)限

hystrix: dashboard: proxy-stream-allow-list: localhost

第五步:監(jiān)控查看

http://localhost:4001/hystrix/

一文讀懂Spring Cloud-Hystrix

第六步:查看hystrix stream監(jiān)控數(shù)據(jù)端點

輸入hystrix監(jiān)控地址

一文讀懂Spring Cloud-Hystrix

訪問item/user/order服務(wù)器,查看儀表盤。

一文讀懂Spring Cloud-Hystrix

第六步:使用ab進行并發(fā)訪問測試

使用 apache 的并發(fā)訪問測試工具 ab進行訪問測試。

打開ab工具/bin文件目錄--cmd--輸入命令:

ab -n 20000 -c 50 http://localhost:3001/item-service/35

并發(fā)50,發(fā)送20000個請求,查看儀表盤。

到此這篇關(guān)于一文讀懂Spring Cloud-Hystrix的文章就介紹到這了,更多相關(guān)Spring Cloud Hystrix內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 欧美日韩在线观看一区 | 亚洲福利精品一区二区三区 | 日韩欧美在线视频观看 | 日韩在线观看一区 | 久久99国产精品久久欧美 | 黄色录像一级大片 | 亚洲 中文 欧美 日韩 在线人 | 亚洲国产精品ⅴa在线观看 亚洲国产精品aaa一区 | 亚洲热图| 国产动作大片中文字幕 | 一级女性黄 色生活片 | 美国大片成人性网 | aa黄色毛片| 美女视频一区二区三区在线 | 毛片一区二区三区 | 国产午夜精品一区二区三区嫩草 | 纯欧美一级毛片免费 | 国产精选91热在线观看 | 免费观看欧美一级特黄 | 亚洲美女在线观看播放 | 麻豆久久精品免费看国产 | 免费精品久久久视频 | 九九综合九九综合 | 亚洲在线观看一区二区 | 亚洲在线第一页 | 欧美一级毛片高清毛片 | 国产成人lu在线视频 | 免费特黄级夫费生活片 | 亚洲国产精品久久久久 | 欧美黄网站免费观看 | 中文字幕日韩精品在线 | 日韩美女专区中文字幕 | 国产一区二区在线观看视频 | 欧美午夜一艳片欧美精品 | 国产v精品成人免费视频400条 | 亚洲一区不卡 | 欧美一级做a影片爱橙影院 欧美一级做一a做片性视频 | 青青久久久国产线免观 | 国产成年人在线观看 | 久久精品这里 | 午夜国产福利视频一区 |