美文网首页Spring Bootspringbootalready
Spring Boot 2.4.x多数据源配置

Spring Boot 2.4.x多数据源配置

作者: EasyNetCN | 来源:发表于2021-02-04 09:00 被阅读0次

    Spring官方文档给出了多数据配置示例(https://docs.spring.io/spring-boot/docs/2.4.2/reference/htmlsingle/#howto-two-datasources),但是比如用默认的HikariCP(https://github.com/brettwooldridge/HikariCP),对一些优化配置参数并不能生效。

    默认单数据源配置

    spring: 
      datasource:
        type: com.zaxxer.hikari.HikariDataSource
        hikari:
          data-source-properties:
            cachePrepStmts: true
            prepStmtCacheSize: 500
            prepStmtCacheSqlLimit: 2048
            useServerPrepStmts: true
            rewriteBatchedStatements: true
            cacheResultSetMetadata: true
            cacheServerConfiguration: true
            elideSetAutoCommits: true
            maintainTimeStats: false
    

    下面的例子,提供了多数据源情况下,如何配置HikariCP的DataSourceProperties。

    自定义HikariCP的DataSourceProperties的Configuration Properties

    import java.util.Map;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    
    @ConfigurationProperties(prefix = "spring.datasource.hikari")
    public class HikariProperties {
        private Map<String, Object> dataSourceProperties;
    
        public Map<String, Object> getDataSourceProperties() {
            return dataSourceProperties;
        }
    
        public void setDataSourceProperties(Map<String, Object> dataSourceProperties) {
            this.dataSourceProperties = dataSourceProperties;
        }
    }
    

    在Application中激活上面的配置

    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.cache.annotation.EnableCaching;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.web.reactive.function.client.WebClient;
    
    import cn.ydyun360.common.config.HikariProperties;
    
    @EnableDiscoveryClient
    @SpringBootApplication
    @EnableCaching
    @EnableConfigurationProperties(HikariProperties.class)
    @ComponentScan("")
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
    }
    

    job数据源配置

    import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
    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 org.springframework.jdbc.core.JdbcTemplate;
    
    import com.zaxxer.hikari.HikariDataSource;
    
    import cn.ydyun360.common.config.HikariProperties;
    
    @Configuration
    public class JobJpaConfig {
        @Bean
        @ConfigurationProperties("spring.datasource.job")
        @Primary
        public DataSourceProperties jobDataSourceProperties() {
            return new DataSourceProperties();
        }
    
        @Bean(name = "jobDataSource")
        @ConfigurationProperties("spring.datasource.job.configuration")
        @Primary
        public HikariDataSource jobDataSource(HikariProperties hikariProperties) {
            var dataSource = jobDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
    
            for (var entry : hikariProperties.getDataSourceProperties().entrySet()) {
                dataSource.addDataSourceProperty(entry.getKey(), entry.getValue());
            }
    
            return dataSource;
        }
    
        @Bean(name = "jobJdbcTemplate")
        @Primary
        public JdbcTemplate jdbcTemplate(HikariProperties hikariProperties) {
            var ds = jobDataSource(hikariProperties);
    
            return new JdbcTemplate(ds);
        }
    
    }
    

    goods数据源配置

    import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.jdbc.core.JdbcTemplate;
    import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
    
    import com.zaxxer.hikari.HikariDataSource;
    
    import cn.ydyun360.common.config.HikariProperties;
    
    @Configuration
    public class GoodsJpaConfig {
        @Bean
        @ConfigurationProperties("spring.datasource.goods")
        public DataSourceProperties goodsDataSourceProperties() {
            return new DataSourceProperties();
        }
    
        @Bean(name = "goodsDataSource")
        @ConfigurationProperties("spring.datasource.goods.configuration")
        public HikariDataSource goodsDataSource(HikariProperties hikariProperties) {
            var dataSource = goodsDataSourceProperties().initializeDataSourceBuilder().type(HikariDataSource.class).build();
    
            for (var entry : hikariProperties.getDataSourceProperties().entrySet()) {
                dataSource.addDataSourceProperty(entry.getKey(), entry.getValue());
            }
    
            return dataSource;
        }
    
        @Bean(name = "goodsJdbcTemplate")
        public JdbcTemplate jdbcTemplate(HikariProperties hikariProperties) {
            return new JdbcTemplate(goodsDataSource(hikariProperties));
        }
    
        @Bean(name = "goodsNamedParameterJdbcTemplate")
        public NamedParameterJdbcTemplate goodsNamedParameterJdbcTemplate(HikariProperties hikariProperties) {
            return new NamedParameterJdbcTemplate(jdbcTemplate(hikariProperties));
        }
    
    }
    

    配置文件

    spring: 
      datasource:
        type: com.zaxxer.hikari.HikariDataSource
        hikari:
          data-source-properties:
            cachePrepStmts: true
            prepStmtCacheSize: 500
            prepStmtCacheSqlLimit: 2048
            useServerPrepStmts: true
            rewriteBatchedStatements: true
            cacheResultSetMetadata: true
            cacheServerConfiguration: true
            elideSetAutoCommits: true
            maintainTimeStats: false
        job:
          driverClassName: com.mysql.cj.jdbc.Driver
        goods:
          driverClassName: com.mysql.cj.jdbc.Driver
    

    相关文章

      网友评论

        本文标题:Spring Boot 2.4.x多数据源配置

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