Sharding-jdbc 5.1.1分库分表配置

1,520 阅读1分钟

所有的配置要学会查看官方文档,怎么把文档和源码对应起来

spring-boot 版本

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.6.3</version>
    <relativePath/>
  </parent>

maven 依赖

 <properties>
    <maven.compiler.source>8</maven.compiler.source>
    <maven.compiler.target>8</maven.compiler.target>
    <shardingsphere.version>5.1.1</shardingsphere.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.apache.shardingsphere</groupId>
      <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
      <version>${shardingsphere.version}</version>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
  </dependencies>

场景一: 订单根据user_id 进行分库,不分表

spring:
  jpa:
    hibernate:
      ddl-auto: update
      naming:
        physical-strategy: org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
    show-sql: true
    generate-ddl: true
    open-in-view: false
    database-platform: org.hibernate.dialect.MySQL5InnoDBDialect
    properties:
      hibernate:
        generate_statistics: false
        jdbc:
          batch_size: 100
        order_inserts: true
  shardingsphere:
    datasource:
      names: ds0,ds1
      ds0:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.jdbc.Driver
        jdbc-url: jdbc:mysql://127.0.0.1:3306/order_1?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&allowPublicKeyRetrieval=true
        username: root
        password: root
      ds1:
        type: com.zaxxer.hikari.HikariDataSource
        driver-class-name: com.mysql.jdbc.Driver
        jdbc-url: jdbc:mysql://127.0.0.1:3306/order_2?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false&allowPublicKeyRetrieval=true
        username: root
        password: root
    mode:
      type: Memory
    rules:
      sharding:
        tables:
          t_order:
            actual-data-nodes: ds$->{0..1}.t_order
            key-generate-strategy:
              column: id
              key-generator-name: snowflake
            database-strategy:
              standard:
                sharding-column: user_id
                sharding-algorithm-name: database-inline
            table-strategy:
              none:
          order_item:
            actual-data-nodes: ds$->{0..1}.order_item
            key-generate-strategy:
              column: id
              key-generator-name: snowflake
            database-strategy:
              standard:
                sharding-column: user_id
                sharding-algorithm-name: database-inline
            table-strategy:
              none:
        key-generators:
          snowflake:
            type: SNOWFLAKE
        sharding-algorithms:
          database-inline:
            type: INLINE
            props:
              algorithm-expression: ds$->{user_id % 2}
        binding-tables:
          - t_order,order_item
    props:
      sql-show: true

  application:
    name: sharding_demo
server:
  port: 8099

文档和源码怎么看

使用spring-boot-starter 的方式进行集成

文档只需要看

关注文档.jpg

最外层属性配置

属性配置.jpg

对应源码

@ConfigurationProperties(
  prefix = "spring.shardingsphere"
)
public final class SpringBootPropertiesConfiguration {
  private Properties props = new Properties();
  private YamlModeConfiguration mode;

  public SpringBootPropertiesConfiguration() {
  }

  @Generated
  public Properties getProps() {
    return this.props;
  }

  @Generated
  public YamlModeConfiguration getMode() {
    return this.mode;
  }

  @Generated
  public void setProps(Properties props) {
    this.props = props;
  }

  @Generated
  public void setMode(YamlModeConfiguration mode) {
    this.mode = mode;
  }
}

规则配置文档

规则配置.jpg

规则配置关注源码

@ConfigurationProperties(
  prefix = "spring.shardingsphere.rules"
)
public final class YamlShardingRuleSpringBootConfiguration {
  private YamlShardingRuleConfiguration sharding;

  public YamlShardingRuleSpringBootConfiguration() {
  }

  @Generated
  public YamlShardingRuleConfiguration getSharding() {
    return this.sharding;
  }

  @Generated
  public void setSharding(YamlShardingRuleConfiguration sharding) {
    this.sharding = sharding;
  }
}

实体类

@Entity
@Table(name = "t_order")
@Data
public class Order {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private String orderSource;

  private BigDecimal money;

  private Long userId;
}

@Entity
@Table(name = "order_item")
@Data
public class OrderItem {

  @Id
  @GeneratedValue(strategy = GenerationType.IDENTITY)
  private Long id;

  private Integer itemCount;

  private Long orderId;

  private Long userId;

}

官网中properties 文件 转为yml

在线yaml转properties-在线properties转yaml-ToYaml.com