shardingsphere5.3+dynamic-datasource+spring boot 3实现注意事项
项目依赖注意事项
- 动态数据数据源使用的是mybatis-plus的插件 dynamic-datasource
使用spring3的话需要引入 dynamic-datasource-spring-boot3-starter的依赖,如果使用 dynamic-datasource-spring-boot-starter的依赖会报错
maven:
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-dependencies -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.1.1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis.spring.boot/mybatis-spring-boot-starter -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.shardingsphere/shardingsphere-jdbc-core -->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core</artifactId>
<version>5.3.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baomidou/dynamic-datasource-spring-boot3-starter -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>dynamic-datasource-spring-boot3-starter</artifactId>
<version>4.1.2</version>
</dependency>
我是使用的是jdk17所以
2. 如果使用的高版本jdk 可能需要引入jaxb-api
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-core</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.activation/activation -->
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jaxb/jaxb-runtime -->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>2.3.0-b170127.1453</version>
</dependency>
- spring cloud 高版本需要注意引入,使用了bootstrap.yml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
</dependency>
配置文件注意事项
1.需要注意sharding.yml配置中databaseName的值.可以自定义,官网默认为logic_db,这里测试用就命名为test1
不用在意我具体分库分表规则,官网也有比较详细的配置说明:shardingsphere-jdbc/yaml-config
*建议先排除动态数据源的依赖,测试sharding配置是否正确,是否可以正常使用.
sharding.yaml:
# !!!数据源名称要和动态数据源中配置的名称一致
databaseName: test1
# sharding配置不用在意,保证正确就可以,具体参看官网文档说明
dataSources:
customDataBase:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://***:3306/customDataBase?useUnicode=true&characterEncoding=utf-8&useSSL=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai
username: root
password: 123456
rules:
- !SHARDING
tables: # 数据分片规则配置
t_user_: # 逻辑表名称
actualDataNodes: customDataBase.t_user_$->{0..1} # 由数据源名 + 表名组成(参考 Inline 语法规则)
databaseStrategy: # 分库策略,缺省表示使用默认分库策略,以下的分片策略只能选其一
none:
tableStrategy: # 分表策略
standard: # 用于单分片键的标准分片场景
shardingColumn: id # 分片列名称
shardingAlgorithmName: user_inline
# 分片算法配置
shardingAlgorithms:
user_inline:
type: INLINE
props:
algorithm-expression: t_user_$->{id % 2}
props:
sql-show: true
动态数据源注意事项
1.动态数据源中需要引入sharding数据源.
2.引入的sharding数据源名称要和sharding配置中databaseName的值要一致,不然无法识别数据源.
bootstrap.yml
spring:
datasource:
# 不使用动态数据源 引入sharding配置
# driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver
# url: jdbc:shardingsphere:classpath:sharding.yaml
dynamic:
primary: master # 设置默认的数据源或者数据源组,默认值即为master
strict: false # 严格匹配数据源,默认false. true未匹配到指定数据源时抛异常,false使用默认数据源
datasource:
master:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://***:3306/***?useUnicode=true&characterEncoding=UTF-8&rewriteBatchedStatements=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai
username: root
password: 123456
# 需要和sharding.yml--->databaseName的值要一致,不然无法识别数据源
test1:
driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver
url: jdbc:shardingsphere:classpath:sharding.yaml
测试
测试类:
import com.**.services.system.service.imp.SysUserServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
/**
* <p>类的详细说明</p>
*
* @author /
* @version 1.0.0 /
* <p> 1.0.0 2023/8/1 / 创建</p>
*/
@SpringBootTest
@Slf4j
public class TestA {
@Autowired
private SysUserServiceImpl service;
/**
* 测试向master数据源插入不做不分库分表,和test1(sharding)数据源插入做分库分表
*/
@Test
public void test1(){
//传入id
service.test(9);
}
}
实现类: @DS注解实现动态源切换
import com.baomidou.dynamic.datasource.annotation.DS;
import com.***.services.system.dao.SysUserMapper;
import com.***.services.system.service.ISysUserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
//向master插入
//@DS("master")
//向sharding插入
@DS("test1")
@Service
public class SysUserServiceImpl implements ISysUserService {
@Resource
private SysUserMapper sysUserMapper;
@Override
public void test(Integer id) {
sysUserMapper.test(id);
}
}
测试结果:
实现类里使用了@DS(“test1”)成功进行了分表操作