当前位置:网站首页>Druid monitoring - Introduction to JMX usage and principle
Druid monitoring - Introduction to JMX usage and principle
2022-07-07 08:57:00 【bboyzqh】
Recently focused on business , There is little time to update the blog , A while ago, it was a big promotion druid Something is wrong , By the way druid Implementation of monitoring , Don't say here druid The principle of connection pool and JMX Basic knowledge of . To configure Druid There are two main ways of monitoring :
- Mode one : adopt DruidStatManagerFacade To get DruidDataSource Relevant monitoring data ( This method is not used JMX The way ), External data related to such exposure monitoring , The user can directly create an instance and use the method to obtain . The sample code is as follows :
public class DruidStatController {
@GetMapping("/durid/stat")
public Object druidStat(){
// DruidStatManagerFacade#getDataSourceStatDataList This method can obtain the monitoring data of all data sources , besides DruidStatManagerFacade There are also some other ways , You can choose to use .
return DruidStatManagerFacade.getInstance().getDataSourceStatDataList();
}
} - Mode two : adopt http Request internal use JMX To obtain monitoring data , In essence, it's also through DruidStatManagerFa- cade To get DruidDataSource Relevant monitoring data . The configuration is as follows :
The first point :java The project startup parameters are as follows , Open port for web End view :
-Djava.net.preferIPv4Stack=true
-Dcom.sun.management.jmxremote
-Djava.rmi.server.hostname=10.8.3.221
-Dcom.sun.management.jmxremote.port=9004
-Dcom.sun.management.jmxremote.authenticate=falseSecond point :web In Engineering web.xml The configuration is as follows :
<!-- Druid, Monitoring the database , as well as WEB Access connection information -->
<!-- Reference resources : https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_%E9%85%8D%E7%BD%AEWebStatFilter -->
<filter>
<filter-name>DruidWebStatFilter</filter-name>
<filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
<init-param>
<param-name>exclusions</param-name>
<param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,*.jsp,/druid/*,/download/*</param-value>
</init-param>
<init-param>
<param-name>sessionStatMaxCount</param-name>
<param-value>2000</param-value>
</init-param>
<init-param>
<param-name>sessionStatEnable</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>principalSessionName</param-name>
<param-value>session_user_key</param-value>
</init-param>
<init-param>
<param-name>profileEnable</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>DruidWebStatFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- To configure Druid Monitoring information display page -->
<servlet>
<servlet-name>DruidStatView</servlet-name>
<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
<init-param>
<!-- Allow to clear Statistics -->
<param-name>resetEnable</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<!-- user name -->
<param-name>loginUsername</param-name>
<param-value>druid</param-value>
</init-param>
<init-param>
<!-- password -->
<param-name>loginPassword</param-name>
<param-value>druid</param-value>
</init-param>
<!-- The remote access JavaSE Project use jmx Connect -->
<init-param>
<param-name>jmxUrl</param-name>
<param-value>service:jmx:rmi:///jndi/rmi://10.8.3.221:9004/jmxrmi</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DruidStatView</servlet-name>
<url-pattern>/druid/*</url-pattern>
</servlet-mapping>
</web-app>
The main logic of this way is StatViewServlet in , If configured jmxUrl Properties will go remote JMX, The process is as follows :

Core source code reference :
/**
* StatViewServlet Class . The program first determines whether it exists jmx Connection address , If it doesn't exist , Then call the local duird service ; If there is , Then call remote jmx service . It's going on jmx signal communication , First of all, let's judge jmx Whether the connection has been established successfully , If it has been established successfully ,
Then communicate directly , If it has not been successfully established before , Will try to rebuild .
* @param url Service address to connect
* @return Returned after calling the service json character string
*/
protected String process(String url) {
String resp = null;
if (jmxUrl == null) {
resp = statService.service(url);
} else {
if (conn == null) {// Connection creation failed during initialization
try {// Try to reconnect
initJmxConn();
} catch (IOException e) {
LOG.error("init jmx connection error", e);
resp = DruidStatService.returnJSONResult(DruidStatService.RESULT_CODE_ERROR,
"init jmx connection error" + e.getMessage());
}
if (conn != null) {// Successful connection
try {
resp = getJmxResult(conn, url);
} catch (Exception e) {
LOG.error("get jmx data error", e);
resp = DruidStatService.returnJSONResult(DruidStatService.RESULT_CODE_ERROR, "get data error:"+ e.getMessage());
}
}
} else {// Successful connection
try {
resp = getJmxResult(conn, url);
} catch (Exception e) {
LOG.error("get jmx data error", e);
resp = DruidStatService.returnJSONResult(DruidStatService.RESULT_CODE_ERROR,
"get data error" + e.getMessage());
}
}
}
return resp;
}
边栏推荐
猜你喜欢

硬核分享:硬件工程师常用工具包

ncs成都新电面试经验

Quick sorting (detailed illustration of single way, double way, three way)

Nanjing commercial housing sales enabled electronic contracts, and Junzi sign assisted in the online signing and filing of housing transactions

Recommended by Alibaba P8, the test coverage tool - Jacobo is very practical

Interpolation lookup (two methods)

使用Typora编辑markdown上传CSDN时图片大小调整麻烦问题

【踩坑】nacos注册一直连接localhost:8848,no available server

MySQL master-slave delay solution

A bug using module project in idea
随机推荐
Simulation volume leetcode [general] 1705 The maximum number of apples to eat
cmake命令行使用
Unity shader beginner's Essentials (I) -- basic lighting notes
Explain Huawei's application market in detail, and gradually reduce 32-bit package applications and strategies in 2022
Digital triangle model acwing 275 Pass a note
Simulation volume leetcode [general] 1706 Where does the ball meet
Quick sorting (detailed illustration of single way, double way, three way)
指针进阶,字符串函数
ChaosBlade:混沌工程简介(一)
硬件大熊原创合集(2022/05更新)
[MySQL] detailed explanation of trigger content of database advanced
Greenplum 6.x reinitialization
OpenGL三维图形绘制
Several common database connection methods
[wechat applet: cache operation]
Alibaba P8 teaches you how to realize multithreading in automated testing? Hurry up and stop
ncs成都新电面试经验
Digital triangle model acwing 1027 Grid access
leetcode134. gas station
Three series of BOM elements