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的数据库配置文件中的属性
* @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来实现操作
入口类:基本的main方法
3. 执行结果
使用jconsole连接并查看MBean结果
使用JMX可将一些需要的信息注册,然后通过jconsole动态查看运行中的属性,也可以修改属性
路上^_^: 大佬我这里报错 能看看不 版本是 3.4.2 我擦一直报错.人嘛了
dzbxq89: 这个已经很古老了,最新的版本解析xsl或者xslt都会提示失败,只找到解析fo的方式,fo还没找到动态填值的方式
weixin_42552838: 谢谢大佬无私分享,自己写的修改properties文件 乱七八糟的还改变了键值对顺序,还是得用阿帕奇的轮子
A_WEI_86: 当前我想重置kafka某个消费组的偏移量,达到数据重新消费的目的,也是一直报错:org.apache.kafka.common.errors.UnknownMemberIdException: Failed altering consumer group offsets for the following partitions: xxx。文中提到需要关闭消费者和生产者,那如何在代码中动态控制生产者、消费组打开和关闭呢?
晚风以北过南巷: org.postgresql.util.PSQLException: 错误: 字段 user0_.id 不存在使用注解报这个错