提交 5831ad5d 编写于 作者: Z zfh

🍻 提交 ShardSphere 分库分表 demo

上级 a49bbbd4
# SpringBoot + ShardSphere 分库分表示例
---
- [x] 分库:ds-0, ds-1,对应 /resources 下的 ds-0.sql 和 ds-1.sql
- [x] 分表:每个库下都有 t_user, t_blog 两张逻辑表的三个分片表
- [x] 广播表:t_config,在 ds-0 和 ds-1 下具有完全相同的内容
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>spring-boot-shardsphere-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-shardsphere-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- shardingsphere的jar包 -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC1</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-core-common</artifactId>
<version>4.0.0-RC1</version>
</dependency>
<!--阿里数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.1</version>
</dependency>
<!-- Mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
<!-- <scope>runtime</scope>-->
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
package com.example.shardspheredemo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
//@ComponentScan(value={"org.apache.shardingsphere", "com.example.shardspheredemo.dao"})
@SpringBootApplication
public class SpringBootShardsphereDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootShardsphereDemoApplication.class, args);
}
}
package com.example.shardspheredemo.bean;
import java.util.Date;
/**
* @author zhangfanghao
* @version 1.0
* @since 2020/10/27 15:48
*/
public class Blog {
private Long id;
private String title;
private String content;
private Long userId;
private Date updateTime;
// tmp
private String userName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public Date getUpdateTime() {
return updateTime;
}
public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
package com.example.shardspheredemo.bean;
import java.util.Date;
/**
* @author zhangfanghao
* @version 1.0
* @since 2020/10/27 15:48
*/
public class Config {
private Long id;
private String remark;
private Date createTime;
private Date lastModifyTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
public Date getLastModifyTime() {
return lastModifyTime;
}
public void setLastModifyTime(Date lastModifyTime) {
this.lastModifyTime = lastModifyTime;
}
}
package com.example.shardspheredemo.bean;
import java.util.Date;
/**
* @author zhangfanghao
* @version 1.0
* @since 2020/10/27 15:48
*/
public class User {
private Long userId;
private String name;
private String password;
private Date createTime;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
package com.example.shardspheredemo.controller;
import com.example.shardspheredemo.bean.Blog;
import com.example.shardspheredemo.dao.BlogDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @author zhangfanghao
* @version 1.0
* @since 2020/10/28 9:46
*/
@RestController
@RequestMapping("blog")
public class BlogAction {
@Autowired
private BlogDAO blogDAO;
@PostMapping("")
public Object save(Blog blog) {
if (blog == null || StringUtils.isEmpty(blog.getTitle())) {
return "标题不能为空";
} else if (StringUtils.isEmpty(blog.getContent())) {
return "正文不能为空";
} else if (blog.getUserId() == null) {
return "请设置 userId";
}
blog.setUpdateTime(new Date());
if (blog.getId() == null) {
return blogDAO.insert(blog);
} else {
return blogDAO.update(blog);
}
}
@GetMapping("list")
public Object list(Integer currentIndex, Integer limit) {
if (currentIndex == null || currentIndex < 0 || limit == null || limit <= 0) {
currentIndex = 0;
limit = 10;
}
Map<String, Object> map = new HashMap<>();
map.put("count", blogDAO.count());
map.put("list", blogDAO.find(currentIndex, limit));
return map;
}
@GetMapping(value = "{id}")
public Object findById(@PathVariable Long id) {
return blogDAO.findById(id);
}
@GetMapping(value = "userId/{userId}")
public Object findByUserId(@PathVariable Long userId) {
return blogDAO.findByUserId(userId);
}
}
package com.example.shardspheredemo.controller;
import com.example.shardspheredemo.bean.Config;
import com.example.shardspheredemo.dao.ConfigDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
/**
* @author zhangfanghao
* @version 1.0
* @since 2020/10/27 19:49
*/
@RestController
@RequestMapping("config")
public class ConfigAction {
@Autowired
private ConfigDAO configDAO;
@PostMapping("")
public Object save(Config config) {
if (config == null || StringUtils.isEmpty(config.getRemark())) {
return "remark 不能为空";
}
config.setCreateTime(new Date());
config.setLastModifyTime(new Date());
return configDAO.insert(config);
}
}
package com.example.shardspheredemo.controller;
import com.example.shardspheredemo.bean.User;
import com.example.shardspheredemo.dao.UserDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @author zhangfanghao
* @version 1.0
* @since 2020/10/27 15:54
*/
@RestController
@RequestMapping(value = "user")
public class UserAction {
@Autowired
private UserDAO orderDAO;
@PostMapping("")
public Object save(User user) {
if (user == null || StringUtils.isEmpty(user.getName())) {
return "name 不能为空";
} else if (StringUtils.isEmpty(user.getPassword())) {
return "password 不能为空";
}
user.setCreateTime(new Date());
return orderDAO.insert(user);
}
@GetMapping(value = "list")
public Object list(Integer currentIndex, Integer limit) {
if (currentIndex == null || currentIndex < 0 || limit == null || limit <= 0) {
currentIndex = 0;
limit = 10;
}
Map<String, Object> map = new HashMap<>();
map.put("count", orderDAO.count());
map.put("list", orderDAO.find(currentIndex, limit));
return map;
}
@GetMapping("{id}")
public String findById(@PathVariable Integer id) {
return "id: " + id;
}
}
package com.example.shardspheredemo.dao;
import com.example.shardspheredemo.bean.Blog;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
import java.util.List;
@Mapper
@Component
public interface BlogDAO {
boolean insert(Blog blog);
boolean update(Blog blog);
List<Blog> find(@Param("currentIndex") Integer currentIndex, @Param("limit") Integer limit);
int count();
Blog findById(@Param("id") Long id);
List<Blog> findByUserId(@Param("userId") Long userId);
boolean deleteById(@Param("id") Long id);
}
package com.example.shardspheredemo.dao;
import com.example.shardspheredemo.bean.Config;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Component;
@Mapper
@Component
public interface ConfigDAO {
boolean insert(Config config);
}
package com.example.shardspheredemo.dao;
import com.example.shardspheredemo.bean.User;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Component;
import java.util.List;
@Mapper
@Component
public interface UserDAO {
boolean insert(User user);
int count();
List<User> find(@Param("currentIndex") Integer currentIndex, @Param("limit") Integer limit);
}
# 服务端口
server:
port: 8223
servlet:
context-path: /api
# 服务名
spring:
# 定义两个全局数据源
shardingsphere:
datasource:
names: ds-0,ds-1
ds-0:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/ds-0?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
username: root
password: root
# 配置数据源 ds-1
ds-1:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/ds-1?useUnicode=true&characterEncoding=utf8&tinyInt1isBit=false&useSSL=false&serverTimezone=GMT
username: root
password: root
sharding:
tables:
t_user:
actual-data-nodes: ds-$->{0..1}.t_user_$->{0..2}
### 分库策略
database-strategy:
inline:
# 分库分片键
sharding-column: userId
# 分库分片算法
algorithm-expression: ds-$->{userId % 2}
# 分表策略
table-strategy:
inline:
# 分表分片健
sharding-column: userId
# 分表算法
algorithm-expression: t_user_$->{userId % 3}
key-generator:
# 自增主键字段
column: userId
# 自增主键ID 生成方案
type: SNOWFLAKE
t_blog:
actual-data-nodes: ds-$->{0..1}.t_blog_$->{0..2}
### 分库策略
database-strategy:
inline:
# 分库分片键
sharding-column: userId
# 分库分片算法
algorithm-expression: ds-$->{userId % 2}
# 分表策略
table-strategy:
inline:
# 分表分片健
sharding-column: userId
# 分表算法
algorithm-expression: t_blog_$->{userId % 3}
key-generator:
# 自增主键字段
column: id
# 自增主键ID 生成方案
type: SNOWFLAKE
# 绑定表关系
binding-tables: t_user, t_blog #,t_order_item
broadcast-tables: t_config
# 是否开启 SQL解析日志
props:
sql:
show: true
main:
allow-bean-definition-overriding: true
# 配置xml 的扫描路径
mybatis:
mapper-locations: classpath:mapper/*.xml
spring:
profiles:
active: demo
\ No newline at end of file