教程:将断路器仪表板与 Azure Spring Apps 配合使用

警告

Hystrix 不再处于活动开发状态,目前为维护模式。

注意

Azure Spring Apps 是 Azure Spring Cloud 服务的新名称。 虽然该服务有新名称,但一些地方仍会使用旧名称,我们仍在更新屏幕截图、视频和图形等资产。

本文介绍如何在 Azure Spring Apps 上使用 Netflix Turbine 和 Netflix Hystrix。 Spring Cloud Netflix Turbine 广泛用于聚合多个 Netflix Hystrix 指标流,以便可以使用 Hystrix 仪表板在单个视图中监视流。

注意

Netflix Hystrix 已广泛用于许多现有的 Spring 应用,但不再处于积极开发阶段。 如果要开发新项目,应改用 Spring Cloud 断路器实现,例如 resilience4j。 与本教程中所示的 Turbine 不同,新的 Spring Cloud 断路器框架将其指标数据管道的所有实现统一到 Micrometer 中,此设备亦受 Azure Spring Apps 支持。 有关详细信息,请参阅通过 Micrometer 收集 Spring Cloud Resilience4J 断路器指标(预览版)

准备示例应用程序

示例从此存储库分叉。

将示例存储库克隆到开发环境中:

git clone https://github.com/Azure-Samples/azure-spring-apps-samples.git
cd azure-spring-apps-samples/hystrix-turbine-sample

生成本教程中的三个应用程序:

  • user-service:具有单个 /personalized/{id} 终结点的简单 REST 服务
  • recommendation-service:具有单个 /recommendations 终结点的简单 REST 服务,由 user-service 调用。
  • hystrix-turbine:Hystrix 仪表板服务(用于显示 Hystrix 流)和 Turbine 服务(用于聚合来自其他服务的 Hystrix 指标流)。
mvn clean package -D skipTests -f user-service/pom.xml
mvn clean package -D skipTests -f recommendation-service/pom.xml
mvn clean package -D skipTests -f hystrix-turbine/pom.xml

预配 Azure Spring Apps 实例

按照快速入门:将第一个应用程序部署到 Azure Spring Apps预配 Azure Spring Apps 实例部分中的步骤进行操作。

将应用程序部署到 Azure Spring Apps

这些应用不使用 Config Server,因此无需为 Azure Spring Apps 设置 Config Server。 按照以下步骤进行创建和部署:

az configure --defaults \
    group=<resource-group-name> \
    spring=<Azure-Spring-Apps-instance-name>

az spring app create --name user-service --assign-endpoint
az spring app create --name recommendation-service
az spring app create --name hystrix-turbine --assign-endpoint

az spring app deploy \
    --name user-service \
    --artifact-path user-service/target/user-service.jar
az spring app deploy \
    --name recommendation-service \
    --artifact-path recommendation-service/target/recommendation-service.jar
az spring app deploy \
    --name hystrix-turbine \
    --artifact-path hystrix-turbine/target/hystrix-turbine.jar

验证应用

在所有应用运行且可发现后,通过浏览器中的路径 https://<Azure-Spring-Apps-instance-name>-user-service.microservices.azure.cn/personalized/1 访问 user-service。 如果 user-service 可以访问 recommendation-service,则应获得以下输出。 如果网页无法运行,请刷新几次。

[{"name":"Product1","description":"Description1","detailsLink":"link1"},{"name":"Product2","description":"Description2","detailsLink":"link3"},{"name":"Product3","description":"Description3","detailsLink":"link3"}]

访问 Hystrix 仪表板和指标流

使用公共终结点或专用测试终结点进行验证。

使用公共终结点

从浏览器使用路径 https://<SERVICE-NAME>-hystrix-turbine.microservices.azure.cn/hystrix 访问 hystrix-turbine。 下图显示在此应用中运行的 Hystrix 仪表板。

Screenshot of the Hystrix dashboard.

将 Turbine 流 URL https://<SERVICE-NAME>-hystrix-turbine.microservices.azure.cn/turbine.stream?cluster=default 复制到文本框中,然后选择“监视流”。 此操作显示仪表板。 如果查看器中未显示任何内容,请点击 user-service 终结点以生成流。

Screenshot of the Hystrix stream page.

注意

在生产环境中,Hystrix 仪表板和指标流不应向 Internet 公开。

使用专用测试终结点

也可以从 test-endpoint 访问 Hystrix 指标流。 作为后端服务,我们没有为 recommendation-service 分配公共终结点,但我们可以在 https://primary:<KEY>@<SERVICE-NAME>.test.microservices.azure.cn/recommendation-service/default/actuator/hystrix.stream 使用 test-endpoint 显示其指标

Screenshot of the Hystrix test-endpoint stream page.

作为 Web 应用,Hystrix 仪表板应在 test-endpoint 上正常工作。 如果无法正常工作,则可能有两个原因:第一,使用 test-endpoint 将基 URL 从 / 更改为了 /<APP-NAME>/<DEPLOYMENT-NAME>;第二,Web 应用将绝对路径用于静态资源。 若要使其在 test-endpoint 上正常工作,可能需要手动编辑前端文件中的 <base>

后续步骤