SpringBoot整合ShardingSphere

SpringBoot整合ShardingSphere

官网:http://shardingsphere.apache.org/




1、Sharding-JDBC(配置文件可参考官网)

分表

  • 第一步:创建数据库和表

    • 数据库:test
    • 数据表:user_0, user_1
  • 第二步:创建SpringBoot项目并导入依赖(pom.xml)

    <!-- Sharding-JDBC -->
    <dependency></dependency>
        <groupId>org.apache.shardingsphere</groupId>
        <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
       <version>4.0.0-RC1</version>
    </dependency>
    
  • 第三步:编写配置文件(application.properties)(按照自己的环境修改

    # 配置数据源,给数据起别名(多个用逗号隔开),可与数据库名称相同
    spring.shardingsphere.datasource.names={数据库别名}
    
    #配置数据源具体内容,包含连接池,驱动,地址,用户名和密码
    spring.shardingsphere.datasource.{数据库别名}.type=com.alibaba.druid.pool.DruidDataSource
    spring.shardingsphere.datasource.{数据库别名}.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.shardingsphere.datasource.{数据库别名}.url=jdbc:mysql://{数据库ip}:{数据库端口号}/{数据库名称}?serverTimezone=GMT%2B8
    spring.shardingsphere.datasource.{数据库别名}.username=
    spring.shardingsphere.datasource.{数据库别名}.password=
    
    #指定 user 表的分布情况,配置表在哪个数据库里面,表名称都是什么,例如 test.user_0, test.user_1
    spring.shardingsphere.sharding.tables.{表名}.actual-data-nodes=test.user_$->{0..1}
    
    # 指定 user 表里面主键 id 生成策略 SNOWFLAKE
    spring.shardingsphere.sharding.tables.{表名}.key-generator.column=id
    spring.shardingsphere.sharding.tables.{表名}.key-generator.type=SNOWFLAKE
    
    # 指定分片策略 约定 id 值偶数添加到 user_0 表,如果 id 是奇数添加到 user_1 表
    spring.shardingsphere.sharding.tables.{表名}.table-strategy.inline.sharding-column=id
    spring.shardingsphere.sharding.tables.{表名}.table-strategy.inline.algorithm-expression=user_$->{id % 2}
    
    # 打开 sql 输出日志
    spring.shardingsphere.props.sql.show=true
    
    # 一个实体类对应两张表,覆盖
    spring.main.allow-bean-definition-overriding=true
    
  • 第四步:编写 实体类(entity)、业务逻辑层(service)、数据访问层(dao)、测试(test)




分库(包括分表)

  • 第一步:创建数据库和表

    • 数据库:db0, db1
    • 数据表:table_0, table_1
  • 第二步:创建SpringBoot项目并导入依赖(pom.xml)(同上

  • 第三步:编写配置文件(application.properties)(按照自己的环境修改

    # 配置数据源,给数据起别名(多个用逗号隔开),可与数据库名称相同
    spring.shardingsphere.datasource.names={第一个数据库别名},{第二个数据库别名}
    
    #配置数据源具体内容,包含连接池,驱动,地址,用户名和密码
    spring.shardingsphere.datasource.{第一个数据库别名}.type=com.alibaba.druid.pool.DruidDataSource
    spring.shardingsphere.datasource.{第一个数据库别名}.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.shardingsphere.datasource.{第一个数据库别名}.url=jdbc:mysql://{第一个数据库ip}:{第一个数据库端口号}/{第一个数据库名称}?serverTimezone=GMT%2B8
    spring.shardingsphere.datasource.{第一个数据库别名}.username=
    spring.shardingsphere.datasource.{第一个数据库别名}.password=
    
    #配置第二个数据源 db1 具体内容:包含连接池,驱动,地址,用户名和密码
    spring.shardingsphere.datasource.{第二个数据库别名}.type=com.alibaba.druid.pool.DruidDataSource
    spring.shardingsphere.datasource.{第二个数据库别名}.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.shardingsphere.datasource.{第二个数据库别名}.url=jdbc:mysql://{第二个数据库ip}:{第二个数据库端口号}/{第二个数据库名称}?serverTimezone=GMT%2B8
    spring.shardingsphere.datasource.{第二个数据库别名}.username=
    spring.shardingsphere.datasource.{第二个数据库别名}.password=
    
    #指定 table 表的分布情况,配置表在哪个数据库里面,表名称都是什么,例如 db0.table_0, db0.table_1, db1.table_0, db1.table_1
    spring.shardingsphere.sharding.tables.{表名}.actual-data-nodes=db$->{0..1}.table_$->{0..1}
    
    # 指定 user 表里面主键 id 生成策略 SNOWFLAKE
    spring.shardingsphere.sharding.tables.{表名}.key-generator.column=id
    spring.shardingsphere.sharding.tables.{表名}.key-generator.type=SNOWFLAKE
    
    # 指定分片策略 约定 id 值偶数添加到 table_0 表,如果 id 是奇数添加到 table_1 表
    spring.shardingsphere.sharding.tables.{表名}.table-strategy.inline.sharding-column=id
    spring.shardingsphere.sharding.tables.{表名}.table-strategy.inline.algorithm-expression=table_$->{id % 2}
    
    # 指定数据库的分片策略,当是 table 表时,字段为 cid 是偶数添加数据库 db0,是奇数添加到 db1
    spring.shardingsphere.sharding.tables.{表名}.database-strategy.inline.sharding-column=cid
    spring.shardingsphere.sharding.tables.{表名}.database-strategy.inline.algorithm-expression=db$->{cid % 2}
    
    # 打开 sql 输出日志
    spring.shardingsphere.props.sql.show=true
    
    # 一个实体类对应两张表,覆盖
    spring.main.allow-bean-definition-overriding=true
    
  • 第四步:编写 实体类(entity)、业务逻辑层(service)、数据访问层(dao)、测试(test)




2、Sharding-Proxy

技术不止,学习不止,------ 待更新

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值