Skip to content

Commit ceb2976

Browse files
committed
5种分片策略和12种分片算法
5种分片策略和12种分片算法
1 parent af963df commit ceb2976

File tree

19 files changed

+1039
-0
lines changed

19 files changed

+1039
-0
lines changed

shardingsphere101/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<module>shardingsphere-2fastcode</module>
2626
<module>shardingsphere-default-strategy</module>
2727
<module>shardingsphere-autocreate-table</module>
28+
<module>shardingsphere-algorithms</module>
2829
</modules>
2930

3031
<dependencies>

shardingsphere101/shardingsphere-algorithms/README.md

Whitespace-only changes.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.shardingsphere101</groupId>
7+
<artifactId>shardingsphere101</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
</parent>
10+
<artifactId>shardingsphere-algorithms</artifactId>
11+
<version>0.0.1-SNAPSHOT</version>
12+
<name>shardingsphere-algorithms</name>
13+
14+
<build>
15+
<plugins>
16+
<plugin>
17+
<groupId>org.springframework.boot</groupId>
18+
<artifactId>spring-boot-maven-plugin</artifactId>
19+
<configuration>
20+
<excludes>
21+
<exclude>
22+
<groupId>org.projectlombok</groupId>
23+
<artifactId>lombok</artifactId>
24+
</exclude>
25+
</excludes>
26+
</configuration>
27+
</plugin>
28+
</plugins>
29+
</build>
30+
31+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.shardingsphere_101;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class AlgorithmsApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(AlgorithmsApplication.class, args);
11+
}
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.shardingsphere_101.algorithm;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import com.google.common.collect.Range;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.apache.shardingsphere.sharding.api.sharding.complex.ComplexKeysShardingAlgorithm;
7+
import org.apache.shardingsphere.sharding.api.sharding.complex.ComplexKeysShardingValue;
8+
9+
import java.util.Collection;
10+
import java.util.Map;
11+
import java.util.Properties;
12+
13+
/**
14+
* 自定义复合分片算法
15+
*
16+
* @author 公众号:程序员小富
17+
* @date 2024/03/22 11:02
18+
*/
19+
@Slf4j
20+
public class OrderComplexCustomAlgorithm implements ComplexKeysShardingAlgorithm<Long> {
21+
22+
/**
23+
* 复合分片算法进入,支持>,>=, <=,<,=,IN 和 BETWEEN AND 等操作符
24+
*
25+
* @param availableTargetNames 所有分片表的集合
26+
* @param shardingValues 多个分片健的值,并SQL中解析出来的分片值
27+
*/
28+
@Override
29+
public Collection<String> doSharding(Collection<String> availableTargetNames, ComplexKeysShardingValue<Long> shardingValues) {
30+
31+
// 多分片健和其对应的分片健范围值
32+
Map<String, Range<Long>> columnNameAndRangeValuesMap = shardingValues.getColumnNameAndRangeValuesMap();
33+
34+
// 多分片健和其对应的分片健值
35+
Map<String, Collection<Long>> columnNameAndShardingValuesMap = shardingValues.getColumnNameAndShardingValuesMap();
36+
return null;
37+
}
38+
39+
@Override
40+
public Properties getProps() {
41+
return null;
42+
}
43+
44+
/**
45+
* 初始化配置
46+
*
47+
* @param properties
48+
*/
49+
@Override
50+
public void init(Properties properties) {
51+
Object prop = properties.get("prop");
52+
log.info("配置信息:{}", JSON.toJSONString(prop));
53+
}
54+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//package com.shardingsphere_101.algorithm;
2+
//
3+
//import org.apache.shardingsphere.sharding.api.sharding.hint.HintShardingAlgorithm;
4+
//import org.apache.shardingsphere.sharding.api.sharding.hint.HintShardingValue;
5+
//
6+
//import java.util.Collection;
7+
//import java.util.Properties;
8+
//
9+
///**
10+
// * 自定义强制路由分片算法
11+
// *
12+
// * @author 公众号:程序员小富
13+
// * @date 2024/03/22 11:02
14+
// */
15+
//public class OrderHintCustomAlgorithm implements HintShardingAlgorithm<String> {
16+
//
17+
// @Override
18+
// public Collection<String> doSharding(Collection availableTargetNames, HintShardingValue shardingValue) {
19+
// return null;
20+
// }
21+
//
22+
// @Override
23+
// public Properties getProps() {
24+
// return null;
25+
// }
26+
//
27+
// @Override
28+
// public void init(Properties properties) {
29+
//
30+
// }
31+
//}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.shardingsphere_101.algorithm;
2+
3+
import com.alibaba.fastjson.JSON;
4+
import com.google.common.collect.Range;
5+
import lombok.extern.slf4j.Slf4j;
6+
import org.apache.shardingsphere.sharding.api.sharding.standard.PreciseShardingValue;
7+
import org.apache.shardingsphere.sharding.api.sharding.standard.RangeShardingValue;
8+
import org.apache.shardingsphere.sharding.api.sharding.standard.StandardShardingAlgorithm;
9+
10+
import java.util.*;
11+
12+
/**
13+
* 自定义标准分片算法
14+
*
15+
* @author 公众号:程序员小富
16+
* @date 2024/03/22 11:02
17+
*/
18+
@Slf4j
19+
public class OrderStandardCustomAlgorithm implements StandardShardingAlgorithm<Long> {
20+
21+
/**
22+
* 精准分片进入 sql中有 = 和 in 等操作符会执行
23+
*
24+
* @param availableTargetNames 所有分片表的集合
25+
* @param shardingValue 分片健的值,SQL中解析出来的分片值
26+
*/
27+
@Override
28+
public String doSharding(Collection<String> availableTargetNames,
29+
PreciseShardingValue<Long> shardingValue) {
30+
log.info("进入精准分片 = 和 in");
31+
int tableSize = availableTargetNames.size();
32+
// 真实表的前缀
33+
String tablePrefix = shardingValue.getDataNodeInfo().getPrefix();
34+
// 分片健的值
35+
long orderId = shardingValue.getValue();
36+
// 对分片健取模后确定位置
37+
long mod = orderId % tableSize;
38+
return tablePrefix + mod;
39+
}
40+
41+
/**
42+
* 范围分片进入 sql中有 between 和 < > 等操作符会执行
43+
*
44+
* @param availableTargetNames 所有分片表的集合
45+
* @param shardingValue 分片健的值,SQL中解析出来的分片值
46+
* @return
47+
*/
48+
@Override
49+
public Collection<String> doSharding(Collection<String> availableTargetNames,
50+
RangeShardingValue<Long> shardingValue) {
51+
log.info("进入范围分片 = 和 in");
52+
// 分片健值的下边界
53+
Range<Long> valueRange = shardingValue.getValueRange();
54+
Long lower = (Long) valueRange.lowerEndpoint();
55+
// 分片健值的上边界
56+
Long upper = (Long) valueRange.upperEndpoint();
57+
// 真实表的前缀
58+
String tablePrefix = shardingValue.getDataNodeInfo().getPrefix();
59+
if (lower != null && upper != null) {
60+
// 分片健的值
61+
long orderId = upper - lower;
62+
// 对分片健取模后确定位置
63+
long mod = orderId % availableTargetNames.size();
64+
return Arrays.asList(tablePrefix + mod);
65+
}
66+
//
67+
return Collections.singletonList("t_order_0");
68+
}
69+
70+
@Override
71+
public Properties getProps() {
72+
return null;
73+
}
74+
75+
/**
76+
* 初始化配置
77+
*
78+
* @param properties
79+
*/
80+
@Override
81+
public void init(Properties properties) {
82+
Object prop = properties.get("prop");
83+
log.info("配置信息:{}", JSON.toJSONString(prop));
84+
}
85+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//package com.shardingsphere_101.config;
2+
//
3+
//import com.zaxxer.hikari.HikariDataSource;
4+
//import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
5+
//import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
6+
//import org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
7+
//import org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
8+
//import org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
9+
//import org.springframework.context.annotation.Bean;
10+
//import org.springframework.context.annotation.Configuration;
11+
//
12+
//import javax.sql.DataSource;
13+
//import java.sql.SQLException;
14+
//import java.util.*;
15+
//
16+
///**
17+
// * 公众号:程序员小富
18+
// */
19+
//@Configuration
20+
//public class ShardingConfiguration {
21+
//
22+
// /**
23+
// * 配置分片数据源
24+
// * 公众号:程序员小富
25+
// */
26+
// @Bean
27+
// public DataSource getShardingDataSource() throws SQLException {
28+
// Map<String, DataSource> dataSourceMap = new HashMap<>();
29+
// dataSourceMap.put("db0", dataSource0());
30+
// dataSourceMap.put("db1", dataSource1());
31+
//
32+
// // 分片rules规则配置
33+
// ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
34+
//
35+
// // 分片算法
36+
// shardingRuleConfig.setShardingAlgorithms(getShardingAlgorithms());
37+
// // 配置 t_order 表分片规则
38+
// ShardingTableRuleConfiguration orderTableRuleConfig = new ShardingTableRuleConfiguration("t_order", "db${0..1}.t_order_${1..1000}");
39+
// orderTableRuleConfig.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "t_order_table_algorithms"));
40+
// orderTableRuleConfig.setDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "t_order_database_algorithms"));
41+
// shardingRuleConfig.getTables().add(orderTableRuleConfig);
42+
//
43+
// // 是否在控制台输出解析改造后真实执行的 SQL
44+
// Properties properties = new Properties();
45+
// properties.setProperty("sql-show", "true");
46+
//
47+
// // 创建 ShardingSphere 数据源
48+
// return ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, Collections.singleton(shardingRuleConfig), properties);
49+
// }
50+
//
51+
// /**
52+
// * 配置数据源1
53+
// * 公众号:程序员小富
54+
// */
55+
// public DataSource dataSource0() {
56+
// HikariDataSource dataSource = new HikariDataSource();
57+
// dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
58+
// dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/db0?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true");
59+
// dataSource.setUsername("root");
60+
// dataSource.setPassword("123456");
61+
// return dataSource;
62+
// }
63+
//
64+
// /**
65+
// * 配置数据源2
66+
// * 公众号:程序员小富
67+
// */
68+
// public DataSource dataSource1() {
69+
// HikariDataSource dataSource = new HikariDataSource();
70+
// dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
71+
// dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true");
72+
// dataSource.setUsername("root");
73+
// dataSource.setPassword("123456");
74+
// return dataSource;
75+
// }
76+
//
77+
// /**
78+
// * 配置分片算法
79+
// * 公众号:程序员小富
80+
// */
81+
// private Map<String, AlgorithmConfiguration> getShardingAlgorithms() {
82+
// Map<String, AlgorithmConfiguration> shardingAlgorithms = new LinkedHashMap<>();
83+
//
84+
// // 自定义分库算法
85+
// Properties databaseAlgorithms = new Properties();
86+
// databaseAlgorithms.setProperty("algorithm-expression", "db$->{order_id % 2}");
87+
// shardingAlgorithms.put("t_order_database_algorithms", new AlgorithmConfiguration("INLINE", databaseAlgorithms));
88+
//
89+
// // 自定义分表算法
90+
// Properties tableAlgorithms = new Properties();
91+
// tableAlgorithms.setProperty("algorithm-expression", "db$->{order_id % 1000}");
92+
// shardingAlgorithms.put("t_order_table_algorithms", new AlgorithmConfiguration("INLINE", tableAlgorithms));
93+
//
94+
// return shardingAlgorithms;
95+
// }
96+
//}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.shardingsphere_101.controller;
2+
3+
import org.springframework.web.bind.annotation.RequestMapping;
4+
import org.springframework.web.bind.annotation.RestController;
5+
6+
@RestController
7+
public class PingController {
8+
9+
@RequestMapping("/ping")
10+
public String ping() {
11+
return "pong";
12+
}
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.shardingsphere_101.dao;
2+
3+
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
4+
import com.shardingsphere_101.entity.OrderPo;
5+
import org.apache.ibatis.annotations.Mapper;
6+
7+
@Mapper
8+
public interface OrderMapper extends BaseMapper<OrderPo> {
9+
10+
}

0 commit comments

Comments
 (0)