SpringBoot JMX的基本使用方式
SpringBoot JMX的基本使用
1. 聲明
當前內容主要為學習和使用SpringBoot注冊JMX的操作,主要方便管理需要的類
當前內容來源:SpringBoot官方文檔
主要內容為:
- 使用SpringBoot注冊JMX中的MBean
- 使用jconsole查看和修改屬性
基本的pom依賴
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.13.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
2. 基本demo
application.properties的內容
spring.jmx.enabled=true
mysqldb.properties的內容
jdbc.driverClassName=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8 jdbc.username=root jdbc.password=123456 # mysql connector timeout check jdbc.maxIdle=216000 jdbc.validationQuery=select 1 jdbc.validationQueryTimeout=1800 jdbc.testOnBorrow=true jdbc.testWhileIdle=true
配置類AppConfig
@Configuration @PropertySource(value = {"mysqldb.properties"}) @EnableConfigurationProperties(value = { MySQLDBProperties.class}) public class AppConfig { }
MySQLDBProperties
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.jmx.export.annotation.ManagedAttribute; import org.springframework.jmx.export.annotation.ManagedResource; /** * @description 當前內容主要為對應SQLServerDB的數(shù)據庫配置文件中的屬性 * @author hy * @createTime 2021-03-31 13:26:36 **/ @ConfigurationProperties(prefix = "jdbc") @ManagedResource("com.hy.springboot.jmx.test.properties:type=MySQLDBProperties,name=MySQLDBProperties") public class MySQLDBProperties { private String url; private String driverClassName; private String username; private String password; private Integer maxIdle; private Integer validationQueryTimeout; private String validationQuery; private Boolean testOnBorrow; // 是否在使用的時候進行檢查操作 private Boolean testWhileIdle;// 測試是否已經不能使用了 @ManagedAttribute public Boolean getTestOnBorrow() { return testOnBorrow; } @ManagedAttribute public void setTestOnBorrow(Boolean testOnBorrow) { this.testOnBorrow = testOnBorrow; } @ManagedAttribute public Boolean getTestWhileIdle() { return testWhileIdle; } @ManagedAttribute public void setTestWhileIdle(Boolean testWhileIdle) { this.testWhileIdle = testWhileIdle; } @ManagedAttribute public Integer getValidationQueryTimeout() { return validationQueryTimeout; } @ManagedAttribute public void setValidationQueryTimeout(Integer validationQueryTimeout) { this.validationQueryTimeout = validationQueryTimeout; } @ManagedAttribute public String getValidationQuery() { return validationQuery; } @ManagedAttribute public void setValidationQuery(String validationQuery) { this.validationQuery = validationQuery; } @ManagedAttribute public Integer getMaxIdle() { return maxIdle; } @ManagedAttribute public void setMaxIdle(Integer maxIdle) { this.maxIdle = maxIdle; } @ManagedAttribute public String getUrl() { return url; } @ManagedAttribute public void setUrl(String url) { this.url = url; } @ManagedAttribute public String getDriverClassName() { return driverClassName; } @ManagedAttribute public void setDriverClassName(String driverClassName) { this.driverClassName = driverClassName; } @ManagedAttribute public String getUsername() { return username; } @ManagedAttribute public void setUsername(String username) { this.username = username; } @ManagedAttribute public String getPassword() { return password; } @ManagedAttribute public void setPassword(String password) { System.out.println("設置新的密碼為:" + password); this.password = password; } }
主要借助:@ManagedAttribute和@ManagedResource來實現(xiàn)操作
入口類:基本的main方法
3. 執(zhí)行結果
使用jconsole連接并查看MBean結果
使用JMX可將一些需要的信息注冊,然后通過jconsole動態(tài)查看運行中的屬性,也可以修改屬性
springboot自定義jmx對象
在使用springboot-admin對springboot項目進行監(jiān)控的時候我們發(fā)現(xiàn)其是具有web訪問jmx對象的功能的,那它內部是怎么實現(xiàn)的呢。
Jolokia是一個JMX-http橋梁,它提供了訪問JMX bean的HTTP訪問方式。
<dependency> <groupId>org.jolokia</groupId> <artifactId>jolokia-core</artifactId> </dependency>
什么情況我們需要使用JMX?
我認為比較實用有如下2點:
1、獲取java對象里的屬性的實時情況。
2、動態(tài)修改對象里的屬性的值。
例如:你有一個耗時較長的定時任務,里面會處理一批數(shù)據,這時通過jmx暴露當前已處理的數(shù)據的相關數(shù)據就能得到實時的結果(當然,你可以通過寫日志、數(shù)據庫、緩存來實現(xiàn),但這無疑增加了更業(yè)務無關的代碼)。
那要怎么做呢?
首先看一下相關注解定義
將類的所有實例標識為JMX受控資源 | ManagedResource | @ManagedResource | Class 類 |
將方法標識為JMX操作 | ManagedOperation | @ManagedOperation | Method方法 |
將getter或者setter標識為部分JMX屬性 | ManagedAttribute | @ManagedAttribute | Method (only getters and setters) 方法(僅getters和setters) |
定義操作參數(shù)說明 | 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就能看到這屬性和這方法。
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java如何將任意類型的Object對象轉換為相應的實體對象
這篇文章主要介紹了Java如何將任意類型的Object對象轉換為相應的實體對象問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01java 多線程Thread與runnable的區(qū)別
這篇文章主要介紹了java 多線程Thread與runnable的區(qū)別的相關資料,java線程有兩種方法繼承thread類與實現(xiàn)runnable接口,下面就提供實例幫助大家理解,需要的朋友可以參考下2017-08-08SpringBoot?Aop實現(xiàn)接口請求次數(shù)統(tǒng)計
我們通過Spring AOP在每次執(zhí)行方法前或執(zhí)行方法后進行切面的處理,進而統(tǒng)計方法訪問的次數(shù)等功能,本文主要介紹了SpringBoot?Aop實現(xiàn)接口請求次數(shù)統(tǒng)計2024-02-02