未验证 提交 be262f10 编写于 作者: kimmking's avatar kimmking 提交者: GitHub

Merge pull request #5599 from yanyzy/dev

Support shadow rule persistence to config center
......@@ -29,5 +29,7 @@ public enum ShardingType {
SHARDING_MASTER_SLAVE,
ENCRYPT
ENCRYPT,
SHADOW
}
......@@ -24,9 +24,11 @@ import org.apache.shardingsphere.example.core.jdbc.service.OrderServiceImpl;
import org.apache.shardingsphere.example.orchestration.raw.jdbc.config.RegistryCenterConfigurationUtil;
import org.apache.shardingsphere.example.orchestration.raw.jdbc.config.cloud.CloudEncryptConfiguration;
import org.apache.shardingsphere.example.orchestration.raw.jdbc.config.cloud.CloudMasterSlaveConfiguration;
import org.apache.shardingsphere.example.orchestration.raw.jdbc.config.cloud.CloudShadowConfiguration;
import org.apache.shardingsphere.example.orchestration.raw.jdbc.config.cloud.CloudShardingDatabasesAndTablesConfiguration;
import org.apache.shardingsphere.example.orchestration.raw.jdbc.config.local.LocalEncryptConfiguration;
import org.apache.shardingsphere.example.orchestration.raw.jdbc.config.local.LocalMasterSlaveConfiguration;
import org.apache.shardingsphere.example.orchestration.raw.jdbc.config.local.LocalShadowConfiguration;
import org.apache.shardingsphere.example.orchestration.raw.jdbc.config.local.LocalShardingDatabasesAndTablesConfiguration;
import org.apache.shardingsphere.example.type.RegistryCenterType;
import org.apache.shardingsphere.example.type.ShardingType;
......@@ -47,6 +49,7 @@ public class JavaConfigurationExampleMain {
private static ShardingType shardingType = ShardingType.SHARDING_DATABASES_AND_TABLES;
// private static ShardingType shardingType = ShardingType.MASTER_SLAVE;
// private static ShardingType shardingType = ShardingType.ENCRYPT;
// private static ShardingType shardingType = ShardingType.SHADOW;
private static boolean loadConfigFromRegCenter = false;
// private static boolean loadConfigFromRegCenter = true;
......@@ -77,6 +80,9 @@ public class JavaConfigurationExampleMain {
case ENCRYPT:
configuration = loadConfigFromRegCenter ? new CloudEncryptConfiguration(centerConfigurationMap) : new LocalEncryptConfiguration(centerConfigurationMap);
break;
case SHADOW:
configuration = loadConfigFromRegCenter ? new CloudShadowConfiguration(centerConfigurationMap) : new LocalShadowConfiguration(centerConfigurationMap);
break;
default:
throw new UnsupportedOperationException(shardingType.name());
}
......
......@@ -40,6 +40,7 @@ public class YamlConfigurationExampleMain {
private static ShardingType shardingType = ShardingType.SHARDING_DATABASES_AND_TABLES;
// private static ShardingType shardingType = ShardingType.MASTER_SLAVE;
// private static ShardingType shardingType = ShardingType.ENCRYPT;
// private static ShardingType shardingType = ShardingType.SHADOW;
private static boolean loadConfigFromRegCenter = false;
// private static boolean loadConfigFromRegCenter = true;
......@@ -68,6 +69,9 @@ public class YamlConfigurationExampleMain {
case ENCRYPT:
yamlFilePath = String.format("/META-INF/%s/%s/encrypt.yaml", registryCenterType.name().toLowerCase(), loadConfigFromRegCenter ? "cloud" : "local");
return YamlOrchestrationShardingSphereDataSourceFactory.createDataSource(getFile(yamlFilePath));
case SHADOW:
yamlFilePath = String.format("/META-INF/%s/%s/shadow.yaml", registryCenterType.name().toLowerCase(), loadConfigFromRegCenter ? "cloud" : "local");
return YamlOrchestrationShardingSphereDataSourceFactory.createDataSource(getFile(yamlFilePath));
default:
throw new UnsupportedOperationException(shardingType.name());
}
......
......@@ -52,6 +52,9 @@ public class RegistryCenterConfigurationUtil {
case ENCRYPT:
instanceConfigurationMap.put("orchestration-encrypt-data-source", result);
break;
case SHADOW:
instanceConfigurationMap.put("orchestration-shadow-data-source", result);
break;
}
return instanceConfigurationMap;
}
......@@ -85,6 +88,10 @@ public class RegistryCenterConfigurationUtil {
instanceConfigurationMap.put("orchestration-encrypt-data-source", nacosResult);
instanceConfigurationMap.put("orchestration-zookeeper-encrypt-data-source", zookeeperResult);
break;
case SHADOW:
instanceConfigurationMap.put("orchestration-shadow-data-source", nacosResult);
instanceConfigurationMap.put("orchestration-zookeeper-shadow-data-source", zookeeperResult);
break;
}
return instanceConfigurationMap;
}
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.shardingsphere.example.orchestration.raw.jdbc.config.cloud;
import org.apache.shardingsphere.example.config.ExampleConfiguration;
import org.apache.shardingsphere.orchestration.center.config.CenterConfiguration;
import org.apache.shardingsphere.orchestration.center.config.OrchestrationConfiguration;
import org.apache.shardingsphere.shardingjdbc.orchestration.api.OrchestrationShardingSphereDataSourceFactory;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Map;
public class CloudShadowConfiguration implements ExampleConfiguration {
private final Map<String, CenterConfiguration> centerConfigurationMap;
public CloudShadowConfiguration(final Map<String, CenterConfiguration> centerConfigurationMap) {
this.centerConfigurationMap = centerConfigurationMap;
}
@Override
public DataSource getDataSource() throws SQLException {
return OrchestrationShardingSphereDataSourceFactory.createDataSource(new OrchestrationConfiguration(centerConfigurationMap));
}
}
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.shardingsphere.example.orchestration.raw.jdbc.config.local;
import org.apache.shardingsphere.example.config.ExampleConfiguration;
import org.apache.shardingsphere.example.core.api.DataSourceUtil;
import org.apache.shardingsphere.orchestration.center.config.CenterConfiguration;
import org.apache.shardingsphere.orchestration.center.config.OrchestrationConfiguration;
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import org.apache.shardingsphere.shardingjdbc.orchestration.api.OrchestrationShardingSphereDataSourceFactory;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class LocalShadowConfiguration implements ExampleConfiguration {
private final Map<String, CenterConfiguration> centerConfigurationMap;
public LocalShadowConfiguration(final Map<String, CenterConfiguration> centerConfigurationMap) {
this.centerConfigurationMap = centerConfigurationMap;
}
@Override
public DataSource getDataSource() throws SQLException {
return OrchestrationShardingSphereDataSourceFactory.createDataSource(createDataSourceMap(), Collections.singleton(getShadowRuleConfiguration()), new Properties(), getOrchestrationConfiguration());
}
private Map<String, DataSource> createDataSourceMap() {
Map<String, DataSource> result = new HashMap<>();
result.put("ds", DataSourceUtil.createDataSource("ds"));
result.put("shadow_ds", DataSourceUtil.createDataSource("shadow_ds"));
return result;
}
private OrchestrationConfiguration getOrchestrationConfiguration() {
return new OrchestrationConfiguration(centerConfigurationMap);
}
private ShadowRuleConfiguration getShadowRuleConfiguration() {
return new ShadowRuleConfiguration("shadow", Collections.singletonMap("ds", "shadow_ds"));
}
}
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
orchestration:
demo_yaml_ds_shadow:
orchestrationType: config_center,registry_center,metadata_center
instanceType: zookeeper
serverLists: localhost:2181
namespace: orchestration-yaml-demo
props:
overwrite: false
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
orchestration:
demo_yaml_ds_shadow:
orchestrationType: config_center,registry_center,metadata_center
instanceType: zookeeper
serverLists: localhost:2181
namespace: orchestration-yaml-demo
props:
overwrite: true
dataSources:
ds: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/ds
username: root
password:
ds_0: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/shadow_ds
username: root
password:
rules:
- !!org.apache.shardingsphere.sharding.core.yaml.config.shadow.YamlShadowRuleConfiguration
column: shadow
shadowMappings:
ds: ds_0
props:
sql.show: true
......@@ -129,7 +129,7 @@ public final class ConfigCenter {
} else if (each instanceof ShadowRuleConfiguration) {
ShadowRuleConfiguration config = (ShadowRuleConfiguration) each;
Preconditions.checkState(!config.getColumn().isEmpty() && null != config.getShadowMappings(), "No available shadow rule configuration in `%s` for orchestration.", shardingSchemaName);
// TODO process shadow
configurations.add(each);
}
}
repository.persist(node.getRulePath(shardingSchemaName), YamlEngine.marshal(new RuleRootConfigurationsYamlSwapper().swap(configurations)));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册