您的位置:首页 > 编程语言 > Java开发

多数据源动态制定 Druid数据源与Spring 关联 监控

2016-02-27 17:15 429 查看
以前项目使用的是hikaricp连接池,现在要监控项目sql以及访问。引入druid连接池。也就是多连接池以及druid与spring的关联监控的记录。

首先上数据源配置文件:spring-data.properties

######################################################

## hikari datasource configure

######################################################

hikaricp.jdbc.driverClassName=com.mysql.jdbc.Driver

#hikaricp.url=jdbc:mysql://192.168.0.163:3306/jy_cf_20151226?useUnicode=true&characterEncoding=utf8

hikaricp.url=jdbc:mysql://127.0.0.1:3306/jycf_one?useUnicode=true&characterEncoding=utf8

hikaricp.username=root

hikaricp.password=pass

hikaricp.connectionTestQuery=show tables

hikaricp.connectionTimeout=30000

hikaricp.idleTimeout=600000

hikaricp.maxLifetime=1800000

hikaricp.maximumPoolSize=1000

hikaricp.minimumIdle=15

hikaricp.isInitializationFailFast=true

hikaricp.isAutoCommit=true

hikaricp.isReadOnly=false

hikaricp.poolName=hikariPool

######################################################

## druid datasource configure

######################################################

druid.jdbc.driverClassName=com.mysql.jdbc.Driver

druid.url=jdbc:mysql://127.0.0.1:3306/jycf_one?useUnicode=true&characterEncoding=utf8

druid.username=root

druid.password=pass

druid.initialSize=15

druid.minIdle=15

druid.maxActive=1000

druid.maxWait=30000

druid.timeBetweenEvictionRunsMillis=60000

druid.minEvictableIdleTimeMillis=1800000

spring-hikari-datasource.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:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:jdbc="http://www.springframework.org/schema/jdbc"

xmlns:jee="http://www.springframework.org/schema/jee"

xmlns:jpa="http://www.springframework.org/schema/data/jpa"

xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
">

<!-- 配置数据源 -->

<bean id="dataSourcehikari" class="com.zaxxer.hikari.HikariDataSource" destroy-method="shutdown">

<constructor-arg>

<bean class="com.zaxxer.hikari.HikariConfig">

<property name="driverClassName" value="${hikaricp.jdbc.driverClassName}"/>

<property name="jdbcUrl" value="${hikaricp.url}"/>

<property name="username" value="${hikaricp.username}"/>

<property name="password" value="${hikaricp.password}"/>

<property name="connectionTestQuery" value="${hikaricp.connectionTestQuery}"/>

<property name="connectionTimeout" value="${hikaricp.connectionTimeout}"/>

<property name="idleTimeout" value="${hikaricp.idleTimeout}"/>

<property name="maxLifetime" value="${hikaricp.maxLifetime}"/>

<property name="maximumPoolSize" value="${hikaricp.maximumPoolSize}"/>

<property name="minimumIdle" value="${hikaricp.minimumIdle}"/>

<property name="initializationFailFast" value="${hikaricp.isInitializationFailFast}"/>

<property name="autoCommit" value="${hikaricp.isAutoCommit}"/>

<property name="readOnly" value="${hikaricp.isReadOnly}" />

<property name="poolName" value="${hikaricp.poolName}"/>

</bean>

</constructor-arg>

</bean>

</beans>

druid 监控配置:

第一步:现在druid的jar包

这个一般百度都能找到,我使用的是druid-1.0.13.jar。

第二步:配置druid数据源

spring-druid-datasource.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:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:jdbc="http://www.springframework.org/schema/jdbc"

xmlns:jee="http://www.springframework.org/schema/jee"

xmlns:jpa="http://www.springframework.org/schema/data/jpa"

xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
">

<!-- 配置数据源 -->

<!-- 数据源配置, 使用 BoneCP 数据库连接池 -->

<bean id="dataSourcedruid" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">

<!-- 数据源驱动类可不写,Druid默认会自动根据URL识别DriverClass -->

<property name="driverClassName" value="${druid.jdbc.driverClassName}" />

