当前位置:   article > 正文

shardingsphere实现分库_shardingsphere 分库

shardingsphere 分库

实验环境

springboot版本:2.2.2.RELEASE

shardingsphere版本:4.0.0-RC3

实验说明:

数据库:demo_ds_0,demo_ds_1

表user,分别在demo_ds_0,demo_ds_1各自建一张user表

  1. CREATE TABLE `user` (
  2. `id` bigint(255) NOT NULL AUTO_INCREMENT,
  3. `name` varchar(255) DEFAULT NULL,
  4. `uid` varchar(255) DEFAULT NULL,
  5. `school` varchar(255) DEFAULT NULL,
  6. `age` int(11) DEFAULT NULL,
  7. PRIMARY KEY (`id`)
  8. )

第一步:引入shardingsphere依赖

  1. <dependency>
  2. <groupId>org.apache.shardingsphere</groupId>
  3. <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
  4. <version>4.0.0-RC3</version>
  5. </dependency>
  6. <!-- for spring namespace -->
  7. <dependency>
  8. <groupId>org.apache.shardingsphere</groupId>
  9. <artifactId>sharding-jdbc-spring-namespace</artifactId>
  10. <version>4.0.0-RC3</version>
  11. </dependency>

第二步:application.properties配置

  1. debug=false
  2. server.port=8081
  3. server.servlet.context-path=
  4. spring.jackson.serialization.INDENT_OUTPUT=true
  5. spring.profiles.active=
  6. spring.shardingsphere.props.sql.show=true
  7. spring.shardingsphere.datasource.names=ds-0,ds-1
  8. spring.shardingsphere.datasource.ds-0.type=com.zaxxer.hikari.HikariDataSource
  9. spring.shardingsphere.datasource.ds-0.driver-class-name=com.mysql.jdbc.Driver
  10. spring.shardingsphere.datasource.ds-0.jdbc-url=jdbc:mysql://youraddress:3306/demo_ds_0
  11. spring.shardingsphere.datasource.ds-0.username=
  12. spring.shardingsphere.datasource.ds-0.password=
  13. spring.shardingsphere.datasource.ds-1.type=com.zaxxer.hikari.HikariDataSource
  14. spring.shardingsphere.datasource.ds-1.driver-class-name=com.mysql.jdbc.Driver
  15. spring.shardingsphere.datasource.ds-1.jdbc-url=jdbc:mysql://youraddress:3306/demo_ds_1
  16. spring.shardingsphere.datasource.ds-1.username=
  17. spring.shardingsphere.datasource.ds-1.password=
  18. spring.shardingsphere.sharding.default-database-strategy.inline.sharding-column=age
  19. spring.shardingsphere.sharding.default-database-strategy.inline.algorithm-expression=ds-$->{age % 2}
  20. spring.shardingsphere.sharding.tables.user.actual-data-nodes=ds-$->{0..1}.user
  21. spring.shardingsphere.sharding.tables.user.key-generator.column=id
  22. spring.shardingsphere.sharding.tables.user.key-generator.type=SNOWFLAKE
  23. spring.shardingsphere.sharding.tables.user.key-generator.props.worker.id=123

watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA3NzIyMzA=,size_16,color_FFFFFF,t_70

 

第三步:验证

1.通过代码,插入一条数据,age=1,发现数据保存在demo_ds_1的user表中

2.通过代码,插入一条数据,age=2,发现数据保存在demo_ds_0的user表中

3.通过代码,插入一条数据,age=3,发现数据保存在demo_ds_0的user表中

4通过代码查询,发现查询出三条记录,到此验证完毕。

 

watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3UwMTA3NzIyMzA=,size_16,color_FFFFFF,t_70

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/757157
推荐阅读
相关标签
  

闽ICP备14008679号