本文介紹通過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