16SpringCloud - 断路器项目示例(Hystrix Dashboard)

简介: 16SpringCloud - 断路器项目示例(Hystrix Dashboard)

简介

Hystrix Dashboard在微服务架构中为例保证程序的可用性,防止程序出错导致网络阻塞,出现了断路器模型。断路器的状况反应了一个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面。

添加依赖

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

开启 HD

修改RibbonConsumerApplication.java

在程序的入口RibbonConsumerApplication类,加上@EnableHystrix注解开启断路器,这个是必须的,并且需要在程序中声明断路点@HystrixCommand;加上@EnableHystrixDashboard注解,开启HystrixDashboard。

@EnableHystrix
@EnableDiscoveryClient
@EnableHystrixDashboard
@SpringBootApplication
public class RibbonConsumerApplication {
    @LoadBalanced
    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate();
    }
    public static void main(String[] args) {
        SpringApplication.run(RibbonConsumerApplication.class, args);
    }
}

声明断路点

声明断路点 @HystrixCommand(fallbackMethod = "defaultStores")

@RestController
public class ConsumerController {
    @Autowired
    private RestTemplate restTemplate;
    @HystrixCommand(fallbackMethod = "defaultStores")
    @GetMapping(value = "/hello")
    public String hello() {
        return restTemplate.getForEntity("http://eureka-provider/", String.class).getBody();
    }
    public String defaultStores() {
        return "feign + hystrix Dashboard ,提供者服务挂了";
    }
}

@HystrixCommand 表明该方法为hystrix包裹,可以对依赖服务进行隔离、降级、快速失败、快速重试等等hystrix相关功能 该注解属性较多,下面讲解其中几个

  • fallbackMethod 降级方法
  • commandProperties 普通配置属性,可以配置HystrixCommand对应属性,例如采用线程池还是信号量隔离、熔断器熔断规则等等
  • ignoreExceptions 忽略的异常,默认HystrixBadRequestException不计入失败
  • groupKey() 组名称,默认使用类名称
  • commandKey 命令名称,默认使用方法名

测试服务

依次启动项目:

spring-cloud-eureka-service

spring-cloud-eureka-provider-1

spring-cloud-eureka-provider-2

spring-cloud-eureka-provider-3

spring-cloud-ribbon-consumer-hystrix-dashboard

启动该工程后,访问服务注册中心,查看服务是否都已注册成功:http://localhost:8761/

Hystrix Dashboard 监控

可以访问 http://127.0.0.1:9090/hystrix ,获取Hystrix Dashboard信息,默认最大打开5个终端获取监控信息,可以增加delay参数指定获取监控数据间隔时间

在界面依次输入:http://127.0.0.1:9000/hystrix.stream 、2000 、hello 点确定。可以访问以下,图形化监控页面。

Hystrix Monitor 图形化监控页面

目录
相关文章
|
监控 Java API
Spring cloud Hystrix 、Dashboard、API(zuul)相关报错
Spring cloud Hystrix 、Dashboard、API(zuul)相关报错
364 2
|
设计模式 监控 Java
解析Spring Cloud中的断路器模式原理
解析Spring Cloud中的断路器模式原理
|
Java 微服务 Spring
微服务(九)-Hystrix(断路器)
微服务(九)-Hystrix(断路器)
hystrix.stream dashboard
hystrix.stream dashboard
186 3
|
监控 Java 数据中心
通用快照方案问题之服务雪崩问题如何解决
通用快照方案问题之服务雪崩问题如何解决
218 0
|
设计模式 监控 Java
深入理解Spring Cloud中的断路器模式
深入理解Spring Cloud中的断路器模式
|
Java API 网络架构
深入了解 Spring Cloud Config、Spring Cloud Gateway 与断路器模式
Spring Microservices 是一个框架,它使用 Spring 框架更容易地构建和管理基于微服务的应用程序。微服务是一种架构风格,其中一个大型应用程序被构建为一组小型、独立可部署的服务。每个服务具有明确定义的职责,并通过 API 与其他服务通信。
275 0
|
监控 负载均衡 数据可视化
SpringCloud - Hystrix断路器-服务熔断与降级和HystrixDashboard
SpringCloud - Hystrix断路器-服务熔断与降级和HystrixDashboard
284 0
|
负载均衡 Dubbo Java
Spring Cloud Alibaba与Spring Cloud区别和联系?
Spring Cloud Alibaba与Spring Cloud区别和联系?
|
Java Nacos Sentinel
Spring Cloud Alibaba:一站式微服务解决方案
Spring Cloud Alibaba(简称SCA) 是一个基于 Spring Cloud 构建的开源微服务框架,专为解决分布式系统中的服务治理、配置管理、服务发现、消息总线等问题而设计。
3237 13
Spring Cloud Alibaba:一站式微服务解决方案

热门文章

最新文章