02 07 2021

前言

前文已经介绍了ShardingSphere及相关组件,Sharding-JDBC是ShardingSphere的第一个产品,也是ShardingSphere的前身。本文主要通过代码实例介绍如何使用Sharding-JDBC 。

第一篇文章介绍是官网对于sharding-jdbc的解释和介绍,其实说的直白一点,就是包含了分库分表功能的JDBC,因此我们可以直接把sharding-jdbc当做普通的jdbc来进行使用。

本系列文章项目基于SpringBoot构建,采用ShardingSphere v4.1.1,由于5.X版本还在公测,可能存在诸多Bug。

Sharding-JDBC 广播表

0、广播表(公共表)

指所有的分片数据源中都存在的表,表结构和表中的数据在每个数据库中均完全一致。适用于数据量不大且需要与海量数据的表进行关联查询的场景,例如:字典表。

1、环境构建

创建一个SpringBoot项目,引入如下依赖:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <parent>
  5. <groupId>cn.zwqh</groupId>
  6. <artifactId>sharding-sphere-4.1.1</artifactId>
  7. <version>1.0-SNAPSHOT</version>
  8. </parent>
  9. <modelVersion>4.0.0</modelVersion>
  10. <groupId>cn.zwqh</groupId>
  11. <artifactId>sharding-sphere-demo-4</artifactId>
  12. <version>${parent.version}</version>
  13. <packaging>jar</packaging>
  14. <name>sharding-sphere-demo-4</name>
  15. <description>Demo project for Spring Boot</description>
  16. <dependencies>
  17. <dependency>
  18. <groupId>org.springframework.boot</groupId>
  19. <artifactId>spring-boot-starter</artifactId>
  20. </dependency>
  21. <dependency>
  22. <groupId>org.mybatis.spring.boot</groupId>
  23. <artifactId>mybatis-spring-boot-starter</artifactId>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.projectlombok</groupId>
  27. <artifactId>lombok</artifactId>
  28. <scope>provided</scope>
  29. </dependency>
  30. <dependency>
  31. <groupId>mysql</groupId>
  32. <artifactId>mysql-connector-java</artifactId>
  33. </dependency>
  34. <dependency>
  35. <groupId>com.alibaba</groupId>
  36. <artifactId>druid-spring-boot-starter</artifactId>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.apache.shardingsphere</groupId>
  40. <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-starter-test</artifactId>
  45. <scope>test</scope>
  46. <exclusions>
  47. <exclusion>
  48. <groupId>org.junit.vintage</groupId>
  49. <artifactId>junit-vintage-engine</artifactId>
  50. </exclusion>
  51. </exclusions>
  52. </dependency>
  53. </dependencies>
  54. <build>
  55. <plugins>
  56. <plugin>
  57. <groupId>org.apache.maven.plugins</groupId>
  58. <artifactId>maven-compiler-plugin</artifactId>
  59. <version>3.8.1</version>
  60. <configuration>
  61. <source>1.8</source>
  62. <target>1.8</target>
  63. <encoding>UTF-8</encoding>
  64. </configuration>
  65. </plugin>
  66. <plugin>
  67. <groupId>org.springframework.boot</groupId>
  68. <artifactId>spring-boot-maven-plugin</artifactId>
  69. <configuration>
  70. <mainClass>cn.zwqh.shardingspheredemo4.ShardingSphereDemo4Application</mainClass>
  71. </configuration>
  72. <executions>
  73. <execution>
  74. <id>repackage</id>
  75. <goals>
  76. <goal>repackage</goal>
  77. </goals>
  78. </execution>
  79. </executions>
  80. </plugin>
  81. </plugins>
  82. </build>
  83. </project>

2、创建数据库ds0、ds1,分别在创建表t_order_type

  1. CREATE TABLE `t_order_type` (
  2. `type_id` int NOT NULL COMMENT '订单类型id',
  3. `type_name` varchar(32) COLLATE utf8mb4_bin DEFAULT NULL COMMENT '订单类型名称',
  4. PRIMARY KEY (`type_id`) USING BTREE
  5. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

3、分片规则

公共表表示所有的库都具备相同的表。

4、创建实体类

  1. @Data
  2. public class OrderType {
  3. private Integer typeId;
  4. private String typeName;
  5. }

5、创建Mapper类

  1. @Mapper
  2. public interface OrderTypeMapper {
  3. @Insert("insert into t_order_type(type_id,type_name) values(#{typeId},#{typeName})")
  4. void insertOrderType(OrderType orderType);
  5. }

6、创建配置文件

  1. serser.port=8080
  2. spring.application.name=spring-boot-shardingsphere
  3. #配置数据源的名称
  4. spring.shardingsphere.datasource.names=ds0,ds1
  5. #配置数据源的具体内容,
  6. spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
  7. spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.cj.jdbc.Driver
  8. spring.shardingsphere.datasource.ds0.url=jdbc:mysql://127.0.0.1:3306/ds0?serverTimezone=UTC
  9. spring.shardingsphere.datasource.ds0.username=root
  10. spring.shardingsphere.datasource.ds0.password=123456
  11. spring.shardingsphere.datasource.ds1.type=com.alibaba.druid.pool.DruidDataSource
  12. spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.cj.jdbc.Driver
  13. spring.shardingsphere.datasource.ds1.url=jdbc:mysql://127.0.0.1:3306/ds1?serverTimezone=UTC
  14. spring.shardingsphere.datasource.ds1.username=root
  15. spring.shardingsphere.datasource.ds1.password=123456
  16. # 广播表配置
  17. spring.shardingsphere.sharding.broadcast-tables=t_order_type
  18. spring.shardingsphere.sharding.tables.t_order_type.actual-data-nodes=ds$->{0..1}.t_order_type
  19. #显示sql
  20. spring.shardingsphere.props.sql.show=true

7、创建测试类

  1. @SpringBootTest
  2. public class OrderTypeTests {
  3. @Resource
  4. private OrderTypeMapper orderTypeMapper;
  5. @Test
  6. public void insertOrderType() {
  7. OrderType orderType1 = new OrderType();
  8. orderType1.setTypeId(1);
  9. orderType1.setTypeName("集采批发");
  10. OrderType orderType2 = new OrderType();
  11. orderType2.setTypeId(2);
  12. orderType2.setTypeName("集采代发");
  13. OrderType orderType3 = new OrderType();
  14. orderType3.setTypeId(3);
  15. orderType3.setTypeName("一件代发");
  16. orderTypeMapper.insertOrderType(orderType1);
  17. orderTypeMapper.insertOrderType(orderType2);
  18. orderTypeMapper.insertOrderType(orderType3);
  19. }
  20. }

8、执行结果

新增的数据会同时插入到ds0和ds1的t_order_type表,且两表数据一致。

源码地址

github

码云

延伸阅读
  1. MySQL 5.7 详细安装步骤
  2. Linux下MySQL的彻底卸载
  3. 一文读懂 MySQL 事务
  4. MySQL 怎么解决幻读问题
  5. MySQL join的使用和原理
发表评论