SpringBoot JMX的基本使用方式
SpringBoot JMX的基本使用
1. 聲明
當(dāng)前內(nèi)容主要為學(xué)習(xí)和使用SpringBoot注冊JMX的操作,主要方便管理需要的類
當(dāng)前內(nèi)容來源:SpringBoot官方文檔
主要內(nèi)容為:
- 使用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的內(nèi)容
spring.jmx.enabled=true
mysqldb.properties的內(nèi)容
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 當(dāng)前內(nèi)容主要為對應(yīng)SQLServerDB的數(shù)據(jù)庫配置文件中的屬性
* @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; // 是否在使用的時候進(jìn)行檢查操作
private Boolean testWhileIdle;// 測試是否已經(jīng)不能使用了
@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("設(shè)置新的密碼為:" + password);
this.password = password;
}
}
主要借助:@ManagedAttribute和@ManagedResource來實(shí)現(xiàn)操作
入口類:基本的main方法
3. 執(zhí)行結(jié)果

使用jconsole連接并查看MBean結(jié)果

使用JMX可將一些需要的信息注冊,然后通過jconsole動態(tài)查看運(yùn)行中的屬性,也可以修改屬性
springboot自定義jmx對象
在使用springboot-admin對springboot項目進(jìn)行監(jiān)控的時候我們發(fā)現(xiàn)其是具有web訪問jmx對象的功能的,那它內(nèi)部是怎么實(shí)現(xiàn)的呢。
Jolokia是一個JMX-http橋梁,它提供了訪問JMX bean的HTTP訪問方式。
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
什么情況我們需要使用JMX?
我認(rèn)為比較實(shí)用有如下2點(diǎn):
1、獲取java對象里的屬性的實(shí)時情況。
2、動態(tài)修改對象里的屬性的值。
例如:你有一個耗時較長的定時任務(wù),里面會處理一批數(shù)據(jù),這時通過jmx暴露當(dāng)前已處理的數(shù)據(jù)的相關(guān)數(shù)據(jù)就能得到實(shí)時的結(jié)果(當(dāng)然,你可以通過寫日志、數(shù)據(jù)庫、緩存來實(shí)現(xiàn),但這無疑增加了更業(yè)務(wù)無關(guān)的代碼)。
那要怎么做呢?
首先看一下相關(guān)注解定義
| 將類的所有實(shí)例標(biāo)識為JMX受控資源 | ManagedResource | @ManagedResource | Class 類 |
| 將方法標(biāo)識為JMX操作 | ManagedOperation | @ManagedOperation | Method方法 |
| 將getter或者setter標(biāo)識為部分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 {
// 臨時表當(dāng)前最大id
private Long tempMaxId = 0L;
/**
* 暴露mbean方法
* @return
*/
@ManagedAttribute(description="temp info now max id")
public Long getNowTempMaxId() {
return tempMaxId;
}
}
在JMC的Mbean選項卡、springboot-admin的jmx就能看到這屬性和這方法。
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java使用文件流實(shí)現(xiàn)查看下載次數(shù)
這篇文章主要為大家詳細(xì)介紹了java使用文件流實(shí)現(xiàn)查看下載次數(shù),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07
Java如何將任意類型的Object對象轉(zhuǎn)換為相應(yīng)的實(shí)體對象
這篇文章主要介紹了Java如何將任意類型的Object對象轉(zhuǎn)換為相應(yīng)的實(shí)體對象問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
java 多線程Thread與runnable的區(qū)別
這篇文章主要介紹了java 多線程Thread與runnable的區(qū)別的相關(guān)資料,java線程有兩種方法繼承thread類與實(shí)現(xiàn)runnable接口,下面就提供實(shí)例幫助大家理解,需要的朋友可以參考下2017-08-08
SpringBoot?Aop實(shí)現(xiàn)接口請求次數(shù)統(tǒng)計
我們通過Spring AOP在每次執(zhí)行方法前或執(zhí)行方法后進(jìn)行切面的處理,進(jìn)而統(tǒng)計方法訪問的次數(shù)等功能,本文主要介紹了SpringBoot?Aop實(shí)現(xiàn)接口請求次數(shù)統(tǒng)計2024-02-02
SpringMVC的REST風(fēng)格的四種請求方式總結(jié)
下面小編就為大家?guī)硪黄猄pringMVC的REST風(fēng)格的四種請求方式總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-08-08

