透過 JMX 進行監控和管理

Java Management Extensions (JMX) 提供了一種標準的機制來監控和管理應用程式。預設情況下,此功能未啟用。你可以透過將 spring.jmx.enabled 配置屬性設定為 true 來開啟它。Spring Boot 將最合適的 MBeanServer 作為 ID 為 mbeanServer 的 bean 公開。任何使用 Spring JMX 註解(@org.springframework.jmx.export.annotation.ManagedResource@ManagedAttribute@ManagedOperation)註解的 bean 都會被暴露給它。

如果你的平臺提供了一個標準的 MBeanServer,Spring Boot 會使用它;必要時,會預設使用 JVM MBeanServer。如果所有這些都失敗了,則會建立一個新的 MBeanServer

spring.jmx.enabled 僅影響 Spring 提供的管理 bean。啟用其他庫(例如 Log4j2Quartz)提供的管理 bean 是獨立的。

有關更多詳細資訊,請參閱 JmxAutoConfiguration 類。

預設情況下,Spring Boot 還將管理端點作為 JMX MBean 公開在 org.springframework.boot 域下。要完全控制 JMX 域中的端點註冊,請考慮註冊你自己的 EndpointObjectNameFactory 實現。

自定義 MBean 名稱

MBean 的名稱通常從端點的 id 生成。例如,health 端點被公開為 org.springframework.boot:type=Endpoint,name=Health

如果你的應用程式包含多個 Spring ApplicationContext,你可能會發現名稱衝突。為了解決這個問題,你可以將 spring.jmx.unique-names 屬性設定為 true,這樣 MBean 名稱將始終是唯一的。

你還可以自定義端點公開的 JMX 域。以下設定顯示了在 application.properties 中執行此操作的示例

  • 屬性

  • YAML

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

停用 JMX 端點

如果你不想透過 JMX 公開端點,可以將 management.endpoints.jmx.exposure.exclude 屬性設定為 *,如以下示例所示

  • 屬性

  • YAML

management.endpoints.jmx.exposure.exclude=*
management:
  endpoints:
    jmx:
      exposure:
        exclude: "*"
© . This site is unofficial and not affiliated with VMware.