所需依赖

1
2
3
4
5
6
7
8
9
10
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core</artifactId>
<version>5.3.0</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.33</version>
</dependency>

application.yml 文件添加

1
2
3
4
5
main:
allow-bean-definition-overriding: true
datasource:
driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver
url: jdbc:shardingsphere:classpath:sharding.yaml

添加分片的yaml文件,命名为sharding.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
dataSources:
ds:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://127.0.0.1:3306/mysql-monitor?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: 123456

rules:
- !SHARDING
tables:
server_status_history:
actualDataNodes: ds.server_status_history
tableStrategy:
standard:
shardingColumn: create_time
shardingAlgorithmName: auto-custom
shardingAlgorithms:
auto-custom:
type: CLASS_BASED
props:
strategy: standard
algorithmClassName: com.fc.v2.common.sharding.CustomTimeShardingAlgorithm
# auto-interval:
# type: AUTO_INTERVAL
# props:
# datetime-lower: '2023-01-01 00:00:00'
# datetime-upper: '2023-12-01 00:00:00'
# # 60 * 60 * 24 * 30=30天
# sharding-seconds: '2592000'
# interval:
# type: INTERVAL
# props:
# datetime-pattern: 'yyyy-MM'
# datetime-lower: '2023-01'
# datetime-upper: '2023-12'
# sharding-suffix-pattern: 'yyyyMM'
# # 间隔大小
# datetime-interval-amount: 1
# datetime-interval-unit: 'Months'

props:
sql-show: false