丽水专业网站建设公司分类模板wordpress
- 作者: 五速梦信息网
- 时间: 2026年03月21日 10:31
当前位置: 首页 > news >正文
丽水专业网站建设公司,分类模板wordpress,在线网页制作工具,珠海好的网站制作平台七、Hystrix断路器
7.1 简介 分布式系统面临的问题 复杂分布式体系结构中的应用程序有数十个依赖关系#xff0c;每个依赖关系在某些时候将不可避免地失败。 多个微服务之间调用的时候#xff0c;假设微服务A调用微服务B和微服务C#xff0c;微服务B和微服务C又调用其它的微…七、Hystrix断路器
7.1 简介 分布式系统面临的问题 复杂分布式体系结构中的应用程序有数十个依赖关系每个依赖关系在某些时候将不可避免地失败。 多个微服务之间调用的时候假设微服务A调用微服务B和微服务C微服务B和微服务C又调用其它的微服务这就是所谓的“扇出”。如果扇出的链路上某个微服务的调用响应时间过长或者不可用对微服务A的调用就会占用越来越多的系统资源进而引起系统崩溃所谓的“雪崩效应”. 对于高流量的应用来说单一的后端依赖可能会导致所有服务器上的所有资源都在几秒钟内饱和。比失败更糟糕的是这些应用程序还可能导致服务之间的延迟增加备份队列线程和其他系统资源紧张导致整个系统发生更多的级联故障。这些都表示需要对故障和延迟进行隔离和管理以便单个依赖关系的失败不能取消整个应用程序或系统。 所以通常当你发现一个模块下的某个实例失败后这时候这个模块依然还会接收流量然后这个有问题的模块还调用了其他的模块这样就会发生级联故障或者叫雪崩。 Hystrix
在分布式系统里许多依赖不可避免的会调用失败比如超时、异常等Hystrix能够保证在一个依赖出问题的情况下不会导致整体服务失败避免级联故障以提高分布式系统的弹性。
“断路器”本身是一种开关装置当某个服务单元发生故障之后通过断路器的故障监控类似熔断保险丝向调用方返回一个符合预期的、可处理的备选响应FallBack而不是长时间的等待或者抛出调用方无法处理的异常这样就保证了服务调用方的线程不会被长时间、不必要地占用从而避免了故障在分布式系统中的蔓延乃至雪崩。
Hystrix的功能
服务降级
不让客户端等待并立刻返回一个友好提示程序运行异常、超时、服务熔断触发服务降级、线程池/信号量打满也会导致服务降级
服务熔断
达到最大服务访问后直接拒绝访问然后调用服务降级的方法并返回友好提示
服务限流
秒杀高并发等操作严禁一窝蜂的过来拥挤大家排队一秒钟N个有序进行
接近实时的监控
Hystrix的停止维护
官网https://github.com/Netflix/Hystrix/wiki/How-To-Use
维护停止说明: https://github.com/Netflix/Hystrix 7.2 Hystrix服务降级案例详解
新建cloud-provider-hystrix-payment8001
7.2.1 pom文件
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdspringcloud/artifactIdgroupIdcom.yyds/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdcloud-provider-hystrix-payment8001/artifactIddependencies!–hystrix–dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-hystrix/artifactId/dependency!–eureka-client–dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdscoperuntime/scopeoptionaltrue/optional/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactId/dependency/dependencies/project7.2.2 yml文件
server:port: 8001spring:application:name: cloud-provider-hystrix-paymenteureka:client:service-url:defaultZone: http://localhost:7001/eureka
defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版本register-with-eureka: truefetch-registry: trueinstance:instance-id: payment8001prefer-ip-address: true # 显示ip7.2.3 主启动类
package com.yyds.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;SpringBootApplication
EnableEurekaClient
public class HystrixPaymentMain8001 {public static void main(String[] args) {SpringApplication.run(HystrixPaymentMain8001.class, args);System.out.println(****************HystrixPaymentMain8001启动成功**************);}
}
7.2.4 业务类
package com.yyds.springcloud.controller;import com.yyds.springcloud.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;RestController
Slf4j
public class PaymentController {Resourceprivate PaymentService paymentService;Value(${server.port})private String serverPort;GetMapping(/payment/hystrix/ok/{id})public String paymentInfo_OK(PathVariable(id) Integer id) {String result paymentService.paymentInfo_OK(id);log.info(result: result);return result;}GetMapping(/payment/hystrix/timeout/{id})public String paymentInfo_TimeOut(PathVariable(id) Integer id) throws InterruptedException {String result paymentService.paymentInfo_TimeOut(id);log.info(**result: result);return result;}}
package com.yyds.springcloud.service;import org.springframework.stereotype.Service;
import java.util.concurrent.TimeUnit;Service
public class PaymentService {/ 正常访问一切OK* /public String paymentInfo_OK(Integer id) {return 线程池: Thread.currentThread().getName() paymentInfo_OK,id: id \t O(∩_∩)O;}/** 超时访问演示降级* /public String paymentInfo_TimeOut(Integer id) {try {TimeUnit.SECONDS.sleep(3);} catch (InterruptedException e) {e.printStackTrace();}return 线程池: Thread.currentThread().getName() paymentInfo_TimeOut,id: id \t O(∩_∩)O耗费3秒;}}
7.2.5 测试
测试接口: http://localhost:8001/payment/hystrix/ok/1
测试接口: http://localhost:8001/payment/hystrix/timeout/1
上述测试正常情况下是没有问题的。但是在高并发的情况下例如开启Jmeter来20000个并发压死800120000个请求都去访问paymentInfo_TimeOut服务两个接口都在自己转圈圈。这是因为tomcat的默认的工作线程数被打满 了没有多余的线程来分解压力和处理。
假如此时外部的消费者80也来访问那消费者只能干等最终导致消费端80不满意服务端8001直接被拖死。
因此对于超时导致服务器变慢(转圈),超时不再等待对于出错(宕机或程序运行出错)出错要有兜底。
具体来说 对方服务(8001)超时了调用者(80)不能一直卡死等待必须有服务降级 对方服务(8001)down机了调用者(80)不能一直卡死等待必须有服务降级 对方服务(8001)OK调用者(80)自己出故障或有自我要求自己的等待时间小于服务提供者自己处理降级
7.2.6 服务降级配置HystrixCommand
cloud-provider-hystrix-payment8001设置自身调用超时时间的峰值峰值内可以正常运行超过了需要有兜底的方法处理作服务降级fallback。
业务类HystrixCommand
package com.yyds.springcloud.service;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.stereotype.Service;import java.util.concurrent.TimeUnit;Service
public class PaymentService {/** 正常访问一切OK* */public String paymentInfo_OK(Integer id) {return 线程池: Thread.currentThread().getName() paymentInfo_OK,id: id \t O(∩_∩)O;}HystrixCommand(fallbackMethod paymentInfo_TimeOutHandler,commandProperties {HystrixProperty(nameexecution.isolation.thread.timeoutInMilliseconds,value3000)})public String paymentInfo_TimeOut(Integer id) {int second 5;try {TimeUnit.SECONDS.sleep(second);} catch (InterruptedException e) {e.printStackTrace();}return 线程池:Thread.currentThread().getName()paymentInfo_TimeOut,id: id\tO(∩_∩)O耗费秒: second;}public String paymentInfo_TimeOutHandler(Integer id){return /(ㄒoㄒ)/调用支付接口超时或异常\t \t当前线程池名字 Thread.currentThread().getName();}}主启动类添加注解EnableCircuitBreaker
package com.yyds.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;SpringBootApplication
EnableEurekaClient
EnableCircuitBreaker // 添加此注解
public class HystrixPaymentMain8001 {public static void main(String[] args) {SpringApplication.run(HystrixPaymentMain8001.class, args);System.out.println(****************HystrixPaymentMain8001启动成功**************);}}此时访问http://localhost:8001/payment/hystrix/timeout/1/(ㄒoㄒ)/调用支付接口超时或异常 当前线程池名字HystrixTimer-17.2.7 消费者cloud-consumer-feign-hystrix-order80
新建模块cloud-consumer-feign-hystrix-order80
pom文件
?xml version1.0 encodingUTF-8?
project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdspringcloud/artifactIdgroupIdcom.yyds/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdcloud-consumer-feign-hystrix-order80/artifactIddependencies!–openfeign–dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-openfeign/artifactId/dependency!–hystrix–dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-hystrix/artifactId/dependency!–eureka client–dependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-eureka-client/artifactId/dependency!–web–dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependency!–一般基础通用配置–dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdscoperuntime/scopeoptionaltrue/optional/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependencies/projectyml文件
spring:application:name: cloud-provider-hystrix-order# 添加下面配置信息
eureka:client:service-url:defaultZone: http://localhost:7001/eureka# defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka # 集群版本register-with-eureka: true # 是否从EurekaServer抓取已有的注册信息默认为true。单节点无所谓集群必须设置为true才能配合ribbon使用负载均衡fetch-registry: truefeign:hystrix:enabled: true
主启动类
package com.yyds.springcloud;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;SpringBootApplication
EnableFeignClients
EnableHystrix // 添加此注解
public class OrderHystrixMain80
{public static void main(String[] args){SpringApplication.run(OrderHystrixMain80.class,args);}
}
业务类
package com.yyds.springcloud.service;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;Component
FeignClient(value CLOUD-PROVIDER-HYSTRIX-PAYMENT)
public interface PaymentHystrixService {GetMapping(/payment/hystrix/ok/{id})String paymentInfo_OK(PathVariable(id) Integer id);GetMapping(/payment/hystrix/timeout/{id})String paymentInfo_TimeOut(PathVariable(id) Integer id);
}package com.yyds.springcloud.controller;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.yyds.springcloud.service.PaymentHystrixService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;RestController
Slf4j
public class OrderHystirxController {Resourceprivate PaymentHystrixService paymentHystrixService;GetMapping(/consumer/payment/hystrix/ok/{id})public String paymentInfo_OK(PathVariable(id) Integer id) {String result paymentHystrixService.paymentInfo_OK(id);return result;}GetMapping(/consumer/payment/hystrix/timeout/{id})HystrixCommand(fallbackMethod paymentTimeOutFallbackMethod,commandProperties {HystrixProperty(nameexecution.isolation.thread.timeoutInMilliseconds,value1500)})public String paymentInfo_TimeOut(PathVariable(id) Integer id){String result paymentHystrixService.paymentInfo_TimeOut(id);return result;}public String paymentTimeOutFallbackMethod(PathVariable(id) Integer id) {return 我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o;}
}测试
http://localhost/consumer/payment/hystrix/timeout/1我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o存在的问题 每个业务方法对应一个兜底的方法代码膨胀 统一和自定义的需要分开
7.2.8 解决问题代码膨胀DefaultProperties(defaultFallback “”)
修改业务类
package com.yyds.springcloud.controller;import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yyds.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;import javax.annotation.Resource;RestController
Slf4j
DefaultProperties(defaultFallback payment_Global_FallbackMethod)
public class OrderHystirxController
{Resourceprivate PaymentHystrixService paymentHystrixService;GetMapping(/consumer/payment/hystrix/ok/{id})public String paymentInfo_OK(PathVariable(id) Integer id){String result paymentHystrixService.paymentInfo_OK(id);return result;}GetMapping(/consumer/payment/hystrix/timeout/{id})HystrixCommand //加了DefaultProperties属性注解并且没有写具体方法名字就用统一全局的public String paymentInfo_TimeOut(PathVariable(id) Integer id){int age 10⁄0;String result paymentHystrixService.paymentInfo_TimeOut(id);return result;}public String paymentTimeOutFallbackMethod(PathVariable(id) Integer id){return 我是消费者80,对方支付系统繁忙请10秒钟后再试或者自己运行出错请检查自己,o(╥﹏╥)o;}// 下面是全局fallback方法public String payment_Global_FallbackMethod(){return Global异常处理信息请稍后再试/(ㄒoㄒ)/;}
}
测试
http://localhost/consumer/payment/hystrix/timeout/1Global异常处理信息请稍后再试/(ㄒoㄒ)/7.2.9 解决问题和业务逻辑混一起
修改cloud-consumer-feign-hystrix-order80
package com.yyds.springcloud.service;import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;Component
FeignClient(value CLOUD-PROVIDER-HYSTRIX-PAYMENT, fallback PaymentFallbackService.class)
public interface PaymentHystrixService {GetMapping(/payment/hystrix/ok/{id})String paymentInfo_OK(PathVariable(id) Integer id);GetMapping(/payment/hystrix/timeout/{id})String paymentInfo_TimeOut(PathVariable(id) Integer id);}
package com.yyds.springcloud.service;import org.springframework.stereotype.Component;Component
public class PaymentFallbackService implements PaymentHystrixService {Overridepublic String paymentInfo_OK(Integer id) {return paymentInfo_OK fall_back;}Overridepublic String paymentInfo_TimeOut(Integer id) {return paymentInfo_TimeOut fall_back;}
}
测试
http://localhost/consumer/payment/hystrix/ok/1
线程池:http-nio-8001-exec-2paymentInfo_OK,id: 1 O(∩_∩)O# 此时关闭8001服务提供者模拟宕机
http://localhost/consumer/payment/hystrix/ok/1
paymentInfo_OK fall_back7.3 Hystrix服务熔断案例详解
熔断机制概述 熔断机制是应对雪崩效应的一种微服务链路保护机制。当扇出链路的某个微服务出错不可用或者响应时间太长时会进行服务的降级进而熔断该节点微服务的调用快速返回错误的响应信息。当检测到该节点微服务调用响应正常后恢复调用链路。
在Spring Cloud框架里熔断机制通过Hystrix实现。Hystrix会监控微服务间调用的状况当失败的调用到一定阈值缺省是5秒内20次调用失败就会启动熔断机制。熔断机制的注解是HystrixCommand。
修改cloud-provider-hystrix-payment8001
PaymentService中添加此段代码 //服务熔断HystrixCommand(fallbackMethod paymentCircuitBreaker_fallback,commandProperties {HystrixProperty(name circuitBreaker.enabled,value true),// 是否开启断路器HystrixProperty(name circuitBreaker.requestVolumeThreshold,value 10),// 请求次数HystrixProperty(name circuitBreaker.sleepWindowInMilliseconds,value 10000), // 时间窗口期HystrixProperty(name circuitBreaker.errorThresholdPercentage,value 60),// 失败率达到多少后跳闸})public String paymentCircuitBreaker(PathVariable(id) Integer id){if(id 0){throw new RuntimeException(id 不能负数);}String serialNumber IdUtil.simpleUUID();return Thread.currentThread().getName()\t调用成功流水号: serialNumber;}public String paymentCircuitBreaker_fallback(PathVariable(id) Integer id){return id 不能负数请稍后再试/(ㄒoㄒ)/~~ id: id;}PaymentController中添加 GetMapping(/payment/circuit/{id})public String paymentCircuitBreaker(PathVariable(id) Integer id) {String result paymentService.paymentCircuitBreaker(id);log.info(*result: result);return result;}测试
持续点击 http://localhost:8001/payment/circuit/-1
出现下面结果
id 不能负数请稍后再试/(ㄒoㄒ)/~~ id: -1# 此时访问下面地址也不能访问 http://localhost:8001/payment/circuit/1 id 不能负数请稍后再试/(ㄒoㄒ)/~~ id: -1# 等一段时间能正常访问 http://localhost:8001/payment/circuit/1 hystrix-PaymentService-10 调用成功流水号: b1d538146c1647019fb4620bc12e648e7.4 Hystrix工作流程 1、创建 HystrixCommand用在依赖的服务返回单个操作结果的时候 或 HystrixObserableCommand用在依赖的服务返回多个操作结果的时候 对象。 2、命令执行。其中 HystrixComand 实现了下面前两种执行方式而 HystrixObservableCommand 实现了后两种执行方式execute()同步执行从依赖的服务返回一个单一的结果对象 或是在发生错误的时候抛出异常。 queue()异步执行 直接返回 一个Future对象 其中包含了服务执行结束时要返回的单一结果对象。 observe()返回 Observable 对象它代表了操作的多个结果它是一个 Hot Obserable不论 “事件源” 是否有 “订阅者”都会在创建后对事件进行发布所以对于 Hot Observable 的每一个 “订阅者” 都有可能是从 “事件源” 的中途开始的并可能只是看到了整个操作的局部过程。 toObservable() 同样会返回 Observable 对象也代表了操作的多个结果但它返回的是一个Cold Observable没有 “订阅者” 的时候并不会发布事件而是进行等待直到有 “订阅者” 之后才发布事件所以对于 Cold Observable 的订阅者它可以保证从一开始看到整个操作的全部过程。 3、若当前命令的请求缓存功能是被启用的 并且该命令缓存命中 那么缓存的结果会立即以 Observable 对象的形式 返回。 4、检查断路器是否为打开状态。如果断路器是打开的那么Hystrix不会执行命令而是转接到 fallback 处理逻辑第 8 步如果断路器是关闭的检查是否有可用资源来执行命令第 5 步。 5、线程池/请求队列/信号量是否占满。如果命令依赖服务的专有线程池和请求队列或者信号量不使用线程池的时候已经被占满 那么 Hystrix 也不会执行命令 而是转接到 fallback 处理逻辑第8步。 6、Hystrix 会根据我们编写的方法来决定采取什么样的方式去请求依赖服务。HystrixCommand.run() 返回一个单一的结果或者抛出异常。HystrixObservableCommand.construct() 返回一个Observable 对象来发射多个结果或通过 onError 发送错误通知。 7、Hystrix会将 “成功”、“失败”、“拒绝”、“超时” 等信息报告给断路器 而断路器会维护一组计数器来统计这些数据。断路器会使用这些统计数据来决定是否要将断路器打开来对某个依赖服务的请求进行 “熔断/短路”。 8、当命令执行失败的时候 Hystrix 会进入 fallback 尝试回退处理 我们通常也称该操作为 “服务降级”。而能够引起服务降级处理的情况有下面几种第4步 当前命令处于熔断/短路状态断路器是打开的时候。第5步 当前命令的线程池、 请求队列或 者信号量被占满的时候。第6步HystrixObservableCommand.construct() 或 HystrixCommand.run() 抛出异常的时候。 9、当Hystrix命令执行成功之后 它会将处理结果直接返回或是以Observable 的形式返回。 tips如果我们没有为命令实现降级逻辑或者在降级处理逻辑中抛出了异常 Hystrix 依然会返回一个 Observable 对象 但是它不会发射任何结果数据 而是通过 onError 方法通知命令立即中断请求并通过onError()方法将引起命令失败的异常发送给调用者。 7.5 服务监控hystrixDashboard 7.5.1 案例详解 除了隔离依赖服务的调用以外Hystrix还提供了准实时的调用监控Hystrix DashboardHystrix会持续地记录所有通过Hystrix发起的请求的执行信息并以统计报表和图形的形式展示给用户包括每秒执行多少请求多少成功多少失败等。 Netflix通过hystrix-metrics-event-stream项目实现了对以上指标的监控。Spring Cloud也提供了Hystrix Dashboard的整合对监控内容转化成可视化界面。 新建cloud-consumer-hystrix-dashboard9001 pom文件 ?xml version1.0 encodingUTF-8? project xmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdspringcloud/artifactIdgroupIdcom.yyds/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdcloud-consumer-hystrix-dashboard9001/artifactIddependenciesdependencygroupIdorg.springframework.cloud/groupIdartifactIdspring-cloud-starter-netflix-hystrix-dashboard/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-devtools/artifactIdscoperuntime/scopeoptionaltrue/optional/dependencydependencygroupIdorg.projectlombok/groupIdartifactIdlombok/artifactIdoptionaltrue/optional/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependency/dependencies/projectyml文件 server:port: 9001主启动类 package com.yyds.springcloud;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;SpringBootApplication EnableHystrixDashboard // 添加此注解 public class HystrixDashboardMain9001 {public static void main(String[] args) {SpringApplication.run(HystrixDashboardMain9001.class, args);} } 访问: http://localhost:9001/hystrix 使用cloud-consumer-hystrix-dashboard9001监控cloud-provider-hystrix-payment8001 修改cloud-provider-hystrix-payment8001 package com.yyds.springcloud;import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.servlet.ServletRegistrationBean; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.context.annotation.Bean;SpringBootApplication EnableEurekaClient EnableCircuitBreaker public class HystrixPaymentMain8001 {public static void main(String[] args) {SpringApplication.run(HystrixPaymentMain8001.class, args);System.out.println(****************HystrixPaymentMain8001启动成功**************);}/此配置是为了服务监控而配置与服务容错本身无关springcloud升级后的坑*ServletRegistrationBean因为springboot的默认路径不是/hystrix.stream只要在自己的项目里配置上下面的servlet就可以了/Beanpublic ServletRegistrationBean getServlet() {HystrixMetricsStreamServlet streamServlet new HystrixMetricsStreamServlet();ServletRegistrationBean registrationBean new ServletRegistrationBean(streamServlet);registrationBean.setLoadOnStartup(1);registrationBean.addUrlMappings(/hystrix.stream);registrationBean.setName(HystrixMetricsStreamServlet);return registrationBean;}} 填写监控地址 http://localhost:8001/hystrix.stream 测试 不断访问http://localhost:8001/payment/circuit/1不断访问http://localhost:8001/payment/circuit/-17.5.2 Hystrix Dashboard参数介绍 Hystrix Dashboard面板可分上下两部分来查询上面部分是断路器方法调用的相关信息Circuit下面部分是Hystrix为断路器方法提供的线程池相关信息Thread Pools。 在图表中左上角的圆圈代表了该方法的流量和状态 圆圈越大代表方法流量越大圆圈为绿色代表断路器健康、黄色代表断路器偶发故障、红色代表断路器故障 右上角的计数器三列数字 第一列从上到下 绿色代表当前成功调用的数量蓝色代表短路请求的数量蓝绿色代表错误请求的数量 第二列从上到下 黄色代表超时请求的数量紫色代表线程池拒绝的数量红色代表失败请求的数量 第三列 过去10s的错误请求百分比 Thread Pools Hystrix会针对一个受保护的类创建一个对应的线程池这样做的目的是Hystrix的命令被调用的时候不会受方法请求线程的影响或者说Hystrix的工作线程和调用者线程相互之间不影响 在图表中左上角的圆圈代表了该线程池的流量和状态 圆圈越大代表线程池越活越流量越大圆圈颜色代表的是线程池的健康状况 左下角从上至下 Active代表线程池中活跃线程的数量Queued代表排队的线程数量该功能默认禁止因此默认情况下始终为0Pool Size代表线程池中线程的数量 右下角从上至下 Max Active代表最大活跃线程这里展示的数据是当前采用周期中活跃线程的最大值Execcutions代表线程池中线程被调用执行Hystrix命令的次数Queue Size代表线程池队列的大小默认禁用无意义
- 上一篇: 丽水网站建设专业的公司wordpress在哪里修改
- 下一篇: 丽水做企业网站的地方长春百度网站优化
相关文章
-
丽水网站建设专业的公司wordpress在哪里修改
丽水网站建设专业的公司wordpress在哪里修改
- 技术栈
- 2026年03月21日
-
丽水网站建设微信推广中国建筑英才网官网
丽水网站建设微信推广中国建筑英才网官网
- 技术栈
- 2026年03月21日
-
丽水网站建设明恩玉杰phpcms 网站转移
丽水网站建设明恩玉杰phpcms 网站转移
- 技术栈
- 2026年03月21日
-
丽水做企业网站的地方长春百度网站优化
丽水做企业网站的地方长春百度网站优化
- 技术栈
- 2026年03月21日
-
励志网站源码安卓系统app
励志网站源码安卓系统app
- 技术栈
- 2026年03月21日
-
利用wix建手机网站优购网上商城
利用wix建手机网站优购网上商城
- 技术栈
- 2026年03月21日
