1.准备工作

昨天已经搭建了MySQL的主从复制,然而MySQL自己无法实现读写分离,所以我们要借助MyCat或者ShardingSphere来实现读写分离。

那么我们首先需要安装好MySQL的主从复制环境

2.步骤

首先导入依赖,我这里用4.0.0版本的ShardingSphere

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-starter-web</artifactId>
  5. </dependency>
  6. <!-- DB: MyBatis Plus, Druid -->
  7. <dependency>
  8. <groupId>mysql</groupId>
  9. <artifactId>mysql-connector-java</artifactId>
  10. <scope>runtime</scope>
  11. </dependency>
  12. <dependency>
  13. <groupId>com.baomidou</groupId>
  14. <artifactId>mybatis-plus-boot-starter</artifactId>
  15. <version>3.3.1</version>
  16. </dependency>
  17. <dependency>
  18. <groupId>com.alibaba</groupId>
  19. <artifactId>druid-spring-boot-starter</artifactId>
  20. <version>1.1.21</version>
  21. </dependency>
  22. <dependency>
  23. <groupId>org.apache.shardingsphere</groupId>
  24. <artifactId>sharding-jdbc-spring-boot-starter</artifactId>
  25. <version>4.0.0-RC1</version>
  26. </dependency>
  27. <dependency>
  28. <groupId>org.projectlombok</groupId>
  29. <artifactId>lombok</artifactId>
  30. <optional>true</optional>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.boot</groupId>
  34. <artifactId>spring-boot-starter-test</artifactId>
  35. <scope>test</scope>
  36. </dependency>
  37. <dependency>
  38. <groupId>junit</groupId>
  39. <artifactId>junit</artifactId>
  40. <version>4.13.1</version>
  41. <scope>test</scope>
  42. </dependency>
  43. </dependencies>

然后创建mapper、entity类

  1. package com.zym.entity;
  2. import com.baomidou.mybatisplus.annotation.IdType;
  3. import com.baomidou.mybatisplus.annotation.TableId;
  4. import com.baomidou.mybatisplus.annotation.TableName;
  5. import lombok.Data;
  6. @Data
  7. @TableName("user")
  8. public class User {
  9. @TableId(type = IdType.AUTO)
  10. private int id;
  11. private String name;
  12. }
  1. package com.zym.mapper;
  2. import com.baomidou.mybatisplus.core.mapper.BaseMapper;
  3. import com.zym.entity.User;
  4. import org.apache.ibatis.annotations.Mapper;
  5. @Mapper
  6. public interface UserMapper extends BaseMapper<User> {
  7. }

配置文件:

  1. spring:
  2. shardingsphere:
  3. datasource:
  4. names: master,slave
  5. master:
  6. type: com.alibaba.druid.pool.DruidDataSource
  7. driver-class-name: com.mysql.jdbc.Driver
  8. url: jdbc:mysql://192.168.60.10:3306/test?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
  9. username: root
  10. password: 123456
  11. slave:
  12. type: com.alibaba.druid.pool.DruidDataSource
  13. driver-class-name: com.mysql.jdbc.Driver
  14. url: jdbc:mysql://192.168.60.10:3307/test?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
  15. username: root
  16. password: 123456
  17. masterslave:
  18. load-balance-algorithm-type: round_robin
  19. name: dataSource
  20. master-data-source-name: master
  21. slave-data-source-names: slave
  22. props:
  23. sql:
  24. show: true
  25. # 一个实体类对应两张表,覆盖
  26. main:
  27. allow-bean-definition-overriding: true

最后是测试类

  1. package com.zym;
  2. import com.zym.entity.User;
  3. import com.zym.mapper.UserMapper;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.boot.test.context.SpringBootTest;
  8. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  9. import java.util.List;
  10. @RunWith(SpringJUnit4ClassRunner.class)
  11. @SpringBootTest
  12. public class ShardingSphereDemoApplicationTests {
  13. @Autowired
  14. private UserMapper userMapper;
  15. @Test
  16. public void insertUser() {
  17. User user = new User();
  18. user.setName("小王");
  19. int i = userMapper.insert(user);
  20. System.out.println(i == 1 ? "插入成功" : "插入失败");
  21. }
  22. @Test
  23. public void getUserList(){
  24. List<User> userList = userMapper.selectList(null);
  25. for (User user : userList) {
  26. System.out.println(user.toString());
  27. }
  28. }
  29. }

然后我们来运行一下测试类,首先我们插入一条数据
1.png
可以看到这里是用master插入的

然后我们再查询一下用户列表
2.png
可以看到这里是用slave去查询的

那么这样我们的读写分离就配置好了,要注意的是,在真实环境中,从库应该建立只读用户,而不是直接使用root用户,这里为了演示就直接用了root用户,切勿在正式环境使用!!!


源码地址:https://gitee.com/zym213/sharding-sphere-demo