通过JMX和Redis在Spring Boot Admin界面显示多个服务某些参数的和

2024-01-15 17:08

本文主要是介绍通过JMX和Redis在Spring Boot Admin界面显示多个服务某些参数的和,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文介绍通过JMX注解,实现自定义MBean的管理,并显示在Spring Boot Admin UI界面上。同时,通过Redis作为存储媒介,将两个服务中Tomcat的参数maxPostSize及maxThreads的和显示在Spring Boot Admin UI的界面上。


目录

    • 目录
    • 服务布局
    • eureka-server
      • pomxml
      • applicationyml
      • 启动类EurekaServerApplicationjava添加注解
    • admin-server
      • pomxml
      • applicationyml
      • 启动类AdminServerApplicationjava注解
    • jmx-client
      • pomxml
      • applicationyml
      • 启动类JmxClientApplication注解
      • MBean类
      • 配置类
    • jmx-client-2
    • 在admin-server中添加求和类
      • sumjava
      • 配置类
    • 启动四个服务
    • 总结

服务布局

四个服务分布图
如图所示,本次实例共有四个服务:eureka-server(端口8761)、admin-server(端口8090)、jmx-client(端口8081)以及jmx-client-2(端口8082)。其中,admin-server(端口8090)、jmx-client(端口8081)以及jmx-client-2(端口8082)均注册于注册中心eureka-server(端口8761)。admin-server(端口8090)通过注册中心eureka-server(端口8761)获得jmx-client(端口8081)以及jmx-client-2(端口8082)服务中的相关信息。
PS:所有服务都是通过在线生成,如图
Spring Initializr

eureka-server

pom.xml

        <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka-server</artifactId></dependency>

eureka-server要引入spring-cloud-starter-eureka-server依赖包。

application.yml

spring:application:name: Eureka-Serverindex: 1
server:port: 8761eureka:server:enable-self-preservation: false #关闭自我保护eviction-interval-timer-in-ms: 4000 #清理间隔(单位毫秒)instance:hostname: eureka-serverclient:register-with-eureka: falsefetch-registry: falseservice-url:defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
management:security:enabled: false

启动类EurekaServerApplication.java添加注解

@SpringBootApplication
@EnableEurekaServer

到此,注册中心就完成配置。

admin-server

pom.xml

        <dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-server</artifactId><version>1.5.4</version></dependency><dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-server-ui</artifactId><version>1.5.4</version></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId><version>1.2.6.RELEASE</version></dependency><!--jmx依赖包--><dependency><groupId>org.jolokia</groupId><artifactId>jolokia-core</artifactId></dependency><!--redis--><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId></dependency>

application.yml

info:version: @project.version@
server:port: 8090spring:application:name: Admin-Serverindex: 1boot:admin:url: http://localhost:${server.port}redis:host: localhostport: 6379password:pool:max-active: 10max-wait: 1000
endpoints:health:sensitive: false
# 向注册中心注册服务
eureka:instance:instance-id: ${spring.application.name}:${spring.application.index}:${random.value}prefer-ip-address: truehostname: admin-sever-${spring.application.index}status-page-url-path: ${management.context-path}/infohealth-check-url-path: ${management.context-path}/healthmetadata-map:management:context-path: ${management.context-path}client:service-url:defaultZone: http://localhost:8761/eureka/
management:security:enabled: falsecontext-path: /manage

启动类AdminServerApplication.java注解

@SpringBootApplication
@EnableAdminServer
@EnableDiscoveryClient

至此,admin-server配置暂时完成。

jmx-client

pom.xml

        <!--jmx依赖--><dependency><groupId>org.jolokia</groupId><artifactId>jolokia-core</artifactId></dependency><!--Redis依赖--><dependency><groupId>org.springframework.data</groupId><artifactId>spring-data-redis</artifactId></dependency><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId></dependency>

application.yml

info:version: @project.version@spring:application:name: JMX-Clientindex: 1boot:admin:url: http://localhost:8090redis:host: localhostport: 6379password:pool:max-active: 10max-wait: 1000
server:port: 8081
# 注册到注册中心
eureka:instance:instance-id: ${spring.application.name}:${spring.application.index}:${random.value}prefer-ip-address: truehostname: jmx-client-${spring.application.index}status-page-url-path: ${management.context-path}/infohealth-check-url-path: ${management.context-path}/healthmetadata-map:management:context-path: ${management.context-path}client:service-url:defaultZone: http://localhost:8761/eureka/
management:security:enabled: falsecontext-path: /manage

