SpringBoot JMX的基本使用方式
SpringBoot JMX的基本使用
1. 聲明
當(dāng)前內(nèi)容主要為學(xué)習(xí)和使用SpringBoot注冊(cè)JMX的操作,主要方便管理需要的類
當(dāng)前內(nèi)容來源:SpringBoot官方文檔
主要內(nèi)容為:
- 使用SpringBoot注冊(cè)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)容主要為對(duì)應(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; // 是否在使用的時(shí)候進(jìn)行檢查操作
private Boolean testWhileIdle;// 測(cè)試是否已經(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可將一些需要的信息注冊(cè),然后通過jconsole動(dòng)態(tài)查看運(yùn)行中的屬性,也可以修改屬性
springboot自定義jmx對(duì)象
在使用springboot-admin對(duì)springboot項(xiàng)目進(jìn)行監(jiān)控的時(shí)候我們發(fā)現(xiàn)其是具有web訪問jmx對(duì)象的功能的,那它內(nèi)部是怎么實(shí)現(xiàn)的呢。
Jolokia是一個(gè)JMX-http橋梁,它提供了訪問JMX bean的HTTP訪問方式。
<dependency>
<groupId>org.jolokia</groupId>
<artifactId>jolokia-core</artifactId>
</dependency>
什么情況我們需要使用JMX?
我認(rèn)為比較實(shí)用有如下2點(diǎn):
1、獲取java對(duì)象里的屬性的實(shí)時(shí)情況。
2、動(dòng)態(tài)修改對(duì)象里的屬性的值。
例如:你有一個(gè)耗時(shí)較長的定時(shí)任務(wù),里面會(huì)處理一批數(shù)據(jù),這時(shí)通過jmx暴露當(dāng)前已處理的數(shù)據(jù)的相關(guān)數(shù)據(jù)就能得到實(shí)時(shí)的結(jié)果(當(dāng)然,你可以通過寫日志、數(shù)據(jù)庫、緩存來實(shí)現(xiàn),但這無疑增加了更業(yè)務(wù)無關(guān)的代碼)。
那要怎么做呢?
首先看一下相關(guān)注解定義
| 將類的所有實(shí)例標(biāo)識(shí)為JMX受控資源 | ManagedResource | @ManagedResource | Class 類 |
| 將方法標(biāo)識(shí)為JMX操作 | ManagedOperation | @ManagedOperation | Method方法 |
| 將getter或者setter標(biāo)識(shí)為部分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 {
// 臨時(shí)表當(dāng)前最大id
private Long tempMaxId = 0L;
/**
* 暴露mbean方法
* @return
*/
@ManagedAttribute(description="temp info now max id")
public Long getNowTempMaxId() {
return tempMaxId;
}
}
在JMC的Mbean選項(xiàng)卡、springboot-admin的jmx就能看到這屬性和這方法。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
性能爆棚的實(shí)體轉(zhuǎn)換復(fù)制工具M(jìn)apStruct使用詳解
這篇文章主要為大家介紹了性能爆棚的實(shí)體轉(zhuǎn)換復(fù)制工具M(jìn)apStruct使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
Java之SpringBoot實(shí)現(xiàn)基本增刪改查(前后端分離版)
這篇文章主要介紹了Java中SpringBoot如何實(shí)現(xiàn)基本的增刪改查,前后端分離版,沒有和前端進(jìn)行聯(lián)系,感興趣的小伙伴可以借鑒閱讀本文2023-03-03
Java8 Stream API 詳細(xì)使用方法與操作技巧指南
這篇文章主要介紹了Java8 Stream API 詳細(xì)使用方法與操作技巧,總結(jié)分析了Java8 Stream API 基本功能、使用方法與操作注意事項(xiàng),需要的朋友可以參考下2020-05-05
使用Java實(shí)現(xiàn)三種等級(jí)的掃雷游戲(完整版)
掃雷是一款大眾類的益智小游戲,根據(jù)點(diǎn)擊格子出現(xiàn)的數(shù)字找出所有非雷格子,同時(shí)避免踩雷,踩到一個(gè)雷即全盤皆輸,下面這篇文章主要給大家介紹了關(guān)于使用Java實(shí)現(xiàn)三種等級(jí)的掃雷游戲的相關(guān)資料,需要的朋友可以參考下2023-01-01
mybatis-plus雪花算法自動(dòng)生成機(jī)器id原理及源碼
Mybatis-Plus是一個(gè)Mybatis的增強(qiáng)工具,它在Mybatis的基礎(chǔ)上做了增強(qiáng),卻不做改變,Mybatis-Plus是為簡(jiǎn)化開發(fā)、提高開發(fā)效率而生,但它也提供了一些很有意思的插件,比如SQL性能監(jiān)控、樂觀鎖、執(zhí)行分析等,下面一起看看mybatis-plus雪花算法自動(dòng)生成機(jī)器id原理解析2021-06-06
Spring-boot的debug調(diào)試代碼實(shí)例
這篇文章主要介紹了Spring-boot的debug調(diào)試代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
8個(gè)簡(jiǎn)單部分開啟Java語言學(xué)習(xí)之路 附j(luò)ava學(xué)習(xí)書單
8個(gè)簡(jiǎn)單部分開啟Java語言學(xué)習(xí)之路,附j(luò)ava學(xué)習(xí)書單,這篇文章主要向大家介紹了學(xué)習(xí)java語言的方向,感興趣的小伙伴們可以參考一下2016-09-09
使用Feign調(diào)用注解組件(實(shí)現(xiàn)字段賦值功能)
這篇文章主要介紹了使用Feign調(diào)用注解組件(實(shí)現(xiàn)字段賦值功能),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03

