SpringBoot JMX的基本使用方式

网友投稿 607 2022-12-09

SpringBoot JMX的基本使用方式

SpringBoot JMX的基本使用方式

目录SpringBoot JMX的基本使用1. 声明2. 基本demo3. 执行结果springboot自定义jmx对象什么情况我们需要使用JMX?那要怎么做呢?

SpringBoot JMX的基本使用

1. 声明

当前内容主要为学习和使用SpringBoot注册JMX的操作,主要方便管理需要的类

当前内容来源:SpringBoot官方文档

主要内容为:

使用SpringBoot注册JMX中的MBean

使用jconsole查看和修改属性

基本的pom依赖

org.springframework.boot

spring-boot-starter-parent

2.2.13.RELEASE

UTF-8

org.springframework.boot

spring-boot-starter-web

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动态查看运行中的属性,也可以修改属性

springboot自定义jmx对象

在使用springboot-admin对springboot项目进行监控的时候我们发现其是具有web访问jmx对象的功能的,那它内部是怎么实现的呢。

Jolokia是一个JMX-http桥梁,它提供了访问JMX bean的HTTP访问方式。

org.jolokia

jolokia-core

什么情况我们需要使用JMX?

我认为比较实用有如下2点:

1、获取java对象里的属性的实时情况。

2、动态修改对象里的属性的值。

例如:你有一个耗时较长的定时任务,里面会处理一批数据,这时通过jmx暴露当前已处理的数据的相关数据就能得到实时的结果(当然,你可以通过写日志、数据库、缓存来实现,但这无疑增加了更业务无关的代码)。

那要怎么做呢?

首先看一下相关注解定义

将类的所有实例标识为JMX受控资源

ManagedResource

@ManagedResource

Class 类

将方法标识为JMX操作

ManagedOperation

@ManagedOperation 

Method方法

将getter或者setter标识为部分JMX属性

ManagedAttribute

@ManagedAttribute

Method (only getters and setters) 方法(仅getters和setters)

定义操作参数说明

ManagedOperationParameter

@ManagedOperationParameter和@ManagedOperationParameters

Method 方法

例子:

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.jmx.export.annotation.ManagedAttribute;

import org.springframework.jmx.exCZVxVzdport.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方法

*CZVxVzd @return

*/

@ManagedAttribute(description="temp info now max id")

public Long getNowTempMaxId() {

return tempMaxId;

}

}

在JMC的Mbean选项卡、springboot-admin的jmx就能看到这属性和这方法。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:JDK1.8新特性之方法引用 ::和Optional详解
下一篇:SpringBoot停止启动时测试检查rabbitmq操作
相关文章

 发表评论

暂时没有评论,来抢沙发吧~