From d5113f0e0565865ab711f3a6bfa67b549454693c Mon Sep 17 00:00:00 2001 From: sunbufu Date: Sun, 14 Jul 2019 21:07:39 +0800 Subject: [PATCH] 1. rename ConfigurationPrinter.printConfiguration() to ConfigurationLogger.log() 2. move configuration log statement from XXXRule to XXXDataBase for JDBC 3. move configuration log statement from XXXRule to Bootstrap and XXXSchema's renew() for Proxy --- .../shardingsphere/core/rule/EncryptRule.java | 5 +- .../core/rule/MasterSlaveRule.java | 5 - .../core/rule/ShardingRule.java | 3 - .../core/util/ConfigurationLogger.java | 127 ++++++++++++ .../core/util/ConfigurationPrinter.java | 111 ---------- .../core/util/ConfigurationLoggerTest.java | 196 ++++++++++++++++++ .../core/util/ConfigurationPrinterTest.java | 153 -------------- .../src/test/resources/logback-test.xml | 2 +- .../src/test/resources/logback-test.xml | 2 +- .../src/test/resources/logback-test.xml | 2 +- .../src/test/resources/logback-test.xml | 2 +- .../src/test/resources/logback-test.xml | 2 +- .../core/datasource/EncryptDataSource.java | 3 + .../datasource/MasterSlaveDataSource.java | 5 + .../core/datasource/ShardingDataSource.java | 3 + .../src/test/resources/logback-test.xml | 2 +- .../src/test/resources/logback-test.xml | 2 +- .../src/test/resources/logback-test.xml | 2 +- .../backend/schema/EncryptSchema.java | 3 + .../backend/schema/MasterSlaveSchema.java | 3 + .../backend/schema/ShardingSchema.java | 3 + .../shardingproxy/Bootstrap.java | 46 ++-- .../context/ShardingProxyContext.java | 10 +- .../src/test/resources/logback-test.xml | 2 +- .../src/test/resources/logback-test.xml | 2 +- .../src/test/resources/logback-test.xml | 2 +- .../src/test/resources/logback-test.xml | 2 +- .../src/test/resources/logback-test.xml | 2 +- .../src/test/resources/logback-test.xml | 2 +- .../src/test/resources/logback-test.xml | 2 +- .../src/test/resources/logback-test.xml | 2 +- 31 files changed, 396 insertions(+), 312 deletions(-) create mode 100644 sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/util/ConfigurationLogger.java delete mode 100644 sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/util/ConfigurationPrinter.java create mode 100644 sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/util/ConfigurationLoggerTest.java delete mode 100644 sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/util/ConfigurationPrinterTest.java diff --git a/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/EncryptRule.java b/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/EncryptRule.java index 078b7bbb68974..83e95a32e5b6d 100644 --- a/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/EncryptRule.java +++ b/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/EncryptRule.java @@ -20,7 +20,6 @@ import lombok.Getter; import org.apache.shardingsphere.api.config.encryptor.EncryptRuleConfiguration; import org.apache.shardingsphere.core.strategy.encrypt.ShardingEncryptorEngine; -import org.apache.shardingsphere.core.util.ConfigurationPrinter; import java.util.Collection; @@ -43,10 +42,8 @@ public EncryptRule() { public EncryptRule(final EncryptRuleConfiguration encryptRuleConfiguration) { this.encryptRuleConfig = encryptRuleConfiguration; encryptorEngine = new ShardingEncryptorEngine(encryptRuleConfiguration); - - ConfigurationPrinter.printConfiguration("encryptRule", encryptRuleConfiguration); } - + /** * Get encrypt table names. * diff --git a/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/MasterSlaveRule.java b/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/MasterSlaveRule.java index 52d8f20542a61..9d5cdc7ee87d1 100644 --- a/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/MasterSlaveRule.java +++ b/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/MasterSlaveRule.java @@ -21,7 +21,6 @@ import org.apache.shardingsphere.api.config.masterslave.LoadBalanceStrategyConfiguration; import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration; import org.apache.shardingsphere.core.spi.algorithm.masterslave.MasterSlaveLoadBalanceAlgorithmServiceLoader; -import org.apache.shardingsphere.core.util.ConfigurationPrinter; import org.apache.shardingsphere.spi.masterslave.MasterSlaveLoadBalanceAlgorithm; import java.util.Collection; @@ -52,8 +51,6 @@ public MasterSlaveRule(final String name, final String masterDataSourceName, fin this.loadBalanceAlgorithm = null == loadBalanceAlgorithm ? new MasterSlaveLoadBalanceAlgorithmServiceLoader().newService() : loadBalanceAlgorithm; masterSlaveRuleConfiguration = new MasterSlaveRuleConfiguration(name, masterDataSourceName, slaveDataSourceNames, new LoadBalanceStrategyConfiguration(this.loadBalanceAlgorithm.getType(), this.loadBalanceAlgorithm.getProperties())); - - ConfigurationPrinter.printConfiguration("masterSlaveRule", masterSlaveRuleConfiguration); } public MasterSlaveRule(final MasterSlaveRuleConfiguration config) { @@ -62,8 +59,6 @@ public MasterSlaveRule(final MasterSlaveRuleConfiguration config) { slaveDataSourceNames = config.getSlaveDataSourceNames(); loadBalanceAlgorithm = createMasterSlaveLoadBalanceAlgorithm(config.getLoadBalanceStrategyConfiguration()); masterSlaveRuleConfiguration = config; - - ConfigurationPrinter.printConfiguration("masterSlaveRule", config); } private MasterSlaveLoadBalanceAlgorithm createMasterSlaveLoadBalanceAlgorithm(final LoadBalanceStrategyConfiguration loadBalanceStrategyConfiguration) { diff --git a/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/ShardingRule.java b/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/ShardingRule.java index 36ca19ea3c881..48bf8699de6b5 100644 --- a/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/ShardingRule.java +++ b/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/rule/ShardingRule.java @@ -34,7 +34,6 @@ import org.apache.shardingsphere.core.strategy.route.ShardingStrategyFactory; import org.apache.shardingsphere.core.strategy.route.hint.HintShardingStrategy; import org.apache.shardingsphere.core.strategy.route.none.NoneShardingStrategy; -import org.apache.shardingsphere.core.util.ConfigurationPrinter; import org.apache.shardingsphere.spi.keygen.ShardingKeyGenerator; import java.util.ArrayList; @@ -86,8 +85,6 @@ public ShardingRule(final ShardingRuleConfiguration shardingRuleConfig, final Co defaultShardingKeyGenerator = createDefaultKeyGenerator(shardingRuleConfig.getDefaultKeyGeneratorConfig()); masterSlaveRules = createMasterSlaveRules(shardingRuleConfig.getMasterSlaveRuleConfigs()); encryptRule = createEncryptRule(shardingRuleConfig.getEncryptRuleConfig()); - - ConfigurationPrinter.printConfiguration("shardingRule", shardingRuleConfig); } private Collection createTableRules(final ShardingRuleConfiguration shardingRuleConfig) { diff --git a/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/util/ConfigurationLogger.java b/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/util/ConfigurationLogger.java new file mode 100644 index 0000000000000..5ba1900a7867d --- /dev/null +++ b/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/util/ConfigurationLogger.java @@ -0,0 +1,127 @@ +/* + * 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.core.util; + +import java.util.Properties; + +import org.apache.shardingsphere.api.config.RuleConfiguration; +import org.apache.shardingsphere.api.config.encryptor.EncryptRuleConfiguration; +import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration; +import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration; +import org.apache.shardingsphere.core.rule.Authentication; +import org.apache.shardingsphere.core.yaml.engine.YamlEngine; +import org.apache.shardingsphere.core.yaml.swapper.impl.AuthenticationYamlSwapper; +import org.apache.shardingsphere.core.yaml.swapper.impl.EncryptRuleConfigurationYamlSwapper; +import org.apache.shardingsphere.core.yaml.swapper.impl.MasterSlaveRuleConfigurationYamlSwapper; +import org.apache.shardingsphere.core.yaml.swapper.impl.ShardingRuleConfigurationYamlSwapper; + +import lombok.extern.slf4j.Slf4j; + +/** + * Configuration printer class. + * + * @author sunbufu + */ +@Slf4j +public final class ConfigurationLogger { + + private ConfigurationLogger() { } + + /** + * log properties configuration. + * + * @param propsConfiguration properties configuration + */ + public static void log(final Properties propsConfiguration) { + if (null != propsConfiguration) { + log(propsConfiguration.getClass().getSimpleName(), YamlEngine.marshal(propsConfiguration)); + } + } + + /** + * log EncryptRuleConfiguration. + * + * @param encryptRuleConfiguration encryptRule configuration + */ + public static void log(final EncryptRuleConfiguration encryptRuleConfiguration) { + if (null != encryptRuleConfiguration) { + log(encryptRuleConfiguration.getClass().getSimpleName(), + YamlEngine.marshal(new EncryptRuleConfigurationYamlSwapper().swap(encryptRuleConfiguration))); + } + } + + /** + * log ruleConfiguration. + * + * @param ruleConfiguration ruleConfiguration + */ + public static void log(final RuleConfiguration ruleConfiguration) { + if (null != ruleConfiguration) { + if (ruleConfiguration instanceof ShardingRuleConfiguration) { + ConfigurationLogger.log((ShardingRuleConfiguration) ruleConfiguration); + } else if (ruleConfiguration instanceof MasterSlaveRuleConfiguration) { + ConfigurationLogger.log((MasterSlaveRuleConfiguration) ruleConfiguration); + } else if (ruleConfiguration instanceof EncryptRuleConfiguration) { + ConfigurationLogger.log((EncryptRuleConfiguration) ruleConfiguration); + } + } + } + + /** + * log ShardingRuleConfiguration. + * + * @param shardingRuleConfiguration shardingRule configuration + */ + public static void log(final ShardingRuleConfiguration shardingRuleConfiguration) { + if (null != shardingRuleConfiguration) { + log(shardingRuleConfiguration.getClass().getSimpleName(), + YamlEngine.marshal(new ShardingRuleConfigurationYamlSwapper().swap(shardingRuleConfiguration))); + } + } + + /** + * log MasterSlaveRuleConfiguration. + * + * @param masterSlaveRuleConfiguration masterSlaveRule configuration + */ + public static void log(final MasterSlaveRuleConfiguration masterSlaveRuleConfiguration) { + if (null != masterSlaveRuleConfiguration) { + log(masterSlaveRuleConfiguration.getClass().getSimpleName(), + YamlEngine.marshal(new MasterSlaveRuleConfigurationYamlSwapper().swap(masterSlaveRuleConfiguration))); + } + } + + /** + * log AuthenticationConfiguration. + * + * @param authenticationConfiguration authentication configuration + */ + public static void log(final Authentication authenticationConfiguration) { + if (null != authenticationConfiguration) { + log(authenticationConfiguration.getClass().getSimpleName(), + YamlEngine.marshal(new AuthenticationYamlSwapper().swap(authenticationConfiguration))); + } + } + + /** + * log configuration log. + * + * @param base base node name + * @param yamlStr yaml string + */ + public static void log(final String base, final String yamlStr) { + log.info("{}\n{}", base, yamlStr); + } + +} diff --git a/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/util/ConfigurationPrinter.java b/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/util/ConfigurationPrinter.java deleted file mode 100644 index c5ca08fb1e75e..0000000000000 --- a/sharding-core/sharding-core-common/src/main/java/org/apache/shardingsphere/core/util/ConfigurationPrinter.java +++ /dev/null @@ -1,111 +0,0 @@ -/* - * 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.core.util; - -import java.util.Map; -import java.util.Properties; - -import org.apache.shardingsphere.api.config.encryptor.EncryptRuleConfiguration; -import org.apache.shardingsphere.api.config.masterslave.MasterSlaveRuleConfiguration; -import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration; -import org.apache.shardingsphere.core.yaml.engine.YamlEngine; -import org.apache.shardingsphere.core.yaml.swapper.impl.EncryptRuleConfigurationYamlSwapper; -import org.apache.shardingsphere.core.yaml.swapper.impl.MasterSlaveRuleConfigurationYamlSwapper; -import org.apache.shardingsphere.core.yaml.swapper.impl.ShardingRuleConfigurationYamlSwapper; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Configuration printer class. - * - * @author sunbufu - */ -public class ConfigurationPrinter { - - private static Logger log = LoggerFactory.getLogger(ConfigurationPrinter.class); - - /** - * set ConfigurationPrinter's log. - * - * @param log new log - */ - public static void setLog(final Logger log) { - ConfigurationPrinter.log = log; - } - - /** - * print properties configuration. - * - * @param base base node name - * @param propsConfiguration properties configuration - */ - public static void printConfiguration(final String base, final Properties propsConfiguration) { - print(base, YamlEngine.marshal(propsConfiguration)); - } - - /** - * print EncryptRuleConfiguration. - * - * @param base base node name. - * @param encryptRuleConfiguration encryptRule configuration - */ - public static void printConfiguration(final String base, final EncryptRuleConfiguration encryptRuleConfiguration) { - print(base, YamlEngine.marshal(new EncryptRuleConfigurationYamlSwapper().swap(encryptRuleConfiguration))); - } - - /** - * print ShardingRuleConfiguration. - * - * @param base base node name - * @param shardingRuleConfiguration shardingRule configuration - */ - public static void printConfiguration(final String base, - final ShardingRuleConfiguration shardingRuleConfiguration) { - print(base, YamlEngine.marshal(new ShardingRuleConfigurationYamlSwapper().swap(shardingRuleConfiguration))); - } - - /** - * print MasterSlaveRuleConfiguration. - * - * @param base base node name - * @param masterSlaveRuleConfiguration masterSlaveRule configuration - */ - public static void printConfiguration(final String base, - final MasterSlaveRuleConfiguration masterSlaveRuleConfiguration) { - print(base, - YamlEngine.marshal(new MasterSlaveRuleConfigurationYamlSwapper().swap(masterSlaveRuleConfiguration))); - } - - /** - * print map configuration. - * - * @param base base node name - * @param mapConfiguration map configuration - */ - public static void printConfiguration(final String base, final Map mapConfiguration) { - print(base, YamlEngine.marshal(mapConfiguration)); - } - - /** - * print configuration log. - * - * @param base base node name - * @param yamlStr yaml string - */ - public static void print(final String base, final String yamlStr) { - log.info("{}\n{}", base, yamlStr); - } - -} diff --git a/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/util/ConfigurationLoggerTest.java b/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/util/ConfigurationLoggerTest.java new file mode 100644 index 0000000000000..aeaba9f986ace --- /dev/null +++ b/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/util/ConfigurationLoggerTest.java @@ -0,0 +1,196 @@ +/* + * 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.core.util; + +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doAnswer; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.Arrays; +import java.util.Collections; +import java.util.Properties; + +import org.apache.shardingsphere.api.config.RuleConfiguration; +import org.apache.shardingsphere.api.config.encryptor.EncryptRuleConfiguration; +import org.apache.shardingsphere.api.config.encryptor.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.core.rule.Authentication; +import org.apache.shardingsphere.core.rule.ProxyUser; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.invocation.InvocationOnMock; +import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.stubbing.Answer; +import org.slf4j.Logger; + +@RunWith(MockitoJUnitRunner.class) +public class ConfigurationLoggerTest { + + @Mock + private Logger log; + + @Before + public void setLog() throws NoSuchFieldException, IllegalAccessException { + Field field = ConfigurationLogger.class.getDeclaredField("log"); + setFinalStatic(field, log); + } + + private static void setFinalStatic(final Field field, final Object newValue) + throws NoSuchFieldException, IllegalAccessException { + field.setAccessible(true); + Field modifiersField = Field.class.getDeclaredField("modifiers"); + modifiersField.setAccessible(true); + modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); + field.set(null, newValue); + } + + public void assertEqualsWithLogInfo(final String base, final String yamlStr) { + doAnswer(new Answer() { + @Override + public Void answer(InvocationOnMock invocationOnMock) { + Assert.assertEquals(base, invocationOnMock.getArgument(1)); + Assert.assertEquals(yamlStr, invocationOnMock.getArgument(2)); + return null; + } + }).when(log).info(anyString(), anyString(), anyString()); + } + + @Test + public void logPropsConfiguration() { + Properties properties = new Properties(); + properties.put("masterDataSourceName", "master_ds"); + properties.put("slaveDataSourceNames", Arrays.asList("slave_ds_0", "slave_ds_1")); + + String base = "Properties"; + String yamlStr = + "slaveDataSourceNames:\n" + "- slave_ds_0\n" + "- slave_ds_1\n" + "masterDataSourceName: master_ds\n"; + assertEqualsWithLogInfo(base, yamlStr); + + ConfigurationLogger.log(properties); + } + + @Test + public void logEncryptRuleConfiguration() { + EncryptRuleConfiguration encryptRuleConfiguration = new EncryptRuleConfiguration(); + Properties properties = new Properties(); + properties.put("aes.key.value", "123456abc"); + EncryptorRuleConfiguration encryptorRuleConfiguration = + new EncryptorRuleConfiguration("aes", "user.user_name", properties); + encryptRuleConfiguration.getEncryptorRuleConfigs().put("encryptor_aes", encryptorRuleConfiguration); + + String base = "EncryptRuleConfiguration"; + String yamlStr = "encryptors:\n" + " encryptor_aes:\n" + " assistedQueryColumns: ''\n" + " props:\n" + + " aes.key.value: 123456abc\n" + " qualifiedColumns: user.user_name\n" + " type: aes\n"; + assertEqualsWithLogInfo(base, yamlStr); + + ConfigurationLogger.log(encryptRuleConfiguration); + } + + @Test + public void logShardingRuleConfiguration() { + ShardingRuleConfiguration shardingRuleConfiguration = new ShardingRuleConfiguration(); + TableRuleConfiguration tableRuleConfiguration = new TableRuleConfiguration("user", "ds_${0}.user_${0..1}"); + shardingRuleConfiguration.getTableRuleConfigs().add(tableRuleConfiguration); + + String base = "ShardingRuleConfiguration"; + String yamlStr = + "tables:\n" + " user:\n" + " actualDataNodes: ds_${0}.user_${0..1}\n" + " logicTable: user\n"; + assertEqualsWithLogInfo(base, yamlStr); + + ConfigurationLogger.log(shardingRuleConfiguration); + } + + @Test + public void logMasterSlaveRuleConfiguration() { + MasterSlaveRuleConfiguration masterSlaveRuleConfiguration = + new MasterSlaveRuleConfiguration("ms_ds", "master_ds", Arrays.asList("slave_ds_0", "slave_ds_1")); + + String base = "MasterSlaveRuleConfiguration"; + String yamlStr = "masterDataSourceName: master_ds\n" + "name: ms_ds\n" + "slaveDataSourceNames:\n" + + "- slave_ds_0\n" + "- slave_ds_1\n"; + assertEqualsWithLogInfo(base, yamlStr); + + ConfigurationLogger.log(masterSlaveRuleConfiguration); + } + + @Test + public void logRuleConfiguration(){ + String base, yamlStr; + // EncryptRuleConfiguration + EncryptRuleConfiguration encryptRuleConfiguration = new EncryptRuleConfiguration(); + Properties properties = new Properties(); + properties.put("aes.key.value", "123456abc"); + EncryptorRuleConfiguration encryptorRuleConfiguration = + new EncryptorRuleConfiguration("aes", "user.user_name", properties); + encryptRuleConfiguration.getEncryptorRuleConfigs().put("encryptor_aes", encryptorRuleConfiguration); + + base = "EncryptRuleConfiguration"; + yamlStr = "encryptors:\n" + " encryptor_aes:\n" + " assistedQueryColumns: ''\n" + " props:\n" + + " aes.key.value: 123456abc\n" + " qualifiedColumns: user.user_name\n" + " type: aes\n"; + assertEqualsWithLogInfo(base, yamlStr); + + ConfigurationLogger.log((RuleConfiguration) encryptRuleConfiguration); + + // ShardingRuleConfiguration + ShardingRuleConfiguration shardingRuleConfiguration = new ShardingRuleConfiguration(); + TableRuleConfiguration tableRuleConfiguration = new TableRuleConfiguration("user", "ds_${0}.user_${0..1}"); + shardingRuleConfiguration.getTableRuleConfigs().add(tableRuleConfiguration); + + base = "ShardingRuleConfiguration"; + yamlStr = + "tables:\n" + " user:\n" + " actualDataNodes: ds_${0}.user_${0..1}\n" + " logicTable: user\n"; + assertEqualsWithLogInfo(base, yamlStr); + + ConfigurationLogger.log((RuleConfiguration) shardingRuleConfiguration); + + // MasterSlaveRuleConfiguration + MasterSlaveRuleConfiguration masterSlaveRuleConfiguration = + new MasterSlaveRuleConfiguration("ms_ds", "master_ds", Arrays.asList("slave_ds_0", "slave_ds_1")); + + base = "MasterSlaveRuleConfiguration"; + yamlStr = "masterDataSourceName: master_ds\n" + "name: ms_ds\n" + "slaveDataSourceNames:\n" + + "- slave_ds_0\n" + "- slave_ds_1\n"; + assertEqualsWithLogInfo(base, yamlStr); + + ConfigurationLogger.log((RuleConfiguration) masterSlaveRuleConfiguration); + } + + @Test + public void logAuthenticationConfiguration() { + Authentication authentication = new Authentication(); + authentication.getUsers().put("root", new ProxyUser("123456", Collections.singletonList("sharding_db"))); + + String base = "Authentication"; + String yamlStr = "users:\n" + " root:\n" + " authorizedSchemas: sharding_db\n" + " password: '123456'\n"; + assertEqualsWithLogInfo(base, yamlStr); + + ConfigurationLogger.log(authentication); + } + + @Test + public void log() { + String base = "base"; + String yamlStr = "yamlStr"; + assertEqualsWithLogInfo(base, yamlStr); + + ConfigurationLogger.log(base, yamlStr); + } + +} \ No newline at end of file diff --git a/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/util/ConfigurationPrinterTest.java b/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/util/ConfigurationPrinterTest.java deleted file mode 100644 index 83a4996fcfa6f..0000000000000 --- a/sharding-core/sharding-core-common/src/test/java/org/apache/shardingsphere/core/util/ConfigurationPrinterTest.java +++ /dev/null @@ -1,153 +0,0 @@ -/* - * 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.core.util; - -import static org.mockito.ArgumentMatchers.anyString; -import static org.mockito.Mockito.doAnswer; -import static org.mockito.Mockito.mock; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; - -import org.apache.shardingsphere.api.config.encryptor.EncryptRuleConfiguration; -import org.apache.shardingsphere.api.config.encryptor.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.junit.Assert; -import org.junit.Before; -import org.junit.Test; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; -import org.slf4j.Logger; - -public class ConfigurationPrinterTest { - - private Logger log; - - @Before - public void setLog(){ - log = mock(Logger.class); - ConfigurationPrinter.setLog(log); - } - - public void assertEqualsWithLogInfo(final String base, final String yamlStr){ - doAnswer(new Answer() { - @Override - public Void answer(InvocationOnMock invocationOnMock) { - Assert.assertEquals(base, invocationOnMock.getArgument(1)); - Assert.assertEquals(yamlStr, invocationOnMock.getArgument(2)); - return null; - } - }).when(log).info(anyString(), anyString(), anyString()); - } - - @Test - public void printMapConfiguration() { - Map configurationMap = new HashMap<>(); - configurationMap.put("masterDataSourceName", "master_ds"); - configurationMap.put("slaveDataSourceNames", Arrays.asList("slave_ds_0", "slave_ds_1")); - - String base = "masterSlaveRule"; - String yamlStr = "slaveDataSourceNames:\n" + - "- slave_ds_0\n" + - "- slave_ds_1\n" + - "masterDataSourceName: master_ds\n"; - assertEqualsWithLogInfo(base, yamlStr); - - ConfigurationPrinter.printConfiguration(base, configurationMap); - } - - @Test - public void printPropsConfiguration() { - Properties properties = new Properties(); - properties.put("masterDataSourceName", "master_ds"); - properties.put("slaveDataSourceNames", Arrays.asList("slave_ds_0", "slave_ds_1")); - - String base = "masterSlaveRule"; - String yamlStr = "slaveDataSourceNames:\n" + - "- slave_ds_0\n" + - "- slave_ds_1\n" + - "masterDataSourceName: master_ds\n"; - assertEqualsWithLogInfo(base, yamlStr); - - ConfigurationPrinter.printConfiguration(base, properties); - } - - @Test - public void printEncryptRuleConfiguration() { - EncryptRuleConfiguration encryptRuleConfiguration = new EncryptRuleConfiguration(); - Properties properties = new Properties(); - properties.put("aes.key.value", "123456abc"); - EncryptorRuleConfiguration encryptorRuleConfiguration = - new EncryptorRuleConfiguration("aes", "user.user_name", properties); - encryptRuleConfiguration.getEncryptorRuleConfigs().put("encryptor_aes", encryptorRuleConfiguration); - - String base = "encryptRule"; - String yamlStr = "encryptors:\n" + - " encryptor_aes:\n" + - " assistedQueryColumns: ''\n" + - " props:\n" + - " aes.key.value: 123456abc\n" + - " qualifiedColumns: user.user_name\n" + - " type: aes\n"; - assertEqualsWithLogInfo(base, yamlStr); - - ConfigurationPrinter.printConfiguration(base, encryptRuleConfiguration); - } - - @Test - public void printShardingRuleConfiguration() { - ShardingRuleConfiguration shardingRuleConfiguration = new ShardingRuleConfiguration(); - TableRuleConfiguration tableRuleConfiguration = new TableRuleConfiguration("user", "ds_${0}.user_${0..1}"); - shardingRuleConfiguration.getTableRuleConfigs().add(tableRuleConfiguration); - - String base = "shardingRule"; - String yamlStr = "tables:\n" + - " user:\n" + - " actualDataNodes: ds_${0}.user_${0..1}\n" + - " logicTable: user\n"; - assertEqualsWithLogInfo(base, yamlStr); - - ConfigurationPrinter.printConfiguration(base, shardingRuleConfiguration); - } - - @Test - public void printMasterSlaveRuleConfiguration() { - MasterSlaveRuleConfiguration masterSlaveRuleConfiguration = - new MasterSlaveRuleConfiguration("ms_ds", "master_ds", Arrays.asList("slave_ds_0", "slave_ds_1")); - - String base = "masterSlaveRule"; - String yamlStr = "masterDataSourceName: master_ds\n" + - "name: ms_ds\n" + - "slaveDataSourceNames:\n" + - "- slave_ds_0\n" + - "- slave_ds_1\n"; - assertEqualsWithLogInfo(base, yamlStr); - - ConfigurationPrinter.printConfiguration("masterSlaveRule", masterSlaveRuleConfiguration); - } - - @Test - public void print() { - String base = "base"; - String yamlStr = "yamlStr"; - assertEqualsWithLogInfo(base, yamlStr); - - ConfigurationPrinter.print(base, yamlStr); - } - -} \ No newline at end of file diff --git a/sharding-core/sharding-core-common/src/test/resources/logback-test.xml b/sharding-core/sharding-core-common/src/test/resources/logback-test.xml index 49ce12c44289d..cade0c6eb570f 100644 --- a/sharding-core/sharding-core-common/src/test/resources/logback-test.xml +++ b/sharding-core/sharding-core-common/src/test/resources/logback-test.xml @@ -25,7 +25,7 @@ - + diff --git a/sharding-core/sharding-core-entry/src/test/resources/logback-test.xml b/sharding-core/sharding-core-entry/src/test/resources/logback-test.xml index 44cbaeb5cdf8b..e180e2b6fec20 100644 --- a/sharding-core/sharding-core-entry/src/test/resources/logback-test.xml +++ b/sharding-core/sharding-core-entry/src/test/resources/logback-test.xml @@ -26,7 +26,7 @@ - + diff --git a/sharding-core/sharding-core-execute/src/test/resources/logback-test.xml b/sharding-core/sharding-core-execute/src/test/resources/logback-test.xml index 44cbaeb5cdf8b..e180e2b6fec20 100644 --- a/sharding-core/sharding-core-execute/src/test/resources/logback-test.xml +++ b/sharding-core/sharding-core-execute/src/test/resources/logback-test.xml @@ -26,7 +26,7 @@ - + diff --git a/sharding-core/sharding-core-rewrite/src/test/resources/logback-test.xml b/sharding-core/sharding-core-rewrite/src/test/resources/logback-test.xml index 44cbaeb5cdf8b..e180e2b6fec20 100644 --- a/sharding-core/sharding-core-rewrite/src/test/resources/logback-test.xml +++ b/sharding-core/sharding-core-rewrite/src/test/resources/logback-test.xml @@ -26,7 +26,7 @@ - + diff --git a/sharding-integration-test/sharding-jdbc-test/src/test/resources/logback-test.xml b/sharding-integration-test/sharding-jdbc-test/src/test/resources/logback-test.xml index 44cbaeb5cdf8b..e180e2b6fec20 100644 --- a/sharding-integration-test/sharding-jdbc-test/src/test/resources/logback-test.xml +++ b/sharding-integration-test/sharding-jdbc-test/src/test/resources/logback-test.xml @@ -26,7 +26,7 @@ - + diff --git a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/EncryptDataSource.java b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/EncryptDataSource.java index 3e9aeddf4acef..231ae91ac6eb8 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/EncryptDataSource.java +++ b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/EncryptDataSource.java @@ -29,6 +29,7 @@ import org.apache.shardingsphere.core.metadata.table.TableMetaData; import org.apache.shardingsphere.core.parse.entry.EncryptSQLParseEntry; import org.apache.shardingsphere.core.rule.EncryptRule; +import org.apache.shardingsphere.core.util.ConfigurationLogger; import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.EncryptConnection; import org.apache.shardingsphere.shardingjdbc.jdbc.unsupported.AbstractUnsupportedOperationDataSource; import org.apache.shardingsphere.spi.database.DatabaseType; @@ -74,6 +75,8 @@ public class EncryptDataSource extends AbstractUnsupportedOperationDataSource im @SneakyThrows public EncryptDataSource(final DataSource dataSource, final EncryptRuleConfiguration encryptRuleConfiguration, final Properties props) { + ConfigurationLogger.log(encryptRuleConfiguration); + ConfigurationLogger.log(props); this.dataSource = dataSource; databaseType = getDatabaseType(); encryptRule = new EncryptRule(encryptRuleConfiguration); diff --git a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/MasterSlaveDataSource.java b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/MasterSlaveDataSource.java index 91c22cedafca9..85ec3ff33d62b 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/MasterSlaveDataSource.java +++ b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/MasterSlaveDataSource.java @@ -22,6 +22,7 @@ import org.apache.shardingsphere.core.constant.properties.ShardingProperties; import org.apache.shardingsphere.core.parse.entry.MasterSlaveSQLParseEntry; import org.apache.shardingsphere.core.rule.MasterSlaveRule; +import org.apache.shardingsphere.core.util.ConfigurationLogger; import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.AbstractDataSourceAdapter; import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.MasterSlaveConnection; import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.metadata.CachedDatabaseMetaData; @@ -53,6 +54,8 @@ public class MasterSlaveDataSource extends AbstractDataSourceAdapter { public MasterSlaveDataSource(final Map dataSourceMap, final MasterSlaveRuleConfiguration masterSlaveRuleConfig, final Properties props) throws SQLException { super(dataSourceMap); + ConfigurationLogger.log(masterSlaveRuleConfig); + ConfigurationLogger.log(props); cachedDatabaseMetaData = createCachedDatabaseMetaData(dataSourceMap); this.masterSlaveRule = new MasterSlaveRule(masterSlaveRuleConfig); parseEngine = new MasterSlaveSQLParseEntry(getDatabaseType()); @@ -61,6 +64,8 @@ public MasterSlaveDataSource(final Map dataSourceMap, final public MasterSlaveDataSource(final Map dataSourceMap, final MasterSlaveRule masterSlaveRule, final Properties props) throws SQLException { super(dataSourceMap); + ConfigurationLogger.log(masterSlaveRule.getMasterSlaveRuleConfiguration()); + ConfigurationLogger.log(props); cachedDatabaseMetaData = createCachedDatabaseMetaData(dataSourceMap); this.masterSlaveRule = masterSlaveRule; shardingProperties = new ShardingProperties(null == props ? new Properties() : props); diff --git a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/ShardingDataSource.java b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/ShardingDataSource.java index be29ab34a251b..66a1cef27ed39 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/ShardingDataSource.java +++ b/sharding-jdbc/sharding-jdbc-core/src/main/java/org/apache/shardingsphere/shardingjdbc/jdbc/core/datasource/ShardingDataSource.java @@ -20,6 +20,7 @@ import com.google.common.base.Preconditions; import lombok.Getter; import org.apache.shardingsphere.core.rule.ShardingRule; +import org.apache.shardingsphere.core.util.ConfigurationLogger; import org.apache.shardingsphere.shardingjdbc.jdbc.adapter.AbstractDataSourceAdapter; import org.apache.shardingsphere.shardingjdbc.jdbc.core.ShardingContext; import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.ShardingConnection; @@ -49,6 +50,8 @@ public ShardingDataSource(final Map dataSourceMap, final Sha public ShardingDataSource(final Map dataSourceMap, final ShardingRule shardingRule, final Properties props) throws SQLException { super(dataSourceMap); + ConfigurationLogger.log(shardingRule.getShardingRuleConfig()); + ConfigurationLogger.log(props); checkDataSourceType(dataSourceMap); shardingContext = new ShardingContext(getDataSourceMap(), shardingRule, getDatabaseType(), props); } diff --git a/sharding-jdbc/sharding-jdbc-core/src/test/resources/logback-test.xml b/sharding-jdbc/sharding-jdbc-core/src/test/resources/logback-test.xml index a8167dcc4ddb4..fe1d5aa5d835b 100644 --- a/sharding-jdbc/sharding-jdbc-core/src/test/resources/logback-test.xml +++ b/sharding-jdbc/sharding-jdbc-core/src/test/resources/logback-test.xml @@ -26,7 +26,7 @@ - + diff --git a/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/logback-test.xml b/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/logback-test.xml index f83f9d0047e6d..0802913124e90 100644 --- a/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/logback-test.xml +++ b/sharding-jdbc/sharding-jdbc-orchestration/src/test/resources/logback-test.xml @@ -25,7 +25,7 @@ - + diff --git a/sharding-orchestration/sharding-orchestration-core/src/test/resources/logback-test.xml b/sharding-orchestration/sharding-orchestration-core/src/test/resources/logback-test.xml index 49ce12c44289d..cade0c6eb570f 100644 --- a/sharding-orchestration/sharding-orchestration-core/src/test/resources/logback-test.xml +++ b/sharding-orchestration/sharding-orchestration-core/src/test/resources/logback-test.xml @@ -25,7 +25,7 @@ - + diff --git a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/EncryptSchema.java b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/EncryptSchema.java index 62a0d1f4275b4..fd7103c729874 100644 --- a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/EncryptSchema.java +++ b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/EncryptSchema.java @@ -28,6 +28,7 @@ import org.apache.shardingsphere.core.parse.entry.EncryptSQLParseEntry; import org.apache.shardingsphere.core.rule.EncryptRule; import org.apache.shardingsphere.core.rule.ShardingRule; +import org.apache.shardingsphere.core.util.ConfigurationLogger; import org.apache.shardingsphere.orchestration.internal.registry.config.event.EncryptRuleChangedEvent; import org.apache.shardingsphere.shardingproxy.config.yaml.YamlDataSourceParameter; @@ -37,6 +38,7 @@ * Encrypt schema. * * @author panjuan + * @author sunbufu */ @Getter public final class EncryptSchema extends LogicSchema { @@ -71,6 +73,7 @@ private ShardingMetaData createShardingMetaData() { @Subscribe @SneakyThrows public synchronized void renew(final EncryptRuleChangedEvent encryptRuleChangedEvent) { + ConfigurationLogger.log(encryptRuleChangedEvent.getEncryptRuleConfiguration()); encryptRule = new EncryptRule(encryptRuleChangedEvent.getEncryptRuleConfiguration()); parseEngine = new EncryptSQLParseEntry(LogicSchemas.getInstance().getDatabaseType(), metaData.getTable()); } diff --git a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/MasterSlaveSchema.java b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/MasterSlaveSchema.java index 32b3c4bda6020..954092bccb572 100644 --- a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/MasterSlaveSchema.java +++ b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/MasterSlaveSchema.java @@ -27,6 +27,7 @@ import org.apache.shardingsphere.core.parse.entry.MasterSlaveSQLParseEntry; import org.apache.shardingsphere.core.rule.MasterSlaveRule; import org.apache.shardingsphere.core.rule.ShardingRule; +import org.apache.shardingsphere.core.util.ConfigurationLogger; import org.apache.shardingsphere.orchestration.internal.registry.config.event.MasterSlaveRuleChangedEvent; import org.apache.shardingsphere.orchestration.internal.registry.state.event.DisabledStateChangedEvent; import org.apache.shardingsphere.orchestration.internal.registry.state.schema.OrchestrationShardingSchema; @@ -39,6 +40,7 @@ * Master-slave schema. * * @author panjuan + * @author sunbufu */ @Getter public final class MasterSlaveSchema extends LogicSchema { @@ -78,6 +80,7 @@ private ShardingMetaData createShardingMetaData() { @Subscribe public synchronized void renew(final MasterSlaveRuleChangedEvent masterSlaveRuleChangedEvent) { if (getName().equals(masterSlaveRuleChangedEvent.getShardingSchemaName())) { + ConfigurationLogger.log(masterSlaveRuleChangedEvent.getMasterSlaveRuleConfiguration()); masterSlaveRule = new OrchestrationMasterSlaveRule(masterSlaveRuleChangedEvent.getMasterSlaveRuleConfiguration()); } } diff --git a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/ShardingSchema.java b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/ShardingSchema.java index 26778675989cc..41ab4ae93fcc9 100644 --- a/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/ShardingSchema.java +++ b/sharding-proxy/sharding-proxy-backend/src/main/java/org/apache/shardingsphere/shardingproxy/backend/schema/ShardingSchema.java @@ -33,6 +33,7 @@ import org.apache.shardingsphere.core.parse.sql.statement.ddl.DropTableStatement; import org.apache.shardingsphere.core.rule.MasterSlaveRule; import org.apache.shardingsphere.core.rule.ShardingRule; +import org.apache.shardingsphere.core.util.ConfigurationLogger; import org.apache.shardingsphere.orchestration.internal.registry.config.event.ShardingRuleChangedEvent; import org.apache.shardingsphere.orchestration.internal.registry.state.event.DisabledStateChangedEvent; import org.apache.shardingsphere.orchestration.internal.registry.state.schema.OrchestrationShardingSchema; @@ -51,6 +52,7 @@ * @author panjuan * @author zhaojun * @author wangkai + * @author sunbufu */ @Getter public final class ShardingSchema extends LogicSchema { @@ -83,6 +85,7 @@ private ShardingMetaData createShardingMetaData() { @Subscribe public synchronized void renew(final ShardingRuleChangedEvent shardingRuleChangedEvent) { if (getName().equals(shardingRuleChangedEvent.getShardingSchemaName())) { + ConfigurationLogger.log(shardingRuleChangedEvent.getShardingRuleConfiguration()); shardingRule = new OrchestrationShardingRule(shardingRuleChangedEvent.getShardingRuleConfiguration(), getDataSources().keySet()); } } diff --git a/sharding-proxy/sharding-proxy-bootstrap/src/main/java/org/apache/shardingsphere/shardingproxy/Bootstrap.java b/sharding-proxy/sharding-proxy-bootstrap/src/main/java/org/apache/shardingsphere/shardingproxy/Bootstrap.java index d7556d7cf17c5..05808f28ec518 100644 --- a/sharding-proxy/sharding-proxy-bootstrap/src/main/java/org/apache/shardingsphere/shardingproxy/Bootstrap.java +++ b/sharding-proxy/sharding-proxy-bootstrap/src/main/java/org/apache/shardingsphere/shardingproxy/Bootstrap.java @@ -19,10 +19,12 @@ import lombok.AccessLevel; import lombok.NoArgsConstructor; +import org.apache.commons.collections4.CollectionUtils; import org.apache.shardingsphere.api.config.RuleConfiguration; import org.apache.shardingsphere.core.config.DataSourceConfiguration; import org.apache.shardingsphere.core.constant.properties.ShardingPropertiesConstant; import org.apache.shardingsphere.core.rule.Authentication; +import org.apache.shardingsphere.core.util.ConfigurationLogger; import org.apache.shardingsphere.core.yaml.config.common.YamlAuthenticationConfiguration; import org.apache.shardingsphere.core.yaml.swapper.impl.AuthenticationYamlSwapper; import org.apache.shardingsphere.core.yaml.swapper.impl.EncryptRuleConfigurationYamlSwapper; @@ -55,12 +57,13 @@ * @author zhangliang * @author wangkai * @author panjuan + * @author sunbufu */ @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class Bootstrap { - + private static final int DEFAULT_PORT = 3307; - + /** * Main entrance. * @@ -69,6 +72,7 @@ public final class Bootstrap { */ public static void main(final String[] args) throws IOException { ShardingConfiguration shardingConfig = new ShardingConfigurationLoader().load(); + logRuleConfigurationMap(getRuleConfiguration(shardingConfig.getRuleConfigurationMap()).values()); int port = getPort(args); if (null == shardingConfig.getServerConfiguration().getOrchestration()) { startWithoutRegistryCenter(shardingConfig.getRuleConfigurationMap(), shardingConfig.getServerConfiguration().getAuthentication(), shardingConfig.getServerConfiguration().getProps(), port); @@ -76,7 +80,7 @@ public static void main(final String[] args) throws IOException { startWithRegistryCenter(shardingConfig.getServerConfiguration(), shardingConfig.getRuleConfigurationMap().keySet(), shardingConfig.getRuleConfigurationMap(), port); } } - + private static int getPort(final String[] args) { if (0 == args.length) { return DEFAULT_PORT; @@ -87,15 +91,15 @@ private static int getPort(final String[] args) { return DEFAULT_PORT; } } - - private static void startWithoutRegistryCenter(final Map ruleConfigs, + + private static void startWithoutRegistryCenter(final Map ruleConfigs, final YamlAuthenticationConfiguration authentication, final Properties prop, final int port) { ShardingProxyContext.getInstance().init(getAuthentication(authentication), prop); LogicSchemas.getInstance().init(getDataSourceParameterMap(ruleConfigs), getRuleConfiguration(ruleConfigs)); initOpenTracing(); ShardingProxy.getInstance().start(port); } - + private static void startWithRegistryCenter(final YamlProxyServerConfiguration serverConfig, final Collection shardingSchemaNames, final Map ruleConfigs, final int port) { try (ShardingOrchestrationFacade shardingOrchestrationFacade = new ShardingOrchestrationFacade( @@ -107,7 +111,7 @@ private static void startWithRegistryCenter(final YamlProxyServerConfiguration s ShardingProxy.getInstance().start(port); } } - + private static Map> getSchemaDataSourceParameterMap(final ShardingOrchestrationFacade shardingOrchestrationFacade) { Map> result = new LinkedHashMap<>(); for (String each : shardingOrchestrationFacade.getConfigService().getAllShardingSchemaNames()) { @@ -115,7 +119,7 @@ private static Map> getSchemaDataSo } return result; } - + private static Map getSchemaRules(final ShardingOrchestrationFacade shardingOrchestrationFacade) { Map result = new LinkedHashMap<>(); for (String each : shardingOrchestrationFacade.getConfigService().getAllShardingSchemaNames()) { @@ -129,23 +133,23 @@ private static Map getSchemaRules(final ShardingOrche } return result; } - + private static void initShardingOrchestrationFacade( final YamlProxyServerConfiguration serverConfig, final Map ruleConfigs, final ShardingOrchestrationFacade shardingOrchestrationFacade) { if (ruleConfigs.isEmpty()) { shardingOrchestrationFacade.init(); } else { - shardingOrchestrationFacade.init(getDataSourceConfigurationMap(ruleConfigs), + shardingOrchestrationFacade.init(getDataSourceConfigurationMap(ruleConfigs), getRuleConfiguration(ruleConfigs), getAuthentication(serverConfig.getAuthentication()), serverConfig.getProps()); } } - + private static void initOpenTracing() { if (ShardingProxyContext.getInstance().getShardingProperties().getValue(ShardingPropertiesConstant.PROXY_OPENTRACING_ENABLED)) { ShardingTracer.init(); } } - + private static Map> getDataSourceConfigurationMap(final Map ruleConfigs) { Map> result = new LinkedHashMap<>(); for (Entry entry : ruleConfigs.entrySet()) { @@ -161,7 +165,7 @@ private static Map> getDataSourcePa } return result; } - + private static Map getRuleConfiguration(final Map localRuleConfigs) { Map result = new HashMap<>(); for (Entry entry : localRuleConfigs.entrySet()) { @@ -175,8 +179,22 @@ private static Map getRuleConfiguration(final Map ruleConfigurations) { + if (CollectionUtils.isNotEmpty(ruleConfigurations)) { + for (RuleConfiguration each : ruleConfigurations) { + ConfigurationLogger.log(each); + } + } + } + } diff --git a/sharding-proxy/sharding-proxy-common/src/main/java/org/apache/shardingsphere/shardingproxy/context/ShardingProxyContext.java b/sharding-proxy/sharding-proxy-common/src/main/java/org/apache/shardingsphere/shardingproxy/context/ShardingProxyContext.java index eba28f8ba79be..544a8cb9c9422 100644 --- a/sharding-proxy/sharding-proxy-common/src/main/java/org/apache/shardingsphere/shardingproxy/context/ShardingProxyContext.java +++ b/sharding-proxy/sharding-proxy-common/src/main/java/org/apache/shardingsphere/shardingproxy/context/ShardingProxyContext.java @@ -21,9 +21,7 @@ import org.apache.shardingsphere.core.constant.properties.ShardingProperties; import org.apache.shardingsphere.core.rule.Authentication; -import org.apache.shardingsphere.core.util.ConfigurationPrinter; -import org.apache.shardingsphere.core.yaml.engine.YamlEngine; -import org.apache.shardingsphere.core.yaml.swapper.impl.AuthenticationYamlSwapper; +import org.apache.shardingsphere.core.util.ConfigurationLogger; import org.apache.shardingsphere.orchestration.internal.eventbus.ShardingOrchestrationEventBus; import org.apache.shardingsphere.orchestration.internal.registry.config.event.AuthenticationChangedEvent; import org.apache.shardingsphere.orchestration.internal.registry.config.event.PropertiesChangedEvent; @@ -39,6 +37,7 @@ * @author chenqingyang * @author panjuan * @author zhangliang + * @author sunbufu */ @Getter public final class ShardingProxyContext { @@ -111,8 +110,7 @@ public synchronized void renew(final CircuitStateChangedEvent event) { * @param authentication new authentication */ public void setAuthentication(final Authentication authentication) { - ConfigurationPrinter.print("authentication", - YamlEngine.marshal(new AuthenticationYamlSwapper().swap(authentication))); + ConfigurationLogger.log(authentication); this.authentication = authentication; } @@ -122,7 +120,7 @@ public void setAuthentication(final Authentication authentication) { * @param props new props */ public void setProperties(final Properties props) { - ConfigurationPrinter.printConfiguration("props", props); + ConfigurationLogger.log(props); this.shardingProperties = new ShardingProperties(props); } } diff --git a/sharding-proxy/sharding-proxy-transport/sharding-proxy-transport-core/src/test/resources/logback-test.xml b/sharding-proxy/sharding-proxy-transport/sharding-proxy-transport-core/src/test/resources/logback-test.xml index 49ce12c44289d..cade0c6eb570f 100644 --- a/sharding-proxy/sharding-proxy-transport/sharding-proxy-transport-core/src/test/resources/logback-test.xml +++ b/sharding-proxy/sharding-proxy-transport/sharding-proxy-transport-core/src/test/resources/logback-test.xml @@ -25,7 +25,7 @@ - + diff --git a/sharding-spring/sharding-jdbc-orchestration-spring/sharding-jdbc-orchestration-spring-boot-starter/src/test/resources/logback-test.xml b/sharding-spring/sharding-jdbc-orchestration-spring/sharding-jdbc-orchestration-spring-boot-starter/src/test/resources/logback-test.xml index b48044a6f3414..038ffc08687bb 100644 --- a/sharding-spring/sharding-jdbc-orchestration-spring/sharding-jdbc-orchestration-spring-boot-starter/src/test/resources/logback-test.xml +++ b/sharding-spring/sharding-jdbc-orchestration-spring/sharding-jdbc-orchestration-spring-boot-starter/src/test/resources/logback-test.xml @@ -27,7 +27,7 @@ - + diff --git a/sharding-spring/sharding-jdbc-orchestration-spring/sharding-jdbc-orchestration-spring-namespace/src/test/resources/logback-test.xml b/sharding-spring/sharding-jdbc-orchestration-spring/sharding-jdbc-orchestration-spring-namespace/src/test/resources/logback-test.xml index f83f9d0047e6d..0802913124e90 100644 --- a/sharding-spring/sharding-jdbc-orchestration-spring/sharding-jdbc-orchestration-spring-namespace/src/test/resources/logback-test.xml +++ b/sharding-spring/sharding-jdbc-orchestration-spring/sharding-jdbc-orchestration-spring-namespace/src/test/resources/logback-test.xml @@ -25,7 +25,7 @@ - + diff --git a/sharding-spring/sharding-jdbc-spring/sharding-jdbc-spring-boot-starter/src/test/resources/logback-test.xml b/sharding-spring/sharding-jdbc-spring/sharding-jdbc-spring-boot-starter/src/test/resources/logback-test.xml index b48044a6f3414..038ffc08687bb 100644 --- a/sharding-spring/sharding-jdbc-spring/sharding-jdbc-spring-boot-starter/src/test/resources/logback-test.xml +++ b/sharding-spring/sharding-jdbc-spring/sharding-jdbc-spring-boot-starter/src/test/resources/logback-test.xml @@ -27,7 +27,7 @@ - + diff --git a/sharding-spring/sharding-jdbc-spring/sharding-jdbc-spring-namespace/src/test/resources/logback-test.xml b/sharding-spring/sharding-jdbc-spring/sharding-jdbc-spring-namespace/src/test/resources/logback-test.xml index 29dc3ae749f18..cff63bb15ba77 100644 --- a/sharding-spring/sharding-jdbc-spring/sharding-jdbc-spring-namespace/src/test/resources/logback-test.xml +++ b/sharding-spring/sharding-jdbc-spring/sharding-jdbc-spring-namespace/src/test/resources/logback-test.xml @@ -25,7 +25,7 @@ - + diff --git a/sharding-transaction/sharding-transaction-2pc/sharding-transaction-xa/sharding-transaction-xa-core/src/test/resources/logback-test.xml b/sharding-transaction/sharding-transaction-2pc/sharding-transaction-xa/sharding-transaction-xa-core/src/test/resources/logback-test.xml index bd513f078b969..b03d65953b492 100644 --- a/sharding-transaction/sharding-transaction-2pc/sharding-transaction-xa/sharding-transaction-xa-core/src/test/resources/logback-test.xml +++ b/sharding-transaction/sharding-transaction-2pc/sharding-transaction-xa/sharding-transaction-xa-core/src/test/resources/logback-test.xml @@ -26,7 +26,7 @@ - + diff --git a/sharding-transaction/sharding-transaction-base/sharding-transaction-base-seata-at/src/test/resources/logback-test.xml b/sharding-transaction/sharding-transaction-base/sharding-transaction-base-seata-at/src/test/resources/logback-test.xml index 49ce12c44289d..cade0c6eb570f 100644 --- a/sharding-transaction/sharding-transaction-base/sharding-transaction-base-seata-at/src/test/resources/logback-test.xml +++ b/sharding-transaction/sharding-transaction-base/sharding-transaction-base-seata-at/src/test/resources/logback-test.xml @@ -25,7 +25,7 @@ - + diff --git a/sharding-transaction/sharding-transaction-core/src/test/resources/logback-test.xml b/sharding-transaction/sharding-transaction-core/src/test/resources/logback-test.xml index d3fe1a3cba659..2fec979534733 100644 --- a/sharding-transaction/sharding-transaction-core/src/test/resources/logback-test.xml +++ b/sharding-transaction/sharding-transaction-core/src/test/resources/logback-test.xml @@ -26,7 +26,7 @@ - +