Druid Monitor monitors javase and Javaweb__java

Source: Internet
Author: User
Tags db2 postgresql sqlite java web maven central log4j
Druid Monitor

For the data source, I believe that we have a lot of contact. such as C3P0, DHCP, Proxool, and then found that the use of TOMCAT-JDBC can greatly improve performance. But for our highly concurrent systems, we always want to find a better, more stable product instead. In the open source China accidentally found that many companies are using Druid this data source, and then search for some relevant information, someone provided the data source performance test report, suddenly found that the data source with good performance has been widely used. So I also replaced the JDBC data source in the project with Druid, then let the tester help to press the system and found the performance improved. So I want to study this data source in depth. In fact, many projects are javase, but the official website for the Javase project did not specify how to use its monitoring statistics function. So I looked at the source found that there is a configuration Jmxurl, you can think of this should be for remote monitoring of the project configuration. Here I will tidy up their use of the situation for future reference, if there is a mistake, also please Daniel testify, I also learn more.

First of all know a few address bar, but also the province of visitors back and forth search.

Official version download address: Http://repo1.maven.org/maven2/com/alibaba/druid/
Druid is an open source project, the source code hosted on the GitHub: Https://github.com/alibaba/druid
After the Druid 0.1.18 version is released to the MAVEN central repository, you only need to add dependency to the project's pom.xml. For example:

<dependency>
        <groupId>com.alibaba</groupId>
        <artifactid>druid</artifactid >
        <version>${druid-version}</version>
</dependency></span>

You can also select the Maven warehouse to find the public warehouse address: Http://www.mvnrepository.com/artifact/com.alibaba/druid

Well, that's a whole bunch of crap. The next step is to show how to view the Druiddatasource monitoring data.
For Web projects, it's easy to use, just configure the Druidstatview in the Web.xml. The rest of the Druid has helped us all come true.


1.Druid Monitor Java Web project

Let me first explain the configuration of the data source, example:

<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" xmlns:
	Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "Http://www.springframework.org/schema/context" xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "Http://www.springframework.org/schema/tx" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring- Beans-3.0.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/s Pring-aop-3.0.xsd Http://www.springframework.org/schema/tx Http://www.springframework.org/schema/tx/sp Ring-tx-3.0.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/ Context/spring-context-3.0.xsd "> <bean id=" dataSource "class=" Com.alibaba.druid.pool.DruidDataSource " Init-method= "Init" destroy-method= "close" > <!--Basic Property URL, user, password--> <property name= "Driverclassname" value= "Oracle.jdbc.OracleDriver"/> <propert Y name= "url" value= "jdbc:oracle:thin:@192.168.102.93:1521:mydb1"/> <property name= "username" value= "my_v31"/&
		Gt <property name= "Password" value= "my_v31"/> <!--configuration initialization size, MIN, max--> <property name= "InitialSize" value = "1"/> <property name= "Minidle" value= "1"/> The <property name= "maxactive" value= "/>" <!--configuration Get Connection wait Time out--> <property name= "maxwait" value= "60000"/> <!--how long does the configuration interval take to detect and detect idle connections that need to be closed, in milliseconds--> &L T;property name= "Timebetweenevictionrunsmillis" value= "60000"/> <!--Configure the minimum time that a connection is to survive in the pool, in milliseconds--> <proper
			Ty name= "Minevictableidletimemillis" value= "300000"/> <!--used to detect the validity of a connection to SQL, the requirement is a query statement. If Validationquery is Null,testonborrow, Testonreturn, Testwhileidle does not function--> <property name= "ValidationQuery" V Alue= "Select 1 from DUAL"/> <pRoperty name= "Testwhileidle" value= "true"/> <property "Name=" Testonborrow "false" value=/> Name = "Testonreturn" value= "false"/> <!--open Pscache and specify the size of Pscache on each connection if Oracle is used, Poolpreparedstatements is configured The true,mysql can be configured to false--> <property name= "poolpreparedstatements" value= "true"/> <property "name=" MaxPoo Lpreparedstatementperconnectionsize "value="/> <!--configuration monitor statistic intercept filters, remove post monitor interface SQL cannot count--> <property na Me= "Filters" value= "stat,wall,log4j"/> <!--if proxyfilters is configured, this configuration can not configure Druid.stat.mergesql=true merge execution of the same SQL, avoid statistics multiple SQL statements because of different parameters druid.stat.slowsqlmillis=10000 used to configure SQL slow standard, execution time over Slowsqlmillis is slow <property name= "conn Ectionproperties "value=" druid.stat.mergesql=true;druid.stat.slowsqlmillis=10000 "/>--> <!--surveillance statistics intercept fil Ters--> <!--and configured in the filters Property log4j--> <property name= "Proxyfilters" > <list> <ref bean=
				"Stat-filter"/><ref bean= "Log-filter"/> </list> </property> </bean> <!--slow SQL record--> <bean id= " Stat-filter "class=" Com.alibaba.druid.filter.stat.StatFilter "> <property name=" Mergesql "value=" true "/> &
	
	Lt;property name= "Slowsqlmillis" value= "10000"/> <property name= "Logslowsql" value= "true"/> </bean> <bean id= "Log-filter" class= "Com.alibaba.druid.filter.logging.Log4jFilter" > <!--<property name= " Resultsetlogenabled "value=" false "/>--> <!--<property name=" statementexecutablesqllogenable "value=" True "/>--> </bean> <!--configuration Druid monitoring spring JDBC--> <bean id=" Druid-stat-interceptor "class=" C Om.alibaba.druid.support.spring.stat.DruidStatInterceptor "/> <bean id=" druid-stat-pointcut "class="
            Org.springframework.aop.support.JdkRegexpMethodPointcut "scope=" prototype "> <property name=" Patterns "> <list> <value>Com. Xxx.stat.service.*</value> <value>com.  xxx.stat.dao.*</value> </list> </property> </bean> <aop:config Proxy-target-class= "true" > <aop:advisor advice-ref= "druid-stat-interceptor pointcut-ref=" Druid-stat-pointc UT "/> </aop:config> </beans>

