提交 d0f9d1b9 编写于 作者: zlt2000's avatar zlt2000

新增sharding-jdbc-demo分库分表演示工程(sharding-sphere3.1.0)

上级 43730902
......@@ -37,7 +37,7 @@
## 2. 项目总体架构图
![](https://gitee.com/zlt2000/images/raw/master/springcloud微服务架构图.jpg)
![](http://processon.com/chart_image/5c7f2ad6e4b02b2ce48d6835.png?_=1554621571250)
 
......@@ -73,6 +73,7 @@
* 应用吞吐量监控(qps、rt)
* 服务降级、熔断监控
* 服务限流监控
* 分库分表、读写分离
* **业务基础功能支撑**
* 高性能方法级幂等性支持
* RBAC权限管理,实现细粒度控制(方法、url级别)
......@@ -122,6 +123,7 @@ central-platform -- 父项目,公共依赖
├─txlcn-tm -- tx-lcn事务管理器[7970]
├─zlt-demo -- demo一级工程
├─txlcn-demo -- txlcn的demo
├─sharding-jdbc-demo -- sharding-jdbc的demo
```
 
......
......@@ -22,7 +22,7 @@
<jjwt.version>0.9.1</jjwt.version>
<druid-starter>1.1.10</druid-starter>
<jasypt.version>1.14</jasypt.version>
<sharding-jdbc>2.0.3</sharding-jdbc>
<sharding-sphere.version>3.1.0</sharding-sphere.version>
<security-oauth2.version>2.3.5.RELEASE</security-oauth2.version>
<security-jwt.version>1.0.9.RELEASE</security-jwt.version>
<redisson.version>3.9.1</redisson.version>
......@@ -280,6 +280,11 @@
<artifactId>txlcn-txmsg-netty</artifactId>
<version>${txlcn.version}</version>
</dependency>
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>${sharding-sphere.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
......
......@@ -14,6 +14,9 @@ import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan({"com.central.user.mapper*"})
public class MybatisPlusConfig extends DefaultMybatisPlusConfig {
/**
* 逻辑删除注入器
*/
@Bean
public ISqlInjector sqlInjector(){
return new LogicSqlInjector();
......
......@@ -12,5 +12,7 @@
<modules>
<!-- txlcn分布式事务demo -->
<module>txlcn-demo</module>
<!-- sharding-jdbc分库分表demo -->
<module>sharding-jdbc-demo</module>
</modules>
</project>
\ No newline at end of file
[TOC]
## 一、说明
本demo的配置是修改自官方example,用于在本项目的依赖下集成sharding-jdbc来实现分库分表、读写分离等场景的演示
sharding-jdbc详细的配置项说明:https://www.kancloud.cn/zlt2000/microservices-platform/1015741
分库分表思路:https://www.kancloud.cn/zlt2000/microservices-platform/1015741
&nbsp;
## 二、运行步骤
### 1. 修改application.yml配置
* 修改`spring.profiles.active`的值为需要运行的场景
1. `master-slave`:一主多从模式下的读写分离
2. `sharding-databases`:使用取模的方式来实现库分片
3. `sharding-databases2`:使用固定值的方式来实现库分片
4. `sharding-databases3`:工程里既有分片的表,也有不分片的表(使用默认的库)
5. `sharding-tables`:使用取模的方式来实现表分片
* 修改数据库的配置
### 2. 初始化数据
执行`zlt-demo\sharding-jdbc-demo\sharding-jdbc-demo.sql`脚本
### 3. 启动工程
运行`ShardingApplication`
&nbsp;
## 三、测试接口
### 1. 初始化数据
生成10条用户数据
http://localhost:11001/init
### 2. 查询所有用户数据
http://localhost:11001/
### 3. 查询某个用户数据
http://localhost:11001/{id}
### 4. 清除所有数据
http://localhost:11001/clean
\ No newline at end of file
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.zlt</groupId>
<artifactId>zlt-demo</artifactId>
<version>1.4.0</version>
</parent>
<artifactId>sharding-jdbc-demo</artifactId>
<description>sharding-jdbc分库分表demo</description>
<dependencies>
<dependency>
<groupId>com.zlt</groupId>
<artifactId>zlt-common-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.zlt</groupId>
<artifactId>zlt-db-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>io.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
-----------------------------------------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)
);
\ No newline at end of file
package com.sharding;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* sharding-jdbc demo
*
* @author zlt
*/
@SpringBootApplication
public class ShardingApplication {
public static void main(String[] args) {
SpringApplication.run(ShardingApplication.class, args);
}
}
package com.sharding;
import com.central.common.utils.IdGenerator;
/**
* sharding-jdbc demo
*
* @author zlt
*/
public class TestApplication {
public static void main(String[] args) {
for (int i = 0; i < 100; i++) {
long userId = IdGenerator.getId();
System.out.println(userId);
userId = userId & 15;
System.out.println(userId);
}
/*
long orderId = IdGenerator.getId();
System.out.println(orderId);
orderId = (orderId << 4) | userId;
System.out.println(orderId);*/
}
}
package com.sharding.demo.config;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.central.db.config.DefaultMybatisPlusConfig;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author zlt
*/
@Configuration
@MapperScan({"com.sharding.demo.mapper*"})
public class MybatisPlusConfig extends DefaultMybatisPlusConfig {
/**
* 逻辑删除注入器
*/
@Bean
public ISqlInjector sqlInjector(){
return new LogicSqlInjector();
}
}
package com.sharding.demo.controller;
import com.sharding.demo.model.User;
import com.sharding.demo.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* @author zlt
*/
@RestController
public class UserController {
private final IUserService userService;
@Autowired
public UserController(IUserService userService) {
this.userService = userService;
}
/**
* 初始化数据
*/
@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";
}
/**
* 查询列表
*/
@GetMapping("/")
public List<User> list() {
return userService.list();
}
/**
* 查询单条记录
*/
@GetMapping("/{id}")
public User get(@PathVariable Long id) {
return userService.getById(id);
}
/**
* 清除数据
*/
@GetMapping("/clean")
public String clean() {
userService.remove(null);
return "success";
}
}
package com.sharding.demo.mapper;
import com.central.db.mapper.SuperMapper;
import com.sharding.demo.model.User;
/**
* @author zlt
*/
public interface UserMapper extends SuperMapper<User> {
}
package com.sharding.demo.model;
import com.baomidou.mybatisplus.annotation.TableName;
import com.central.common.model.SuperEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* @author zlt
*/
@Data
@EqualsAndHashCode(callSuper = false)
@TableName("user")
public class User extends SuperEntity {
private static final long serialVersionUID = 8898492657846787286L;
private String companyId;
private String name;
}
package com.sharding.demo.service;
import com.central.common.service.ISuperService;
import com.sharding.demo.model.User;
/**
* @author zlt
*/
public interface IUserService extends ISuperService<User> {
}
package com.sharding.demo.service.impl;
import com.central.common.service.impl.SuperServiceImpl;
import com.sharding.demo.mapper.UserMapper;
import com.sharding.demo.model.User;
import com.sharding.demo.service.IUserService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;
/**
* @author zlt
*/
@Slf4j
@Service
public class UserServiceImpl extends SuperServiceImpl<UserMapper, User> implements IUserService {
}
\ No newline at end of file
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://${zlt.datasource.ip}:3306/demo_ds_master?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: ${zlt.datasource.username}
password: ${zlt.datasource.password}
ds-slave-0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${zlt.datasource.ip}:3306/demo_ds_slave_0?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: ${zlt.datasource.username}
password: ${zlt.datasource.password}
ds-slave-1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${zlt.datasource.ip}:3306/demo_ds_slave_1?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: ${zlt.datasource.username}
password: ${zlt.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
\ No newline at end of file
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://${zlt.datasource.ip}:3306/demo_ds_0?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: ${zlt.datasource.username}
password: ${zlt.datasource.password}
ds-1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${zlt.datasource.ip}:3306/demo_ds_1?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: ${zlt.datasource.username}
password: ${zlt.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
\ No newline at end of file
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://${zlt.datasource.ip}:3306/demo_ds_alibaba?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: ${zlt.datasource.username}
password: ${zlt.datasource.password}
ds-baidu:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${zlt.datasource.ip}:3306/demo_ds_baidu?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: ${zlt.datasource.username}
password: ${zlt.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']}.user
key-generator-column-name: id
props:
sql:
show: true
\ No newline at end of file
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://${zlt.datasource.ip}:3306/demo_ds?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: ${zlt.datasource.username}
password: ${zlt.datasource.password}
ds-0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${zlt.datasource.ip}:3306/demo_ds_0?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: ${zlt.datasource.username}
password: ${zlt.datasource.password}
ds-1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${zlt.datasource.ip}:3306/demo_ds_1?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: ${zlt.datasource.username}
password: ${zlt.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
\ No newline at end of file
sharding:
jdbc:
datasource:
names: ds
ds:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://${zlt.datasource.ip}:3306/demo_ds?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull
username: ${zlt.datasource.username}
password: ${zlt.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
\ No newline at end of file
server:
port: 11001
spring:
profiles:
active: sharding-databases
application:
name: sharding-jdbc-demo
zlt:
datasource:
ip: 192.168.28.131
username: root
password: 1q2w3e4r
mybatis-plus:
mapper-locations: classpath:/mapper/*Mapper.xml
#实体扫描,多个package用逗号或者分号分隔
typeAliasesPackage: com.sharding.demo.model
global-config:
db-config:
id-type: ID_WORKER
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册