使用 Azure Spring Boot Actuator 管理和监视应用Manage and monitor app with Azure Spring Boot Actuator

在将新的二进制文件部署到应用后,可能需要检查功能并查看有关正在运行的应用程序的信息。After deploying new binary to your app, you may want to check the functionality and see information about your running application. 本文介绍了如何从 Azure Spring Cloud 提供的测试终结点访问 API,并公开了用于应用的生产就绪功能。This article explains how to access the API from a test endpoint provided by Azure Spring Cloud and expose the production-ready features for your app.

先决条件Prerequisites

本文假定你有一个可以在 Azure Spring Cloud 服务上成功部署和启动的 Spring Boot 2.x 应用程序。This article assumes that you have a Spring Boot 2.x application that can be successfully deployed and booted on Azure Spring Cloud service. 请参阅快速入门:使用 Azure 门户启动现有的 Azure Spring Cloud 应用程序See Quickstart: Launch an existing Azure Spring Cloud application using the Azure portal

通过测试终结点验证应用Verify app through test endpoint

  1. 请转到“应用程序仪表板”并单击你的应用,以进入应用概述页。Go to Application dashboard and click your app to enter the app overview page.

  2. 在“概述”窗格中,应该会看到“测试终结点” 。In the Overview pane, you should see Test Endpoint. 从命令行或浏览器访问此终结点,并观察 API 响应。Access this endpoint from command line or browser and observe the API response.

  3. 请记录将会在后面部分中用到的测试终结点 URI。Note the Test endpoint URI that will be used in the coming section.

提示

  • 如果该应用返回前端页并通过相对路径引用其他文件,请确认你的测试终结点是以斜杠 (/) 结尾的。If the app returns a front-end page and references other files through relative path, confirm that your test endpoint ends with a slash (/). 这将确保正确加载 CSS 文件。This will ensure that the CSS file is loaded correctly.
  • 如果你从浏览器中查看 API,并且你的浏览器要求你在查看该页之前输入登录凭据,请使用 URL 解码对测试终结点进行解码。If you view your API from a brower and your browser requires you to enter login credentials to view the page, use URL decode to decode your test endpoint. URL 解码会返回格式为“https://<username>:<password>@<cluster-name>.test.azureapps.io/<app-name>/<deployment-name>”的 URL。URL decode returns a URL in the form "https://<username>:<password>@<cluster-name>.test.azureapps.io/<app-name>/<deployment-name>". 请使用这种格式来访问终结点。Use this form to access your endpoint.

添加执行器依赖项Add actuator dependency

若要将执行器添加到基于 Maven 的项目,请添加“Starter”依赖项:To add the actuator to a Maven-based project, add the 'Starter' dependency:

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

请编译这个新的二进制文件并将它部署到你的应用。Compile the new binary and deploy it to your app.

启用生产就绪功能Enable production-ready features

利用执行器终结点,可以监视应用程序并与之进行交互。Actuator endpoints let you monitor and interact with your application. 默认情况下,Spring Boot 应用程序公开 healthinfo 终结点,以显示任意应用程序信息和运行状况信息。By default, Spring Boot application exposes health and info endpoints to show arbitrary application info and health information.

若要观察配置以及可配置的环境,还需要启用 envconfiggrops 终结点。To observe the configuration and configurable environment, we need to enable env and configgrops endpoints as well.

  1. 转到应用“概述”窗格,单击“设置”菜单中的“配置”,转到“环境变量”配置页 。Go to app Overview pane, click Configuration in the setting menu, go to the Environment variables configuration page.

  2. 采用类似于“key:value”的形式添加以下属性。Add the following properties as in the "key:value" form. 此环境将打开 Spring Actuator 终结点“env”、“health”、“info”。This environment will open the Spring Actuator endpoint "env", "health", "info".

    management.endpoints.web.exposure.include: env,health,info
    
  3. 单击“保存”按钮,应用程序将自动重启并加载新环境变量。Click Save button, your application will restart automatically and load the new environment variables.

现在可以返回到应用概述窗格,并等待“预配状态”更改为“成功”。You can now go back to the app overview pane and wait until the Provisioning Status changes to "Succeeded". 运行的实例将会有多个。There will be more than one running instance.

备注

在将应用公开后,这些执行器终结点也会公开。Once you expose the app to public, these actuator endpoints are exposed to public as well. 可通过删除环境变量 management.endpoints.web.exposure.include 来隐藏所有的终结点,并设置 management.endpoints.web.exposure.exclude=*You can hide all endpoints by deleting the environment variables management.endpoints.web.exposure.include, and set management.endpoints.web.exposure.exclude=*

通过查看执行器终结点来查看应用程序信息View the actuator endpoint to view application information

  1. 你现在可以访问 URL "<test-endpoint>/actuator/" 来查看 Spring Boot Actuator 公开的所有终结点。You can now access the url "<test-endpoint>/actuator/" to see all endpoints exposed by Spring Boot Actuator.
  2. 访问 URL "<test-endpoint>/actuator/env",可以看到应用使用的活动配置文件,以及所有加载的环境变量。Access url "<test-endpoint>/actuator/env", you can see active profiles used by the app, and all environment variables loaded.
  3. 如果需要搜索某个特定环境,可访问 URL "<test-endpoint>/actuator/env/{toMatch}" 来查看该环境。If you want to search a specific environment, you can access url "<test-endpoint>/actuator/env/{toMatch}" to view it.

若要查看所有内置的终结点,请参阅公开终结点To view all the endpoints built-in, see Exposing Endpoints

后续步骤Next steps