Although Druid has stated that only the URL address in the configuration database is required to automatically match the driverclassname, it is found that if this is not configured, there is always a warning in the log, so it is configured. The official website offers a bit of his own driverclassname:

Jdbc:derby:=org.apache.derby.jdbc.embeddeddriver	 
jdbc:mysql:=com.mysql.jdbc.driver
jdbc:log4jdbc:= Net.sf.log4jdbc.DriverSpy
jdbc:oracle:=oracle.jdbc.driver.oracledriver
jdbc:microsoft:= Com.microsoft.jdbc.sqlserver.SQLServerDriver	 
jdbc:jtds:=net.sourceforge.jtds.jdbc.driver	 
jdbc: Postgresql:=org.postgresql.driver	 
jdbc:fake:=com.alibaba.druid.mock.mockdriver	 
jdbc:hsqldb:= Org.hsqldb.jdbcDriver	 
jdbc:db2:=com.ibm.db2.jdbc.app.db2driver
jdbc:sqlite:=org.sqlite.jdbc	 
jdbc: Ingres:=com.ingres.jdbc.ingresdriver	 
jdbc:h2:=org.h2.driver	 
jdbc:mckoi:=com.mckoi.jdbcdriver
Next, let's talk about the configuration parameters in Web.xml, where you can also configure Loginusername and Loginpassword to avoid all people seeing your own database. In this way, you must enter a password to access the page when you log on to the monitor. Of course, you can add your IP to the Allow, so that only your own machine can access the monitoring interface. Cases:
<filter> <filter-name>DruidWebStatFilter</filter-name> <filter-class> Com.alibaba.druid.support.http.webstatfilter</filter-class> <init-param> <!--often need to exclude some unnecessary URLs, Like. js,/jslib/and so on. Configuration in Init-param--> <param-name>exclusions</param-name> <param-value>*.js,*.gif,*.jpg,*.png,* .css,*.ico,/druid/*</param-value> </init-param> <!--default Sessionstatmaxcount is 1000. You can configure--> <init-param> <param-name>sessionStatMaxCount</param-name> <param-value>1 as needed 000</param-value> </init-param> <!--druid version 0.2.7 begins to support profile, configuring profileenable to monitor a single URL invocation of the SQL list-- > <init-param> <param-name>profileEnable</param-name> <param-value>true</param-valu e> </init-param> <init-param> <param-name>principalSessionName</param-name> <param -value>users.username</param-value> </init-param> <!--you can turn off the session statistic function <init-param> <param-name>sessionStatEnable</param-name> <param-value& gt;true</param-value> </init-param>--> </filter> <servlet> <servlet-name>druids Tatview</servlet-name> <servlet-class>com.alibaba.druid.support.http.statviewservlet</
			Servlet-class> <!--deny takes precedence over allow, and is rejected even in the Allow list in the Deny list. If allow is not configured or empty, all Access--> <init-param> <param-name>allow</param-name> <param-value&gt are allowed ;128.242.127.1/24,127.0.0.1</param-value> </init-param> <init-param> <param-name>deny< /param-name> <param-value>192.168.1.118</param-value> </init-param> <!--lost in Statviewserlvet Out of the HTML page, there is a function is reset all, after performing this operation, will cause all counters to clear zero, to recount--> <span style= "White-space:pre" > </span>< init-param> <span style= "White-space:pre" > </span><param-name>resetenable</param-name> <span style= "White-space:pre" > </span><param-value>false</param-v alue> <span style= "White-space:pre" > </span></init-param> <span style= "White-space:pre" &	Gt </span><!--username and password--> <span style= "White-space:pre" > </span><init-param> <param -name>loginusername</param-name> <param-value>druid</param-value> </init-param> < init-param> <param-name>loginPassword</param-name> <param-value>druid</param-value> &L
		t;/init-param> </servlet> <servlet-mapping> <servlet-name>DruidStatView</servlet-name> <url-pattern>/druid/*</url-pattern> </servlet-mapping>
After configuring the Web.xml, start your own Javaweb service and visit the address to see the Druid Monitor interface. I am here to visit the address is: http://192.168.1.118:8778/druid-monitor/druid/sql.html
Enter the user name password (druid/druid) to enter the monitoring page.
 

2.Druid Monitor Javase Project


The monitor Javase project can access the remote server via JMX, or it can run the druidstat.sh command provided on the server side. Let's explain how to access through JMX, a server that is both convenient and not logged into the production environment. First, since JMX is needed to access, the server must provide the access port and IP address of the JMX. Then you need to add Java running parameters to our startup files. Need to add-dcom.sun.management.jmxremote-djava.rmi.server.hostname-dcom.sun.management.jmxremote.port- Dcom.sun.management.jmxremote.authenticate-dcom.sun.management.jmxremote.ssl the parameter to burst its own server address. Example: Running scripts for Linux
Resinwebinf=/home/v31/common-biz
libdirs=${resinwebinf}/lib
cp=.:${resinwebinf}/resources for
Jars In ' ls ${libdirs} '
did
cp=${cp}:${libdirs}/${jars}
done

java  -xms2048m-xmx2048m-cp ${CP}   - djava.net.preferipv4stack=true-dcom.sun.management.jmxremote-djava.rmi.server.hostname=192.168.1.76  - dcom.sun.management.jmxremote.port=9004  -dcom.sun.management.jmxremote.authenticate=false  - Dcom.sun.management.jmxremote.ssl=false  com.company.main.StartMain  >>logs/error.log 2>&1 &

Then deploy a Web project to access this JMX's IP and port. Start the service and check to see if our port has been started



Finally, we can view the monitoring data of the data source by configuring the Jmxurl address in our local web.xml. Cases

	 
	<servlet>
		<servlet-name>DruidStatView</servlet-name>
		<servlet-class> Com.alibaba.druid.support.http.statviewservlet</servlet-class>
		
	    <!--remote Access Javase Project uses JMX connectivity-->
	    <init-param>
            <param-name>jmxUrl</param-name>
            <param-value>service:jmx:rmi:/// jndi/rmi://192.168.1.76:9004/jmxrmi</param-value>
        </init-param>
	</servlet>
	< servlet-mapping>
		<servlet-name>DruidStatView</servlet-name>
		<url-pattern>/druid /*</url-pattern>
	</servlet-mapping>


After you configure these, visit our own web container and find that you can view the statistics. There are people who use Jconsole to visit, I have not tried, because I think that the operation is too much trouble for me.

3.Druid monitor monitoring, using druidstat.sh

This command script is in the druid\src\main\scripts directory of Git source code,
The Druidstat.bat script is:

@echo off

rem Copyright 1999-2011 Alibaba Group Holding Ltd.
REM 
REM licensed under the Apache License, Version 2.0 (the "License");
REM-May-not use this file except in compliance with the License.
REM Obtain a copy of the License at REM rem      http://www.apache.org/licenses/LICENSE-2.0
rem 
REM unless required by applicable or agreed into writing, software
REM distributed under the License is Distribu Ted on, ' as is ' basis,
REM without warranties or CONDITIONS to any KIND, either express OR implied.
REM The License for the specific language governing permissions and REM limitations under the License
.


Set _runjava= "%java_home%\bin\java.exe"
set _toolsjar= "%java_home%\lib\tools.jar"

%_runjava%-classpath "./druid-0.2.6.jar;%_toolsjar%" Com.alibaba.druid.support.console.DruidStat%*
The druidstat.sh script is:
#!/bin/sh

If [-Z "$JAVA _home"]; then
  echo "Error:java_home is not defined."
  Exit 1
fi

"$JAVA _home/bin/java"-dfile.encoding= "UTF-8"-CP "./druid-0.2.6.jar: $JAVA _home/lib/tools.jar" Com.alibaba.druid.support.console.DruidStat  $@

With this script, we can run the statistics directly on the server. Here I only explain the way Linux is viewed. Let's change the directory and name of the druidstat.sh Druid-0.2.6.jar, and then run it on your own server. Let's explain how to use the command.


See the druidstat.sh command Help, I believe that we have been half. Then explain how to use. Directly above:

If you need to see the specifics of an ID, you can use-id-detail to view the details of the SQL.



End: Provide a download address for this project: http://download.csdn.net/detail/binglovezi/9422882
This is just a Tomcat run project package, not a source package, so just copy it to your tomcat and modify the parameters in it so you don't import into your eclipse.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.