原配置结构为spring+maven profile整合,现在整改也在此基础上做调整
1. 项目pom.xml ,增加druid依赖
<!-- alibaba --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>0.2.23</version> </dependency>
2. 在config资源文件夹下新增不同环境的jdbc配置文件,这里举例为
jdbc-develop.properties
jdbc-test.properties
jdbc-product.properties,每个文件内容均为如下
## JDBC set jdbc.url=jdbc:oracle:thin:@192.168.xx.x:1521:oracle jdbc.username=pjm jdbc.password=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx+Zu1xI4coeWXqOJYBCwxqoHnmc+D9Lhoku5lSphi3ZvOJ0tJ6rzzA==
这里采用密码加密,解决机制在3中配置,加密方法如下:
cmd命令:
❶、进入druid-0.2.23.jar包所在目录(这里用的druid为0.2.23版本)
❷、在命令行中执行如下命令:pwd为需要加密的密码
java -cp druid-0.2.23.jar com.alibaba.druid.filter.config.ConfigTools pwd
输出加密结果
3. 项目中新增druid-profile.xml,注意红字标出,配置数据库连接、配置文件、最后跟上profiles定;filters(stat)为性能监控
<?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:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd" default-lazy-init="true"> <description>各环境数据库配置 </description> <beans profile="develop"> <context:property-placeholder location="classpath*:jdbc-develop.properties" /> <!-- 基于Druid数据库链接池的数据源配置 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本属性driverClassName、 url、user、password --> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 配置初始化大小、最小、最大 --> <!-- 通常来说,只需要修改initialSize、minIdle、maxActive --> <property name="initialSize" value="2" /> <property name="minIdle" value="2" /> <property name="maxActive" value="30" /> <property name="testWhileIdle" value="true" /> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="5000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="30000" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 解密密码必须要配置的项/性能监控 --> <property name="filters" value="config,stat" /> <property name="connectionProperties" value="config.decrypt=true" /> </bean> </beans> <beans profile="product"> <context:property-placeholder location="classpath*:jdbc-production.properties" /> … <!-- 同上设置 --> </beans> <beans profile="test"> <context:property-placeholder location="classpath*:jdbc-test.properties" /> … <!-- 同上设置 --> </bean> </beans> <beans profile="develop,test,product"></beans> </beans>
或者以下格式
<description>各环境数据库配置 </description> <beans profile="develop"> <context:property-placeholder location="classpath*:jdbc-develop.properties" /> </beans> <beans profile="product"> <context:property-placeholder location="classpath*:jdbc-product.properties" /> </beans> <beans profile="test"> <context:property-placeholder location="classpath*:jdbc-test.properties" /> </beans> <beans profile="develop,test,product"> <!-- 基于Druid数据库链接池的数据源配置 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <!-- 基本属性driverClassName、 url、user、password --> <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> <!-- 配置初始化大小、最小、最大 --> <!-- 通常来说,只需要修改initialSize、minIdle、maxActive --> <property name="initialSize" value="2" /> <property name="minIdle" value="2" /> <property name="maxActive" value="30" /> <property name="testWhileIdle" value="true" /> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="5000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="30000" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 解密密码必须要配置的项/性能监控 --> <property name="filters" value="config,stat" /> <property name="connectionProperties" value="config.decrypt=true" /> </bean> </beans>
4. 删除原database.xml配置
5. 项目web.xml中添加红字信息,如果不需要监控,去除即可
<!-- 加载配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/*.xml</param-value>
</context-param>
<!-- 定义环境参数 -->
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>develop</param-value>
</context-param>
<!-- 定义druid监控 start -->
<servlet>
<servlet-name>DruidStatView</servlet-name>
<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DruidStatView</servlet-name>
<url-pattern>/druid/*</url-pattern>
</servlet-mapping>
<!-- 定义druid监控 end –>
好了,搞定~查看监控地址为:http://localhost:8080/project/druid/sql.html