druid配置

servlet.xml

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>/WEB-INF/conf/db.properties</value>
            </list>
        </property>
    </bean>

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

    <bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
        <!--慢SQL统计,如果SQL执行时间超过一定时间则记录为慢SQL -->
        <property name="slowSqlMillis" value="3000" />
        <!--慢SQL统计日志输出 -->
        <property name="logSlowSql" value="true" />
        <!--合并SQL统计 例如select * from table t where t.id =1,会被变为select * from table t where t.id =?来统计 -->
        <property name="mergeSql" value="true" />
    </bean>

    <bean id="wall-filter-config" class="com.alibaba.druid.wall.WallConfig" init-method="init">
        <!-- 指定配置装载的目录 -->
        <property name="dir" value="META-INF/druid/wall/oracle" />
        <property name="selectWhereAlwayTrueCheck" value="false" />
        <property name="selectHavingAlwayTrueCheck" value="false" />
    </bean>

    <bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
        <property name="dbType" value="oracle" />
        <property name="config" ref="wall-filter-config" />
    </bean>


    <bean id="db01" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = host)(PORT = 1521))(CONNECT_DATA =(SERVER = DEDICATED)(SERVICE_NAME = servername)))" />
        <property name="username" value="${config.userid" />
        <property name="password" value="${config.password}"/>
        <property name="filters" value="config" />
        <property name="maxActive" value="5" />
        <property name="initialSize" value="5" />
        <property name="maxWait" value="60000" />
        <property name="minIdle" value="5" />
        <property name="timeBetweenEvictionRunsMillis" value="60000" />
        <property name="minEvictableIdleTimeMillis" value="300000" />
        <property name="validationQuery" value="SELECT 1 FROM DUAL" />
        <property name="testWhileIdle" value="true" />
        <!--这两项配置会降低连接池的性能 <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> -->
        <property name="poolPreparedStatements" value="true" />
        <property name="maxOpenPreparedStatements" value="20"/>
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
        <property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=publickey" />

        <!-- 超过时间限制是否回收 -->
        <property name="removeAbandoned" value="true" />
        <!-- 超时时间;单位为秒。180秒=3分钟 -->
        <property name="removeAbandonedTimeout" value="1800" />
        <!-- 关闭abanded连接时输出错误日志 -->
        <property name="logAbandoned" value="true" />

        <!-- 配置监控统计拦截的filters -->
        <property name="proxyFilters">
            <list>
                <ref bean="stat-filter" />
                <ref bean="wall-filter" />
                <ref bean="log-filter" />
            </list>
        </property>

        </bean>

web.xml中的配置信息:

    <servlet>
        <servlet-name>druid</servlet-name>
        <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
        <!-- 允许与拒绝查看数据库监控信息的IP地址 <init-param> <param-name>allow</param-name> <param-value>127.0.0.1,192.168.1.100</param-value> </init-param> <init-param> <param-name>deny</param-name> <param-value>127.0.0.1</param-value> </init-param> -->
        <init-param>
            <!-- 允许清空统计数据 -->
            <param-name>resetEnable</param-name>
            <param-value>true</param-value>
        </init-param>
        <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>druid</servlet-name>
        <url-pattern>/druid/*</url-pattern>
    </servlet-mapping>

    <!-- WebStatFilter用于采集web-jdbc关联监控的数据 -->
    <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,/druid/*</param-value>
        </init-param>
        <!-- 配置profileEnable能够监控单个url调用的sql列表 -->
        <init-param>
            <param-name>profileEnable</param-name>
            <param-value>true</param-value>
        </init-param>
        <!-- 你可以配置principalSessionName,使得druid能够知道当前的session的用户是谁 -->
        <init-param>
            <param-name>principalSessionName</param-name>
            <param-value>User.user</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>DruidWebStatFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

具体的参数信息请查看:
https://github.com/alibaba/druid/wiki

druid 1.0.16及其以后的版本修改了数据库密码的加密方式:

在命令行中执行如下命令:
java -cp druid-1.0.16.jar com.alibaba.druid.filter.config.ConfigTools you_password


输出
privateKey:MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEA6+4avFnQKP+O7bu5YnxWoOZjv3no4aFV558HTPDoXs6EGD0HP7RzzhGPOKmpLQ1BbA5viSht+aDdaxXp6SvtMQIDAQABAkAeQt4fBo4SlCTrDUcMANLDtIlax/I87oqsONOg5M2JS0jNSbZuAXDv7/YEGEtMKuIESBZh7pvVG8FV531/fyOZAiEA+POkE+QwVbUfGyeugR6IGvnt4yeOwkC3bUoATScsN98CIQDynBXC8YngDNwZ62QPX+ONpqCel6g8NO9VKC+ETaS87wIhAKRouxZL38PqfqV/WlZ5ZGd0YS9gA360IK8zbOmHEkO/AiEAsES3iuvzQNYXFL3x9Tm2GzT1fkSx9wx+12BbJcVD7AECIQCD3Tv9S+AgRhQoNcuaSDNluVrL/B/wOmJRLqaOVJLQGg==
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAOvuGrxZ0Cj/ju27uWJ8VqDmY7956OGhVeefB0zw6F7OhBg9Bz+0c84RjzipqS0NQWwOb4kobfmg3WsV6ekr7TECAwEAAQ==
password:PNak4Yui0+2Ft6JSoKBsgNPl+A033rdLhFw+L0np1o+HDRrCo9VkCuiiXviEMYwUgpHZUFxb2FpE0YmSguuRww==

输入你的数据库密码,输出的是加密后的结果。

密码加密部分:https://github.com/alibaba/druid/wiki/%E4%BD%BF%E7%94%A8ConfigFilter

部分内容参考网络。
参考:https://github.com/cncounter/cncounter/blob/master/cncounter/src/main/webapp/WEB-INF/web.xml

你可能感兴趣的:(druid配置)