0
点赞
收藏
分享

微信扫一扫

springboot2 shardingjdbc4 yml配置单库分表+根据年月份进行分表


文章目录


1.下面已经实现了根据年份月份进行分表,使用的是shardingJDBC ID 生成策略为 SNOWFLAKE


2.自己传ID进来也可以根据ID进行分表


3.要注意配置文件中的每个参数名的配置层级,我在进行配置的时候,就是因为参数层级位置不对的问题弄了一下午


比如:spring下面是shardingsphere 必须严格按照层级问题,重点注意


需要注意如果shardingJDBC要和druid一起使用,那么druid是spring的jar包不是springboot的jar重点注意

<?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 http://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.2.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.mayikt</groupId>
<artifactId>shardingjdbc4</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<sharding-sphere.version>4.1.1</sharding-sphere.version>
</properties>

<dependencies>
<!-- SpringBoot整合Web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- druid数据源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.11</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

<!-- shardingJDBC-->
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>${sharding-sphere.version}</version>
</dependency>
</dependencies>

</project>

全部采用YML配置进行数据源的配置
主配置源
application.yml

spring:
profiles:
active: sharding
#日期格式化
jackson:
time-zone: GMT+8
date-format: yyyy-MM-dd HH:mm:ss
datasource:
url: jdbc:mysql://127.0.0.1:3306/tg_project?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
druid:
initial-size: 5 #初始化大小
min-idle: 5 #最小
max-active: 100 #最大
max-wait: 60000 #连接超时时间
##配置间隔多久进行一次检测,检测需要关闭的空闲连接,单位毫秒
time-between-eviction-runs-millis: 60000
##指定空闲连接最少空闲多久可以被清除
min-evictable-idle-time-millis: 30000
validation-query: select 'X'
##当连接空闲时,是否执行连接测试
test-while-idle: true
##当从连接池借用连接时,是否测试该连接
test-on-borrow: false
##当连接归还到连接池时,是否测试该连接
test-on-return: false
##配置监控统计拦截的filters,去掉后监控界面sql无法统计,‘wall’ 用于防火墙
filters: wall,stat,log4j
pool-prepared-statements: true
max-pool-prepared-statement-per-connection-size: 20
max-open-prepared-statements: 20
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties: druid.stat.slowSqlMillis=200;druid.stat.logSlowSql=true
# WebStatFilter配置,说明请参考Druid Wiki,配置_配置WebStatFilter
#是否启用StatFilter默认值true
web-stat-filter:
enabled: true
url-pattern: /*
exclusions: /druid/*,*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico
session-stat-enable: true
session-stat-max-count: 10
# StatViewServlet配置,说明请参考Druid Wiki,配置_StatViewServlet配置
#是否启用StatViewServlet默认值true
stat-view-servlet:
enabled: true
url-pattern: /druid/*
reset-enable: true
login-username: admin
login-password: admin
filter:
stat:
db-type: mysql
log-slow-sql: true
slow-sql-millis: 5000
wall:
enabled: true
db-type: mysql
config:
drop-table-allow: false
resources:
static-locations: classpath:/static/**,classpath:/templates/
thymeleaf:
#prefix:指定模板所在的目录
prefix: classpath:templates/
suffix: .html
check-template-location: true
encoding: UTF-8
content-type: text/html
mode: HTML5
cache: false

mybatis:
type-aliases-package: com.taguan.entity
mapper-locations: classpath:mapper/*.xml
configuration:
map-underscore-to-camel-case: true

server:
port: 8080
tomcat:
uri-encoding: UTF-8
max-threads: 1000
min-spare-threads: 10

application-sharding.yml
shardingJDBC配置,具体配置参数,可以看官网,不做多的介绍,东西太多了

# 数据分片应用于单库分表操作
spring:
shardingsphere:
datasource:
names: db1
db1: # 数据库
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/sys-admin?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
username: root
password: root
sharding:
tables:
tg_weld_after_rule:
# 配置表的分布,表的策略db1.tg_weld_after_rule_$->{2020..2021}0$->{1..9},db1.tg_weld_after_rule_$->{2020..2021}1$->{0..2}
actual-data-nodes: db1.tg_weld_after_rule_$->{2021..2021}0$->{1..5}
# 指定tg_weld_after_rule_表 主键id 生成策略为 SNOWFLAKE
key-generator:
column: id
type: SNOWFLAKE
# 指定分片策略
table-strategy:
# inline:
## 约定id值是偶数添加到tg_weld_after_rule_0表,如果id是奇数添加到tg_weld_after_rule_1表
# sharding-column: id
# algorithm-expression: tg_weld_after_rule_$->{id%2}
standard:
#根据年月份进行分表,分表规则自定义handler
sharding-column: create_time
precise-algorithm-class-name: com.taguan.utils.USerTablePreciseShardingAlgorithm
#range-algorithm-class-name: com.taguan.utils.MyRangeShardingAlgorithm
tg_weld_after_rule_test:
# 配置表的分布,表的策略db1.tg_weld_after_rule_$->{2020..2021}0$->{1..9},db1.tg_weld_after_rule_$->{2020..2021}1$->{0..2}
actual-data-nodes: db1.tg_weld_after_rule_test_$->{2021..2021}0$->{1..5}
# 指定tg_weld_after_rule_表 主键id 生成策略为 SNOWFLAKE
key-generator:
column: id
type: SNOWFLAKE
# 指定分片策略
table-strategy:
standard:
#根据年月份进行分表,分表规则自定义handler
sharding-column: create_time
precise-algorithm-class-name: com.taguan.utils.USerTablePreciseShardingAlgorithm
range-algorithm-class-name: com.taguan.utils.MyRangeShardingAlgorithm
props:
# 打开sql输出日志
sql:
show: true

分片策略采用
精确分片算法
对应PreciseShardingAlgorithm,用于处理使用单一键作为分片键的=与IN进行分片的场景。需要配合StandardShardingStrategy使用。

具体可以看shardingjdbc官网:​​https://shardingsphere.apache.org/document/legacy/3.x/document/cn/features/sharding/concept/sharding/​​ 采用精确分片算法 PreciseShardingAlgorithm 可以不使用 范围分片算法 RangeShardingAlgorithm

采用范围分片算 RangeShardingAlgorithm 就必须使用精确分片算法 PreciseShardingAlgorithm

分表分库策略类:
USerTablePreciseShardingAlgorithm

public class USerTablePreciseShardingAlgorithm implements PreciseShardingAlgorithm<Date> {

private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

@Override
public String doSharding(Collection<String> collection, PreciseShardingValue<Date> preciseShardingValue) {
try {
String tableName = preciseShardingValue.getLogicTableName();
log.info("tableName--------------{}",tableName);
String dataTime = formatter.format(preciseShardingValue.getValue());
String year = dataTime.substring(0, 4);
System.out.println("-------------------------"+year);
String key = dataTime.substring(5, 7);
System.out.println("-------------------------"+key);
return tableName.concat("_").concat(year).concat(key);
}catch (Exception e){
throw new IllegalArgumentException("没有匹配到库:" + preciseShardingValue.getValue());
}


}
}


举报

相关推荐

0 条评论