文章目录


一、说明
本demo的配置是修改自官方example,用于在本项目的依赖下集成sharding-jdbc来实现分库分表、读写分离等场景的演示
sharding-jdbc详细的配置项说明:https://www.kancloud.cn/zlt2000/microservices-platform/1015741
分库分表思路:https://www.kancloud.cn/mall2000/microservices-platform/1015741
二、准备
1、目录结构
2、配置文件及数据库sql脚本
- pom.xml文件 (其余依赖根据项目需要引入)
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
</dependency>
<!-- druid 官方 starter -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!-- sharding-jdbc分表分库 -->
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
- sql脚本:
-----------------------------------------sharding-databases
CREATE SCHEMA IF NOT EXISTS demo_ds_0;
CREATE SCHEMA IF NOT EXISTS demo_ds_1;
CREATE TABLE IF NOT EXISTS demo_ds_0.user (
id BIGINT NOT NULL AUTO_INCREMENT,
company_id varchar(32) NOT NULL,
name varchar(50) NULL,
create_time datetime(0) NULL,
update_time datetime(0) NULL,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS demo_ds_1.user (
id BIGINT NOT NULL AUTO_INCREMENT,
company_id varchar(32) NOT NULL,
name varchar(50) NULL,
create_time datetime(0) NULL,
update_time datetime(0) NULL,
PRIMARY KEY (id)
);
-----------------------------------------sharding-databases2
CREATE SCHEMA IF NOT EXISTS demo_ds_alibaba;
CREATE SCHEMA IF NOT EXISTS demo_ds_baidu;
CREATE TABLE IF NOT EXISTS demo_ds_alibaba.user (
id BIGINT NOT NULL AUTO_INCREMENT,
company_id varchar(32) NOT NULL,
name varchar(50) NULL,
create_time datetime(0) NULL,
update_time datetime(0) NULL,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS demo_ds_baidu.user (
id BIGINT NOT NULL AUTO_INCREMENT,
company_id varchar(32) NOT NULL,
name varchar(50) NULL,
create_time datetime(0) NULL,
update_time datetime(0) NULL,
PRIMARY KEY (id)
);
-----------------------------------------master-slave
CREATE SCHEMA IF NOT EXISTS demo_ds_master;
CREATE SCHEMA IF NOT EXISTS demo_ds_slave_0;
CREATE SCHEMA IF NOT EXISTS demo_ds_slave_1;
CREATE TABLE IF NOT EXISTS demo_ds_master.user (
id BIGINT NOT NULL AUTO_INCREMENT,
company_id varchar(32) NOT NULL,
name varchar(50) NULL,
create_time datetime(0) NULL,
update_time datetime(0) NULL,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS demo_ds_slave_0.user (
id BIGINT NOT NULL AUTO_INCREMENT,
company_id varchar(32) NOT NULL,
name varchar(50) NULL,
create_time datetime(0) NULL,
update_time datetime(0) NULL,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS demo_ds_slave_1.user (
id BIGINT NOT NULL AUTO_INCREMENT,
company_id varchar(32) NOT NULL,
name varchar(50) NULL,
create_time datetime(0) NULL,
update_time datetime(0) NULL,
PRIMARY KEY (id)
);
-----------------------------------------sharding-tables
CREATE SCHEMA IF NOT EXISTS demo_ds;
CREATE TABLE IF NOT EXISTS demo_ds.user_0 (
id BIGINT NOT NULL AUTO_INCREMENT,
company_id varchar(32) NOT NULL,
name varchar(50) NULL,
create_time datetime(0) NULL,
update_time datetime(0) NULL,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS demo_ds.user_1 (
id BIGINT NOT NULL AUTO_INCREMENT,
company_id varchar(32) NOT NULL,
name varchar(50) NULL,
create_time datetime(0) NULL,
update_time datetime(0) NULL,
PRIMARY KEY (id)
);
- 需要测试哪个分库分表类型就执行哪段sql脚本
- 对应创建user实体类和接口逻辑:
package com.sharding.demo.model;
import com.baomidou.mybatisplus.annotation.TableName;
import com.mallplus.common.model.SuperEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* @author lxy
*/
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("user")
public class User {
private static final long serialVersionUID = 8898492657846787286L;
/**
* 主键ID
*/
@TableId
private Long id;
private String companyId;
private String name;
}
3、
—3.1、application.yml:
server:
port: 11001
spring:
profiles:
active: sharding-tables #sharding-databases 根据需要引入想要的分表分库方案对应的配置文件
application:
name: sharding-jdbc-demo
mall:
datasource:
ip: localhost
username: root
password: 123456
mybatis-plus:
mapper-locations: classpath:/mapper/*Mapper.xml
#实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage: com.sharding.demo.model
global-config:
db-config:
id-type: ID_WORKER
—3.2、application-master-slave.yml:
sharding:
jdbc:
datasource:
names: ds-master,ds-slave-0,ds-slave-1
ds-master:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${mall.datasource.ip}:3306/demo_ds_master?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: ${mall.datasource.username}
password: ${mall.datasource.password}
ds-slave-0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${mall.datasource.ip}:3306/demo_ds_slave_0?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: ${mall.datasource.username}
password: ${mall.datasource.password}
ds-slave-1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${mall.datasource.ip}:3306/demo_ds_slave_1?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: ${mall.datasource.username}
password: ${mall.datasource.password}
config:
masterslave:
load-balance-algorithm-type: round_robin
name: ds_ms
master-data-source-name: ds-master
slave-data-source-names: ds-slave-0,ds-slave-1
props:
sql:
show: true
—3.3、application-sharding-databases.yml:
sharding:
jdbc:
datasource:
names: ds-0,ds-1
ds-0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${mall.datasource.ip}:3306/demo_ds_0?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: ${mall.datasource.username}
password: ${mall.datasource.password}
ds-1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${mall.datasource.ip}:3306/demo_ds_1?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: ${mall.datasource.username}
password: ${mall.datasource.password}
config:
sharding:
default-database-strategy:
inline:
sharding-column: id
algorithm-expression: ds-$->{id % 2}
tables:
user:
actual-data-nodes: ds-$->{0..1}.user
#key-generator-column-name: id
props:
sql:
show: true
—3.4、application-sharding-databases2.yml:
sharding:
jdbc:
datasource:
names: ds-alibaba,ds-baidu
ds-alibaba:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${mall.datasource.ip}:3306/demo_ds_alibaba?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: ${mall.datasource.username}
password: ${mall.datasource.password}
ds-baidu:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${mall.datasource.ip}:3306/demo_ds_baidu?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: ${mall.datasource.username}
password: ${mall.datasource.password}
config:
sharding:
default-database-strategy:
inline:
sharding-column: company_id
algorithm-expression: ds-$->{company_id}
tables:
user:
actual-data-nodes: ds-$->{['alibaba','baidu']}.ums
key-generator-column-name: id
props:
sql:
show: true
—3.5、application-sharding-databases3.yml:
sharding:
jdbc:
datasource:
names: ds,ds-0,ds-1
ds:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${mall.datasource.ip}:3306/demo_ds?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: ${mall.datasource.username}
password: ${mall.datasource.password}
ds-0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${mall.datasource.ip}:3306/demo_ds_0?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: ${mall.datasource.username}
password: ${mall.datasource.password}
ds-1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${mall.datasource.ip}:3306/demo_ds_1?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: ${mall.datasource.username}
password: ${mall.datasource.password}
config:
sharding:
default-data-source-name: ds
default-database-strategy:
inline:
sharding-column: id
algorithm-expression: ds-$->{id % 2}
tables:
user:
actual-data-nodes: ds-$->{0..1}.user
key-generator-column-name: id
props:
sql:
show: true
—3.6、application-sharding-tables.yml:
sharding:
jdbc:
datasource:
names: ds
ds:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${mall.datasource.ip}:3306/demo_ds?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: ${mall.datasource.username}
password: ${mall.datasource.password}
config:
sharding:
tables:
user:
actual-data-nodes: ds.user_$->{0..1}
table-strategy:
inline:
sharding-column: id
algorithm-expression: user_$->{id % 2}
key-generator-column-name: id
props:
sql:
show: true
三、运行步骤
1. 修改application.yml配置
- 修改
spring.profiles.active
的值为需要运行的场景master-slave
:一主多从模式下的读写分离sharding-databases
:使用取模的方式来实现库分片sharding-databases2
:使用固定值的方式来实现库分片sharding-databases3
:工程里既有分片的表,也有不分片的表(使用默认的库)sharding-tables
:使用取模的方式来实现表分片
- 修改数据库的配置
2. 初始化数据
执行mall-demo\sharding-jdbc-demo\sharding-jdbc-demo.sql
脚本
3. 启动工程
运行ShardingApplication
四、测试接口
1. 初始化数据
生成10条用户数据
http://localhost:11001/init
/**
* 初始化数据
*/
@GetMapping("/init")
public String initDate() {
String companyId;
for (int i = 0; i < 100; i++) {
User u = new User();
if (i % 2 == 0) {
companyId = "alibaba";
} else {
companyId = "baidu";
}
u.setCompanyId(companyId);
u.setName(String.valueOf(i));
userService.save(u);
}
return "success";
}
2. 查询所有用户数据
http://localhost:11001/
/**
* 查询列表
*/
@GetMapping("/")
public List<User> list() {
return userService.list();
}
3. 查询某个用户数据
http://localhost:11001/{id}
/**
* 查询单条记录
*/
@GetMapping("/{id}")
public User get(@PathVariable Long id) {
return userService.getById(id);
}
4. 清除所有数据
http://localhost:11001/clean
/**
* 清除数据
*/
@GetMapping("/clean")
public String clean() {
userService.remove(null);
return "success";
}