springboot 不同数据库多数据源

现在有个项目要多数据库,多数据源,oracle两个数据连接,sqlserver1个,用yml怎么配置druid啊

阅读 649
评论
    1 个回答

    之前写的多数据源的demo,你可以参考一下


    配置文件

    spring.datasource.primary.type=com.alibaba.druid.pool.DruidDataSource
    spring.datasource.primary.url=jdbc:mysql://localhost:3306/dt_screen_3?useUnicode=true&characterEncoding=utf-8&useSSL=false
    spring.datasource.primary.username=root
    spring.datasource.primary.password=password
    spring.datasource.primary.driver-class-name=com.mysql.jdbc.Driver
    
    spring.datasource.primary.initial-size=10
    spring.datasource.primary.max-active=100
    spring.datasource.primary.max-idle=8
    spring.datasource.primary.maximum-pool-size=100
    spring.datasource.primary.max-wait=100000
    spring.datasource.primary.validation-query=SELECT 1
    spring.datasource.primary.test-on-borrow=true
    spring.datasource.primary.test-while-idle=true
    spring.datasource.primary.time-between-eviction-runs-millis=18800
    spring.datasource.primary.pool-name=SpringBootHikariCP
    spring.datasource.primary.max-lifetime=2000000
    spring.datasource.primary.connection-timeout=30000
    
    
    spring.datasource.secondary.type=com.alibaba.druid.pool.DruidDataSource
    spring.datasource.secondary.url=jdbc:mysql://172.16.10.*:3306/reportdb?useUnicode=true&characterEncoding=utf-8&useSSL=false
    spring.datasource.secondary.username=root
    spring.datasource.secondary.password=password
    spring.datasource.secondary.driver-class-name=com.mysql.jdbc.Driver
    spring.datasource.secondary.initial-size=10
    spring.datasource.secondary.max-active=100
    spring.datasource.secondary.max-idle=8
    spring.datasource.secondary.maximum-pool-size=100
    spring.datasource.secondary.max-wait=100000
    
    #mysql
    spring.datasource.secondary.validation-query=SELECT 
    #oracle
    #spring.datasource.secondary.validation-query=select 1 from dual
    
    spring.datasource.secondary.test-on-borrow=true
    spring.datasource.secondary.test-while-idle=true
    spring.datasource.secondary.time-between-eviction-runs-millis=18800
    spring.datasource.secondary.pool-name=SpringBootHikariCP
    spring.datasource.secondary.max-lifetime=2000000
    spring.datasource.secondary.connection-timeout=30000
    spring.datasource.secondary.continue-on-error=true

    DataSourceConfiguration.java

    @Configuration
    public class DataSourceConfiguration {
    
        @Bean(name = "primaryDataSource")
        @Qualifier("primaryDataSource")
        @Primary
        @ConfigurationProperties(prefix = "spring.datasource.primary")
        public DataSource primaryDataSource() {
            return DataSourceBuilder.create().build();
        }
    
        @Bean(name = "secondaryDataSource")
        @Qualifier("secondaryDataSource")
        @ConfigurationProperties(prefix = "spring.datasource.secondary")
        public DataSource secondaryDataSource() {
            return DataSourceBuilder.create().build();
        }
    
        @Bean(name = "primaryJdbcTemplate")
        public JdbcTemplate primaryJdbcTemplate(
                @Qualifier("primaryDataSource") DataSource dataSource) {
            return new JdbcTemplate(dataSource);
        }
    
        @Bean(name = "secondaryJdbcTemplate")
        public JdbcTemplate secondaryJdbcTemplate(
                @Qualifier("secondaryDataSource") DataSource dataSource) {
            return new JdbcTemplate(dataSource);
        }
    }
      撰写回答

      登录后参与交流、获取后续更新提醒