This is an automated email from the ASF dual-hosted git repository.

zhangliang 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 312009f  Add shadow jdbc example and template (#13931)
312009f is described below

commit 312009f5c92c1694bf5183989118133b169a8a84
Author: Guocheng Tang <[email protected]>
AuthorDate: Sat Dec 4 23:37:56 2021 +0800

    Add shadow jdbc example and template (#13931)
---
 .../main/resources/dataModel/jdbc/data-model.yaml  |   2 +-
 .../src/main/resources/template/Example.ftl        |   4 +-
 .../main/resources/template/jdbc/Configuration.ftl |  22 ++--
 .../resources/template/jdbc/ExampleService.ftl     |  10 +-
 ...rExampleService.ftl => orderExampleService.ftl} |  32 ++---
 .../template/jdbc/shadowConfiguration.ftl          | 102 +++++++++++++++
 ...ptExampleService.ftl => userExampleService.ftl} |   0
 .../pom.xml                                        |   1 +
 .../pom.xml                                        |  10 +-
 .../pom.xml                                        |  17 ++-
 .../jdbc/MemoryLocalShadowJdbcConfiguration.java   | 141 +++++++++++++++++++++
 .../shadow/jdbc/MemoryLocalShadowJdbcExample.java  |  31 +++++
 .../jdbc/MemoryLocalShadowJdbcExampleService.java} |  68 +++++++---
 .../example/shadow/jdbc/entity/User.java           |  60 +++++++++
 .../src/main/resources/logback.xml                 |  34 +++++
 15 files changed, 474 insertions(+), 60 deletions(-)

diff --git 
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/dataModel/jdbc/data-model.yaml
 
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/dataModel/jdbc/data-model.yaml
index 5bdd944..95d5537 100644
--- 
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/dataModel/jdbc/data-model.yaml
+++ 
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/dataModel/jdbc/data-model.yaml
@@ -17,7 +17,7 @@
 
 mode: memory
 transaction: local
-feature: encrypt
+feature: shadow
 framework: jdbc
 
 host: localhost
