05 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、读写分离

MySQL读写分离基本原理是让master数据库处理写操作,slave数据库处理读操作。master将写操作的变更同步到各个slave节点。

MySQL读写分离能提高系统性能的原因在于:

1、物理服务器增加,机器处理能力提升。拿硬件换性能。

2、主从只负责各自的读和写,极大程度缓解X锁和S锁争用。

3、slave可以配置myiasm引擎,提升查询性能以及节约系统开销。

4、master直接写是并发的,slave通过主库发送来的binlog恢复数据是异步。

5、slave可以单独设置一些参数来提升其读的性能。

6、增加冗余,提高可用性。

关联知识:

经过分库分表及广播表的示例可以发现,所有的操作都是配置上的问题,本文具体讲配置。

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-5</artifactId>
  12. <version>${parent.version}</version>
  13. <packaging>jar</packaging>
  14. <name>sharding-sphere-demo-5</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.shardingspheredemo5.ShardingSphereDemo5Application</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、在mysql 127.0.0.1:3306创建数据库ds2(写库)、mysql 127.0.0.1:3307数据库ds2(读库),创建表t_user

  1. CREATE TABLE `t_user` (
  2. `user_id` int NOT NULL COMMENT '用户id',
  3. `user_name` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT '用户名称',
  4. PRIMARY KEY (`user_id`)
  5. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

3、创建实体类

  1. @Data
  2. public class UserEntity {
  3. private Integer userId;
  4. private String userName;
  5. }

4、创建Mapper类

  1. @Mapper
  2. public interface UserMapper {
  3. @Insert("insert into t_user(user_id,user_name) values(#{userId},#{userName})")
  4. void insertUserInfo(UserEntity userEntity);
  5. @Select("select * from t_user where user_id=#{userId}")
  6. @Results({
  7. @Result(property = "userId", column = "user_id", jdbcType = JdbcType.INTEGER),
  8. @Result(property = "userName", column = "user_name", jdbcType = JdbcType.VARCHAR)
  9. })
  10. UserEntity getUserInfo(Integer userId);
  11. }

5、创建配置文件

  1. serser.port=8080
  2. spring.application.name=spring-boot-shardingsphere
  3. #配置数据源的名称
  4. spring.shardingsphere.datasource.names=master,slave
  5. #配置数据源的具体内容,
  6. spring.shardingsphere.datasource.master.type=com.alibaba.druid.pool.DruidDataSource
  7. spring.shardingsphere.datasource.master.driver-class-name=com.mysql.cj.jdbc.Driver
  8. spring.shardingsphere.datasource.master.url=jdbc:mysql://127.0.0.1:3306/ds2?serverTimezone=UTC
  9. spring.shardingsphere.datasource.master.username=root
  10. spring.shardingsphere.datasource.master.password=123456
  11. spring.shardingsphere.datasource.slave.type=com.alibaba.druid.pool.DruidDataSource
  12. spring.shardingsphere.datasource.slave.driver-class-name=com.mysql.cj.jdbc.Driver
  13. spring.shardingsphere.datasource.slave.url=jdbc:mysql://127.0.0.1:3307/ds2?serverTimezone=UTC
  14. spring.shardingsphere.datasource.slave.username=root
  15. spring.shardingsphere.datasource.slave.password=123456
  16. # 主库从库逻辑定义
  17. spring.shardingsphere.masterslave.name=ms
  18. spring.shardingsphere.masterslave.master-data-source-name=master
  19. spring.shardingsphere.masterslave.slave-data-source-names=slave
  20. #显示sql
  21. spring.shardingsphere.props.sql.show=true

6、创建测试类

  1. @SpringBootTest
  2. public class UserTests {
  3. @Resource
  4. private UserMapper userMapper;
  5. @Test
  6. public void insertUserInfo() {
  7. for (int i = 1; i <= 10; i++) {
  8. UserEntity userEntity = new UserEntity();
  9. userEntity.setUserId(i);
  10. userEntity.setUserName("user" + i);
  11. userMapper.insertUserInfo(userEntity);
  12. }
  13. }
  14. @Test
  15. public void getUserInfo() {
  16. System.out.println(userMapper.getUserInfo(1));
  17. }
  18. }

7、执行结果

image-20210705111618564

image-20210705112150713

可以看到数据从master新增,从slave读取。

源码地址

github

码云

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