SpringCloud-第五篇-Hystrix快速上手二

2次阅读

共计 2235 个字符,预计需要花费 6 分钟才能阅读完成。

1:SpringCloud 中应用:

1.1:退出依赖

<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>

1.2:启动类上增加

@SpringBootApplication
@EnableHystrixDashboard
@EnableCircuitBreaker
//@EnableHystrix

1.3:Controller 上配置

在 Controller 的办法上增加 Hystrix 的配置,形如:

@HystrixCommand(fallbackMethod = "error", commandProperties = {@HystrixProperty(name="execution.isolation.strategy", value = "THREAD"),
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "4000"),
            @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
            @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value =”50")
    }, threadPoolProperties = {@HystrixProperty(name = "coreSize", value = "1"),
            @HystrixProperty(name = "maxQueueSize", value = "10"),
            @HystrixProperty(name = "keepAliveTimeMinutes", value = "1000"),
            @HystrixProperty(name = "queueSizeRejectionThreshold", value = "8"),
            @HystrixProperty(name = "metrics.rollingStats.numBuckets", value = "12"),
            @HystrixProperty(name = "metrics.rollingStats.timeInMilliseconds", value = "1500")
    })

Hystrix 反对两种形式定义 HystrixCommand,一种是将类继承自 HystrixCommand 类,重写 run 办法,另一种是在办法头上写注解的形式,应用注解的形式代码会比拟清晰,将 Hystrix 代码和业务代码隔离开

2: DashBoard

2.1: 概述

Hystrix 自带了 DashBoard,如果监控单个实例,能够很不便的通过 Hystrix 的 dashboard 进行查看运行状况,间接进入 http://localhost:8080/hystrix
Hystrix Dashboard 共反对三种不同的监控形式,顺次为:

  • 1:默认的集群监控:通过 URLhttp://turbine-hostname:port/turbine.stream 开启,实现对默认集群的监控。
  • 2:指定的集群监控:通过 URLhttp://turbine-hostname:port/turbine.stream?cluster=[clusterName] 开启,实现对 clusterName 集群的监控。
  • 3:单体利用的监控:通过 URLhttp://hystrix-app:port/hystrix.stream 开启,实现对具体某个服务实例的监控。

前两者都对集群的监控,须要整合 Turbine 能力实现

2.2: 示例如下

  • Delay:该参数用来管制服务器上轮询监控信息的延迟时间,默认为 2000 毫秒,能够通过配置该属性来升高客户端的网络和 CPU 耗费。
  • Title:该参数对应了头部题目 Hystrix Stream 之后的内容,默认会应用具体监控实例的 URL,能够通过配置该信息来展现更适合的题目。
  • 监控信息的左上局部找到两个重要的图形信息:一个实心圆和一条曲线。

(1)实心圆:共有两种含意。它通过色彩的变动代表了实例的衰弱水平,它的衰弱度从绿色、黄色、橙色、红色递加。该实心圆除了色彩的变动之外,它的大小也会依据实例的申请流量发生变化,流量越大该实心圆就越大。所以通过该实心圆的展现,就能够在大量的实例中疾速的发现故障实例和高压力实例。
(2)曲线:用来记录 2 分钟内流量的绝对变动,能够通过它来察看到流量的回升和降落趋势。

正文完
 0