电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術文章
文章詳情頁

一文讀懂Spring Cloud-Hystrix

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

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

hystrix可以實現降級和熔斷:

降級

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

快速失敗

熔斷

當訪問量過大,出現大量失敗,可以做過熱保護,斷開遠程服務不再調用

限流

防止故障傳播、雪崩效應

一文讀懂Spring Cloud-Hystrix

在微服務系統中,服務之間進行依賴,避免有調用其中服務失敗,而引起其他服務大范圍宕機,造成雪崩效應,hystrix熔斷可在滿足熔斷條件(默認10秒20次以上請求,同時50%失?。┖髨绦薪导?。快速斷開故障服務,保護其他服務不受影響。

降級

一文讀懂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); } /** * 創建RestTemplate實例 * 放入spring容器 * @LoadBalanced-對RestTemplate進行增強,封裝RestTemplate,添加負載均衡功能 */ @LoadBalanced @Bean public RestTemplate restTemplate(){ //設置調用超時時間,超時后認為調用失敗 SimpleClientHttpRequestFactory f = new SimpleClientHttpRequestFactory(); f.setConnectTimeout(1000);//建立連接等待時間 f.setReadTimeout(1000);//連接建立后,發送請求后,等待接收響應的時間 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); } //降級方法的參數和返回值,需要和原始方法一致,方法名任意 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服務器進行測試

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

一文讀懂Spring Cloud-Hystrix

hystrix超時設置:

超時時間設置應該超過ribbon重試時間,否則重試失效。

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

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

可在yml中設置超時時間:

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

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

一文讀懂Spring Cloud-Hystrix

Hystrix故障監控-Hystrix Dashboard斷路器儀表盤

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

一文讀懂Spring Cloud-Hystrix

actuator中的hystrix.stream可以監控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中配置健康監控的內容

# '*'暴露所有監控端點management: endpoints: web: exposure: include: '*'

第三步:測試actuator健康監控

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

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

第一步:創建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); }}

第四步:配置允許給那些服務器開啟權限

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

第五步:監控查看

http://localhost:4001/hystrix/

一文讀懂Spring Cloud-Hystrix

第六步:查看hystrix stream監控數據端點

輸入hystrix監控地址

一文讀懂Spring Cloud-Hystrix

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

一文讀懂Spring Cloud-Hystrix

第六步:使用ab進行并發訪問測試

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

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

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

并發50,發送20000個請求,查看儀表盤。

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

標簽: Spring
相關文章:
主站蜘蛛池模板: 太阳能发电系统-太阳能逆变器,控制器-河北沐天太阳能科技首页 | 高效节能电机_伺服主轴电机_铜转子电机_交流感应伺服电机_图片_型号_江苏智马科技有限公司 | 工控机,嵌入式主板,工业主板,arm主板,图像采集卡,poe网卡,朗锐智科 | 赛默飞Thermo veritiproPCR仪|ProFlex3 x 32PCR系统|Countess3细胞计数仪|371|3111二氧化碳培养箱|Mirco17R|Mirco21R离心机|仟诺生物 | 干洗店加盟_洗衣店加盟_干洗店设备-伊蔻干洗「武汉总部」 | 首页-瓜尔胶系列-化工单体系列-油田压裂助剂-瓜尔胶厂家-山东广浦生物科技有限公司 | 刘秘书_你身边专业的工作范文写作小秘书 | 澳威全屋定制官网|极简衣柜十大品牌|衣柜加盟代理|全屋定制招商 百度爱采购运营研究社社群-店铺托管-爱采购代运营-良言多米网络公司 | 宝元数控系统|对刀仪厂家|东莞机器人控制系统|东莞安川伺服-【鑫天驰智能科技】 | 集装箱标准养护室-集装箱移动式养护室-广州璟业试验仪器有限公司 | 继电器模组-IO端子台-plc连接线-省配线模组厂家-世麦德 | 中药二氧化硫测定仪,食品二氧化硫测定仪|俊腾百科 | 智慧养老_居家养老_社区养老_杰佳通| 智能汉显全自动量热仪_微机全自动胶质层指数测定仪-鹤壁市科达仪器仪表有限公司 | 烟雾净化器-滤筒除尘器-防爆除尘器-除尘器厂家-东莞执信环保科技有限公司 | 防锈油-助焊剂-光学玻璃清洗剂-贝塔防锈油生产厂家 | 山东彩钢板房,山东彩钢活动房,临沂彩钢房-临沂市贵通钢结构工程有限公司 | 焊锡丝|焊锡条|无铅锡条|无铅锡丝|无铅焊锡线|低温锡膏-深圳市川崎锡业科技有限公司 | 滑板场地施工_极限运动场地设计_滑板公园建造_盐城天人极限运动场地建设有限公司 | 河北码上网络科技|邯郸小程序开发|邯郸微信开发|邯郸网站建设 | 广州展台特装搭建商|特装展位设计搭建|展会特装搭建|特装展台制作设计|展览特装公司 | 西安烟道厂家_排气道厂家_包立管厂家「陕西西安」推荐西安天宇烟道 | 东莞注册公司-代办营业执照-东莞公司注册代理记账-极刻财税 | 珠海网站建设_响应网站建设_珠海建站公司_珠海网站设计与制作_珠海网讯互联 | Copeland/谷轮压缩机,谷轮半封闭压缩机,谷轮涡旋压缩机,型号规格,技术参数,尺寸图片,价格经销商 CTP磁天平|小电容测量仪|阴阳极极化_双液系沸点测定仪|dsj电渗实验装置-南京桑力电子设备厂 | 西门子气候补偿器,锅炉气候补偿器-陕西沃信机电工程有限公司 | 磁力抛光机_磁力研磨机_磁力去毛刺机-冠古设备厂家|维修|租赁【官网】 | 纯水设备_苏州皙全超纯水设备水处理设备生产厂家 | 网站建设-高端品牌网站设计制作一站式定制_杭州APP/微信小程序开发运营-鼎易科技 | 志高装潢官网-苏州老房旧房装修改造-二手房装修翻新 | 意大利Frascold/富士豪压缩机_富士豪半封闭压缩机_富士豪活塞压缩机_富士豪螺杆压缩机 | 重庆监控_电子围栏设备安装公司_门禁停车场管理系统-劲浪科技公司 | 精益专家 - 设备管理软件|HSE管理系统|设备管理系统|EHS安全管理系统 | 校园文化空间设计-数字化|中医文化空间设计-党建|法治廉政主题文化空间施工-山东锐尚文化传播公司 | 120kv/2mA直流高压发生器-60kv/2mA-30kva/50kv工频耐压试验装置-旭明电工 | 防腐木批发价格_深圳_惠州_东莞防腐木厂家_森源(深圳)防腐木有限公司 | 伶俐嫂培训学校_月嫂培训班在哪里报名学费是多少_月嫂免费政府培训中心推荐 | 螺杆真空泵_耐腐蚀螺杆真空泵_水环真空泵_真空机组_烟台真空泵-烟台斯凯威真空 | 深圳诚暄fpc首页-柔性线路板,fpc柔性线路板打样生产厂家 | 无痕胶_可移胶_无痕双面胶带_可移无痕胶厂家-东莞凯峰 | 北京发电车出租-发电机租赁公司-柴油发电机厂家 - 北京明旺盛安机电设备有限公司 |