转载

微服务熔断限流Hystrix之Dashboard

微服务熔断限流Hystrix之Dashboard

简介

Hystrix Dashboard是一款针对Hystrix进行实时监控的工具,通过Hystrix Dashboard可以直观地看到各Hystrix Command的请求响应时间,请求成功率等数据。

快速上手

工程说明

工程名 端口 作用
eureka-server 8761 注册中心
service-hi 8762 服务提供者
service-consumer 8763 服务消费者

核心代码

eureka-server 工程

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

application.yml

server:
  port: 8761
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:/${server.port}/eureka/
spring:
  application:
    name: eureka-server

启动类

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run( EurekaServerApplication.class, args );
    }

}

service-hi 工程

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

application.yml

server:
  port: 8762

spring:
  application:
    name: service-hi
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

HelloController

@RestController
public class HelloController {

    @GetMapping("/hi")
    public String hi() {
        return "hello ~";
    }

    @GetMapping("/hey")
    public String hey() {
        return "hey ~";
    }


    @GetMapping("/oh")
    public String oh() {
        return "ah ~";
    }

    @GetMapping("/ah")
    public String ah() {
        //模拟接口1/3的概率超时
        Random rand = new Random();
        int randomNum = rand.nextInt(3) + 1;
        if (3 == randomNum) {
            try {
                Thread.sleep( 3000 );
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return "来了老弟~";
    }

}

启动类

@SpringBootApplication
@EnableEurekaClient
public class ServiceHiApplication {

    public static void main(String[] args) {
        SpringApplication.run( ServiceHiApplication.class, args );
    }

}

service-consumer 工程

pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-ribbon</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-netflix-hystrix</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

application.yml

server:
  port: 8763

  tomcat:
    uri-encoding: UTF-8
    max-threads: 1000
    max-connections: 20000

spring:
  application:
    name: service-consumer
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

management:
  endpoints:
    web:
      exposure:
        include: "*"
      cors:
        allowed-origins: "*"
        allowed-methods: "*"

HelloService

@Service
public class HelloService {

    @Autowired
    private RestTemplate restTemplate;

    /**
     * 简单用法
     */
    @HystrixCommand
    public String hiService() {
        return restTemplate.getForObject("http://SERVICE-HI/hi" , String.class);
    }

    /**
     * 定制超时
     */
    @HystrixCommand(commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "30000") })
    public String heyService() {
        return restTemplate.getForObject("http://SERVICE-HI/hey" , String.class);
    }

    /**
     * 定制降级方法
     */
    @HystrixCommand(fallbackMethod = "getFallback")
    public String ahService() {
        return restTemplate.getForObject("http://SERVICE-HI/ah" , String.class);
    }

    /**
     * 定制线程池隔离策略
     */
    @HystrixCommand(fallbackMethod = "getFallback",
            threadPoolKey = "studentServiceThreadPool",
            threadPoolProperties = {
                    @HystrixProperty(name="coreSize", value="30"),
                    @HystrixProperty(name="maxQueueSize", value="50")
            }
    )
    public String ohService() {
        return restTemplate.getForObject("http://SERVICE-HI/oh" , String.class);
    }


    public String getFallback() {
        return "Oh , sorry , error !";
    }


}

HelloController

@RestController
public class HelloController {

    @Autowired
    private HelloService helloService;


    @GetMapping("/hi")
    public String hi() {
        return helloService.hiService();
    }

    @GetMapping("/hey")
    public String hey() {
        return helloService.heyService();
    }

    @GetMapping("/oh")
    public String oh() {
        return helloService.ohService();
    }

    @GetMapping("/ah")
    public String ah() {
        return helloService.ahService();
    }


}

启动类

@SpringBootApplication
@EnableEurekaClient
@EnableHystrixDashboard
@EnableHystrix
@EnableCircuitBreaker
public class ServiceConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run( ServiceConsumerApplication.class, args );
    }

    @LoadBalanced
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

}

Hystrix Dashboard 的使用

JSON格式监控信息

先访问http://localhost:8762/hi

再打开http://localhost:8763/actuator/hystrix.stream,可以看到一些具体的数据:

微服务熔断限流Hystrix之Dashboard

Hystrix仪表盘监控信息

单纯的查看json数据,很难分析出结果,所以,要在Hystrix仪表盘中来查看这一段json,在hystrix仪表盘中输入监控地址进行监控:

打开仪表盘地址:http://localhost:8762/hystrix

微服务熔断限流Hystrix之Dashboard

在界面依次输入:http://localhost:8763/actuator/hystrix.stream 、2000 、service-consumer;点确定。

微服务熔断限流Hystrix之Dashboard

Hystrix仪表盘指标含义

微服务熔断限流Hystrix之Dashboard

测试

编一个测试脚本curl.sh

while true;
do
curl "http://localhost:8763/hi";
curl "http://localhost:8763/hey";
curl "http://localhost:8763/oh";
curl "http://localhost:8763/ah";
done

执行测试脚本,查看Hystrix仪表盘:

微服务熔断限流Hystrix之Dashboard

可以看出 ahService 因为模拟了1/3的概率超时,所以监控中呈现了30%左右的错误百分比。

源码

https://github.com/gf-huanchupk/SpringCloudLearning/tree/master/chapter17

-- END --

微服务熔断限流Hystrix之Dashboard

每一个“ ”,都是对我最大的肯定 微服务熔断限流Hystrix之Dashboard

原文  http://mp.weixin.qq.com/s?__biz=MzI0NTYzMjM0Ng==&mid=2247484383&idx=1&sn=ed855ca1ea58c1c0b17346373ba78d0c
正文到此结束
Loading...