在使用springboot-admin對springboot項目進行監控的時候咱們發現其是具備web訪問jmx對象的功能的,那它內部是怎麼實現的呢。java
Jolokia是一個JMX-http橋樑,它提供了訪問JMX bean的HTTP訪問方式。web
<dependency> <groupId>org.jolokia</groupId> <artifactId>jolokia-core</artifactId> </dependency>
什麼狀況咱們須要使用JMX? 我認爲比較實用有以下2點:spring
一、獲取java對象裏的屬性的實時狀況。數據庫
二、動態修改對象裏的屬性的值。緩存
例如:你有一個耗時較長的定時任務,裏面會處理一批數據,這時經過jmx暴露當前已處理的數據的相關數據就能獲得實時的結果(固然,你能夠經過寫日誌、數據庫、緩存來實現,但這無疑增長了更業務無關的代碼)。springboot
那要怎麼作呢?ide
首先看一下相關注解定義spa
將類的全部實例標識爲JMX受控資源 | ManagedResource | @ManagedResource | Class 類 |
將方法標識爲JMX操做 | ManagedOperation | @ManagedOperation | Method方法 |
將getter或者setter標識爲部分JMX屬性 | ManagedAttribute | @ManagedAttribute | Method (only getters and setters) 方法(僅getters和setters) |
定義操做參數說明 | ManagedOperationParameter | @ManagedOperationParameter 和@ManagedOperationParameters |
Method 方法 |
例子:日誌
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jmx.export.annotation.ManagedAttribute; import org.springframework.jmx.export.annotation.ManagedResource; import lombok.extern.slf4j.Slf4j; @Service @Slf4j @ManagedResource (objectName= "com.longge:name=spideMpbServiceImpl" , description= "brower spider service" ) public class SpideMpbServiceImpl implements SpideMpbService { // 臨時表當前最大id private Long tempMaxId = 0L; /** * 暴露mbean方法 * @return */ @ManagedAttribute(description="temp info now max id") public Long getNowTempMaxId() { return tempMaxId; } }
在JMC的Mbean選項卡、springboot-admin的jmx就能看到這屬性和這方法code