springboot2.6 + shardingsphere5.2.1 + dynamic动态数据源 + Mybatis-Plus
shardingsphere
·
Maven
<properties>
<snakeyaml.version>1.33</snakeyaml.version>
</properties>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc6</artifactId>
<version>4.0</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId>
<version>5.2.1</version>
</dependency>
数据源配置
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource;
import com.baomidou.dynamic.datasource.provider.AbstractDataSourceProvider;
import com.baomidou.dynamic.datasource.provider.DynamicDataSourceProvider;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DataSourceProperty;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceAutoConfiguration;
import com.baomidou.dynamic.datasource.spring.boot.autoconfigure.DynamicDataSourceProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Map;
@Configuration
@AutoConfigureBefore({DynamicDataSourceAutoConfiguration.class, SpringBootConfiguration.class})
public class DataSourceConfiguration {
/**
* 动态数据源配置项
*/
@Autowired
private DynamicDataSourceProperties properties;
/**
* 使用shardingSphereDataSource 自动装载的 DataSource
* 要加@Lazy
*/
@Lazy
@Resource(name = "shardingSphereDataSource")
private DataSource shardingSphereDataSource;
@Bean
public DynamicDataSourceProvider dynamicDataSourceProvider() {
Map<String, DataSourceProperty> datasourceMap = properties.getDatasource();
return new AbstractDataSourceProvider() {
@Override
public Map<String, DataSource> loadDataSources() {
Map<String, DataSource> dataSourceMap = createDataSourceMap(datasourceMap);
// 将 shardingjdbc 管理的数据源也交给动态数据源管理
dataSourceMap.put("sharding", shardingSphereDataSource);
return dataSourceMap;
}
};
}
/**
* 将动态数据源设置为首选的
* 当spring存在多个数据源时,自动注入的是首选的对象
* 设置为主要的数据源之后,就可以支持shardingjdbc原生的配置方式了
*
* @return
*/
@Primary
@Bean
public DataSource dataSource(DynamicDataSourceProvider dynamicDataSourceProvider) {
DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource();
dataSource.setPrimary(properties.getPrimary());
dataSource.setStrict(properties.getStrict());
dataSource.setStrategy(properties.getStrategy());
dataSource.setProvider(dynamicDataSourceProvider);
dataSource.setP6spy(properties.getP6spy());
dataSource.setSeata(properties.getSeata());
return dataSource;
}
}
yml配置
spring:
shardingsphere:
datasource:
names: db0,db1
db0:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
url: jdbc:sqlserver://127.0.0.1:1433;SelectMethod=cursor;DatabaseName=mydatabases1
username: sa
password: xxx
db1:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
url: jdbc:sqlserver://127.0.0.1:1433;SelectMethod=cursor;DatabaseName=mydatabases2
username: sa
password: xxx
props:
sql-show: true
rules:
sharding:
tables:
produc_tdata:
actual_data_nodes: db$->{0..1}.product_data_$->{0..10}
database-strategy:
standard:
sharding-column: column_name1
sharding-algorithm-name: alg_custom_column_name1
table-strategy:
standard:
sharding-column: column_name2
sharding-algorithm-name: alg_custom_column_name2
sharding-algorithms:
alg_custom_column_name1:
type: INLINE
props:
algorithm-expression: db$->{column_name1 % 2}
alg_custom_column_name2:
type: INLINE
props:
algorithm-expression: product_data_$->{ column_name2 % 2}
Mapper
public interface SharingSphereMapper {
/**
* 指定查询用ShardingSphere查询
*/
@DS("sharding")
List<ProductDataVo> myDataList(@Param("et") ProductDataVo productDataVo);
}
注意
- 表字段尽量用 xxx_xxx,带有下划线的字段名,避免和SharingSphere 关键字冲突
- 版本的SharingSphere 4版本和5版本 配置的差异比较大,切换版本要看下官方文档属性配置或者看源码

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)