This is an automated email from the ASF dual-hosted git repository.
panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new b2abe39 Update shadow example (#13003)
b2abe39 is described below
commit b2abe39223477d360ea5166582d7aae82778d776
Author: gin <[email protected]>
AuthorDate: Wed Oct 13 14:08:55 2021 +0800
Update shadow example (#13003)
* Fix shadow docs.
* Add shadow raw example.
---
.../content/features/shadow/principle.cn.md | 2 +-
.../jdbc/repository/ShadowUserRepositoryImpl.java | 49 +++++++++--
.../single-feature-example/pom.xml | 1 -
.../single-feature-example/shadow-example/pom.xml | 1 +
.../{ => shadow-raw-jdbc-example}/pom.xml | 22 +++--
.../jdbc/ShadowRawJavaConfigurationExample.java | 47 +++++++++++
.../jdbc/ShadowRawYamlConfigurationExample.java | 44 ++++++++++
.../raw/jdbc/config/BaseShadowConfiguration.java | 80 ++++++++++++++++++
.../raw/jdbc/config/ShadowConfiguration.java | 74 ++++++++++++++++
.../jdbc/config/ShadowEncryptConfiguration.java | 98 ++++++++++++++++++++++
.../shadow/raw/jdbc/factory/DataSourceFactory.java | 40 +++++++++
.../raw/jdbc/factory/YamlDataSourceFactory.java | 44 ++++++++++
.../main/resources/META-INF/shadow-encrypt.yaml | 96 +++++++++++++++++++++
.../src/main/resources/META-INF/shadow.yaml | 76 +++++++++++++++++
.../src/main/resources/logback.xml} | 34 ++++----
15 files changed, 674 insertions(+), 34 deletions(-)
diff --git a/docs/document/content/features/shadow/principle.cn.md
b/docs/document/content/features/shadow/principle.cn.md
index f224778..fed3288 100644
--- a/docs/document/content/features/shadow/principle.cn.md
+++ b/docs/document/content/features/shadow/principle.cn.md
@@ -88,7 +88,7 @@ props:
```
注意:使用注解影子算法,需配合开起 SQL 注解解析。
-### 3. 混用模式
+3. 混用模式
假设对 `t_order` 表压测以上两种场景都需要覆盖。即,`INSERT INTO t_order (order_id, user_id, ...)
VALUES (xxx..., 0, ...)` 和 `SELECT * FROM t_order WHERE order_id = xxx
/*shadow:true,foo:bar,...*/` 都执行到影子库。
diff --git
a/examples/example-core/example-raw-jdbc/src/main/java/org/apache/shardingsphere/example/core/jdbc/repository/ShadowUserRepositoryImpl.java
b/examples/example-core/example-raw-jdbc/src/main/java/org/apache/shardingsphere/example/core/jdbc/repository/ShadowUserRepositoryImpl.java
index 53fea49..248dc46 100644
---
a/examples/example-core/example-raw-jdbc/src/main/java/org/apache/shardingsphere/example/core/jdbc/repository/ShadowUserRepositoryImpl.java
+++
b/examples/example-core/example-raw-jdbc/src/main/java/org/apache/shardingsphere/example/core/jdbc/repository/ShadowUserRepositoryImpl.java
@@ -18,7 +18,7 @@
package org.apache.shardingsphere.example.core.jdbc.repository;
import org.apache.shardingsphere.example.core.api.entity.ShadowUser;
-import org.apache.shardingsphere.example.core.api.repository.CommonRepository;
+import
org.apache.shardingsphere.example.core.api.repository.ShadowUserRepository;
import javax.sql.DataSource;
import java.sql.Connection;
@@ -29,7 +29,9 @@ import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;
-public final class ShadowUserRepositoryImpl implements
CommonRepository<ShadowUser, Long> {
+public final class ShadowUserRepositoryImpl implements ShadowUserRepository {
+
+ private static final String SQL_NOTE = "/*shadow:true,foo:bar*/";
private final DataSource dataSource;
@@ -39,32 +41,67 @@ public final class ShadowUserRepositoryImpl implements
CommonRepository<ShadowUs
@Override
public void createTableIfNotExists() throws SQLException {
- String sql = "CREATE TABLE IF NOT EXISTS t_user "
- + "(user_id INT NOT NULL AUTO_INCREMENT, user_type INT NOT
NULL, user_name VARCHAR(200), pwd VARCHAR(200), PRIMARY KEY (user_id))";
+ String sql = "CREATE TABLE IF NOT EXISTS t_user (user_id INT NOT NULL
AUTO_INCREMENT, user_type INT(11), user_name VARCHAR(200), pwd VARCHAR(200),
PRIMARY KEY (user_id))";
+ createTableIfNotExistsShadow(sql);
+ createTableIfNotExistsNative(sql);
+ }
+
+ private void createTableIfNotExistsNative(final String sql) throws
SQLException {
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
statement.executeUpdate(sql);
}
}
+ private void createTableIfNotExistsShadow(final String sql) throws
SQLException {
+ try (Connection connection = dataSource.getConnection();
+ Statement statement = connection.createStatement()) {
+ statement.executeUpdate(sql + SQL_NOTE);
+ }
+ }
+
@Override
public void dropTable() throws SQLException {
- String sql = "DROP TABLE t_user";
+ String sql = "DROP TABLE IF EXISTS t_user;";
+ dropTableShadow(sql);
+ dropTableNative(sql);
+ }
+
+ private void dropTableNative(final String sql) throws SQLException {
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
statement.executeUpdate(sql);
}
}
+ private void dropTableShadow(final String sql) throws SQLException {
+ try (Connection connection = dataSource.getConnection();
+ Statement statement = connection.createStatement()) {
+ statement.executeUpdate(sql + SQL_NOTE);
+ }
+ }
+
@Override
public void truncateTable() throws SQLException {
String sql = "TRUNCATE TABLE t_user";
+ truncateTableShadow(sql);
+ truncateTableNative(sql);
+ }
+
+ private void truncateTableNative(final String sql) throws SQLException {
try (Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement()) {
statement.executeUpdate(sql);
}
}
+ private void truncateTableShadow(final String sql) throws SQLException {
+ try (Connection connection = dataSource.getConnection();
+ Statement statement = connection.createStatement()) {
+ statement.executeUpdate(sql + SQL_NOTE);
+ }
+ }
+
@Override
public Long insert(final ShadowUser entity) throws SQLException {
String sql = "INSERT INTO t_user (user_id, user_type, user_name, pwd)
VALUES (?, ?, ?, ?)";
@@ -81,7 +118,7 @@ public final class ShadowUserRepositoryImpl implements
CommonRepository<ShadowUs
@Override
public void delete(final Long id) throws SQLException {
- String sql = "DELETE FROM t_user WHERE user_id = ? and shadow= ?";
+ String sql = "DELETE FROM t_user WHERE user_id = ? AND user_type = ?";
deleteUser(sql, id, (int) (id % 2));
deleteUser(sql, id, (int) (id % 2));
}
diff --git
a/examples/shardingsphere-jdbc-example/single-feature-example/pom.xml
b/examples/shardingsphere-jdbc-example/single-feature-example/pom.xml
index 46e1070..ea259a0 100644
--- a/examples/shardingsphere-jdbc-example/single-feature-example/pom.xml
+++ b/examples/shardingsphere-jdbc-example/single-feature-example/pom.xml
@@ -33,7 +33,6 @@
<module>cluster-mode-example</module>
<module>encrypt-example</module>
<module>extension-example</module>
- <!-- <module>future-shadow-example</module> -->
<module>shadow-example</module>
<module>sharding-example</module>
<module>transaction-example</module>
diff --git
a/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/pom.xml
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/pom.xml
index be3a0ac..662fd2e 100644
---
a/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/pom.xml
+++
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/pom.xml
@@ -32,5 +32,6 @@
<modules>
<module>shadow-spring-namespace-mybatis-example</module>
<module>shadow-spring-boot-mybatis-example</module>
+ <module>shadow-raw-jdbc-example</module>
</modules>
</project>
diff --git
a/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/pom.xml
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/pom.xml
similarity index 71%
copy from
examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/pom.xml
copy to
examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/pom.xml
index be3a0ac..049c2be 100644
---
a/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/pom.xml
+++
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/pom.xml
@@ -22,15 +22,21 @@
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.apache.shardingsphere.example</groupId>
- <artifactId>single-feature-example</artifactId>
+ <artifactId>shadow-example</artifactId>
<version>5.0.0-RC1-SNAPSHOT</version>
</parent>
- <artifactId>shadow-example</artifactId>
- <packaging>pom</packaging>
+ <artifactId>shadow-raw-jdbc-example</artifactId>
<name>${project.artifactId}</name>
- <modules>
- <module>shadow-spring-namespace-mybatis-example</module>
- <module>shadow-spring-boot-mybatis-example</module>
- </modules>
-</project>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.shardingsphere.example</groupId>
+ <artifactId>example-raw-jdbc</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-jdbc-core</artifactId>
+ </dependency>
+ </dependencies>
+</project>
\ No newline at end of file
diff --git
a/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/raw/jdbc/ShadowRawJavaConfigurationExample.java
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/raw/jdbc/ShadowRawJavaConfigurationExample.java
new file mode 100644
index 0000000..a6166eb
--- /dev/null
+++
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/raw/jdbc/ShadowRawJavaConfigurationExample.java
@@ -0,0 +1,47 @@
+/*
+ * 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.
+ */
+
+/*
+ * Please make sure primary replica data replication sync on MySQL is running
correctly. Otherwise this example will query empty data from replica.
+ */
+
+package org.apache.shardingsphere.example.shadow.raw.jdbc;
+
+import org.apache.shardingsphere.example.core.api.ExampleExecuteTemplate;
+import org.apache.shardingsphere.example.core.api.service.ExampleService;
+import
org.apache.shardingsphere.example.core.jdbc.repository.ShadowUserRepositoryImpl;
+import
org.apache.shardingsphere.example.core.jdbc.service.ShadowUserServiceImpl;
+import org.apache.shardingsphere.example.type.ShardingType;
+import
org.apache.shardingsphere.example.shadow.raw.jdbc.factory.DataSourceFactory;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+
+public final class ShadowRawJavaConfigurationExample {
+
+ private static ShardingType shardingType = ShardingType.SHADOW;
+// private static ShardingType shardingType = ShardingType.ENCRYPT_SHADOW;
+
+ public static void main(final String[] args) throws SQLException {
+ DataSource dataSource = DataSourceFactory.newInstance(shardingType);
+ ExampleExecuteTemplate.run(getExampleService(dataSource));
+ }
+
+ private static ExampleService getExampleService(final DataSource
dataSource) {
+ return new ShadowUserServiceImpl(new
ShadowUserRepositoryImpl(dataSource));
+ }
+}
diff --git
a/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/raw/jdbc/ShadowRawYamlConfigurationExample.java
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/raw/jdbc/ShadowRawYamlConfigurationExample.java
new file mode 100644
index 0000000..36c8fbd
--- /dev/null
+++
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/raw/jdbc/ShadowRawYamlConfigurationExample.java
@@ -0,0 +1,44 @@
+/*
+ * 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.shadow.raw.jdbc;
+
+import org.apache.shardingsphere.example.core.api.ExampleExecuteTemplate;
+import org.apache.shardingsphere.example.core.api.service.ExampleService;
+import
org.apache.shardingsphere.example.core.jdbc.repository.ShadowUserRepositoryImpl;
+import
org.apache.shardingsphere.example.core.jdbc.service.ShadowUserServiceImpl;
+import
org.apache.shardingsphere.example.shadow.raw.jdbc.factory.YamlDataSourceFactory;
+import org.apache.shardingsphere.example.type.ShardingType;
+
+import javax.sql.DataSource;
+import java.io.IOException;
+import java.sql.SQLException;
+
+public final class ShadowRawYamlConfigurationExample {
+
+ private static ShardingType shardingType = ShardingType.SHADOW;
+// private static ShardingType shardingType = ShardingType.ENCRYPT_SHADOW;
+
+ public static void main(final String[] args) throws SQLException,
IOException {
+ DataSource dataSource =
YamlDataSourceFactory.newInstance(shardingType);
+ ExampleExecuteTemplate.run(getExampleService(dataSource));
+ }
+
+ private static ExampleService getExampleService(final DataSource
dataSource) {
+ return new ShadowUserServiceImpl(new
ShadowUserRepositoryImpl(dataSource));
+ }
+}
diff --git
a/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/raw/jdbc/config/BaseShadowConfiguration.java
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/raw/jdbc/config/BaseShadowConfiguration.java
new file mode 100644
index 0000000..d37da7b
--- /dev/null
+++
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/raw/jdbc/config/BaseShadowConfiguration.java
@@ -0,0 +1,80 @@
+/*
+ * 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.shadow.raw.jdbc.config;
+
+import org.apache.shardingsphere.example.config.ExampleConfiguration;
+import org.apache.shardingsphere.example.core.api.DataSourceUtil;
+import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+import
org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
+
+import javax.sql.DataSource;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Properties;
+
+public abstract class BaseShadowConfiguration implements ExampleConfiguration {
+
+ protected Map<String, DataSource> createDataSourceMap() {
+ Map<String, DataSource> result = new LinkedHashMap<>();
+ result.put("ds", DataSourceUtil.createDataSource("ds"));
+ result.put("shadow-ds", DataSourceUtil.createDataSource("ds_shadow"));
+ return result;
+ }
+
+ protected Properties createShardingSphereProps() {
+ Properties result = new Properties();
+ result.setProperty(ConfigurationPropertyKey.SQL_SHOW.getKey(), "true");
+
result.setProperty(ConfigurationPropertyKey.SQL_COMMENT_PARSE_ENABLED.getKey(),
"true");
+ return result;
+ }
+
+ protected Collection<String> createShadowAlgorithmNames() {
+ Collection<String> result = new LinkedList<>();
+ result.add("user-id-insert-match-algorithm");
+ result.add("user-id-delete-match-algorithm");
+ result.add("user-id-select-match-algorithm");
+ result.add("simple-note-algorithm");
+ return result;
+ }
+
+ protected Map<String, ShardingSphereAlgorithmConfiguration>
createShadowAlgorithmConfigurations() {
+ Map<String, ShardingSphereAlgorithmConfiguration> result = new
LinkedHashMap<>();
+ Properties userIdInsertProps = new Properties();
+ userIdInsertProps.setProperty("operation", "insert");
+ userIdInsertProps.setProperty("column", "user_type");
+ userIdInsertProps.setProperty("regex", "[1]");
+ result.put("user-id-insert-match-algorithm", new
ShardingSphereAlgorithmConfiguration("COLUMN_REGEX_MATCH", userIdInsertProps));
+ Properties userIdDeleteProps = new Properties();
+ userIdDeleteProps.setProperty("operation", "delete");
+ userIdDeleteProps.setProperty("column", "user_type");
+ userIdDeleteProps.setProperty("regex", "[1]");
+ result.put("user-id-delete-match-algorithm", new
ShardingSphereAlgorithmConfiguration("COLUMN_REGEX_MATCH", userIdDeleteProps));
+ Properties userIdSelectProps = new Properties();
+ userIdSelectProps.setProperty("operation", "select");
+ userIdSelectProps.setProperty("column", "user_type");
+ userIdSelectProps.setProperty("regex", "[1]");
+ result.put("user-id-select-match-algorithm", new
ShardingSphereAlgorithmConfiguration("COLUMN_REGEX_MATCH", userIdSelectProps));
+ Properties noteAlgorithmProps = new Properties();
+ noteAlgorithmProps.setProperty("shadow", "true");
+ noteAlgorithmProps.setProperty("foo", "bar");
+ result.put("simple-note-algorithm", new
ShardingSphereAlgorithmConfiguration("SIMPLE_NOTE", noteAlgorithmProps));
+ return result;
+ }
+}
diff --git
a/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/raw/jdbc/config/ShadowConfiguration.java
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/raw/jdbc/config/ShadowConfiguration.java
new file mode 100644
index 0000000..a5fe31b
--- /dev/null
+++
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/raw/jdbc/config/ShadowConfiguration.java
@@ -0,0 +1,74 @@
+/*
+ * 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.shadow.raw.jdbc.config;
+
+import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
+import
org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
+import
org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.Map;
+
+public final class ShadowConfiguration extends BaseShadowConfiguration {
+
+ @Override
+ public DataSource getDataSource() throws SQLException {
+ Map<String, DataSource> dataSourceMap = createDataSourceMap();
+ Collection<RuleConfiguration> ruleConfigurations =
createRuleConfiguration();
+ return ShardingSphereDataSourceFactory.createDataSource(dataSourceMap,
ruleConfigurations, createShardingSphereProps());
+ }
+
+ private Collection<RuleConfiguration> createRuleConfiguration() {
+ Collection<RuleConfiguration> result = new LinkedList<>();
+ result.add(createShadowRuleConfiguration());
+ return result;
+ }
+
+ private RuleConfiguration createShadowRuleConfiguration() {
+ ShadowRuleConfiguration result = new ShadowRuleConfiguration();
+ result.setEnable(true);
+ result.setShadowAlgorithms(createShadowAlgorithmConfigurations());
+ result.setDataSources(createShadowDataSources());
+ result.setTables(createShadowTables());
+ return result;
+ }
+
+ private Map<String, ShadowTableConfiguration> createShadowTables() {
+ Map<String, ShadowTableConfiguration> result = new LinkedHashMap<>();
+ result.put("t_user", new
ShadowTableConfiguration(createDataSourceNames(),
createShadowAlgorithmNames()));
+ return result;
+ }
+
+ private Collection<String> createDataSourceNames() {
+ Collection<String> result = new LinkedList<>();
+ result.add("shadow-data-source");
+ return result;
+ }
+
+ private Map<String, ShadowDataSourceConfiguration>
createShadowDataSources() {
+ Map<String, ShadowDataSourceConfiguration> result = new
LinkedHashMap<>();
+ result.put("shadow-data-source", new
ShadowDataSourceConfiguration("ds", "shadow-ds"));
+ return result;
+ }
+}
diff --git
a/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/raw/jdbc/config/ShadowEncryptConfiguration.java
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/raw/jdbc/config/ShadowEncryptConfiguration.java
new file mode 100644
index 0000000..9d9d2d7
--- /dev/null
+++
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/raw/jdbc/config/ShadowEncryptConfiguration.java
@@ -0,0 +1,98 @@
+/*
+ * 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.shadow.raw.jdbc.config;
+
+import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
+import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
+import
org.apache.shardingsphere.encrypt.api.config.rule.EncryptColumnRuleConfiguration;
+import
org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
+import
org.apache.shardingsphere.shadow.api.config.datasource.ShadowDataSourceConfiguration;
+import
org.apache.shardingsphere.shadow.api.config.table.ShadowTableConfiguration;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.Map;
+import java.util.Properties;
+
+public final class ShadowEncryptConfiguration extends BaseShadowConfiguration {
+
+ @Override
+ public DataSource getDataSource() throws SQLException {
+ Map<String, DataSource> dataSourceMap = createDataSourceMap();
+ Collection<RuleConfiguration> ruleConfigurations =
createRuleConfiguration();
+ return ShardingSphereDataSourceFactory.createDataSource(dataSourceMap,
ruleConfigurations, createShardingSphereProps());
+ }
+
+ private Collection<RuleConfiguration> createRuleConfiguration() {
+ Collection<RuleConfiguration> result = new LinkedList<>();
+ result.add(new
EncryptRuleConfiguration(getEncryptTableRuleConfigurations(),
getEncryptAlgorithmConfigurations()));
+ result.add(createShadowRuleConfiguration());
+ return result;
+ }
+
+ private ShadowRuleConfiguration createShadowRuleConfiguration() {
+ ShadowRuleConfiguration result = new ShadowRuleConfiguration();
+ result.setEnable(true);
+ result.setShadowAlgorithms(createShadowAlgorithmConfigurations());
+ result.setDataSources(createShadowDataSources());
+ result.setTables(createShadowTables());
+ return result;
+ }
+
+ private Map<String, ShadowDataSourceConfiguration>
createShadowDataSources() {
+ Map<String, ShadowDataSourceConfiguration> result = new
LinkedHashMap<>();
+ result.put("shadow-data-source", new
ShadowDataSourceConfiguration("ds", "shadow-ds"));
+ return result;
+ }
+
+ private Map<String, ShadowTableConfiguration> createShadowTables() {
+ Map<String, ShadowTableConfiguration> result = new LinkedHashMap<>();
+ result.put("t_user", new
ShadowTableConfiguration(createDataSourceNames(),
createShadowAlgorithmNames()));
+ return result;
+ }
+
+ private Collection<String> createDataSourceNames() {
+ Collection<String> result = new LinkedList<>();
+ result.add("shadow-data-source");
+ return result;
+ }
+
+ private Collection<EncryptTableRuleConfiguration>
getEncryptTableRuleConfigurations() {
+ Collection<EncryptTableRuleConfiguration> result = new LinkedList<>();
+ Collection<EncryptColumnRuleConfiguration> columns = new
LinkedList<>();
+ columns.add(new EncryptColumnRuleConfiguration("user_name",
"user_name", "", "user_name_plain", "name_encryptor"));
+ columns.add(new EncryptColumnRuleConfiguration("pwd", "pwd",
"assisted_query_pwd", "", "pwd_encryptor"));
+ result.add(new EncryptTableRuleConfiguration("t_user", columns));
+ return result;
+ }
+
+ private Map<String, ShardingSphereAlgorithmConfiguration>
getEncryptAlgorithmConfigurations() {
+ Map<String, ShardingSphereAlgorithmConfiguration> result = new
LinkedHashMap<>(2, 1);
+ Properties props = new Properties();
+ props.setProperty("aes-key-value", "123456");
+ result.put("name_encryptor", new
ShardingSphereAlgorithmConfiguration("AES", props));
+ result.put("pwd_encryptor", new
ShardingSphereAlgorithmConfiguration("assistedTest", null));
+ return result;
+ }
+}
diff --git
a/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/raw/jdbc/factory/DataSourceFactory.java
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/raw/jdbc/factory/DataSourceFactory.java
new file mode 100644
index 0000000..f1cb0ea
--- /dev/null
+++
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/raw/jdbc/factory/DataSourceFactory.java
@@ -0,0 +1,40 @@
+/*
+ * 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.shadow.raw.jdbc.factory;
+
+
+import
org.apache.shardingsphere.example.shadow.raw.jdbc.config.ShadowConfiguration;
+import
org.apache.shardingsphere.example.shadow.raw.jdbc.config.ShadowEncryptConfiguration;
+import org.apache.shardingsphere.example.type.ShardingType;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+
+public final class DataSourceFactory {
+
+ public static DataSource newInstance(final ShardingType shardingType)
throws SQLException {
+ switch (shardingType) {
+ case SHADOW:
+ return new ShadowConfiguration().getDataSource();
+ case ENCRYPT_SHADOW:
+ return new ShadowEncryptConfiguration().getDataSource();
+ default:
+ throw new UnsupportedOperationException(shardingType.name());
+ }
+ }
+}
diff --git
a/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/raw/jdbc/factory/YamlDataSourceFactory.java
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/raw/jdbc/factory/YamlDataSourceFactory.java
new file mode 100644
index 0000000..eced935
--- /dev/null
+++
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/raw/jdbc/factory/YamlDataSourceFactory.java
@@ -0,0 +1,44 @@
+/*
+ * 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.shadow.raw.jdbc.factory;
+
+import
org.apache.shardingsphere.driver.api.yaml.YamlShardingSphereDataSourceFactory;
+import org.apache.shardingsphere.example.type.ShardingType;
+
+import javax.sql.DataSource;
+import java.io.File;
+import java.io.IOException;
+import java.sql.SQLException;
+
+public final class YamlDataSourceFactory {
+
+ public static DataSource newInstance(final ShardingType shardingType)
throws SQLException, IOException {
+ switch (shardingType) {
+ case SHADOW:
+ return
YamlShardingSphereDataSourceFactory.createDataSource(getFile("/META-INF/shadow.yaml"));
+ case ENCRYPT_SHADOW:
+ return
YamlShardingSphereDataSourceFactory.createDataSource(getFile("/META-INF/shadow-encrypt.yaml"));
+ default:
+ throw new UnsupportedOperationException(shardingType.name());
+ }
+ }
+
+ private static File getFile(final String fileName) {
+ return new
File(YamlDataSourceFactory.class.getResource(fileName).getFile());
+ }
+}
diff --git
a/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/resources/META-INF/shadow-encrypt.yaml
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/resources/META-INF/shadow-encrypt.yaml
new file mode 100644
index 0000000..02e02d8
--- /dev/null
+++
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/resources/META-INF/shadow-encrypt.yaml
@@ -0,0 +1,96 @@
+#
+# 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.
+#
+
+dataSources:
+ ds:
+ dataSourceClassName: com.zaxxer.hikari.HikariDataSource
+ driverClassName: com.mysql.jdbc.Driver
+ jdbcUrl:
jdbc:mysql://localhost:3306/ds?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
+ username: root
+ password:
+ ds_shadow:
+ dataSourceClassName: com.zaxxer.hikari.HikariDataSource
+ driverClassName: com.mysql.jdbc.Driver
+ jdbcUrl:
jdbc:mysql://localhost:3306/ds_shadow?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
+ username: root
+ password:
+
+rules:
+- !ENCRYPT
+ tables:
+ t_user:
+ columns:
+ user_name:
+ plainColumn: user_name_plain
+ cipherColumn: user_name
+ encryptorName: name_encryptor
+ pwd:
+ cipherColumn: pwd
+ assistedQueryColumn: assisted_query_pwd
+ encryptorName: pwd_encryptor
+ encryptors:
+ name_encryptor:
+ type: AES
+ props:
+ aes-key-value: 123456abc
+ pwd_encryptor:
+ type: assistedTest
+
+- !SHADOW
+ enable: true
+ dataSources:
+ shadowDataSource:
+ sourceDataSourceName: ds
+ shadowDataSourceName: ds_shadow
+ tables:
+ t_user:
+ dataSourceNames:
+ - shadowDataSource
+ shadowAlgorithmNames:
+ - user-id-insert-match-algorithm
+ - user-id-select-match-algorithm
+ - user-id-update-match-algorithm
+ - user-id-delete-match-algorithm
+ - simple-note-algorithm
+ shadowAlgorithms:
+ user-id-insert-match-algorithm:
+ type: COLUMN_REGEX_MATCH
+ props:
+ operation: insert
+ column: user_id
+ regex: "[1]"
+ user-id-select-match-algorithm:
+ type: COLUMN_REGEX_MATCH
+ props:
+ operation: select
+ column: user_id
+ regex: "[1]"
+ user-id-delete-match-algorithm:
+ type: COLUMN_REGEX_MATCH
+ props:
+ operation: delete
+ column: user_id
+ regex: "[1]"
+ simple-note-algorithm:
+ type: SIMPLE_NOTE
+ props:
+ shadow: true
+ foo: bar
+
+props:
+ sql-show: true
+ sql-comment-parse-enabled: true
diff --git
a/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/resources/META-INF/shadow.yaml
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/resources/META-INF/shadow.yaml
new file mode 100644
index 0000000..4b9578f
--- /dev/null
+++
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/resources/META-INF/shadow.yaml
@@ -0,0 +1,76 @@
+#
+# 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.
+#
+
+dataSources:
+ ds:
+ dataSourceClassName: com.zaxxer.hikari.HikariDataSource
+ driverClassName: com.mysql.jdbc.Driver
+ jdbcUrl:
jdbc:mysql://localhost:3306/ds?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
+ username: root
+ password:
+ ds_shadow:
+ dataSourceClassName: com.zaxxer.hikari.HikariDataSource
+ driverClassName: com.mysql.jdbc.Driver
+ jdbcUrl:
jdbc:mysql://localhost:3306/ds_shadow?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
+ username: root
+ password:
+
+rules:
+- !SHADOW
+ enable: true
+ dataSources:
+ shadowDataSource:
+ sourceDataSourceName: ds
+ shadowDataSourceName: ds_shadow
+ tables:
+ t_user:
+ dataSourceNames:
+ - shadowDataSource
+ shadowAlgorithmNames:
+ - user-id-insert-match-algorithm
+ - user-id-select-match-algorithm
+ - user-id-update-match-algorithm
+ - user-id-delete-match-algorithm
+ - simple-note-algorithm
+ shadowAlgorithms:
+ user-id-insert-match-algorithm:
+ type: COLUMN_REGEX_MATCH
+ props:
+ operation: insert
+ column: user_id
+ regex: "[1]"
+ user-id-select-match-algorithm:
+ type: COLUMN_REGEX_MATCH
+ props:
+ operation: select
+ column: user_id
+ regex: "[1]"
+ user-id-delete-match-algorithm:
+ type: COLUMN_REGEX_MATCH
+ props:
+ operation: delete
+ column: user_id
+ regex: "[1]"
+ simple-note-algorithm:
+ type: SIMPLE_NOTE
+ props:
+ shadow: true
+ foo: bar
+
+props:
+ sql-show: true
+ sql-comment-parse-enabled: true
diff --git
a/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/pom.xml
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/resources/logback.xml
similarity index 54%
copy from
examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/pom.xml
copy to
examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/resources/logback.xml
index be3a0ac..3d24ca1 100644
---
a/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/pom.xml
+++
b/examples/shardingsphere-jdbc-example/single-feature-example/shadow-example/shadow-raw-jdbc-example/src/main/resources/logback.xml
@@ -16,21 +16,19 @@
~ limitations under the License.
-->
-<project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.apache.shardingsphere.example</groupId>
- <artifactId>single-feature-example</artifactId>
- <version>5.0.0-RC1-SNAPSHOT</version>
- </parent>
- <artifactId>shadow-example</artifactId>
- <packaging>pom</packaging>
- <name>${project.artifactId}</name>
-
- <modules>
- <module>shadow-spring-namespace-mybatis-example</module>
- <module>shadow-spring-boot-mybatis-example</module>
- </modules>
-</project>
+<configuration>
+ <property name="log.context.name" value="shadow-raw-jdbc-example" />
+ <property name="log.charset" value="UTF-8" />
+ <property name="log.pattern" value="[%-5level] %date --%thread-- [%logger]
%msg %n" />
+ <contextName>${log.context.name}</contextName>
+
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ <encoder charset="${log.charset}">
+ <pattern>${log.pattern}</pattern>
+ </encoder>
+ </appender>
+ <root>
+ <level value="INFO" />
+ <appender-ref ref="STDOUT" />
+ </root>
+</configuration>