caol64 commented on issue #16725:
URL:
https://github.com/apache/shardingsphere/issues/16725#issuecomment-1212655026
> @wlklnn
>
> * Your sharing looks very cool! It's the first time I see such an
application.
>
> @strongduanmu
>
> * Is it possible to provide a `Util` class within `ShardingSphere JDBC`
that wraps the method `updateShardRuleActualDataNodes()` for updating
`actual-data-nodes` mentioned by @wlklnn ? I believe many people are looking
for how to update it dynamically, and it is convenient to simplify the process.
>
> ```java
> package com.lingh.ao;
>
> import
org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
> import org.apache.shardingsphere.infra.config.RuleConfiguration;
> import
org.apache.shardingsphere.sharding.algorithm.config.AlgorithmProvidedShardingRuleConfiguration;
> import
org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
> import org.springframework.scheduling.annotation.EnableScheduling;
> import org.springframework.scheduling.annotation.Scheduled;
> import org.springframework.stereotype.Component;
>
> import javax.annotation.Resource;
> import java.util.Collection;
> import java.util.LinkedList;
>
> @Component
> public class InitActualDataNodesAO {
> @Resource
> private ShardingSphereDataSource shardingSphereDataSource;
> private final String logicTableNameForTOrder = "t_order";
> private final String schemaNameForTOrder = "sharding_db";
>
> public void testSharding() {
> // generate actualDataNodes
> String actualDataNodes = "ds-0.t_order_$->{[1..8]}";
>
> this.updateShardRuleActualDataNodes(shardingSphereDataSource,
schemaNameForTOrder, logicTableNameForTOrder, actualDataNodes);
> }
>
> private void updateShardRuleActualDataNodes(ShardingSphereDataSource
dataSource, String schemaName, String logicTableName, String
newActualDataNodes) {
> // Context manager.
> org.apache.shardingsphere.mode.manager.ContextManager
contextManager = dataSource.getContextManager();
>
> // Rule configuration.
> Collection<RuleConfiguration> newRuleConfigList = new
LinkedList<>();
> Collection<RuleConfiguration> oldRuleConfigList =
dataSource.getContextManager()
> .getMetaDataContexts()
> .getMetaData(schemaName)
> .getRuleMetaData()
> .getConfigurations();
>
> for (RuleConfiguration oldRuleConfig : oldRuleConfigList) {
> if (oldRuleConfig instanceof
AlgorithmProvidedShardingRuleConfiguration) {
>
> // Algorithm provided sharding rule configuration
> AlgorithmProvidedShardingRuleConfiguration
oldAlgorithmConfig = (AlgorithmProvidedShardingRuleConfiguration) oldRuleConfig;
> AlgorithmProvidedShardingRuleConfiguration
newAlgorithmConfig = new AlgorithmProvidedShardingRuleConfiguration();
>
> // Sharding table rule configuration Collection
> Collection<ShardingTableRuleConfiguration>
newTableRuleConfigList = new LinkedList<>();
> Collection<ShardingTableRuleConfiguration>
oldTableRuleConfigList = oldAlgorithmConfig.getTables();
>
> oldTableRuleConfigList.forEach(oldTableRuleConfig -> {
> if
(logicTableName.equals(oldTableRuleConfig.getLogicTable())) {
> ShardingTableRuleConfiguration newTableRuleConfig
= new ShardingTableRuleConfiguration(oldTableRuleConfig.getLogicTable(),
newActualDataNodes);
>
newTableRuleConfig.setTableShardingStrategy(oldTableRuleConfig.getTableShardingStrategy());
>
newTableRuleConfig.setDatabaseShardingStrategy(oldTableRuleConfig.getDatabaseShardingStrategy());
>
newTableRuleConfig.setKeyGenerateStrategy(oldTableRuleConfig.getKeyGenerateStrategy());
>
> newTableRuleConfigList.add(newTableRuleConfig);
> } else {
> newTableRuleConfigList.add(oldTableRuleConfig);
> }
> });
>
> newAlgorithmConfig.setTables(newTableRuleConfigList);
>
newAlgorithmConfig.setAutoTables(oldAlgorithmConfig.getAutoTables());
>
newAlgorithmConfig.setBindingTableGroups(oldAlgorithmConfig.getBindingTableGroups());
>
newAlgorithmConfig.setBroadcastTables(oldAlgorithmConfig.getBroadcastTables());
>
newAlgorithmConfig.setDefaultDatabaseShardingStrategy(oldAlgorithmConfig.getDefaultDatabaseShardingStrategy());
>
newAlgorithmConfig.setDefaultTableShardingStrategy(oldAlgorithmConfig.getDefaultTableShardingStrategy());
>
newAlgorithmConfig.setDefaultKeyGenerateStrategy(oldAlgorithmConfig.getDefaultKeyGenerateStrategy());
>
newAlgorithmConfig.setDefaultShardingColumn(oldAlgorithmConfig.getDefaultShardingColumn());
>
newAlgorithmConfig.setShardingAlgorithms(oldAlgorithmConfig.getShardingAlgorithms());
>
newAlgorithmConfig.setKeyGenerators(oldAlgorithmConfig.getKeyGenerators());
>
> newRuleConfigList.add(newAlgorithmConfig);
> }
> }
>
> // update context
> contextManager.alterRuleConfiguration(schemaName,
newRuleConfigList);
> }
> }
> ```
@linghengqian In 5.1.2 version, ShardingSphereDataSource doesn't have
getContextManager method. Is this solution needs to update?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]