blob: 4aa234738af2bae22e882cfa28ed7eea0afa3c16 [file] [log] [blame]
package org.apache.shardingsphere.example.config;
import com.google.common.collect.Lists;
import org.apache.shardingsphere.api.config.encrypt.EncryptColumnRuleConfiguration;
import org.apache.shardingsphere.api.config.encrypt.EncryptRuleConfiguration;
import org.apache.shardingsphere.api.config.encrypt.EncryptTableRuleConfiguration;
import org.apache.shardingsphere.api.config.encrypt.EncryptorRuleConfiguration;
import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.InlineShardingStrategyConfiguration;
import org.apache.shardingsphere.example.core.api.DataSourceUtil;
import org.apache.shardingsphere.example.core.api.DatabaseType;
import org.apache.shardingsphere.orchestration.config.OrchestrationConfiguration;
import org.apache.shardingsphere.orchestration.reg.api.RegistryCenterConfiguration;
import org.apache.shardingsphere.shardingjdbc.orchestration.api.OrchestrationShardingDataSourceFactory;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.*;
public class ShardingMasterSlaveEncryptConfiguration implements ExampleConfiguration{
@Override
public DataSource getDataSource() throws SQLException {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
shardingRuleConfig.getTableRuleConfigs().add(getTableRuleConfiguration());
shardingRuleConfig.setMasterSlaveRuleConfigs(getMasterSlaveRuleConfigurations());
shardingRuleConfig.setEncryptRuleConfig(getEncryptRuleConfiguration());
return OrchestrationShardingDataSourceFactory.createDataSource(
createDataSourceMap()
, shardingRuleConfig
, new Properties()
,new OrchestrationConfiguration("orchestration-postgresql-sharding-MS-encrypt",getRegistryCenterConfiguration(),true));
}
private static TableRuleConfiguration getTableRuleConfiguration() {
TableRuleConfiguration result = new TableRuleConfiguration("t_user", "ds_${0..1}.t_user_${[0, 1]}");
result.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "ds_${user_id % 2}"));
result.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id","t_user_${user_id % 2}"));
return result;
}
private static List<MasterSlaveRuleConfiguration> getMasterSlaveRuleConfigurations() {
MasterSlaveRuleConfiguration masterSlaveRuleConfig1 = new MasterSlaveRuleConfiguration("ds_0", "demo_ds_master_0", Arrays.asList("demo_ds_master_0_slave_0", "demo_ds_master_0_slave_1"));
MasterSlaveRuleConfiguration masterSlaveRuleConfig2 = new MasterSlaveRuleConfiguration("ds_1", "demo_ds_master_1", Arrays.asList("demo_ds_master_1_slave_0", "demo_ds_master_1_slave_1"));
return Lists.newArrayList(masterSlaveRuleConfig1, masterSlaveRuleConfig2);
}
private EncryptRuleConfiguration getEncryptRuleConfiguration() {
Properties props = new Properties();
props.setProperty("aes.key.value", "123456");
EncryptorRuleConfiguration encryptorAES = new EncryptorRuleConfiguration("AES", props);
EncryptorRuleConfiguration encryptorMD5 = new EncryptorRuleConfiguration("MD5",new Properties());
Map<String, EncryptColumnRuleConfiguration> columns = new HashMap<>();
EncryptColumnRuleConfiguration columnUserName = new EncryptColumnRuleConfiguration("user_name", "user_name_cipher", "", "aes");
EncryptColumnRuleConfiguration columnPwd = new EncryptColumnRuleConfiguration("pwd_plain", "pwd_cipher", "", "md5");
columns.put("user_name",columnUserName);
columns.put("pwd",columnPwd);
EncryptTableRuleConfiguration tableConfig = new EncryptTableRuleConfiguration(columns);
EncryptRuleConfiguration result = new EncryptRuleConfiguration();
result.getEncryptors().put("aes", encryptorAES);
result.getEncryptors().put("md5",encryptorMD5);
result.getTables().put("t_user", tableConfig);
return result;
}
private static Map<String, DataSource> createDataSourceMap() {
final Map<String, DataSource> result = new HashMap<>();
result.put("demo_ds_master_0", DataSourceUtil.createDataSource("demo_ds_master_0", DatabaseType.POSTGRESQL));
result.put("demo_ds_master_0_slave_0", DataSourceUtil.createDataSource("demo_ds_master_0_slave_0", DatabaseType.POSTGRESQL));
result.put("demo_ds_master_0_slave_1", DataSourceUtil.createDataSource("demo_ds_master_0_slave_1", DatabaseType.POSTGRESQL));
result.put("demo_ds_master_1", DataSourceUtil.createDataSource("demo_ds_master_1", DatabaseType.POSTGRESQL));
result.put("demo_ds_master_1_slave_0", DataSourceUtil.createDataSource("demo_ds_master_1_slave_0", DatabaseType.POSTGRESQL));
result.put("demo_ds_master_1_slave_1", DataSourceUtil.createDataSource("demo_ds_master_1_slave_1", DatabaseType.POSTGRESQL));
return result;
}
private RegistryCenterConfiguration getRegistryCenterConfiguration() {
RegistryCenterConfiguration regConfig = new RegistryCenterConfiguration("zookeeper");
regConfig.setServerLists("localhost:2181");
regConfig.setNamespace("orchestration-raw-jdbc-postgresql");
return regConfig;
}
private static Properties getProperties() {
Properties result = new Properties();
result.setProperty("worker.id", "123");
return result;
}
}