Druid monitoring and slow SQL logging

http://www.cnblogs.com/han-1034683568/p/6730869.html

Summary of this article

The previous article also mentioned that druid is not just a connection pool technology, so after integrating druid into the project, this article will introduce other features and functions of druid as an auxiliary tool to help improve the performance of the project. The point is two words: monitoring.
My github address click here

druid monitoring

Because the integration has been done, this step is relatively simple. You only need to do a simple Servlet configuration in web.xml.

<!--druid监控页面 -->
    <servlet>
        <servlet-name>DruidStatView</servlet-name>
        <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
        <init-param>
            <!-- 不允许清空统计数据 -->
            <param-name>resetEnable</param-name>
            <param-value>false</param-value>
        </init-param>
        <init-param>
            <!-- 用户名 -->
            <param-name>loginUsername</param-name>
            <param-value>yourname</param-value>
        </init-param>
        <init-param>
            <!-- 密码 -->
            <param-name>loginPassword</param-name>
            <param-value>yourpassword</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>DruidStatView</servlet-name>
        <url-pattern>/druid/*</url-pattern>
    </servlet-mapping>
    <!--druid监控页面 -->

Rebuild the project and start tomcat, enter druid in the browser to enter the login page of the druid monitoring panel.
log in page

Enter the account and password configured in web.xml to enter the monitoring background. Note that the configured account and password are in plain text. What you configure here is what you configure, and it is not encrypted.
Druid brief information

OK, we saw some brief information about the system. Of course, you should also see the startup time. Yes, I started writing this blog at 9:00 after get off work.

Next is a more important page, SQL monitoring
SQL monitoring

From this page, we can see the statistics of the sql statement executed by the website from the start, the number of executions of each statement, the sum of the execution time, the slowest execution time, the number of execution errors, and many other statistical indicators. Through this statistical data, we can You can find out the execution rules of sql statements and the insufficiencies of sql statements, and you can also locate the deficiencies of the program through the number of errors and modify them in time.

When configuring the druid data source, the following configuration was made to enable the druid firewall.

 <property name="filters" value="wall,stat"/>

Therefore, the SQL firewall panel can also be seen in the control background, as shown in the following figure:
SQL Firewall

There are defense statistics and sql statistics in the panel. Here are some default filtering rules of druid, and there are no custom complex firewall rules, so I will not go into details.

But when I clicked on several other pages, I found that the pages were all empty data, because the druid-related functions were not enabled. This is a rougher approach. There is no need to do other configurations, that is, the default filters configuration plus Servlet configuration. Although there are monitoring and statistics, it does not particularly meet the needs.

Enable slow sql monitoring

In SQL monitoring, there is a statistic about the slowest execution time of SQL execution, but there is only one value, which is the record of the slowest execution time of a SQL statement. Other execution times cannot be seen, and can only be roughly calculated by the total time. Another problem is that once the project is restarted, all these records will be lost, so it is extremely necessary to formulate a corresponding log output strategy.
The general idea is to obtain the slow SQL execution records of all projects running through druid, output these data to the log file, check the information of druid, debug for a period of time, and finally successfully implemented.

1. Modify the data source configuration and add an interceptor:

 <property name="proxyFilters">
            <list>
                <ref bean="stat-filter"/>
                <ref bean="log-filter"/>
            </list>
        </property>

2. Configure slow sql and log interceptor:

    <!-- 慢SQL记录 -->
    <bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
        <!-- 慢sql时间设置,即执行时间大于200毫秒的都是慢sql -->
        <property name="slowSqlMillis" value="200"/>
        <property name="logSlowSql" value="true"/>
    </bean>

    <bean id="log-filter" class="com.alibaba.druid.filter.logging.Log4jFilter">
        <property name="dataSourceLogEnabled" value="true" />
        <property name="statementExecutableSqlLogEnable" value="true" />
    </bean>

3. Modify the log4j configuration file and increase the output strategy of the slow sql log:

log4j.rootLogger=DEBUG,debug,druid
# Druid
log4j.logger.druid.sql=WARN,druid
log4j.logger.druid.sql.DataSource=WARN,druid
log4j.logger.druid.sql.Connection=WARN,druid
log4j.logger.druid.sql.Statement=WARN,druid

log4j.appender.druid=org.apache.log4j.DailyRollingFileAppender
log4j.appender.druid.layout=org.apache.log4j.PatternLayout
log4j.appender.druid.layout.ConversionPattern= [%d{HH\:mm\:ss}] %c{1} - %m%n
log4j.appender.druid.datePattern='.'yyyy-MM-dd
log4j.appender.druid.Threshold = WARN
log4j.appender.druid.append=true
log4j.appender.druid.File=${catalina.home}/logs/ssm-maven/druid-slow-sql.log

Restart and check the druid monitoring background, you can see that there are some differences from the original. Since the slow SQL time is set to be greater than 200 milliseconds, the execution time greater than 200 milliseconds will be marked in red.
Slow sql marked red

Go to the tomcat log folder to view the log file, you can see that the configured slow sql log file already exists in the log file, click to view the data recorded by the slow sql, the slow sql and the execution time of this sql statement There are records.
slow sql log

Enable spring monitoring

In the monitoring panel, I saw that there is a function of spring monitoring, but because it has not been configured, the function cannot be used. I checked the druid documentation, and finally turned on the spring monitoring function.

The configuration is as follows:

    <bean id="druid-stat-interceptor"
          class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor">
    </bean>

    <bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut"
          scope="prototype">
        <property name="patterns">
            <list>
                <value>com.ssm.maven.core.service.*</value>
                <value>com.ssm.maven.core.dao.*</value>
            </list>
        </property>
    </bean>

    <aop:config>
        <aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut"/>
    </aop:config>

Looking at the spring monitoring page again, there is already data:
spring monitoring

The original method can only monitor sql statements and jdbc related operations, but cannot monitor the operation at the code level. Therefore, it is further set up to monitor the running effect of the code combined with the AOP feature of spring. Druid can monitor the method level. This The function allows you to discover the calling frequency of the method and the running time of the method, and make adjustments and corrections in time to make the project more robust.

Epilogue

Because I want to monitor some information and operation of the website, I integrated the relevant functions of druid in the code and uploaded it to the github repository. If you don't need it, you can delete the relevant configuration files according to the comments I wrote in the code. That's it.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326527184&siteId=291194637