与dynamic-datasource整合

PR参考

baomidou的dynamic-datasource团队提交了一个相同需求的PRopen in new window

原理

  • 通过 DataSourceConfig.javaShardingJDBC 数据源配置为动态数据源之一。
  • 通过 @DS(DataSourceConfig.SHARDING_DATA_SOURCE_NAME) 使用 ShardingJDBC 的数据源进行分表操作

代码示例

依赖

<dependency>
  <groupId>com.baomidou</groupId>
  <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid</artifactId>
    <version>1.2.8</version>
</dependency>

DataSourceConfiguration配置

/**
 * @Description 动态数据源配置
 (切换为sharding-jdbc数据源 => @DS(DataSourceConfig.SHARDING_DATA_SOURCE_NAME))
 */
@Configuration
@AutoConfigureBefore({DynamicDataSourceAutoConfiguration.class,SpringBootConfiguration.class})
public class DataSourceConfiguration {
    
	/**
     * 分表数据源名称
     */
    private static final String SHARDING_DATA_SOURCE_NAME = "sharding-data-source";

    /**
     * 动态数据源配置项
     */
    @Autowired
    private DynamicDataSourceProperties properties;

    /**
     * shardingjdbc有四种数据源,需要根据业务注入不同的数据源
     *
     * 1. 未使用分片, 脱敏的名称(默认): shardingDataSource;
     * 2. 主从数据源: masterSlaveDataSource;
     * 3. 脱敏数据源:encryptDataSource;
     * 4. 影子数据源:shadowDataSource
     *
     */
    @Lazy
    @Resource(name = "shardingDataSource")
    AbstractDataSourceAdapter shardingDataSource;

    @Bean
    public DynamicDataSourceProvider dynamicDataSourceProvider() {
        Map<String, DataSourceProperty> datasourceMap = properties.getDatasource();
        return new AbstractDataSourceProvider() {
            @Override
            public Map<String, DataSource> loadDataSources() {
                Map<String, DataSource> dataSourceMap = createDataSourceMap(datasourceMap);
                // 将 shardingjdbc 管理的数据源也交给动态数据源管理
                dataSourceMap.put(SHARDING_DATA_SOURCE_NAME, shardingDataSource);
                return dataSourceMap;
            }
        };
    }

    /**
     * 将动态数据源设置为首选的
     * 当spring存在多个数据源时, 自动注入的是首选的对象
     * 设置为主要的数据源之后,就可以支持shardingjdbc原生的配置方式了
     *
     * @return
     */
    @Primary
    @Bean
    public DataSource dataSource(DynamicDataSourceProvider dynamicDataSourceProvider) {
        DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource();
        dataSource.setPrimary(properties.getPrimary());
        dataSource.setStrict(properties.getStrict());
        dataSource.setStrategy(properties.getStrategy());
        /**
         * 动态多数据源提供者,
         * 在DynamicRoutingDataSource需要要DynamicDataSourceProvider加载进去,
         * 否则会导致datasource注入失败,抛出空指针异常
         */
        dataSource.setProvider(dynamicDataSourceProvider);
        dataSource.setP6spy(properties.getP6spy());
        dataSource.setSeata(properties.getSeata());
        return dataSource;
    }
}

yml配置

server:
  port: 8081

spring:
  # 多数据源配置
  datasource:
    dynamic:
      primary: mydb1
      datasource:
        mydb1:
          url: jdbc:mysql://localhost:3306/mydb1?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
          username: root
          password: root
          driver-class-name: com.mysql.cj.jdbc.Driver
  # sharding-jdbc配置
  shardingsphere:
    # 打印sql
    props:
      sql:
        show: true
    datasource:
      names: mydb2
      mydb2:
        type: com.alibaba.druid.pool.DruidDataSource
        url: jdbc:mysql://localhost:3306/mydb2?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
        driver-class-name: com.mysql.cj.jdbc.Driver
        username: root
        password: root
        # 数据源其他配置
        initialSize: 5
        minIdle: 5
        maxActive: 20
        maxWait: 60000
        timeBetweenEvictionRunsMillis: 60000
        minEvictableIdleTimeMillis: 300000
        validationQuery: SELECT 1 FROM DUAL
        testWhileIdle: true
        testOnBorrow: false
        testOnReturn: false
        poolPreparedStatements: true
        # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
        #filters: stat,wall,log4j
        maxPoolPreparedStatementPerConnectionSize: 20
        useGlobalDataSourceStat: true
        connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=500
    sharding:
      # 表策略配置
      tables:
        # t_user 是逻辑表
        t_user:
          # 分表节点 可以理解为分表后的那些表 比如 t_user_1 ,t_user_2
          actualDataNodes: mydb2.t_user_$->{1..2}
          tableStrategy:
            inline:
              # 根据哪列分表
              shardingColumn: age
              # 分表算法 例如:age为奇数 -> t_user_2; age为偶数 -> t_user_1
              algorithmExpression: t_user_$->{age % 2 + 1}
#              keyGenerator:
#                type: SNOWFLAKE
#                # 对id列采用 sharding-jdbc的全局id生成策略
#                column: id
# mybatis-plus
mybatis-plus:
  mapper-locations: classpath*:/mapper/*Mapper.xml
  # 实体扫描,多个package用逗号或者分号分隔
  typeAliasesPackage: cn.agile.stats.*.entity
  # 测试环境打印sql
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

使用示例

public interface TUserService extends IService<TUser> {

    /**
     * 查询 全部用户(mydb1数据库)
     * @return 全部用户
     */
    List<TUser> listFromDB1();
}


@Service
@DS(DataSourceConfig.SHARDING_DATA_SOURCE_NAME)
public class TUserServiceImpl extends ServiceImpl<TUserMapper, TUser> implements TUserService {

    @DS("mydb1")
    @Override
    public List<TUser> listFromDB1() {
        // 查询 全部用户(mydb1数据库)
        return this.list();
    }
}
@SpringBootTest
class SpringbootDemoApplicationTests {

    @Autowired
    private TUserService userService;

    @Test
    void saveTest() {
        List<TUser> users = new ArrayList<>(3);
        users.add(new TUser("Ajia_1", "111111", 10));
        users.add(new TUser("Ajia_2", "222222", 20));
        users.add(new TUser("Ajia_3", "333333", 30));
        userService.saveBatch(users);
    }

    @Test
    void listTest() {
        List<TUser> users1 = userService.listFromDB1();
        // 查询没有数据插入的mydb1,没有查到数据
        System.out.println("------ 【Result1】------ ");
        users1.forEach(System.out::println);
        List<TUser> users2 = userService.list();
        // 查询插入了 3 条数据的mydb2,查询到了 3 条
        System.out.println("------ 【Result2】------ ");
        users2.forEach(System.out::println);
    }

}
Last Updated 2024/5/24 16:21:58