本文介绍通过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:所有服务都是通过在线生成,如图
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-Server
index:
server:
port:
eureka:
server:
enable-self-preservation: false #关闭自我保护
eviction-interval-timer-in-ms: #清理间隔(单位毫秒)
instance:
hostname: eureka-server
client:
register-with-eureka: false
fetch-registry: false
service-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:
spring:
application:
name: Admin-Server
index:
boot:
admin:
url: http://localhost:${server.port}
redis:
host: localhost
port:
password:
pool:
max-active:
max-wait:
endpoints:
health:
sensitive: false
# 向注册中心注册服务
eureka:
instance:
instance-id: ${spring.application.name}:${spring.application.index}:${random.value}
prefer-ip-address: true
hostname: admin-sever-${spring.application.index}
status-page-url-path: ${management.context-path}/info
health-check-url-path: ${management.context-path}/health
metadata-map:
management:
context-path: ${management.context-path}
client:
service-url:
defaultZone: http://localhost:/eureka/
management:
security:
enabled: false
context-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-Client
index:
boot:
admin:
url: http://localhost:
redis:
host: localhost
port:
password:
pool:
max-active:
max-wait:
server:
port:
# 注册到注册中心
eureka:
instance:
instance-id: ${spring.application.name}:${spring.application.index}:${random.value}
prefer-ip-address: true
hostname: jmx-client-${spring.application.index}
status-page-url-path: ${management.context-path}/info
health-check-url-path: ${management.context-path}/health
metadata-map:
management:
context-path: ${management.context-path}
client:
service-url:
defaultZone: http://localhost:/eureka/
management:
security:
enabled: false
context-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 = , persistPolicy = "OnUpdate", persistPeriod = , persistLocation = "foo",
persistName = "bar")
public class MyBean {
private ApplicationContext applicationContext;
@Autowired
private StringRedisTemplate stringRedisTemplate;
private ValueOperations<String, String> valueOperations;
public MyBean(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@PostConstruct
private 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 {
@Bean
public MyBean myBean(ApplicationContext applicationContext){
return new MyBean(applicationContext);
}
}
至此,jmx-client配置已经完成。
jmx-client-2
复制一份jmx-client ,并更改**application.yml**server端口号为8082
server:
port:
更改自定义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 {
@Autowired
private StringRedisTemplate stringRedisTemplate;
private ValueOperations<String, String> valueOperations;
@PostConstruct
private 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 {
@Bean
public SumBean sumBean(){
return new SumBean();
}
}
启动四个服务
如图
PS:idea 2.6+版本可以同时启动多个服务,而不需要挨个去找application类了。
Eureka注册中心
Admin Server
点击JMX-CLIENT后 【Details】链接,然后再点开【JMX】链接,就可以看到自定义的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