美文网首页
springboot项目配置多数据源

springboot项目配置多数据源

作者: 初心myp | 来源:发表于2019-04-09 10:09 被阅读0次

    我们一起看一下多数据源的配置:(我这里配置的是主数据源是MySQL,第二数据源是Oracle)
    业务场景:我们的主数据来自Oracle的数据库里面,然后我们的一些业务数据来自于MySQL数据库,Oracle数据库只提供了一个视图供我们使用。

    先看一下application.yml中的配置:

    #mysql配置
    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://127.0.0.1:3306/asset_inventory_prod?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&sessionVariables=sql_mode='STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'&useSSL=false&useCursorFetch=true
        username: asset_inventory_prod
        password: Asset_Inventory_Prod@123
        type: com.alibaba.druid.pool.DruidDataSource
        # 连接池配置:
        filters: stat
        minIdle: 50
        maxActive: 700
        initialSize: 10
        # 等待获取连接的时间
        maxWait: 5000
        # 连接保持空闲而不被驱逐的最小时间
        minEvictableIdleTimeMillis: 60000
        # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
        timeBetweenEvictionRunsMillis: 30000
        # 测试连接有效性
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        keepAlive: false
        # 指定获取连接时连接校验的sql查询语句
        validationQuery: SELECT 1 FROM DUAL
        # 启用ps
        poolPreparedStatements: false
        maxPoolPreparedStatementPerConnectionSize: -1
        maxOpenPreparedStatements: -1
        # 记录被判定为异常的连接
        logAbandoned: true
    #Oracle配置
    oracle:
      jdbc:
        url: jdbc:oracle:thin:@127.0.0.1:8031:EAMPROD
        username: ASSET_INV
        password: asset_inv_prod
        driver: oracle.jdbc.driver.OracleDriver
    

    接下来我们看一下需要的配置类。数据源配置类里面需要配置mysql和Oracle两种

    import com.alibaba.druid.pool.DruidDataSource;
    import com.alibaba.druid.support.http.StatViewServlet;
    import com.alibaba.druid.support.http.WebStatFilter;
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.web.servlet.FilterRegistrationBean;
    import org.springframework.boot.web.servlet.ServletRegistrationBean;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    
    import javax.sql.DataSource;
    import java.sql.SQLException;
    
    @Slf4j
    @Configuration
    public class DruidDBConfig {
        @Value("${spring.datasource.url}")
        private String dbUrl;
    
        @Value("${spring.datasource.username}")
        private String username;
    
        @Value("${spring.datasource.password}")
        private String password;
    
        @Value("${spring.datasource.driver-class-name}")
        private String driverClassName;
    
        @Value("${spring.datasource.initialSize}")
        private int initialSize;
    
        @Value("${spring.datasource.minIdle}")
        private int minIdle;
    
        @Value("${spring.datasource.maxActive}")
        private int maxActive;
    
        @Value("${spring.datasource.maxWait}")
        private int maxWait;
    
        @Value("${spring.datasource.timeBetweenEvictionRunsMillis}")
        private int timeBetweenEvictionRunsMillis;
    
        @Value("${spring.datasource.minEvictableIdleTimeMillis}")
        private int minEvictableIdleTimeMillis;
    
        @Value("${spring.datasource.validationQuery}")
        private String validationQuery;
    
        @Value("${spring.datasource.testWhileIdle}")
        private boolean testWhileIdle;
    
        @Value("${spring.datasource.testOnBorrow}")
        private boolean testOnBorrow;
    
        @Value("${spring.datasource.testOnReturn}")
        private boolean testOnReturn;
    
        @Value("${spring.datasource.poolPreparedStatements}")
        private boolean poolPreparedStatements;
    
        @Value("${spring.datasource.maxPoolPreparedStatementPerConnectionSize}")
        private int maxPoolPreparedStatementPerConnectionSize;
    
        @Value("${spring.datasource.filters}")
        private String filters;
    
    
        @Value("${oracle.jdbc.url}")
        private String oracleUrl;
    
        @Value("${oracle.jdbc.driver}")
        private String oracleDriver;
    
        @Value("${oracle.jdbc.username}")
        private String oracleUserName;
    
        @Value("${eam.jdbc.password}")
        private String oraclePassword;
    
    
        @Bean(name = "primaryDataSource")
        @Qualifier("primaryDataSource")
        @Primary
        public DataSource dataSource() {
            DruidDataSource datasource = new DruidDataSource();
    
            datasource.setUrl(this.dbUrl);
            datasource.setUsername(username);
            datasource.setPassword(password);
            datasource.setDriverClassName(driverClassName);
    
            //configuration
            datasource.setInitialSize(initialSize);
            datasource.setMinIdle(minIdle);
            datasource.setMaxActive(maxActive);
            datasource.setMaxWait(maxWait);
            datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
            datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
            datasource.setValidationQuery(validationQuery);
            datasource.setTestWhileIdle(testWhileIdle);
            datasource.setTestOnBorrow(testOnBorrow);
            datasource.setTestOnReturn(testOnReturn);
            datasource.setPoolPreparedStatements(poolPreparedStatements);
            datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
            datasource.setRemoveAbandoned(true);
            try {
                datasource.setFilters(filters);
            } catch (SQLException e) {
                log.error("druid configuration initialization filter", e);
            }
            return datasource;
        }
    
        @Bean
        public ServletRegistrationBean druidServlet() {
            return new ServletRegistrationBean(new StatViewServlet(), "/druid/*");
        }
    
        @Bean
        public FilterRegistrationBean filterRegistrationBean() {
            FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
            filterRegistrationBean.setFilter(new WebStatFilter());
            filterRegistrationBean.addUrlPatterns("/*");
            filterRegistrationBean.addInitParameter("exclusions",
                    "*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
            return filterRegistrationBean;
        }
    
        /**
         * <p>Description: [配置oracle数据源, 使用时在方法上添加注解:@Transient,即可指定使用oracle数据源]</p>
         * Created on: 2019-1-15 14:52:42
         * @return DataSource
         * @author muyuanpei
         */
        @Bean(name = "secondaryDataSource")
        @Qualifier("secondaryDataSource")
        public DataSource secondaryDataSource() {
            DruidDataSource datasource = new DruidDataSource();
    
            datasource.setUrl(this.oracleUrl);
            datasource.setUsername(oracleUserName);
            datasource.setPassword(oraclePassword);
            datasource.setDriverClassName(oracleDriver);
    
            //configuration
            datasource.setInitialSize(initialSize);
            datasource.setMinIdle(minIdle);
            datasource.setMaxActive(maxActive);
            datasource.setMaxWait(maxWait);
            datasource.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
            datasource.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
            datasource.setValidationQuery(validationQuery);
            datasource.setTestWhileIdle(testWhileIdle);
            datasource.setTestOnBorrow(testOnBorrow);
            datasource.setTestOnReturn(testOnReturn);
            datasource.setPoolPreparedStatements(poolPreparedStatements);
            datasource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
            datasource.setRemoveAbandoned(true);
            try {
                datasource.setFilters(filters);
            } catch (SQLException e) {
                log.error("druid configuration initialization filter", e);
            }
            return datasource;
        }
    }
    

    接下来写一下每个数据源需要的配置类:

    主数据源mysql的配置类
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.SqlSessionTemplate;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.Primary;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    import org.springframework.transaction.PlatformTransactionManager;
    
    import javax.sql.DataSource;
    
    @Configuration
    @MapperScan(basePackages = "com.chuxin.dao.mapper", sqlSessionTemplateRef = "primarySqlSessionTemplate")
    public class SqlSessionTemplate1 {
    
    
        @Bean(name = "primarySqlSessionFactory")
        @Primary
        public SqlSessionFactory primarySqlSessionFactory(@Qualifier("primaryDataSource") DataSource dataSource)
                throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dataSource);
            bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
            return bean.getObject();
        }
    
        //配置声明式事务管理器
        @Bean(name = "primaryTransactionManager")
        @Primary
        public PlatformTransactionManager primaryTransactionManager(@Qualifier("primaryDataSource") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        @Bean(name = "primarySqlSessionTemplate")
        @Primary
        public SqlSessionTemplate primarySqlSessionTemplate(
                @Qualifier("primarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }
    
    第二数据源配置类:
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.mybatis.spring.SqlSessionFactoryBean;
    import org.mybatis.spring.SqlSessionTemplate;
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.beans.factory.annotation.Qualifier;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
    import org.springframework.jdbc.datasource.DataSourceTransactionManager;
    import org.springframework.transaction.PlatformTransactionManager;
    
    import javax.sql.DataSource;
    
    @Configuration
    @MapperScan(basePackages = "com.chuxin.dao.oracleMapper", sqlSessionTemplateRef = "secondarySqlSessionTemplate")
    public class SqlSessionTemplate2 {
    
        @Bean(name = "secondarySqlSessionFactory")
        public SqlSessionFactory secondarySqlSessionFactory(@Qualifier("secondaryDataSource") DataSource dataSource)
                throws Exception {
            SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
            bean.setDataSource(dataSource);
            bean.setMapperLocations(
                    new PathMatchingResourcePatternResolver().getResources("classpath:oracleMapper/*.xml"));
            return bean.getObject();
        }
    
        @Bean(name = "secondaryTransactionManager")
        public PlatformTransactionManager secondaryTransactionManager(
                @Qualifier("secondaryDataSource") DataSource dataSource) {
            return new DataSourceTransactionManager(dataSource);
        }
    
        @Bean(name = "secondarySqlSessionTemplate")
        public SqlSessionTemplate secondarySqlSessionTemplate(
                @Qualifier("secondarySqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
            return new SqlSessionTemplate(sqlSessionFactory);
        }
    }
    

    通过如上的配置既完成了多数据源的配置。
    在使用的过程中,如果像但数据源那样编码,默认使用的是主数据源,如果需要使用到第二数据源的时候,只需要在服务实现类的方法上面添加注解@Transient即可。
    使用方法如下:

    image.png

    注:我们需要将mapper接口和mapper.xml放在一起
    包结构如下图:

    包结构

    如上所示,就可以完成多数据源的配置!

    另一种方案参考:https://www.jianshu.com/p/dfd5ae340011

    相关文章

      网友评论

          本文标题:springboot项目配置多数据源

          本文链接:https://www.haomeiwen.com/subject/zfpviqtx.html