Monitoring and Management over JMX

Java 管理扩展 (JMX) 提供了监视和管理应用程序的标准机制。默认情况下,此功能未启用。您可以通过将 configprop:spring.jmx.enabled[] 配置属性设置为“true”来启用它。Spring Boot 作为 ID 为“mbeanServer”的 Bean 公开了最合适的“MBeanServer”。使用 Spring JMX 注释(“@ManagedResource”、“@ManagedAttribute”或“@ManagedOperation”)注释的任何 Bean 都可用于它。

Java Management Extensions (JMX) provide a standard mechanism to monitor and manage applications. By default, this feature is not enabled. You can turn it on by setting the configprop:spring.jmx.enabled[] configuration property to true. Spring Boot exposes the most suitable MBeanServer as a bean with an ID of mbeanServer. Any of your beans that are annotated with Spring JMX annotations (@ManagedResource, @ManagedAttribute, or @ManagedOperation) are exposed to it.

如果您的平台提供标准“MBeanServer”,Spring Boot 将使用它并根据需要默认为 VM“MBeanServer”。如果所有方法都失败,则将新建“MBeanServer”。

If your platform provides a standard MBeanServer, Spring Boot uses that and defaults to the VM MBeanServer, if necessary. If all that fails, a new MBeanServer is created.

有关更多详细信息,请参阅 {code-spring-boot-autoconfigure-src}/jmx/JmxAutoConfiguration.java[JmxAutoConfiguration] 类。

See the {code-spring-boot-autoconfigure-src}/jmx/JmxAutoConfiguration.java[JmxAutoConfiguration] class for more details.

默认情况下,Spring Boot 还会在 org.springframework.boot 域下将管理端点作为 JMX MBean 暴露出来。如需对 JMX 域中的端点注册进行全控制,请考虑注册自己的 EndpointObjectNameFactory 实现。

By default, Spring Boot also exposes management endpoints as JMX MBeans under the org.springframework.boot domain. To take full control over endpoint registration in the JMX domain, consider registering your own EndpointObjectNameFactory implementation.

Customizing MBean Names

MBean 的名称通常由端点的 id 生成。例如,health 端点被暴露为 org.springframework.boot:type=Endpoint,name=Health

The name of the MBean is usually generated from the id of the endpoint. For example, the health endpoint is exposed as org.springframework.boot:type=Endpoint,name=Health.

如果你的应用程序包含多个 Spring ApplicationContext,你可能会发现名称冲突。为解决该问题,可以将 configprop:spring.jmx.unique-names[] 属性设置为 true,以便 MBean 名称始终是唯一的。

If your application contains more than one Spring ApplicationContext, you may find that names clash. To solve this problem, you can set the configprop:spring.jmx.unique-names[] property to true so that MBean names are always unique.

还可自定义端点暴露的 JMX 域。以下设置展示了在 application.properties 中执行此操作的一个示例:

You can also customize the JMX domain under which endpoints are exposed. The following settings show an example of doing so in application.properties:

spring:
  jmx:
    unique-names: true
management:
  endpoints:
    jmx:
      domain: "com.example.myapp"

Disabling JMX Endpoints

如果你不想通过 JMX 暴露端点,可以将 configprop:management.endpoints.jmx.exposure.exclude[] 属性设置为 *,如下例所示:

If you do not want to expose endpoints over JMX, you can set the configprop:management.endpoints.jmx.exposure.exclude[] property to *, as the following example shows:

management:
  endpoints:
    jmx:
      exposure:
        exclude: "*"