可以发现,jmx-client模块并没有直接注册到admin-server服务中,而是直接注册到eureka-server服务中。同时,admin-server也注册在eureka-server中,admin-server就能够通过注册中心获得jmx-client的相关信息。

启动类JmxClientApplication注解

@SpringBootApplication
@EnableEurekaClient

MBean类

@ManagedResource(objectName = "bean:name=MyBean", description = "My Managed Bean", log = true, logFile = "jmx.log",currencyTimeLimit = 15, persistPolicy = "OnUpdate", persistPeriod = 200, persistLocation = "foo",persistName = "bar")
public class MyBean {private ApplicationContext applicationContext;@Autowiredprivate StringRedisTemplate stringRedisTemplate;private ValueOperations<String, String> valueOperations;public MyBean(ApplicationContext applicationContext) {this.applicationContext = applicationContext;}@PostConstructprivate void init(){this.valueOperations = stringRedisTemplate.opsForValue();}private Connector getConnector(){EmbeddedServletContainer embeddedServletContainer =((EmbeddedWebApplicationContext) applicationContext).getEmbeddedServletContainer();Tomcat tomcat = ((TomcatEmbeddedServletContainer) embeddedServletContainer).getTomcat();return tomcat.getConnector();}@ManagedAttribute(description = "获得最小备用线程数,tomcat启动时的初始化的线程数")public int getMaxPostSize() {int maxPostSize = (int) IntrospectionUtils.getProperty(getConnector(), "maxPostSize");valueOperations.set("maxPostSize", String.valueOf(maxPostSize));return maxPostSize;}@ManagedAttribute(description = "设置最小备用线程数,tomcat启动时的初始化的线程数")public void setMaxPostSize(int value){valueOperations.set("maxPostSize", String.valueOf(value));IntrospectionUtils.setProperty(getConnector(), "maxPostSize", String.valueOf(value));}@ManagedAttribute(description = "获得tomcat起动的最大线程数,即同时处理的任务个数")public int getMaxThreads(){int maxThreads = (int) IntrospectionUtils.getProperty(getConnector(), "maxThreads");valueOperations.set("maxThreads", String.valueOf(maxThreads));return maxThreads;}@ManagedAttribute(description = "设置tomcat起动的最大线程数,即同时处理的任务个数")public void setMaxThreads(int value){valueOperations.set("maxThreads", String.valueOf(value));IntrospectionUtils.setProperty(getConnector(), "maxThreads", String.valueOf(value));}

其中,getXXX是获得相应的属性,setXXX是具有修改属性值的权限。

配置类

public class BeanConfig {@Beanpublic MyBean myBean(ApplicationContext applicationContext){return new MyBean(applicationContext);}}

至此,jmx-client配置已经完成。

jmx-client-2

复制一份jmx-client ,并更改**application.yml**server端口号为8082

server:port: 8082

更改自定义MBean存储在Redis中的key值,以避免与jmx-client存储的key值重复,即

