注:我使用的是shardingsphere-jdbc-core-spring-boot-starter 5.1.1
未使用HikariCP连接池
一、 pom依赖
<?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.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>gitee.simonzhaojia</groupId>
<artifactId>custom-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>mystarter</name>
<description>Custom Spring Boot Starter</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- 一个starter必须要有的依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 用于读取additional-spring-configuration-metadata.json中的提示 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- lombok插件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.3.8</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<version>5.1.1</version>
</dependency>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<!--Mybatis-Plus-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.3.2</version>
</dependency>
</dependencies>
</project>
二、 controller层
package com.example.sharding.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.sharding.entity.User;
import com.example.sharding.service.UserService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;
import java.util.Random;
import static org.springframework.http.HttpStatus.*;
/**
* @author chen
*/
@Slf4j
@RestController
@RequestMapping("/users")
public class UserController {
private final Random random = new Random();
@Resource
UserService userService;
@GetMapping("/list")
@ResponseStatus(OK)
public List<User> list() {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.last("limit 10");
return userService.list(queryWrapper);
}
@ApiOperation(value = "创建", notes = "创建")
@PostMapping
@ResponseStatus(CREATED)
public User create(@Validated @RequestBody User user) {
user.setAge(random.nextInt(100) + 1);
userService.save(user);
return user;
}
@GetMapping("{id}")
@ResponseStatus(OK)
@ApiResponses({
@ApiResponse(code = 200, message = "OK"),
@ApiResponse(code = 404, message = "Demand_not_found\nDemand_is_invalid")
})
@ApiOperation(value = "单个获取", notes = "单个获取")
public User get(@PathVariable Long id) {
log.debug("Cet Demand by: {}", id);
return userService.getById(id);
}
@DeleteMapping("{id}")
@ResponseStatus(NO_CONTENT)
@ApiResponse(code = 204, message = "No Content")
@ApiOperation(value = "删除", notes = "删除")
public void delete(@PathVariable Long id) {
userService.removeById(id);
}
}
二、 实体
package com.example.sharding.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import nonapi.io.github.classgraph.json.Id;
import java.io.Serializable;
/**
* 描述:角色实体
*
* @author chen
*/
@Data
@TableName("userEntity") //必须和yml文件中的tableName保持一致否则抛出会找不到表
public class User implements Serializable {
private static final long serialVersionUID = 337361630075002456L;
@Id
private Long id;
private String name;
private Integer age;
}
三、mappper层
package com.example.sharding.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.sharding.entity.User;
/**
* @author chen
*/
public interface UserMapper extends BaseMapper<User> {
}
四、service 层
package com.example.sharding.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.sharding.entity.User;
/**
* @author chen
*/
public interface UserService extends IService<User> {
}
五、impl层
package com.example.sharding.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.sharding.entity.User;
import com.example.sharding.mapper.UserMapper;
import com.example.sharding.service.UserService;
import org.springframework.stereotype.Service;
/**
* @author chen
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
}
六、配置文件如下:
1、application.yml
#服务端口号
server:
port: 9096
#swagger 的名称 properties
name: sharding主从分离
spring:
main:
allow-bean-definition-overriding: true
mvc:
pathmatch:
matching-strategy: ant_path_matcher
jmx:
default-domain: njgis1
profiles:
include: dev
mybatis-plus:
#批量注册指定包下的类
type-aliases-package: com.example.sharding
mapper-locations:
- classpath*:/mapper/*.xml
- classpath*:/mapper/**/*.xml
configuration:
map-underscore-to-camel-case: true
#日志打印
logging:
level:
com:
example: DEBUG
2、application-dev.yml
spring:
shardingsphere:
mode:
type: Memory # 内存模式,元数据保存在当前进程中
datasource:
names: master-test$->{0..1},slave-test$->{0..1}
master-test0: # 跟上面的数据源对应
driver-class-name: org.postgresql.Driver
type: com.alibaba.druid.pool.DruidDataSource # 连接池
url: jdbc:postgresql://192.168.72.130:5432/ds1 #?currentSchema=authority #数据库连接
username: postgres
password: 123456
master-test1: # 跟上面的数据源对应
driver-class-name: org.postgresql.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:postgresql://192.168.72.130:5432/ds0 #?currentSchema=authority #数据库连接
username: postgres
password: 123456
slave-test0: # 跟上面的数据源对应
driver-class-name: org.postgresql.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:postgresql://192.168.72.130:5433/ds1 #?currentSchema=authority #数据库连接
username: postgres
password: 123456
slave-test1: # 跟上面的数据源对应
driver-class-name: org.postgresql.Driver
type: com.alibaba.druid.pool.DruidDataSource
url: jdbc:postgresql://192.168.72.130:5433/ds0 #?currentSchema=authority #数据库连接
username: postgres
password: 123456
rules: # 配置分库分表以及读写分离的规则
sharding: # 配置分库分表规则
tables:
userEntity: # 和实体中的 tableName 必须一致 否则会找不到表
actual-data-nodes: master-test$->{0..1}.user$->{0..1} # 实际节点名称,格式为 库名$->{0..n1}.表名$->{0..n2}
database-strategy: # 分库策略
standard: # 标准分库策略
sharding-column: id # 分库列名
sharding-algorithm-name: id-mod # 分库算法名字
table-strategy: # 分表策略
standard: # 标准分表策略
sharding-column: age # 分表列名
sharding-algorithm-name: age-mod # 分表算法名字
key-generate-strategy:
#主键
column: id
# 雪花算法
key-generator-name: snowflake
sharding-algorithms: # 配置分库和分表的算法
age-mod: # 分库算法名字
type: MOD # 算法类型为取模
props: # 算法配置的键名,所有算法配置都需要在props下
sharding-count: 2 # 分片数量
id-mod: # 分表算法名字
type: MOD # 算法类型为取模
props: # 算法配置的键名,所有算法配置都需要在props下
sharding-count: 2 # 分片数量
readwrite-splitting: # 配置读写分离规则
data-sources: # 数据源
master-test0: # 这个可以随便取,带有区分意义即可,比如这里表示的是主库test0的规则
type: Static # 静态类型
load-balancer-name: ROUND_ROBIN # 负载均衡算法名字
props: # 具体读写数据源需要配置在props下
write-data-source-name: master-test0 # 写数据源
read-data-source-names: slave-test0 # 读数据源
master-test1:
type: Static # 静态类型
load-balancer-name: round_robin # 负载均衡算法名字
props: # 具体读写数据源需要配置在props下
write-data-source-name: master-test1 # 写数据源
read-data-source-names: slave-test1 # 读数据源
load-balancers: # 负载均衡配置
round_robin: # 跟上面负载均衡算法的名字对应
type: ROUND_ROBIN # 负载均衡算法
props:
sql-show: true # 打印SQL
format: true
八、运行结果
1、查询分库分表
2、添加
九、数据库
**需要新建2个库ds0 ds1 库中有2个表user0 user1 **
十、访问
ip:端口/doc.html