Druid 配置及内置监控使用

1. Druid是什么?

Druid是Java语言中最好的数据库连接池。Druid能够提供强大的监控和扩展功能。

2. 在哪里下载druid

 正式版本下载:
    maven中央仓库: http://central.maven.org/maven2/com/alibaba/druid/

3. 怎么获取Druid的源码

Druid是一个开源项目,源码托管在github上,源代码仓库地址是 :
https://github.com/alibaba/druid
同时每次Druid发布正式版本和快照的时候,都会把源码打包,你可以从上面的下载地址中找到相关版本的源码
4. 怎么配置maven

Druid 0.1.18 之后版本都发布到maven中央仓库中,所以你只需要在项目的pom.xml中加上dependency就可以了。例如:


    com.alibaba
    druid
    ${druid-version}

也可以选择 Maven仓库查找公共的仓库地址:
http://www.mvnrepository.com/artifact/com.alibaba/druid

5. 怎么打开Druid的监控统计功能

applicationContext.xml 数据源配置


<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    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.2.xsd
  http://www.springframework.org/schema/aop 
  http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  http://www.springframework.org/schema/context 
  http://www.springframework.org/schema/context/spring-context-3.2.xsd
  http://www.springframework.org/schema/tx 
  http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
    <description>Spring公共配置文件 description>
    <context:property-placeholder
        ignore-resource-not-found="true" location="classpath:spring/jdbc.properties" />

    
    <context:component-scan base-package="com">
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    context:component-scan>


    <tx:annotation-driven />

    

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:/sqlmapper/sqlConfig.xml" />
        <property name="mapperLocations" value="classpath:/sqlmapper/**/*sqlmapper.xml" />
    bean>

    <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory" />
        <constructor-arg index="1" value="REUSE" />
    bean>

    
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    bean>

    
    <aop:config>
        <aop:pointcut id="fooServiceMethods" expression="execution(* com.serviceImpl..*.*(..))" />
        <aop:advisor advice-ref="txAdvice" pointcut-ref="fooServiceMethods" />
    aop:config>

    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*" read-only="true" propagation="REQUIRED"/>
            <tx:method name="create*" rollback-for="Exception" propagation="REQUIRED" />
            <tx:method name="save*" rollback-for="Exception" propagation="REQUIRED" />
            <tx:method name="insert*" rollback-for="Exception" propagation="REQUIRED" />
            <tx:method name="update*" rollback-for="Exception" propagation="REQUIRED" />
            <tx:method name="delete*" rollback-for="Exception" propagation="REQUIRED" />
        tx:attributes>
    tx:advice>

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        destroy-method="close">
        
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.mysql.url}" />
        <property name="username" value="${jdbc.mysql.username}" />
        <property name="password" value="${jdbc.mysql.password}" />

        
        <property name="maxActive" value="${druid.maxActive}" />
        <property name="minIdle" value="1" />
        <property name="defaultAutoCommit" value="false" />
        
        <property name="timeBetweenEvictionRunsMillis" value="3600000" />
        <property name="minEvictableIdleTimeMillis" value="3600000" />
        
        <property name="validationQuery" value="SELECT SYSDATE() FROM DUAL" />
        <property name="testWhileIdle" value="true" />
        <property name="poolPreparedStatements" value="true" />
        <property name="maxPoolPreparedStatementPerConnectionSize"
            value="20" />
        <property name="filters" value="stat,config" />
        <property name="connectionProperties"
            value="druid.stat.slowSqlMillis=2000;config.decrypt=false" />
        <property name="removeAbandoned" value="true" /> 
        <property name="removeAbandonedTimeout" value="1800" /> 
        <property name="logAbandoned" value="true" /> 
    bean>
    


    
    <tx:annotation-driven transaction-manager="transactionManager"
        proxy-target-class="true" />


    
    <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.dao.base.*value>
            list>
        property>
    bean>
    
    <aop:config>
        <aop:advisor advice-ref="druid-stat-interceptor"
            pointcut-ref="druid-stat-pointcut" />
    aop:config>
    
    <aop:aspectj-autoproxy />


beans>

web.xml 配置

<servlet>
        <servlet-name>DruidStatViewservlet-name>
        <servlet-class>com.alibaba.druid.support.http.StatViewServletservlet-class>
        <init-param>
            
            <param-name>loginUsernameparam-name>
            <param-value>druidparam-value>
        init-param>
        <init-param>
            
            <param-name>loginPasswordparam-name>
            <param-value>druidparam-value>
        init-param>
    servlet>
    <servlet-mapping>
        <servlet-name>DruidStatViewservlet-name>
        <url-pattern>/druid/*url-pattern>
    servlet-mapping>

访问地址:
http://127.0.0.1:8080/项目/druid/index.html

Druid 配置及内置监控使用_第1张图片



参考文档:
配置_StatFilter: https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_StatFilter
常见问题: https://github.com/alibaba/druid/wiki/%E5%B8%B8%E8%A7%81%E9%97%AE%E9%A2%98

你可能感兴趣的:(框架应用)