 @ManagedAttribute(description = "获得最小备用线程数,tomcat启动时的初始化的线程数")public int getMaxPostSize() {int maxPostSize = (int) IntrospectionUtils.getProperty(getConnector(), "maxPostSize");valueOperations.set("maxPostSize2", String.valueOf(maxPostSize));return maxPostSize;}@ManagedAttribute(description = "设置最小备用线程数,tomcat启动时的初始化的线程数")public void setMaxPostSize(int value){valueOperations.set("maxPostSize2", String.valueOf(value));IntrospectionUtils.setProperty(getConnector(), "maxPostSize", String.valueOf(value));}@ManagedAttribute(description = "获得tomcat起动的最大线程数,即同时处理的任务个数")public int getMaxThreads(){int maxThreads = (int) IntrospectionUtils.getProperty(getConnector(), "maxThreads");valueOperations.set("maxThreads2", String.valueOf(maxThreads));return maxThreads;}@ManagedAttribute(description = "设置tomcat起动的最大线程数,即同时处理的任务个数")public void setMaxThreads(int value){valueOperations.set("maxThreads2", String.valueOf(value));IntrospectionUtils.setProperty(getConnector(), "maxThreads", String.valueOf(value));}

在admin-server中添加求和类

sum.java

@ManagedResource(objectName = "bean:name=sumBean",description = "两个服务参数的和")
public class SumBean {@Autowiredprivate StringRedisTemplate stringRedisTemplate;private ValueOperations<String, String> valueOperations;@PostConstructprivate void init() {this.valueOperations = stringRedisTemplate.opsForValue();}@ManagedAttribute(description = "两个服务maxPostSize的和")public int getSumMaxPostSize() {// 取出jmx-client保存的maxPostSize值int maxPostSize = Integer.parseInt(valueOperations.get("maxPostSize"));// 取出jmx-client-2保存的maxPostSize2值int maxPostSize2 = Integer.parseInt(valueOperations.get("maxPostSize2"));return maxPostSize + maxPostSize2;}@ManagedAttribute(description = "两个服务maxThreads的和")public int getSumMaxThreads(){// 取出jmx-client保存的maxThreads值int maxThreads = Integer.parseInt(valueOperations.get("maxThreads"));// 取出jmx-client-2保存的maxThreads2值int maxThreads2 = Integer.parseInt(valueOperations.get("maxThreads2"));return maxThreads + maxThreads2;}
}

配置类

@Configuration
public class BeanConfig {@Beanpublic SumBean sumBean(){return new SumBean();}
}

启动四个服务

如图
启动四个服务
PS:idea 2.6+版本可以同时启动多个服务,而不需要挨个去找application类了。

Eureka注册中心
Eureka注册中心

Admin Server
Admin Server

点击JMX-CLIENT后 【Details】链接,然后再点开【JMX】链接,就可以看到自定义的bean
自定义bean
更改【write】后面的数值,点击【reload】,就能看到更改后的数值:
更改数值
对于jmx-client-2也有同样的效果。在Admin Server UI界面点击ADMIN SERVER后的【Details】链接,进入【JMX】链接,看到自定义的MBean类,查看求和值为jmx-client和jmx-client-2各参数的加和:
求和值
将jmx-client MaxThreads值改回200后,【reload】界面,返回求和界面,可以看到MaxThreads值已经变为400:
更改求和值

总结

通过本实例可以初步了解如何联合JMX、Redis、Spring Boot Admin以及Eureka实现多个服务某些参数值的监控。源码在https://github.com/GitBaymin/spring-jmx


这篇关于通过JMX和Redis在Spring Boot Admin界面显示多个服务某些参数的和的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/609616

相关文章

SpringBoot中六种批量更新Mysql的方式效率对比分析

《SpringBoot中六种批量更新Mysql的方式效率对比分析》文章比较了MySQL大数据量批量更新的多种方法,指出REPLACEINTO和ONDUPLICATEKEY效率最高但存在数据风险,MyB... 目录效率比较测试结构数据库初始化测试数据批量修改方案第一种 for第二种 case when第三种

Java docx4j高效处理Word文档的实战指南

《Javadocx4j高效处理Word文档的实战指南》对于需要在Java应用程序中生成、修改或处理Word文档的开发者来说,docx4j是一个强大而专业的选择,下面我们就来看看docx4j的具体使用... 目录引言一、环境准备与基础配置1.1 Maven依赖配置1.2 初始化测试类二、增强版文档操作示例2.

一文详解如何使用Java获取PDF页面信息

《一文详解如何使用Java获取PDF页面信息》了解PDF页面属性是我们在处理文档、内容提取、打印设置或页面重组等任务时不可或缺的一环,下面我们就来看看如何使用Java语言获取这些信息吧... 目录引言一、安装和引入PDF处理库引入依赖二、获取 PDF 页数三、获取页面尺寸(宽高)四、获取页面旋转角度五、判断

Spring Boot中的路径变量示例详解

《SpringBoot中的路径变量示例详解》SpringBoot中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs

Redis中Stream详解及应用小结

《Redis中Stream详解及应用小结》RedisStreams是Redis5.0引入的新功能,提供了一种类似于传统消息队列的机制,但具有更高的灵活性和可扩展性,本文给大家介绍Redis中Strea... 目录1. Redis Stream 概述2. Redis Stream 的基本操作2.1. XADD

JAVA中安装多个JDK的方法

《JAVA中安装多个JDK的方法》文章介绍了在Windows系统上安装多个JDK版本的方法,包括下载、安装路径修改、环境变量配置(JAVA_HOME和Path),并说明如何通过调整JAVA_HOME在... 首先去oracle官网下载好两个版本不同的jdk(需要登录Oracle账号,没有可以免费注册)下载完

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

Java中Integer128陷阱

《Java中Integer128陷阱》本文主要介绍了Java中Integer与int的区别及装箱拆箱机制,重点指出-128至127范围内的Integer值会复用缓存对象,导致==比较结果为true,下... 目录一、Integer和int的联系1.1 Integer和int的区别1.2 Integer和in

SpringSecurity整合redission序列化问题小结(最新整理)

《SpringSecurity整合redission序列化问题小结(最新整理)》文章详解SpringSecurity整合Redisson时的序列化问题,指出需排除官方Jackson依赖,通过自定义反序... 目录1. 前言2. Redission配置2.1 RedissonProperties2.2 Red