Druid使用配置

一、介绍

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

GitHub地址
https://github.com/alibaba/druid

正式版本下载地址:
http://repo1.maven.org/maven2/com/alibaba/druid/

二、安装配置

web.xml

    
    <filter>
        <filter-name>DruidWebStatFilterfilter-name>
        <filter-class>com.alibaba.druid.support.http.WebStatFilterfilter-class>
        <init-param>
            <param-name>exclusionsparam-name>
            <param-value>*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*param-value>
        init-param>
        <init-param>
            <param-name>profileEnableparam-name>
            <param-value>trueparam-value>
        init-param>
        <init-param>
            <param-name>principalCookieNameparam-name>
            <param-value>USER_COOKIEparam-value>
        init-param>
        <init-param>
            <param-name>principalSessionNameparam-name>
            <param-value>USER_SESSIONparam-value>
        init-param>
   filter>
   <filter-mapping>
        <filter-name>DruidWebStatFilterfilter-name>
        <url-pattern>/*url-pattern>
   filter-mapping>    

    <servlet>
      <servlet-name>DruidStatViewservlet-name>
      <servlet-class>com.alibaba.druid.support.http.StatViewServletservlet-class>
       
      <init-param> 
        <param-name>resetEnableparam-name> 
        <param-value>trueparam-value> 
      init-param> 
       
      <init-param>  
         <param-name>allowparam-name>  
         <param-value>106.14.64.145,127.0.0.1param-value>  
      init-param>  
       
      <init-param>  
          <param-name>denyparam-name>  
          <param-value>param-value>  
      init-param> 
      
      <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>
     

maven引入

<dependency>  
    <groupId>com.alibabagroupId>  
    <artifactId>druidartifactId>  
    <version>1.0.20version>  
dependency>  

数据源配置

    <bean id="dataSourceProduct" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" lazy-init="false">
           
        <property name="driverClassName" value="${product_driver}" />
        <property name="url" value="${product_url}" />
        <property name="username" value="${product_jdbc.username}" />
        <property name="password" value="${product_jdbc.password}" />

          
        <property name="initialSize" value="${product_initialSize}" />
        <property name="maxActive" value="${product_maxActive}" />
        <property name="maxIdle" value="${product_minIdle}" />

          
        <property name="maxWait" value="${product_maxWait}" />

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

          
        <property name="timeBetweenEvictionRunsMillis" value="3000" /> 

           
        <property name="minEvictableIdleTimeMillis" value="300000" /> 

           
        <property name="validationQuery" value="SELECT 'x'" />  
        <property name="testWhileIdle" value="true" />  
        <property name="testOnBorrow" value="false" />  
        <property name="testOnReturn" value="false" />  

          
        <property name="poolPreparedStatements" value="false" />  
        <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
    bean> 

三、问题处理

远程无法加载Druid的样式

本地环境通过配置以后能正常浏览Druid的管理界面,但程序部署到远程出现相关Js及Css样式加载不成功。
目前项目部署采用的Nginx+Tomcat,使用代理方式从Nginx访问JavaWeb项目的,访问路径如www.bac.com/project/
如果直接用IP加端口号样式加载正常,如果用单独域名指向项目应该也会正常加载样式。
样式加载不正常:
http://api.abc.com/project/druid/login.html

样式加载正常:
http://106.14.23.145:9597/project/druid/index.html

start(2018-07-08)

目前直接用域名加载druid下的css及js文件还是失败,需要在nginx进行配置。
本地访问地址
http://172.0.0.1:9595/project1/druid/css/bootstrap.min.css
http://172.0.0.1:9595/project1/druid/js/jquery.min.js

需要将以下文件定向到本地访问地址
http://api.domain.com/project1/druid/css/bootstrap.min.css
http://api.domain.com/project1/druid/js/jquery.min.js

试了几次nginx配置,但都没有成功。最后只能将druid中的静态资源提出,放入到项目根目录下面

end(2018-07-08)

Druid使用配置_第1张图片

设备Druid访问权限

在Servlet中添加初始化用户名及密码

      
      <init-param>  
          <param-name>loginUsernameparam-name>  
          <param-value>druidparam-value>  
      init-param>  
      <init-param>  
          <param-name>loginPasswordparam-name>  
          <param-value>druidparam-value>  
      init-param> 

设置数据库密码加载方法

执行命令: java -cp druid-1.1.6.jar com.alibaba.druid.filter.config.ConfigTools 密码

root-2:1.1.6 root$ java -cp druid-1.1.6.jar com.alibaba.druid.filter.config.ConfigTools 111111
privateKey:MIIBVgIBADANBgkqhkiG9w0BAQEFAASCAUAwggE8AgEAAkEAriRbBxh1AxNFjdebNfYTW31dRmwoVhPCb7iok2A5hzCsMjhB858DG+zspSHJlV6KXl5jW89UzXkh611dwYFLhQIDAQABAkEAhxb85ow6eaBBnL9dUIcMjTS7Xx4YTG4Xr8N4QpurBQucPv0rzSmnuBG55gXEyTIzhifURFBp4jFIeLg5XS0VAQIhAOYH54GAqIZqmuxa5yYmYrGxS4zk95c6vQaPDuE3WHQdAiEAwc02tviDvF8s7j6McIVOSEyUDd+D65BC9jWXRI8fSIkCIGkRBy7hgxgSLTzzsEx+j+m1n1p1YplSnPAX+6Xmz6IJAiEAkse+PO4TuEPNX9P3wfD0zUVSjP9QhsUIMjl8i+xqISECIQDhIMdh1t5G3vou1y2sJjADRJf+dWUB2uvjVMznmkkRGQ==
publicKey:MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAK4kWwcYdQMTRY3XmzX2E1t9XUZsKFYTwm+4qJNgOYcwrDI4QfOfAxvs7KUhyZVeil5eY1vPVM15IetdXcGBS4UCAwEAAQ==
password:XGId3Rnnku03GTgou7iXJUS1/eNUmlSVA38j4mGKjnB7P9Z6B9jwvLGTDk1HiK7mmsctGt+6bXcox/Y+F0py4g==

在数据库链接中配置内容:

<property name="connectionProperties" value="config.decrypt=true;config.decrypt.key=${product_jdbc.publicKey}"/>
<property name="filters" value="config,stat,log4j,wall" />      

你可能感兴趣的:(Java,druid)