通过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

相关文章

mysqld_multi在Linux服务器上运行多个MySQL实例

《mysqld_multi在Linux服务器上运行多个MySQL实例》在Linux系统上使用mysqld_multi来启动和管理多个MySQL实例是一种常见的做法,这种方式允许你在同一台机器上运行多个... 目录1. 安装mysql2. 配置文件示例配置文件3. 创建数据目录4. 启动和管理实例启动所有实例

Spring IOC的三种实现方式详解

《SpringIOC的三种实现方式详解》:本文主要介绍SpringIOC的三种实现方式,在Spring框架中,IOC通过依赖注入来实现,而依赖注入主要有三种实现方式,构造器注入、Setter注入... 目录1. 构造器注入(Cons编程tructor Injection)2. Setter注入(Setter

Java function函数式接口的使用方法与实例

《Javafunction函数式接口的使用方法与实例》:本文主要介绍Javafunction函数式接口的使用方法与实例,函数式接口如一支未完成的诗篇,用Lambda表达式作韵脚,将代码的机械美感... 目录引言-当代码遇见诗性一、函数式接口的生物学解构1.1 函数式接口的基因密码1.2 六大核心接口的形态学

Spring IOC控制反转的实现解析

《SpringIOC控制反转的实现解析》:本文主要介绍SpringIOC控制反转的实现,IOC是Spring的核心思想之一,它通过将对象的创建、依赖注入和生命周期管理交给容器来实现解耦,使开发者... 目录1. IOC的基本概念1.1 什么是IOC1.2 IOC与DI的关系2. IOC的设计目标3. IOC

Spring Boot统一异常拦截实践指南(最新推荐)

《SpringBoot统一异常拦截实践指南(最新推荐)》本文介绍了SpringBoot中统一异常处理的重要性及实现方案,包括使用`@ControllerAdvice`和`@ExceptionHand... 目录Spring Boot统一异常拦截实践指南一、为什么需要统一异常处理二、核心实现方案1. 基础组件

java中的HashSet与 == 和 equals的区别示例解析

《java中的HashSet与==和equals的区别示例解析》HashSet是Java中基于哈希表实现的集合类,特点包括:元素唯一、无序和可包含null,本文给大家介绍java中的HashSe... 目录什么是HashSetHashSet 的主要特点是HashSet 的常用方法hasSet存储为啥是无序的

IDEA运行spring项目时,控制台未出现的解决方案

《IDEA运行spring项目时,控制台未出现的解决方案》文章总结了在使用IDEA运行代码时,控制台未出现的问题和解决方案,问题可能是由于点击图标或重启IDEA后控制台仍未显示,解决方案提供了解决方法... 目录问题分析解决方案总结问题js使用IDEA,点击运行按钮,运行结束,但控制台未出现http://

解决Spring运行时报错:Consider defining a bean of type ‘xxx.xxx.xxx.Xxx‘ in your configuration

《解决Spring运行时报错:Considerdefiningabeanoftype‘xxx.xxx.xxx.Xxx‘inyourconfiguration》该文章主要讲述了在使用S... 目录问题分析解决方案总结问题Description:Parameter 0 of constructor in x

解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题

《解决IDEA使用springBoot创建项目,lombok标注实体类后编译无报错,但是运行时报错问题》文章详细描述了在使用lombok的@Data注解标注实体类时遇到编译无误但运行时报错的问题,分析... 目录问题分析问题解决方案步骤一步骤二步骤三总结问题使用lombok注解@Data标注实体类,编译时

JSON字符串转成java的Map对象详细步骤

《JSON字符串转成java的Map对象详细步骤》:本文主要介绍如何将JSON字符串转换为Java对象的步骤,包括定义Element类、使用Jackson库解析JSON和添加依赖,文中通过代码介绍... 目录步骤 1: 定义 Element 类步骤 2: 使用 Jackson 库解析 jsON步骤 3: 添