Spring Boot+Druid多数据源配置

[toc]

依赖版本

Spring Boot:1.5.18

druid-spring-boot-starter:1.1.10

参考文档

druid-spring-boot-starter

pom配置



    4.0.0

    com.mytest
    druid
    0.0.1-SNAPSHOT

    

        3.15.1
        5.1.41
        1.1.10
    

    
        org.springframework.boot
        spring-boot-starter-parent
        1.5.18.RELEASE
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        
        
        
            org.springframework.boot
            spring-boot-configuration-processor
            true
        
        
        
            org.springframework.boot
            spring-boot-devtools
            true
        
        
        
            org.springframework.boot
            spring-boot-starter-jdbc
        
        
        
            com.alibaba
            druid-spring-boot-starter
            ${druid-starter.version}
        

  
        
        
            org.springframework.boot
            spring-boot-starter-test
        
    
        
            org.xerial
            sqlite-jdbc
            ${sqlite.version}
            runtime
        


        
            mysql
            mysql-connector-java
            ${mysql.version}
            runtime
        

        
            org.springframework.boot
            spring-boot-starter-data-jpa
        
    


    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            

            
                org.apache.maven.plugins
                maven-compiler-plugin
                3.8.0
                
                    1.7
                    1.7
                    UTF-8
                
            
        
    

springboot配置文件

spring.application.name=druid_test

# 数据源配置
# 数据源 one
spring.datasource.druid.one.url=jdbc:mysql://127.0.0.1:3306/one?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false
spring.datasource.druid.one.username=root
spring.datasource.druid.one.password=root
spring.datasource.druid.one.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.one.initial-size=5
spring.datasource.druid.one.min-idle=15
spring.datasource.druid.one.max-active=60
spring.datasource.druid.one.validation-query=SELECT 1
spring.datasource.druid.one.test-on-borrow=true
spring.datasource.druid.one.test-while-idle=true
spring.datasource.druid.one.time-between-eviction-runs-millis=60000
#数据源 two
spring.datasource.druid.two.url=jdbc:mysql://127.0.0.1:3306/two?useUnicode=true&characterEncoding=utf8&autoReconnect=true&useSSL=false
spring.datasource.druid.two.username=root
spring.datasource.druid.two.password=root
spring.datasource.druid.two.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.druid.two.initial-size=5
spring.datasource.druid.two.min-idle=15
spring.datasource.druid.two.max-active=60
spring.datasource.druid.two.validation-query=SELECT 1
spring.datasource.druid.two.test-on-borrow=true
spring.datasource.druid.two.test-while-idle=true
spring.datasource.druid.two.time-between-eviction-runs-millis=60000


# 合并多个datasource监控
spring.datasource.druid.use-global-data-source-stat=true
# WebStatFilter配置,说明请参考Druid Wiki,配置_配置WebStatFilter
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.web-stat-filter.url-pattern=/*
spring.datasource.druid.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
#spring.datasource.druid.web-stat-filter.session-stat-enable=
#spring.datasource.druid.web-stat-filter.session-stat-max-count=
#spring.datasource.druid.web-stat-filter.principal-session-name=
#spring.datasource.druid.web-stat-filter.principal-cookie-name=
#spring.datasource.druid.web-stat-filter.profile-enable=

# StatViewServlet配置,说明请参考Druid Wiki,配置_StatViewServlet配置
spring.datasource.druid.stat-view-servlet.enabled=true
spring.datasource.druid.stat-view-servlet.url-pattern=/admin/druid/*
spring.datasource.druid.stat-view-servlet.reset-enable=false
#spring.datasource.druid.stat-view-servlet.login-username=admin
#spring.datasource.druid.stat-view-servlet.login-password=admin
#spring.datasource.druid.stat-view-servlet.allow=
#spring.datasource.druid.stat-view-servlet.deny=

# Spring监控配置,说明请参考Druid Github Wiki,配置_Druid和Spring关联监控配置
#spring.datasource.druid.aop-patterns= # Spring监控AOP切入点,如x.y.z.service.*,配置多个英文逗号分隔

配置注入


import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceBuilder;
import org.slf4j.Logger;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.sql.DataSource;

@Configuration
public class DataSourceConfigurer {
    private static final Logger log = org.slf4j.LoggerFactory.getLogger(DataSourceConfigurer.class);
    @Primary
    @Bean(value = "dataSourceOne",initMethod = "init")
    @ConfigurationProperties("spring.datasource.druid.one")
    public DataSource dataSourceOne(){
        log.info("Init DataSourceOne");
        return DruidDataSourceBuilder.create().build();
    }

    @Bean(value = "dataSourceTwo",initMethod = "init")
    @ConfigurationProperties("spring.datasource.druid.two")
    public DataSource dataSourceTwo(){
        log.info("Init DataSourceTwo");
        return DruidDataSourceBuilder.create().build();
    }
}

你可能感兴趣的:(Spring Boot+Druid多数据源配置)