SpringCloud系列之Turbine

作者 胡萝虎 日期 2018-05-16
SpringCloud系列之Turbine

SpringCloud系列之Hystrix一文中,我们介绍Hystrix的监控功能。通过在Hystrix Dashboard中访问hystrix.stream端点,可以监控一个应用的断路器情况,但是每次只能监控一个服务。在微服务系统中,一个服务可能被部署多个节点,一个一个查看监控情况显然不合理。所以Spring Cloud为我们提供了一个Hystrix监控聚合工具——Turbine

Turbine

Turbine是一个Hystrix监控数据聚合工具,可以将应用的hystrix.stream端点的响应数据聚合到一个turbine.stream端点下,方便监控的管理。

image-20180516151400970

实战

Turbine Server

首先创建新工程turbine-server

image-20180516152233292

添加依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>huluohu-spring-cloud-demo</artifactId>
<groupId>com.huluohu.cloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>turbine-server</artifactId>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<finalName>${project.name}</finalName>
</configuration>
</plugin>
</plugins>
</build>
</project>

添加配置

bootstrap.yml

spring:
application:
name: turbine-server
cloud:
config:
uri: http://localhost:8888 # 配置服务器地址
fail-fast: true # 快速失败
name: ${spring.application.name} #默认就是spring.application.name
profile: ${spring.profiles.active:default} # 默认就是spring.profiles.active
label: master #标签
retry:
max-attempts: 6 #最大重试次数
max-interval: 2000 #间隔时间

application.yml

management:
security:
enabled: false

application-local.yml

server:
port: 7776
eureka:
instance:
prefer-ip-address: true
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka/
turbine:
app-config: provider-user,consumer-movie #设置关注的服务名称
cluster-name-expression: "'default'"

创建启动类

package com.huluohu.cloud.turbine;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.turbine.EnableTurbine;

@SpringBootApplication
@EnableTurbine #启用turbine,同时会启用eureka client
public class TurbineServer {
public static void main(String[] args) {
SpringApplication.run(TurbineServer.class, args);
}
}

测试

  1. 启动Eureka Server(local profile)
  2. 启动Config Server(不需要区分profile)
  3. 启动provider-user-hystrix(local profile)
  4. 启动consumer-movie-hystrix(local profile)
  5. 启动hystrix-dashboard(不区分profile)
  6. 启动turbine-server(local profile)
  • 应用列表

    image-20180516160203104

  • 浏览器中访问http://localhost:7777/hystrix/,打开Hystrix Dashboard

    image-20180516160404669

  • 在Hystrix Dashboard的url输入框中输入http://localhost:7776/turbine.stream,即Turbine Server的turbine.stream端点,title随便填,点击Monitor Stream

    image-20180516160627716

  • 可以看到consumer-movie的Hystrix监控数据已经被聚合了,由于provider-user服务没用启用hystrix,所以这里没有展示出来

    image-20180516160740571

在这里,Turbine Server直接通过服务名称从Eureka Server获取服务地址,并聚合服务应用上的Hystrix监控数据。

进阶

在一些情况下,比如微服务与Turbine Server之间网络不通,则无法通过Turbine对微服务进行监控。这时候可以借助MQ来完成数据的采集,即微服务将Hystrix监控数据发送到MQ,然后Turbine再从MQ订阅消费这些数据,从而实现Hystrix监控。

以下以RbbitMQ为例进行演示。

修改微服务

复制consumer-movie-hystrix,改名为consumer-movie-hystrix-mq

image-20180522152543869

修改依赖

增加以下依赖,原有的依赖保留

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-netflix-hystrix-stream</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-stream-rabbit</artifactId>
</dependency>

修改配置

application-local.yml新增rabbitmq配置

spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest

修改Turbine Server

复制turbine-server,改名为turbine-server-mq

image-20180522152854060

####修改依赖

  • 增加依赖

    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-turbine-stream</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-stream-rabbit</artifactId>
    </dependency>
  • 删除依赖

    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-turbine</artifactId>
    </dependency>

    修改配置

application-local.yml增加rabbitmq配置

spring:
rabbitmq:
host: localhost
port: 5672
username: guest
password: guest

修改启动类

删除@EnableTurbin,增加@EnableTurbineStream

package com.huluohu.cloud.turbine;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.turbine.stream.EnableTurbineStream;

@SpringBootApplication
@EnableTurbineStream
public class TurbineServer {
public static void main(String[] args) {
SpringApplication.run(TurbineServer.class, args);
}
}

测试

  1. 启动Eureka Server(local profile)
  2. 启动Config Server(不需要区分profile)
  3. 启动provider-user-hystrix(local profile)
  4. 启动consumer-movie-hystrix-mq(local profile)
  5. 启动hystrix-dashboard(不区分profile)
  6. 启动turbine-server-mq(local profile)
  • 应用列表

    image-20180522153610382

  • 浏览器中访问http://localhost:7777/hystrix/,打开Hystrix Dashboard,前面已经介绍过

  • 此时,在url输入框中,不再输入Turbine Server的turbine.stream端点,而是直接输入ip和端口即可,这里即是http://localhost:7776

  • 后续的操作和效果跟本篇上面介绍的一样,不再赘述。

你可能会喜欢

“扫一扫接着看”