您的位置:首页 > 教程文章 > 编程开发

SpringBoot JMX的基本使用方式

:0 :2021-12-30 21:36:16

目录
SpringBoot JMX的基本使用
1. 声明
2. 基本demo
3. 执行结果
springboot自定义jmx对象
什么情况我们需要使用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:
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动态查看运行中的属性,也可以修改属性
springboot自定义jmx对象
在使用springboot-admin对springboot项目进行监控的时候我们发现其是具有web访问jmx对象的功能的,那它内部是怎么实现的呢。
Jolokia是一个JMX-http桥梁,它提供了访问JMX bean的HTTP访问方式。
<dependency>
    <groupId>org.jolokia</groupId>
    <artifactId>jolokia-core</artifactId>
</dependency>
什么情况我们需要使用JMX?
我认为比较实用有如下2点:
1、获取java对象里的属性的实时情况。
2、动态修改对象里的属性的值。
例如:你有一个耗时较长的定时任务,里面会处理一批数据,这时通过jmx暴露当前已处理的数据的相关数据就能得到实时的结果(当然,你可以通过写日志、数据库、缓存来实现,但这无疑增加了更业务无关的代码)。
那要怎么做呢?
首先看一下相关注解定义
将类的所有实例标识为JMX受控资源ManagedResource@ManagedResourceClass 类
将方法标识为JMX操作ManagedOperation@ManagedOperation Method方法
将getter或者setter标识为部分JMX属性ManagedAttribute@ManagedAttributeMethod (only getters and setters) 方法(仅getters和setters)
定义操作参数说明ManagedOperationParameter@ManagedOperationParameter和@ManagedOperationParametersMethod 方法
例子:
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就能看到这属性和这方法。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持无名。

JDK1.8新特性之方法引用 ::和Optional详解
SpringBoot之自定义Banner详解