更新时间:2024-10-23 GMT+08:00
分享

使用多数据源

上面的方式都是只引入一个数据源,在某些场景下,需要引入多个数据库,操作不同的数据库,此时,可以通过配置不同前缀来注入多个datasource的bean,方法如下:

  • 引入依赖。
    <dependency>
        <groupId>com.huaweicloud.devspore</groupId>
        <artifactId>spring-cloud-starter-huawei-devspore-datasource</artifactId>
        <version>${lastest}</version>
    </dependency>
  • 配置文件中配置多个数据源。
    devspore:
       datasource1:
            props:
              version: v1
              appId: xxx
              monitorId: xxx
              databaseName: xxx
              decipherClassName: xxx.xxx.xxx
              region: az0
            etcd:
              address: 127.0.0.2:2379,127.0.0.2:2379,127.0.0.2:2379
              apiVersion: v3
              username: etcduser
              password: etcdpwd
              httpsEnable: false
            sources:
              ds1:
                driverClassName: com.mysql.jdbc.Driver
                jdbcUrl: jdbc:mysql://127.0.0.1:3306/ds0
                username: datasourceuser
                password: datasourcepwd
                type: com.zaxxer.hikari.HikariDataSource
              ds2:
                driverClassName: com.mysql.jdbc.Driver
                jdbcUrl: jdbc:mysql://127.0.0.1:3306/ds1
                username: datasourceuser
                password: datasourcepwd
                type: com.zaxxer.hikari.HikariDataSource
            router:
              active: dc1
              routeAlgorithm: single-read-write 
              nodes:
                dc1:  
                  master: ds1
                dc2:  
                  master: ds1
        datasource2:
            props:
              version: v1
              appId: xxx
              monitorId: xxx
              databaseName: xxx
              decipherClassName: xxx.xxx.xxx
              region: az0
            etcd:
              address: 127.0.0.2:2379,127.0.0.2:2379,127.0.0.2:2379
              apiVersion: v3
              username: etcduser
              password: etcdpwd
              httpsEnable: false
            sources:
              ds1:
                driverClassName: com.mysql.jdbc.Driver
                jdbcUrl: jdbc:mysql://127.0.0.1:3306/ds0
                username: datasourceuser
                password: datasourcepwd
                type: com.zaxxer.hikari.HikariDataSource
              ds2:
                driverClassName: com.mysql.jdbc.Driver
                jdbcUrl: jdbc:mysql://127.0.0.1:3306/ds1
                username: datasourceuser
                password: datasourcepwd
                type: com.zaxxer.hikari.HikariDataSource
            router:
              active: dc1
              routeAlgorithm: single-read-write 
              nodes:
                dc1:  
                  master: ds1
                dc2:  
                  master: ds1
  • 根据前缀注入多个datasource。

    在配置类中根据yml中的前缀,分别注入bean,并进行命名,然后即可分别操作不同的datasource。

    @Configuration
    public class App {
        @ConfigurationProperties(prefix = "devspore.datasource1")
        @Bean(name = "ds1")
        public DataSource dataSource1() {
            return new ClusterDataSource();
        }
    
        @ConfigurationProperties(prefix = "devspore.datasource2")
        @Bean(name = "ds2")
        public DataSource dataSource2() {
            return new ClusterDataSource();
        }
    }

相关文档