diff --git 
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/Example.ftl
 
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/Example.ftl
index 738a519..9ca339c 100644
--- 
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/Example.ftl
+++ 
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/Example.ftl
@@ -56,8 +56,8 @@ public class 
${mode?cap_first}${transaction?cap_first}${featureName}${frameworkN
     
     public static void main(final String[] args) throws SQLException {
     <#if framework=="jdbc">
-        
${mode?cap_first}${transaction?cap_first}${featureName}${framework?cap_first}Configuration
 shardingConfiguration = new 
${mode?cap_first}${transaction?cap_first}${featureName}${framework?cap_first}Configuration();
-        DataSource dataSource = shardingConfiguration.getDataSource();
+        
${mode?cap_first}${transaction?cap_first}${featureName}${framework?cap_first}Configuration
 configuration = new 
${mode?cap_first}${transaction?cap_first}${featureName}${framework?cap_first}Configuration();
+        DataSource dataSource = configuration.getDataSource();
         
${mode?cap_first}${transaction?cap_first}${featureName}${framework?cap_first}ExampleService
 exampleService = new 
${mode?cap_first}${transaction?cap_first}${featureName}${framework?cap_first}ExampleService(dataSource);
         exampleService.run();
     <#else>
diff --git 
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/Configuration.ftl
 
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/Configuration.ftl
index 1245145..36f4e9f 100644
--- 
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/Configuration.ftl
+++ 
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/Configuration.ftl
@@ -27,16 +27,21 @@ import 
org.apache.shardingsphere.sharding.api.config.ShardingRuleConfiguration;
 import 
org.apache.shardingsphere.sharding.api.config.rule.ShardingTableRuleConfiguration;
 import 
org.apache.shardingsphere.sharding.api.config.strategy.keygen.KeyGenerateStrategyConfiguration;
 import 
org.apache.shardingsphere.sharding.api.config.strategy.sharding.StandardShardingStrategyConfiguration;
-</#if>
-<#if feature=="readwrite-splitting">
+<#elseif feature=="readwrite-splitting">
 import 
org.apache.shardingsphere.readwritesplitting.api.ReadwriteSplittingRuleConfiguration;
 import 
org.apache.shardingsphere.readwritesplitting.api.rule.ReadwriteSplittingDataSourceRuleConfiguration;
-</#if>
-<#if feature=="encrypt">
+<#elseif feature=="encrypt">
 import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
 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;
+<#elseif feature=="shadow">
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+import 
org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
+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;
 </#if>
 
 import javax.sql.DataSource;
@@ -62,13 +67,14 @@ public final class 
${mode?cap_first}${transaction?cap_first}${featureName}${fram
     private static final String PASSWORD = "${(password)?c}";
 <#if feature=="sharding">
     <#include "shardingConfiguration.ftl">
-</#if>
-<#if feature=="readwrite-splitting">
+<#elseif feature=="readwrite-splitting">
     <#include "readwritesplittingConfiguration.ftl">
-</#if>
-<#if feature=="encrypt">
+<#elseif feature=="encrypt">
     <#include "encryptConfiguration.ftl">
+<#elseif feature=="shadow">
+    <#include "shadowConfiguration.ftl">
 </#if>
+    
     private DataSource createDataSource(final String dataSourceName) {
         HikariDataSource result = new HikariDataSource();
         result.setDriverClassName("com.mysql.jdbc.Driver");
diff --git 
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/ExampleService.ftl
 
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/ExampleService.ftl
index 63946b9..d4677c4 100644
--- 
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/ExampleService.ftl
+++ 
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/ExampleService.ftl
@@ -18,7 +18,7 @@
 package org.apache.shardingsphere.example.${feature?replace('-', 
'.')}.${framework?replace('-', '.')};
 
 import lombok.AllArgsConstructor;
-<#if feature=="encrypt">
+<#if feature=="encrypt" || feature=="shadow" >
 import org.apache.shardingsphere.example.${feature?replace('-', 
'.')}.${framework?replace('-', '.')}.entity.User;
 <#else>
 import org.apache.shardingsphere.example.${feature?replace('-', 
'.')}.${framework?replace('-', '.')}.entity.Order;
@@ -67,10 +67,10 @@ public final class 
${mode?cap_first}${transaction?cap_first}${featureName}${fram
             this.cleanEnvironment();
         }
     }
-
-<#if feature=="encrypt">
-    <#include "encryptExampleService.ftl">
+    
+<#if feature=="encrypt" || feature=="shadow">
+    <#include "userExampleService.ftl">
 <#else>
-    <#include "otherExampleService.ftl">
+    <#include "orderExampleService.ftl">
 </#if>
 }
diff --git 
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/otherExampleService.ftl
 
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/orderExampleService.ftl
similarity index 91%
rename from 
examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/otherExampleService.ftl
rename to 
examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/orderExampleService.ftl
index e68b044..7e76fda 100644
--- 
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/otherExampleService.ftl
+++ 
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/orderExampleService.ftl
@@ -1,19 +1,19 @@
-/*
- * 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.
- */
+<#--
+  ~ 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.
+  -->
     
     /**
      * Initialize the database test environment.
diff --git 
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/shadowConfiguration.ftl
 
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/shadowConfiguration.ftl
new file mode 100644
index 0000000..ed7219b
--- /dev/null
+++ 
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/shadowConfiguration.ftl
@@ -0,0 +1,102 @@
+<#--
+  ~ 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.
+  -->
+    
+    public DataSource getDataSource() throws SQLException {
+        Map<String, DataSource> dataSourceMap = createDataSourceMap();
+        Collection<RuleConfiguration> ruleConfigurations = 
createRuleConfiguration();
+        return ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, 
ruleConfigurations, createShardingSphereProps());
+    }
+
+    private Map<String, DataSource> createDataSourceMap() {
+        Map<String, DataSource> result = new LinkedHashMap<>();
+        result.put("demo_ds_0", createDataSource("demo_ds_0"));
+        result.put("ds_shadow", createDataSource("ds_shadow"));
+        return result;
+    }
+
+    private Properties createShardingSphereProps() {
+        Properties result = new Properties();
+        result.setProperty(ConfigurationPropertyKey.SQL_SHOW.getKey(), "true");
+        
result.setProperty(ConfigurationPropertyKey.SQL_COMMENT_PARSE_ENABLED.getKey(), 
"true");
+        return result;
+    }
+
+    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> 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-hint-algorithm");
+        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;
+    }
+
+    private 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("value", "1");
+        result.put("user-id-insert-match-algorithm", new 
ShardingSphereAlgorithmConfiguration("VALUE_MATCH", userIdInsertProps));
+        Properties userIdDeleteProps = new Properties();
+        userIdDeleteProps.setProperty("operation", "delete");
+        userIdDeleteProps.setProperty("column", "user_type");
+        userIdDeleteProps.setProperty("value", "1");
+        result.put("user-id-delete-match-algorithm", new 
ShardingSphereAlgorithmConfiguration("VALUE_MATCH", userIdDeleteProps));
+        Properties userIdSelectProps = new Properties();
+        userIdSelectProps.setProperty("operation", "select");
+        userIdSelectProps.setProperty("column", "user_type");
+        userIdSelectProps.setProperty("value", "1");
+        result.put("user-id-select-match-algorithm", new 
ShardingSphereAlgorithmConfiguration("VALUE_MATCH", userIdSelectProps));
+        Properties noteAlgorithmProps = new Properties();
+        noteAlgorithmProps.setProperty("shadow", "true");
+        noteAlgorithmProps.setProperty("foo", "bar");
+        result.put("simple-hint-algorithm", new 
ShardingSphereAlgorithmConfiguration("SIMPLE_HINT", noteAlgorithmProps));
+        return result;
+    }
diff --git 
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/encryptExampleService.ftl
 
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/userExampleService.ftl
similarity index 100%
copy from 
examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/encryptExampleService.ftl
copy to 
examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/userExampleService.ftl
diff --git 
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml
 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml
index ef58733..4c6114f 100644
--- 
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml
+++ 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml
@@ -32,5 +32,6 @@
         <module>shardingsphere-jdbc-memory-local-sharding-example</module>
         
<module>shardingsphere-jdbc-memory-local-readwrite-splitting-example</module>
         <module>shardingsphere-jdbc-memory-local-encrypt-example</module>
+        <module>shardingsphere-jdbc-memory-local-shadow-example</module>
     </modules>
 </project>
diff --git 
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml
 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-shadow-example/pom.xml
similarity index 78%
copy from 
examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml
copy to 
examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-shadow-example/pom.xml
index ef58733..d514b8d 100644
--- 
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml
+++ 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-shadow-example/pom.xml
@@ -20,17 +20,17 @@
          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";>
     <parent>
-        <artifactId>shardingsphere-jdbc-memory-example</artifactId>
+        <artifactId>shardingsphere-jdbc-memory-local-example</artifactId>
         <groupId>org.apache.shardingsphere.example</groupId>
         <version>5.0.1-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <packaging>pom</packaging>
-    <artifactId>shardingsphere-jdbc-memory-local-example</artifactId>
+    <artifactId>shardingsphere-jdbc-memory-local-shadow-example</artifactId>
     <name>${project.artifactId}</name>
+    
     <modules>
-        <module>shardingsphere-jdbc-memory-local-sharding-example</module>
-        
<module>shardingsphere-jdbc-memory-local-readwrite-splitting-example</module>
-        <module>shardingsphere-jdbc-memory-local-encrypt-example</module>
+        <module>shardingsphere-jdbc-memory-local-shadow-jdbc-example</module>
     </modules>
+    
 </project>
diff --git 
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml
 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-shadow-example/shardingsphere-jdbc-memory-local-shadow-jdbc-example/pom.xml
similarity index 76%
copy from 
examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml
copy to 
examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-shadow-example/shardingsphere-jdbc-memory-local-shadow-jdbc-example/pom.xml
index ef58733..e0e21d0 100644
--- 
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/pom.xml
+++ 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-shadow-example/shardingsphere-jdbc-memory-local-shadow-jdbc-example/pom.xml
@@ -20,17 +20,20 @@
          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";>
     <parent>
-        <artifactId>shardingsphere-jdbc-memory-example</artifactId>
+        
<artifactId>shardingsphere-jdbc-memory-local-shadow-example</artifactId>
         <groupId>org.apache.shardingsphere.example</groupId>
         <version>5.0.1-SNAPSHOT</version>
     </parent>
     <modelVersion>4.0.0</modelVersion>
     <packaging>pom</packaging>
-    <artifactId>shardingsphere-jdbc-memory-local-example</artifactId>
+    
<artifactId>shardingsphere-jdbc-memory-local-shadow-jdbc-example</artifactId>
     <name>${project.artifactId}</name>
-    <modules>
-        <module>shardingsphere-jdbc-memory-local-sharding-example</module>
-        
<module>shardingsphere-jdbc-memory-local-readwrite-splitting-example</module>
-        <module>shardingsphere-jdbc-memory-local-encrypt-example</module>
-    </modules>
+    
+    <dependencies>
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-jdbc-core</artifactId>
+        </dependency>
+    </dependencies>
+    
 </project>
diff --git 
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-shadow-example/shardingsphere-jdbc-memory-local-shadow-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/jdbc/MemoryLocalShadowJdbcConfiguration.java
 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardings
 [...]
new file mode 100644
index 0000000..3408ea0
--- /dev/null
+++ 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-shadow-example/shardingsphere-jdbc-memory-local-shadow-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/jdbc/MemoryLocalShadowJdbcConfiguration.java
@@ -0,0 +1,141 @@
+/*
+ * 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.jdbc;
+
+import com.zaxxer.hikari.HikariDataSource;
+import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import 
org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+import 
org.apache.shardingsphere.infra.config.properties.ConfigurationPropertyKey;
+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 MemoryLocalShadowJdbcConfiguration {
+    
+    private static final String HOST = "localhost";
+    
+    private static final int PORT = 3306;
+    
+    private static final String USER_NAME = "root";
+    
+    private static final String PASSWORD = "root";
+    
+    public DataSource getDataSource() throws SQLException {
+        Map<String, DataSource> dataSourceMap = createDataSourceMap();
+        Collection<RuleConfiguration> ruleConfigurations = 
createRuleConfiguration();
+        return ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, 
ruleConfigurations, createShardingSphereProps());
+    }
+
+    private Map<String, DataSource> createDataSourceMap() {
+        Map<String, DataSource> result = new LinkedHashMap<>();
+        result.put("demo_ds_0", createDataSource("demo_ds_0"));
+        result.put("ds_shadow", createDataSource("ds_shadow"));
+        return result;
+    }
+
+    private Properties createShardingSphereProps() {
+        Properties result = new Properties();
+        result.setProperty(ConfigurationPropertyKey.SQL_SHOW.getKey(), "true");
+        
result.setProperty(ConfigurationPropertyKey.SQL_COMMENT_PARSE_ENABLED.getKey(), 
"true");
+        return result;
+    }
+
+    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> 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-hint-algorithm");
+        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;
+    }
+
+    private 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("value", "1");
+        result.put("user-id-insert-match-algorithm", new 
ShardingSphereAlgorithmConfiguration("VALUE_MATCH", userIdInsertProps));
+        Properties userIdDeleteProps = new Properties();
+        userIdDeleteProps.setProperty("operation", "delete");
+        userIdDeleteProps.setProperty("column", "user_type");
+        userIdDeleteProps.setProperty("value", "1");
+        result.put("user-id-delete-match-algorithm", new 
ShardingSphereAlgorithmConfiguration("VALUE_MATCH", userIdDeleteProps));
+        Properties userIdSelectProps = new Properties();
+        userIdSelectProps.setProperty("operation", "select");
+        userIdSelectProps.setProperty("column", "user_type");
+        userIdSelectProps.setProperty("value", "1");
+        result.put("user-id-select-match-algorithm", new 
ShardingSphereAlgorithmConfiguration("VALUE_MATCH", userIdSelectProps));
+        Properties noteAlgorithmProps = new Properties();
+        noteAlgorithmProps.setProperty("shadow", "true");
+        noteAlgorithmProps.setProperty("foo", "bar");
+        result.put("simple-hint-algorithm", new 
ShardingSphereAlgorithmConfiguration("SIMPLE_HINT", noteAlgorithmProps));
+        return result;
+    }
+    
+    private DataSource createDataSource(final String dataSourceName) {
+        HikariDataSource result = new HikariDataSource();
+        result.setDriverClassName("com.mysql.jdbc.Driver");
+        
result.setJdbcUrl(String.format("jdbc:mysql://%s:%s/%s?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8",
 HOST, PORT, dataSourceName));
+        result.setUsername(USER_NAME);
+        result.setPassword(PASSWORD);
+        return result;
+    }
+}
diff --git 
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-shadow-example/shardingsphere-jdbc-memory-local-shadow-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/jdbc/MemoryLocalShadowJdbcExample.java
 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-
 [...]
new file mode 100644
index 0000000..40817eb
--- /dev/null
+++ 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-shadow-example/shardingsphere-jdbc-memory-local-shadow-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/jdbc/MemoryLocalShadowJdbcExample.java
@@ -0,0 +1,31 @@
+/*
+ * 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.jdbc;
+
+import javax.sql.DataSource;
+import java.sql.SQLException;
+
+public class MemoryLocalShadowJdbcExample {
+    
+    public static void main(final String[] args) throws SQLException {
+        MemoryLocalShadowJdbcConfiguration configuration = new 
MemoryLocalShadowJdbcConfiguration();
+        DataSource dataSource = configuration.getDataSource();
+        MemoryLocalShadowJdbcExampleService exampleService = new 
MemoryLocalShadowJdbcExampleService(dataSource);
+        exampleService.run();
+    }
+}
diff --git 
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/encryptExampleService.ftl
 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-shadow-example/shardingsphere-jdbc-memory-local-shadow-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/jdbc/MemoryLocalShadowJdbcExampleService.java
similarity index 71%
rename from 
examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/encryptExampleService.ftl
rename to 
examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-shadow-example/shardingsphere-jdbc-memory-local-shadow-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/jdbc/MemoryLocalShadowJdbcExampleService.java
index 5d9d28d..7301856 100644
--- 
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/jdbc/encryptExampleService.ftl
+++ 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-shadow-example/shardingsphere-jdbc-memory-local-shadow-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/jdbc/MemoryLocalShadowJdbcExampleService.java
@@ -1,19 +1,54 @@
-<#--
-  ~ 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.
-  -->
+/*
+ * 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.jdbc;
+
+import lombok.AllArgsConstructor;
+import org.apache.shardingsphere.example.shadow.jdbc.entity.User;
+
+import javax.sql.DataSource;
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.LinkedList;
+import java.util.List;
+
+@AllArgsConstructor
+public final class MemoryLocalShadowJdbcExampleService {
+    
+    private final DataSource dataSource;
+
+    /**
+     * Execute test.
+     *
+     * @throws SQLException
+     */
+    public void run() throws SQLException {
+        try {
+            this.initEnvironment();
+            this.processSuccess();
+        } finally {
+            this.cleanEnvironment();
+        }
+    }
+    
 
     /**
      * Initialize the database test environment.
@@ -113,3 +148,4 @@
             statement.executeUpdate(dropUserSql);
         }
     }
+}
diff --git 
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-shadow-example/shardingsphere-jdbc-memory-local-shadow-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/jdbc/entity/User.java
 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local
 [...]
new file mode 100644
index 0000000..f1cd3b7
--- /dev/null
+++ 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-shadow-example/shardingsphere-jdbc-memory-local-shadow-jdbc-example/src/main/java/org/apache/shardingsphere/example/shadow/jdbc/entity/User.java
@@ -0,0 +1,60 @@
+/*
+ * 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.jdbc.entity;
+
+import java.io.Serializable;
+
+public class User implements Serializable {
+    
+    private static final long serialVersionUID = 263434701950670170L;
+    
+    private int userId;
+
+    private String userName;
+
+    private String pwd;
+    
+    public int getUserId() {
+        return userId;
+    }
+    
+    public void setUserId(final int userId) {
+        this.userId = userId;
+    }
+    
+    public String getUserName() {
+        return userName;
+    }
+    
+    public void setUserName(final String userName) {
+        this.userName = userName;
+    }
+    
+    public String getPwd() {
+        return pwd;
+    }
+    
+    public void setPwd(final String pwd) {
+        this.pwd = pwd;
+    }
+    
+    @Override
+    public String toString() {
+        return String.format("user_id: %d, user_name: %s, pwd: %s", userId, 
userName, pwd);
+    }
+}
diff --git 
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-shadow-example/shardingsphere-jdbc-memory-local-shadow-jdbc-example/src/main/resources/logback.xml
 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-shadow-example/shardingsphere-jdbc-memory-loc
 [...]
new file mode 100644
index 0000000..154abba
--- /dev/null
+++ 
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-shadow-example/shardingsphere-jdbc-memory-local-shadow-jdbc-example/src/main/resources/logback.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+
+<configuration>
+    <property name="log.context.name" value="shardingsphere-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>

Reply via email to