微服务熔断限流Hystrix之Dashboard

发布时间:2020-08-09 07:13:39 作者:程序员果果
来源:网络 阅读:681

简介

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

欢迎关注我的公众号《程序员果果》,关注有惊喜~~
微服务熔断限流Hystrix之Dashboard

推荐阅读:
  1. SpringCloud微服务(03):Hystrix组件,实现服务熔断
  2. SpringCloud之熔断器Hystrix(二)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring cloud hystrix st

上一篇:如何杀掉一个用户下的所有进程并drop掉这个用户

下一篇:当知乎跳起科技华尔兹

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》