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

以前项目使用的是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>


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Spring Boot中,配置Druid多数据源有以下几个步骤: 1. 引入依赖:在`pom.xml`文件中,引入Druid和jdbc依赖。 ```xml <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.4</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> ``` 2. 配置数据源信息:在`application.properties`或`application.yml`配置文件中,配置Druid数据源相关信息,包括数据库URL、用户名、密码等。 ```properties # 主数据源 spring.datasource.url=jdbc:mysql://localhost:3306/main_db?useUnicode=true&characterEncoding=UTF-8 spring.datasource.username=root spring.datasource.password=root spring.datasource.driver-class-name=com.mysql.jdbc.Driver # 第二个数据源 spring.second-datasource.url=jdbc:mysql://localhost:3306/second_db?useUnicode=true&characterEncoding=UTF-8 spring.second-datasource.username=root spring.second-datasource.password=root spring.second-datasource.driver-class-name=com.mysql.jdbc.Driver ``` 3. 配置多数据源:在`@Configuration`类中,配置多个Druid数据源,并将其注入到`DataSource`对象中。 ```java @Configuration public class DataSourceConfig { @Bean(name = "mainDataSource") @ConfigurationProperties(prefix = "spring.datasource") public DataSource mainDataSource() { return DruidDataSourceBuilder.create().build(); } @Bean(name = "secondDataSource") @ConfigurationProperties(prefix = "spring.second-datasource") public DataSource secondDataSource() { return DruidDataSourceBuilder.create().build(); } } ``` 4. 配置事务管理器:在`@Configuration`类中,配置多数据源的事务管理器,用于管理多个数据源的事务。 ```java @Configuration public class TransactionManagerConfig { @Autowired @Qualifier("mainDataSource") private DataSource mainDataSource; @Autowired @Qualifier("secondDataSource") private DataSource secondDataSource; @Bean public PlatformTransactionManager mainTransactionManager() { return new DataSourceTransactionManager(mainDataSource); } @Bean public PlatformTransactionManager secondTransactionManager() { return new DataSourceTransactionManager(secondDataSource); } } ``` 5. 使用多数据源:在需要使用的地方,使用注解来指定使用哪个数据源。 ```java @Service public class UserService { @Autowired @Qualifier("mainDataSource") private JdbcTemplate mainJdbcTemplate; @Autowired @Qualifier("secondDataSource") private JdbcTemplate secondJdbcTemplate; public List<User> getAllUsersFromMainDataSource() { return mainJdbcTemplate.query("SELECT * FROM users", new BeanPropertyRowMapper(User.class)); } public List<User> getAllUsersFromSecondDataSource() { return secondJdbcTemplate.query("SELECT * FROM users", new BeanPropertyRowMapper(User.class)); } } ``` 通过以上步骤,我们就成功配置了Druid多数据源,并且可以在代码中灵活地使用不同的数据源

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值