Springboot 2.3.2 多数据源和分库分表的Sharding-JDBC 4.1.1解决方案

研发背景

有这个的使用场景调研,遇到MySQL数据库的数据量大,希望分表,希望系统能自动处理分表数据,还有希望实现读写分离的业务需求,又不想使用Mycat类的中间件,希望直接能通过代码实现,那么现在属于Apache的顶级项目的ShardingSpheres也许可以帮助到你。

代码示例

1.加依赖

<dependencies>
        <dependency>
            <groupId>vip.mategroupId>
            <artifactId>mate-starter-commonartifactId>
        dependency>
        
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-boot-starterartifactId>
        dependency>
        <dependency>
            <groupId>com.baomidougroupId>
            <artifactId>mybatis-plus-extensionartifactId>
        dependency>
        
        <dependency>
            <groupId>com.alibabagroupId>
            <artifactId>druid-spring-boot-starterartifactId>
        dependency>
        
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
        dependency>
        <dependency>
            <groupId>org.apache.shardingspheregroupId>
            <artifactId>sharding-jdbc-spring-boot-starterartifactId>
        dependency>
    dependencies>

2.写代码

package vip.mate.core.sharding.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import vip.mate.core.common.factory.YamlPropertySourceFactory;

@Configuration
@PropertySource(factory = YamlPropertySourceFactory.class, value = "classpath:mate-sharding-db.yml")
public class DataSourceConfiguration {
}

以上代码是读取Yaml的配置文件

3.写配置

以下是yml的配置文件内容

db:
  one: primary
  two: secondary
spring:
  shardingsphere:
    #配置多数据源
    datasource:
      names: ${db.one}, ${db.two}
      #第一个数据源
      primary:
        type: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://127.0.0.1:3306/matex1?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
        username: root
        password: root
        max-active: 16
      #第二个数据源
      secondary:
        type: com.mysql.cj.jdbc.Driver
        jdbc-url: jdbc:mysql://127.0.0.1:3306/matex2?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
        username: root
        password: root
        max-active: 16
    #配置分片规则,参见https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/configuration/yaml/sharding/
    sharding:
      tables:
        mate_sys_log:
          actual-data-nodes: ${db.one}.mate_sys_log_$->{1..8}
          table-strategy:
            inline:
              sharding-column: today
              algorithm-expression: mate_sys_log_$->{today}
          key-generator:
            column: id
            type: SNOWFLAKE
        #不分表的策略
        mate_sys_user:
          actual-data-nodes: ${db.two}.mate_sys_user

官方文档说明

https://shardingsphere.apache.org/document/current/cn/user-manual/shardingsphere-jdbc/configuration/spring-boot-starter/

项目示例

https://github.com/matevip/matecloud/tree/master/mate-core/mate-starter-sharding

你可能感兴趣的:(微服务,mysql,mybatis,spring,sharding,spring,boot)