Turbine

举报
李子捌 发表于 2021/11/14 19:45:04 2021/11/14
【摘要】 1、简介Hystrix Dashboard虽然好用,但是它有一个缺点:一个Hystrix Dashboard只能收集一个微服务的Hystrix流。也就是说对于每个微服务,我们都需要开启一个Hystrix Dashboard来监控其健康情况。可以看到如下Hystrix Dashboard只能输入一个actuator端点地址。这能忍?反正我是忍不了。忍不了我们就可以使用Turbine;Netfi...

1、简介

Hystrix Dashboard虽然好用,但是它有一个缺点:一个Hystrix Dashboard只能收集一个微服务的Hystrix流。也就是说对于每个微服务,我们都需要开启一个Hystrix Dashboard来监控其健康情况。可以看到如下Hystrix Dashboard只能输入一个actuator端点地址。

这能忍?反正我是忍不了。

忍不了我们就可以使用Turbine;Netfilx的Turbine项目,提供了将多个微服务的Hystrix流数据聚合到一个流中,并且通过一个Hystrix Dashboard进行展示,这样就可以很nice的同时监控多个微服务的健康状况啦!


Turbine项目的大致架构图如下所示:

没有Turbine之前,每个微服务都需要开启一个Hystrix Dashboard页面来监控当前微服务的健康情况,有了Turbine之后,多个微服务的信息先通过Turbine进行聚合,再统一在一个Hystrix Dashboard页面展示。

2、正文

2.1 快速搭建

服务列表:

我这里一共搭建了六个服务,其中eureka-server为注册中心,turbine-server为turbine服务,hystrix-dashboard-server为hystrix-dashboard服务(这些服务如果有需要你也可以在一个服务中启动),user-server、order-server、message-server三个服务是受hystrix保护的服务。

<modules>
  <module>eureka-server</module>
  <module>turbine-server</module>
  <module>hystrix-dashboard-server</module>
  <module>user-server</module>
  <module>order-server</module>
  <module>message-server</module>
</modules>

服务依赖

所有的依赖和版本均在下面,自行在指定服务中选择需要的依赖

<parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.3.4.RELEASE</version>
</parent>

<properties>
  <spring-cloud.version>Hoxton.RELEASE</spring-cloud.version>
</properties>

<dependencies>
  <!--web-->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <!--actuator-->
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
  <!--eureka server-->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
  </dependency>
  <!--eureka client-->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>
  <!--hystrix-->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  </dependency>
  <!--turbine-->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
  </dependency>
  <!--hystrix-dashboard-->
  <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
  </dependency>
</dependencies>


<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-dependencies</artifactId>
      <version>${spring-cloud.version}</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>


eureka-server服务搭建:

application.yml配置文件

server:
  port: 4010

spring:
  application:
    name: eureka-server

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

服务启动类

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApp {

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

}


user-server、order-server、message-server服务搭建:

user-server服务application.yml配置文件(其他两个相似)

server:
  port: 22222

spring:
  application:
    name: user-server

eureka:
  client:
    service-url:
      defaultZone: http://localhost:4010/eureka

## 开启hystrix.stream端点
management:
  endpoints:
    web:
      exposure:
        include: 'hystrix.stream'

user-server服务启动类(其他两个相似)

@SpringBootApplication
@EnableEurekaClient
// 开启Hystrix
@EnableHystrix
public class UserServerApp {

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

}

user-server编写受hystrix保护控制器(其他两个相似)

@RestController
@RequestMapping(value = "/user")
public class UserController {

    @GetMapping
    @HystrixCommand(fallbackMethod = "fallback")
    public String userDemo() {
        return "user-98";
    }

    // 写两个方法是为了演示方法名称不同,hystrix dashboard会创建不同的circuit
    @GetMapping("/demo1")
    @HystrixCommand(fallbackMethod = "fallback")
    public String userDemo1() {
        return "user-98";
    }

    private String fallback() {
        return "user-NaN";
    }

}


turbine-server服务搭建:

application.yml配置文件

server:
  port: 11111

spring:
  application:
    name: turbine-server

eureka:
  client:
    service-url:
      defaultZone: http://localhost:4010/eureka

turbine:
  ## eureka中服务名称列表	
  app-config: order-server,user-server,message-server
  ## eureka集群名称,默认为default
  cluster-name-expression: "'default'"
  ## 开启主机+端口组合聚合服务,默认情况下turbine会将统一主机下的服务聚合成一个服务来统计
  combine-host-port: true

启动类,添加@EnableTurbine启动turbine

@SpringBootApplication
// 开启turbine
@EnableTurbine
public class TurbineServerApp {

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

}


hystrix-dashboard-server服务搭建:

application.yml配置文件

server:
  port: 55555

eureka:
  client:
    service-url:
      defaultZone: http://localhost:4010/eureka
    ## 不注册  
    register-with-eureka: false

启动类,添加@EnableHystrixDashboard启动HystrixDashboard

@SpringBootApplication
@EnableHystrixDashboard
public class HystrixDashboardServerApp {

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

}


2.2 服务启动

服务启动应先启动注册中心eureka-server再启动user-server、order-server、message-server服务,最后启动turbine-server和hystrix-dashboard-server。

启动完成之后,先查看eureka-server注册中心上服务是否注册正常

之后访问hystrix-dashboard-server服务的hystrix端点,确保看到如下hystrix dashboard界面

在hystrix dashboard中输入turbine-server提供的turbine.stream端点

初始进入的时候由于各个受hystrix保护的方法并未调用,因此未上报任何数据,所以需要调用各个接口触发数据上报。

之后看到如下界面,说明服务启动成功,整个监控服务整合完毕


2.3 注意事项

hystrix dashboard中展示的circuit数据,会根据方法名来创建,因此不管是不是同一个服务中,只要受hystrix保护的方法,如果方法名相同,将会被聚合到一起展示,这里指的注意哦!

此外不要理解成一个circuit会对应一个thread pools

  • circuit的个数与方法名个数相同
  • thread pools每一个受hystrix保护的方法所在类会创建一个

👇🏻 关注公众号 获取更多资料👇🏻

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。