<!-- 基本属性 url、user、password -->

<property name="url" value="${druid.url}" />

<property name="username" value="${druid.username}" />

<property name="password" value="${druid.password}" />

<!-- 配置初始化大小、最小、最大 -->

<property name="initialSize" value="${druid.initialSize}" />

<property name="minIdle" value="${druid.minIdle}" />

<property name="maxActive" value="${druid.maxActive}" />

<!-- 配置获取连接等待超时的时间 -->

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

<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->

<property name="timeBetweenEvictionRunsMillis" value="${druid.timeBetweenEvictionRunsMillis}" />

<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->

<property name="minEvictableIdleTimeMillis" value="${druid.minEvictableIdleTimeMillis}" />

<property name="validationQuery" value="SELECT 'x'" />

<property name="testWhileIdle" value="true" />

<property name="testOnBorrow" value="false" />

<property name="testOnReturn" value="false" />

<!-- 打开PSCache,并且指定每个连接上PSCache的大小(Oracle使用)

<property name="poolPreparedStatements" value="true" />

<property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> -->

<!-- 配置监控统计拦截的filters -->

<property name="filters" value="stat" />

</bean>

</beans>

第三步:配置web.xml文件

<!-- druid配置 -->

<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>/static/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>DruidWebStatFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<!-- druid监控 -->

<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与spring的关联

<!-- druid Spring 关联监控 : 切面-->

<bean id="druid-stat-interceptor" class="com.alibaba.druid.support.spring.stat.DruidStatInterceptor"></bean>

<!-- 配置spring 拦截监听的包:切点 -->

<bean id="druid-stat-pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut" scope="prototype">

<property name="patterns">

<list>

<value>com.jingycf.service.*</value>

<value>com.jingycf.dao.*</value>

</list>

</property>

</bean>

<aop:config>

<!-- 面向切面监控 advice-ref:切面 pointcut-ref:切点-->

<aop:advisor advice-ref="druid-stat-interceptor" pointcut-ref="druid-stat-pointcut" />

</aop:config>

第五步:在浏览器输入项目访问的根目录+/druid/index.html就可看到监控的情况

多数据源动态改变:

目的:这次记录的动态改变数据源是使用注解配置来制定当前的请求使用哪个数据源

资料:http://www.cnblogs.com/davidwang456/p/4318303.html

spring-hikari-datasource.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:context="http://www.springframework.org/schema/context"

xmlns:p="http://www.springframework.org/schema/p"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop"

xmlns:jdbc="http://www.springframework.org/schema/jdbc"

xmlns:jee="http://www.springframework.org/schema/jee"

xmlns:jpa="http://www.springframework.org/schema/data/jpa"

xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
">

<!-- 配置数据源 -->

<bean id="dataSourcehikari" class="com.zaxxer.hikari.HikariDataSource" destroy-method="shutdown">

<constructor-arg>

<bean class="com.zaxxer.hikari.HikariConfig">

<property name="driverClassName" value="${hikaricp.jdbc.driverClassName}"/>

<property name="jdbcUrl" value="${hikaricp.url}"/>

<property name="username" value="${hikaricp.username}"/>

<property name="password" value="${hikaricp.password}"/>

<property name="connectionTestQuery" value="${hikaricp.connectionTestQuery}"/>

<property name="connectionTimeout" value="${hikaricp.connectionTimeout}"/>

<property name="idleTimeout" value="${hikaricp.idleTimeout}"/>

<property name="maxLifetime" value="${hikaricp.maxLifetime}"/>

<property name="maximumPoolSize" value="${hikaricp.maximumPoolSize}"/>

<property name="minimumIdle" value="${hikaricp.minimumIdle}"/>

<property name="initializationFailFast" value="${hikaricp.isInitializationFailFast}"/>

<property name="autoCommit" value="${hikaricp.isAutoCommit}"/>

<property name="readOnly" value="${hikaricp.isReadOnly}" />

<property name="poolName" value="${hikaricp.poolName}"/>

</bean>

</constructor-arg>

</bean>

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: