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就可以了。例如:

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

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

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

applicationContext.xml 数据源配置

<?xml version="1.0" encoding="UTF-8"?>
<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" />

    <!-- 使用annotation 自动注册bean,并保证@Required,@Autowired的属性被注入 -->
    <context:component-scan base-package="com">
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>


    <tx:annotation-driven />

    <!-- beecell_weixin_card 数据库配置连接开始 -->

    <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>

    <!-- 事务管理器配置, 使用jdbc事务 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- spring declarative transaction management -->
    <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">
        <!-- Connection Info -->
        <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}" />

        <!-- Connection Pooling Info -->
        <property name="maxActive" value="${druid.maxActive}" />
        <property name="minIdle" value="1" />
        <property name="defaultAutoCommit" value="false" />
        <!-- 连接Idle一个小时后超时 -->
        <property name="timeBetweenEvictionRunsMillis" value="3600000" />
        <property name="minEvictableIdleTimeMillis" value="3600000" />
        <!-- DataSource Druid配置 -->
        <property name="validationQuery" value="SELECT SYSDATE() FROM DUAL" />
        <property name="testWhileIdle" value="true" />
        <property name="poolPreparedStatements" value="true" /><!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
        <property name="maxPoolPreparedStatementPerConnectionSize"
            value="20" />
        <property name="filters" value="stat,config" /><!-- 开启druid监控支持(stat),SQL防火墙(wall)以及SQL合并(mergeStat) -->
        <property name="connectionProperties"
            value="druid.stat.slowSqlMillis=2000;config.decrypt=false" /><!-- 慢SQL标准 -->
        <property name="removeAbandoned" value="true" /> <!-- 打开removeAbandoned功能 -->
        <property name="removeAbandonedTimeout" value="1800" /> <!-- 1800秒,也就是30分钟 -->
        <property name="logAbandoned" value="true" /> <!-- 关闭abanded连接时输出错误日志 -->
    </bean>
    <!-- beecell_weixin_card 数据库配置连接结束 -->


    <!-- 使用annotation定义事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"
        proxy-target-class="true" />


    <!-- Druid AOP配置 -->
    <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>DruidStatView</servlet-name>
        <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
        <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>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>DruidStatView</servlet-name>
        <url-pattern>/druid/*</url-pattern>
    </servlet-mapping>

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

这里写图片描述



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值