Sharding Sphere3.1.0下的springboot+Hikari

A, pom-dependent

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
<dependency>
      <groupId>io.shardingsphere</groupId>
      <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
      <version>3.1.0</version>
 </dependency>

First, the configuration file

#数据库名称不可以带下划线:_,默认识别第一个DB
sharding.jdbc.datasource.names=springcloud,ds0,ds1

sharding.jdbc.datasource.springcloud.type=com.zaxxer.hikari.HikariDataSource
sharding.jdbc.datasource.springcloud.driver-class-name=com.mysql.cj.jdbc.Driver
sharding.jdbc.datasource.springcloud.jdbcUrl=jdbc:mysql://localhost:3306/springcloud
sharding.jdbc.datasource.springcloud.username=root
sharding.jdbc.datasource.springcloud.password=xiaochao

#数据库名称不可以带下划线:_,ds0可以,ds_0不可以,报错
sharding.jdbc.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource
sharding.jdbc.datasource.ds0.driver-class-name=com.mysql.cj.jdbc.Driver
sharding.jdbc.datasource.ds0.jdbcUrl=jdbc:mysql://localhost:3306/ds_0
sharding.jdbc.datasource.ds0.username=root
sharding.jdbc.datasource.ds0.password=xiaochao

#数据库名称不可以带下划线:_,ds1可以,ds_1不可以,报错
sharding.jdbc.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource
sharding.jdbc.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driver
sharding.jdbc.datasource.ds1.jdbcUrl=jdbc:mysql://localhost:3306/ds_1
sharding.jdbc.datasource.ds1.username=root
sharding.jdbc.datasource.ds1.password=xiaochao

#这个配置是啥有待验证
sharding.jdbc.config.sharding.default-database-strategy.inline.sharding-column=user_id
sharding.jdbc.config.sharding.default-database-strategy.inline.algorithm-expression=ds$->{user_id % 2}

#数据库名称不可以带下划线:_,ds$可以,ds_$不可以,报错
sharding.jdbc.config.sharding.tables.t_order.actual-data-nodes=ds$->{0..1}.t_order_$->{0..1}
sharding.jdbc.config.sharding.tables.t_order.table-strategy.inline.sharding-column=order_id
sharding.jdbc.config.sharding.tables.t_order.table-strategy.inline.algorithm-expression=t_order_$->{order_id % 2}
sharding.jdbc.config.sharding.tables.t_order.key-generator-column-name=order_id
sharding.jdbc.config.sharding.tables.t_order_item.actual-data-nodes=ds$->{0..1}.t_order_item_$->{0..1}
sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.sharding-column=order_id
sharding.jdbc.config.sharding.tables.t_order_item.table-strategy.inline.algorithm-expression=t_order_item_$->{order_id % 2}
sharding.jdbc.config.sharding.tables.t_order_item.key-generator-column-name=order_item_id

#如果有关联表没配置绑定有可能会出现笛卡尔积
sharding.jdbc.config.sharding.binding-tables[0]=t_order,t_order_item
sharding.jdbc.config.sharding.props.sql.show=true

note:

  1. After the database name can not be underlined names such as: ds_0, otherwise an error
  2. Db0 points made will go to the library sub-table configuration, db1 library to find;
  3. Did not do the first sub-library sub-table will go to a library to find sharding.jdbc.datasource.names: springcloud

SpringBoot using Sharding-JDBC for generating a sub-library and the sub-table ID of the distributed

Reproduced in: https: //www.jianshu.com/p/0a08a854f1ea

Guess you like

Origin blog.csdn.net/weixin_33796177/article/details/91173019