SpringBoot JMX的基本使用方式
SpringBoot JMX的基本使用
1. 聲明
當(dāng)前內(nèi)容主要為學(xué)習(xí)和使用SpringBoot注冊(cè)JMX的操作,主要方便管理需要的類(lèi)
當(dāng)前內(nèi)容來(lái)源:SpringBoot官方文檔
主要內(nèi)容為:
- 使用SpringBoot注冊(cè)JMX中的MBean
- 使用jconsole查看和修改屬性
基本的pom依賴(lài)
<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
配置類(lèi)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ù)庫(kù)配置文件中的屬性 * @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來(lái)實(shí)現(xiàn)操作
入口類(lèi):基本的main方法
3. 執(zhí)行結(jié)果
使用jconsole連接并查看MBean結(jié)果
使用JMX可將一些需要的信息注冊(cè),然后通過(guò)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訪問(wèn)jmx對(duì)象的功能的,那它內(nèi)部是怎么實(shí)現(xiàn)的呢。
Jolokia是一個(gè)JMX-http橋梁,它提供了訪問(wèn)JMX bean的HTTP訪問(wèn)方式。
<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í)較長(zhǎng)的定時(shí)任務(wù),里面會(huì)處理一批數(shù)據(jù),這時(shí)通過(guò)jmx暴露當(dāng)前已處理的數(shù)據(jù)的相關(guān)數(shù)據(jù)就能得到實(shí)時(shí)的結(jié)果(當(dāng)然,你可以通過(guò)寫(xiě)日志、數(shù)據(jù)庫(kù)、緩存來(lái)實(shí)現(xiàn),但這無(wú)疑增加了更業(yè)務(wù)無(wú)關(guān)的代碼)。
那要怎么做呢?
首先看一下相關(guān)注解定義
將類(lèi)的所有實(shí)例標(biāo)識(shí)為JMX受控資源 | ManagedResource | @ManagedResource | Class 類(lèi) |
將方法標(biāo)識(shí)為JMX操作 | ManagedOperation | @ManagedOperation | Method方法 |
將getter或者setter標(biāo)識(shí)為部分JMX屬性 | ManagedAttribute | @ManagedAttribute | Method (only getters and setters) 方法(僅getters和setters) |
定義操作參數(shù)說(shuō)明 | 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)文章
java使用文件流實(shí)現(xiàn)查看下載次數(shù)
這篇文章主要為大家詳細(xì)介紹了java使用文件流實(shí)現(xiàn)查看下載次數(shù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Java如何將任意類(lèi)型的Object對(duì)象轉(zhuǎn)換為相應(yīng)的實(shí)體對(duì)象
這篇文章主要介紹了Java如何將任意類(lèi)型的Object對(duì)象轉(zhuǎn)換為相應(yīng)的實(shí)體對(duì)象問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01java 多線程Thread與runnable的區(qū)別
這篇文章主要介紹了java 多線程Thread與runnable的區(qū)別的相關(guān)資料,java線程有兩種方法繼承thread類(lèi)與實(shí)現(xiàn)runnable接口,下面就提供實(shí)例幫助大家理解,需要的朋友可以參考下2017-08-08SpringBoot?Aop實(shí)現(xiàn)接口請(qǐng)求次數(shù)統(tǒng)計(jì)
我們通過(guò)Spring AOP在每次執(zhí)行方法前或執(zhí)行方法后進(jìn)行切面的處理,進(jìn)而統(tǒng)計(jì)方法訪問(wèn)的次數(shù)等功能,本文主要介紹了SpringBoot?Aop實(shí)現(xiàn)接口請(qǐng)求次數(shù)統(tǒng)計(jì)2024-02-02Spring MVC 啟動(dòng)過(guò)程源碼分析詳解
這篇文章主要介紹了Spring MVC 啟動(dòng)過(guò)程源碼分析詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-07-07SpringMVC的REST風(fēng)格的四種請(qǐng)求方式總結(jié)
下面小編就為大家?guī)?lái)一篇SpringMVC的REST風(fēng)格的四種請(qǐng)求方式總結(jié)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-08-08