Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jeecgboot集成shardingsphere相关问题 #7827

Open
ahong-czf opened this issue Feb 19, 2025 · 1 comment
Open

jeecgboot集成shardingsphere相关问题 #7827

ahong-czf opened this issue Feb 19, 2025 · 1 comment

Comments

@ahong-czf
Copy link

ahong-czf commented Feb 19, 2025

jeecgboot集成shardingsphere进行分表操作的相关问题。按照文档进行处理,但是分片规则类能进入初始化的init方法,无法执行dosharding方法进行分片处理,可能是什么原因呢
相关代码:
配置文件:

shardingsphere:
  props:
    sql-show: true
  datasource:
    names: ds0
    ds0:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/gdie-zhsd?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
      username: root
      password: 123456
      type: com.alibaba.druid.pool.DruidDataSource
  rules:
    sharding:
      binding-tables:
        - fenbiao_test
      key-generators:
        snowflake:
          type: SNOWFLAKE
          props:
            worker-id: 123
      tables:
        fenbiao_test:
          actual-data-nodes: ds0.fenbiao_test$->{0..1}
          table-strategy:
            standard:
              sharding-algorithm-name: tablefenbiao
              sharding-column: type
      sharding-algorithms:
        tablefenbiao:
          type: CLASS_BASED
          props:
            strategy: standard
            algorithmClassName: org.jeecg.modules.sharding.algorithm.StandardModTableShardAlgorithm

分片类

public class StandardModTableShardAlgorithm implements StandardShardingAlgorithm<Integer> {

  private Properties props = new Properties();
  private int mod = 2; // 默认取模值
  @Override
  public String doSharding(Collection<String> collection, PreciseShardingValue<Integer> preciseShardingValue) {
      Integer value = preciseShardingValue.getValue();
      String logicTableName = preciseShardingValue.getLogicTableName();
      // 根据分片值计算目标表名
      String targetTableName = logicTableName + (value % mod);
      // 检查目标表名是否存在
      if (collection.contains(targetTableName)) {
          return targetTableName;
      }
      throw new UnsupportedOperationException("无法找到目标表: " + targetTableName);
  }
  @Override
  public Collection<String> doSharding(Collection<String> collection,
      RangeShardingValue<Integer> rangeShardingValue) {
      Integer lowerValue = rangeShardingValue.getValueRange().lowerEndpoint();
      Integer upperValue = rangeShardingValue.getValueRange().upperEndpoint();
      String logicTableName = rangeShardingValue.getLogicTableName();
      Set<String> targetTables = new HashSet<>();
      // 遍历范围值,计算涉及的分片表
      for (int i = lowerValue; i <= upperValue; i++) {
          String targetTableName = logicTableName + (i % mod);
          if (collection.contains(targetTableName)) {
              targetTables.add(targetTableName);
          }
      }
      return targetTables;
  }
  @Override
  public Optional<String> findMatchedTargetName(Collection<String> availableTargetNames, String suffix,
      DataNodeInfo dataNodeInfo) {
      return Optional.empty();
  }
  @Override
  public Properties getProps() {
      return this.props;
  }
  @Override
  public void init(Properties properties) {
      System.out.println("初始化!!!" + properties);
      this.props = properties;
      this.mod = Integer.parseInt(properties.getProperty("mod", "2"));
  }
  @Override
  public String getType() {
      return "CUSTOM_MOD";
  }
  @Override
  public Collection<String> getTypeAliases() {
      return null;
  }

}

使用的包是5.1.2版本

@ahong-czf
Copy link
Author

ahong-czf commented Feb 20, 2025

新建一个springboot项目,同样的配置代码能够正常通过sharding插件实现分表,在jeecg中就无法实现,是否是jeecg中还有其他配置或者什么内容影响到了分表呢???

Image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant