使用 Azure Spring Cloud 的断路器仪表板Use Circuit Breaker Dashboard with Azure Spring Cloud

Spring Cloud Netflix Turbine 广泛用于聚合多个 Hystrix 指标流,以便可以使用 Hystrix 仪表板在单个视图中监视流。Spring Cloud Netflix Turbine is widely used to aggregate multiple Hystrix metrics streams so that streams can be monitored in a single view using Hystrix dashboard. 本教程演示如何在 Azure Spring Cloud 上使用它们。This tutorial demonstrates how to use them on Azure Spring Cloud.

备注

Netflix Hystrix 已广泛用于许多现有的 Spring Cloud 应用,但不再处于积极开发阶段。Netflix Hystrix is widely used in many existing Spring Cloud apps but it is no longer in active development. 如果要开发新项目,请改用 Spring Cloud 断路器实现,例如 resilience4jIf you are developing new project, use instead Spring Cloud Circuit Breaker implementations like resilience4j. 与本教程中所示的 Turbine 不同,新的 Spring Cloud 断路器框架将其指标数据管道的所有实现统一到 Micrometer 中。Different from Turbine shown in this tutorial, the new Spring Cloud Circuit Breaker framework unifies all implementations of its metrics data pipeline into Micrometer. 我们仍在 Azure Spring Cloud 中支持 Micrometer,因此本教程不会对此进行介绍。We are still working on supporting micrometer in Azure Spring Cloud, thus it will not be covered by this tutorial.

准备示例应用程序Prepare your sample applications

示例从此存储库分叉。The sample is forked from this repository.

将示例存储库克隆到开发环境中:Clone the sample repository to your develop environment:

git clone https://github.com/Azure-Samples/Azure-Spring-Cloud-Samples.git
cd Azure-Spring-Cloud-Samples/hystrix-turbine-sample

生成将在本教程中使用的 3 个应用程序:Build the 3 applications that will be used in this tutorial:

  • user-service:具有单个 /personalized/{id} 终结点的简单 REST 服务user-service: A simple REST service that has a single endpoint of /personalized/{id}
  • recommendation-service:具有单个 /recommendations 终结点的简单 REST 服务,并将由 user-service 调用。recommendation-service: A simple REST service that has a single endpoint of /recommendations, which will be called by user-service.
  • hystrix-turbine:Hystrix 仪表板服务(用于显示 Hystrix 流)和 Turbine 服务(用于聚合来自其他服务的 Hystrix 指标流)。hystrix-turbine: A Hystrix dashboard service to display Hystrix streams and a Turbine service aggregating Hystrix metrics stream from other services.
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 Cloud 实例Provision your Azure Spring Cloud instance

按照在 Azure CLI 中预配服务实例过程执行操作。Follow the procedure, Provision a service instance on the Azure CLI.

将应用程序部署到 Azure Spring CloudDeploy your applications to Azure Spring Cloud

这些应用不使用配置服务器,因此无需为 Azure Spring Cloud 设置配置服务器。These apps do not use Config Server, so there is no need to set up Config Server for Azure Spring Cloud. 按照以下步骤进行创建和部署:Create and deploy as follows:

az spring-cloud app create -n user-service --is-public
az spring-cloud app create -n recommendation-service
az spring-cloud app create -n hystrix-turbine --is-public

az spring-cloud app deploy -n user-service --jar-path user-service/target/user-service.jar
az spring-cloud app deploy -n recommendation-service --jar-path recommendation-service/target/recommendation-service.jar
az spring-cloud app deploy -n hystrix-turbine --jar-path hystrix-turbine/target/hystrix-turbine.jar

验证应用Verify your apps

所有应用都已运行并且可发现后,请从浏览器中使用路径 https://-user-service.azuremicroservices.io/personalized/1 访问 user-serviceAfter all the apps are running and discoverable, access user-service with the path https://-user-service.azuremicroservices.io/personalized/1 from your browser. 如果 user-service 可以访问 recommendation-service,则应获得以下输出。If the user-service can access recommendation-service, you should get the following output. 如果网页无法运行,请刷新几次。Refresh the web page a few times if it doesn't work.

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

访问 Hystrix 仪表板和指标流Access your Hystrix dashboard and metrics stream

使用公共终结点或专用测试终结点进行验证。Verify using public endpoints or private test endpoints.

使用公共终结点Using public endpoints

从浏览器使用路径 https://<SERVICE-NAME>-hystrix-turbine.azuremicroservices.io/hystrix 访问 hystrix-turbine。Access hystrix-turbine with the path https://<SERVICE-NAME>-hystrix-turbine.azuremicroservices.io/hystrix from your browser. 下图显示在此应用中运行的 Hystrix 仪表板。The following figure shows the Hystrix dashboard running in this app.

Hystrix 仪表板

将 Turbine 流 URL https://<SERVICE-NAME>-hystrix-turbine.azuremicroservices.io/turbine.stream?cluster=default 复制到文本框中,然后单击“监视流”。Copy the Turbine stream url https://<SERVICE-NAME>-hystrix-turbine.azuremicroservices.io/turbine.stream?cluster=default into the text box, and click Monitor Stream. 此时将显示仪表板。This will display the dashboard. 如果查看器中未显示任何内容,请点击 user-service 终结点以生成流。If nothing shows in the viewer, hit the user-service endpoints to generate streams.

Hystrix 流现在,你可以体验断路器仪表板。Hystrix stream Now you can experiment with the Circuit Breaker Dashboard.

备注

在生产环境中,Hystrix 仪表板和指标流不应向 Internet 公开。In production, the Hystrix dashboard and metrics stream should not be exposed to the Internet.

使用专用测试终结点Using private test endpoints

也可以从 test-endpoint 访问 Hystrix 指标流。Hystrix metrics streams are also accessible from test-endpoint. 作为后端服务,我们没有为 recommendation-service 分配公共终结点,但我们可以在 https://primary:<KEY>@<SERVICE-NAME>.test.azuremicroservices.io/recommendation-service/default/actuator/hystrix.stream 使用 test-endpoint 显示其指标As a backend service, we didn't assign a public end-point for recommendation-service, but we can show its metrics with test-endpoint at https://primary:<KEY>@<SERVICE-NAME>.test.azuremicroservices.io/recommendation-service/default/actuator/hystrix.stream

Hystrix test-endpoint 流

作为 Web 应用,Hystrix 仪表板应在 test-endpoint 上正常工作。As a web app, Hystrix dashboard should be working on test-endpoint. 如果无法正常工作,则可能有两个原因:第一,使用 test-endpoint/ to /<APP-NAME>/<DEPLOYMENT-NAME> 更改基 URL,或者,第二,Web 应用将绝对路径用于静态资源。If it is not working properly, there may be two reasons: first, using test-endpoint changed the base URL from / to /<APP-NAME>/<DEPLOYMENT-NAME>, or, second, the web app is using absolute path for static resource. 若要使其在 test-endpoint 上正常工作,可能需要手动编辑前端文件中的 To get it working on test-endpoint, you might need to manually edit the in the front-end files.

后续步骤Next steps