前提条件:MySQL已搭建好主从复制环境,这里采用一主二从形式(尚未搭建)
使用Sharding版本:4.0.0-RC1
使用SpringBoot版本:2.7.0
官网:https://shardingsphere.apache.org/index_zh.html
<properties>
<java.version>11java.version>
<sharding-sphere.version>4.0.0-RC1sharding-sphere.version>
properties>
<dependency>
<groupId>javax.xml.bindgroupId>
<artifactId>jaxb-apiartifactId>
<version>2.3.1version>
dependency>
<dependency>
<groupId>com.sun.xml.bindgroupId>
<artifactId>jaxb-implartifactId>
<version>2.3.1version>
dependency>
<dependency>
<groupId>org.glassfish.jaxbgroupId>
<artifactId>jaxb-runtimeartifactId>
<version>2.3.1version>
dependency>
<dependency>
<groupId>org.apache.shardingspheregroupId>
<artifactId>sharding-jdbc-spring-boot-starterartifactId>
<version>${sharding-sphere.version}version>
dependency>
<dependency>
<groupId>org.apache.shardingspheregroupId>
<artifactId>sharding-core-commonartifactId>
<version>${sharding-sphere.version}version>
dependency>
<dependency>
<groupId>org.mybatis.spring.bootgroupId>
<artifactId>mybatis-spring-boot-starterartifactId>
<version>2.2.2version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<scope>runtimescope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<optional>trueoptional>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>druid-spring-boot-starterartifactId>
<version>1.2.11version>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
主配置 application.yml
:
server:
port: 8099
spring:
main:
allow-bean-definition-overriding: true
profiles:
active: MasterSlave # 激活指定配置文件
mybatis: # 使用mybatis-plus不用配置
mapper-locations: classpath:mapper/*.xml
type-aliases-package: com.konan.sharding.entity
从配置application-MasterSlave.yml
:
spring:
shardingsphere:
# 参数配置,显示sql
props:
sql:
show: true
# 配置数据源
datasource:
# 给每个数据源取别名,下面的ds1,ds2,ds3任意取名字,ds1对应的是主库
names: ds1,ds2,ds3
# 给master-ds1每个数据源配置数据库连接信息
ds1:
# 配置druid数据源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://117.50.185.199:3307/sharding-study?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
username: root
password: 1234Abc666
maxPoolSize: 100
minPoolSize: 5
# 配置ds2-slave
ds2:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://117.50.185.199:3308/sharding-study?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
username: root
password: 1234Abc666
maxPoolSize: 100
minPoolSize: 5
ds3:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3308/sharding-study?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
username: root
password: 12345Abc666
maxPoolSize: 100
minPoolSize: 5
# 配置默认数据源ds1
sharding:
# 默认数据源,主要用于写,注意一定要配置读写分离 ,注意:如果不配置,那么就会把三个节点都当做从slave节点,新增,修改和删除会出错。
default-data-source-name: ds1
# 配置数据源的读写分离,但是数据库一定要做主从复制
masterslave:
# 配置主从名称,可以任意取名字
name: ms
# 配置主库master,负责数据的写入
master-data-source-name: ds1
# 配置从库slave节点
slave-data-source-names: ds2,ds3
# 配置slave节点的负载均衡均衡策略,采用轮询机制,只有一个从库,无须配置
load-balance-algorithm-type: round_robin
只需在主库中建立即可,从库会自动复制,建立如下表:
对应SQL:
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
`id` int NOT NULL AUTO_INCREMENT,
`nickname` varchar(255) DEFAULT NULL,
`password` varchar(255) DEFAULT NULL,
`sex` int DEFAULT NULL,
`birthday` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
SET FOREIGN_KEY_CHECKS = 1;
package com.konan.shardingstudy.entity;
import lombok.Data;
import java.util.Date;
@Data
public class User {
private Integer id;
private String nickname;
private String password;
private Integer sex;
private Date birthday;
}
package com.konan.shardingstudy.mapper;
import com.konan.shardingstudy.entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper //表示这是一个mybatis的mapper类
@Repository
public interface UserMapper {
@Insert("insert into user(nickname,password,sex,birthday) values(#{nickname},#{password},#{sex},#{birthday})")
@Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id") //可以回填主键的值,mbp中自动回填
void addUser(User user);
@Select("select * from user)
List<User> findUsers();
}
package com.konan.shardingstudy.controller;
import com.konan.shardingstudy.entity.User;
import com.konan.shardingstudy.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.List;
import java.util.Random;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserMapper userMapper;
@GetMapping("/save")
public String insert() {
User user = new User();
user.setNickname("zhangsan"+ new Random().nextInt());
user.setPassword("1234567");
user.setSex((int)(Math.random()*2));
user.setBirthday(new Date());
userMapper.addUser(user);
return user.getId().toString();
}
@GetMapping("/list")
public List<User> listuser() {
return userMapper.findUsers();
}
}
1:访问 http://localhost:8099/user/save
一直进入到ds1主节点
2:访问 http://localhost:8099/user/list
一直进入到ds2、ds3节点,并且轮询进入。
日志查看
总结
ShardingSphere 的 3 个产品(Sharding-JDBC、Sharding-Proxy、Sharding-Sidecar)的数据分片主要流程是完全一致的
核心由 SQL 解析 => 执行器优化 => SQL 路由 => SQL 改写 => SQL 执行 => 结果归并的流程组成
Sharding-JDBC的读写分离原理图
推荐一波个人博客:https://www.notre1024.com