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 4166bdb Add db-discovery spring namespace jdbc example and template
(#14330)
4166bdb is described below
commit 4166bdb21dffec86603ddb9e95e444f458e243f0
Author: Guocheng Tang <[email protected]>
AuthorDate: Sun Dec 26 12:29:35 2021 +0800
Add db-discovery spring namespace jdbc example and template (#14330)
---
.../main/resources/template/xml/application.ftl | 8 +-
.../main/resources/template/xml/db-discovery.ftl | 56 +++++
.../pom.xml | 1 +
.../pom.xml | 22 +-
...LocalDbDiscoverySpringNamespaceJdbcExample.java | 32 +++
...DiscoverySpringNamespaceJdbcExampleService.java | 227 +++++++++++++++++++++
.../spring/namespace/jdbc/entity/Order.java | 70 +++++++
.../spring/namespace/jdbc/entity/OrderItem.java | 70 +++++++
.../src/main/resources/application.xml} | 84 ++++----
.../src/main/resources/logback.xml | 34 +++
10 files changed, 545 insertions(+), 59 deletions(-)
diff --git
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/application.ftl
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/application.ftl
index 3da5275..4417e73 100644
---
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/application.ftl
+++
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/application.ftl
@@ -26,6 +26,7 @@
xmlns:readwrite-splitting="http://shardingsphere.apache.org/schema/shardingsphere/readwrite-splitting"
xmlns:encrypt="http://shardingsphere.apache.org/schema/shardingsphere/encrypt"
xmlns:shadow="http://shardingsphere.apache.org/schema/shardingsphere/shadow"
+
xmlns:database-discovery="http://shardingsphere.apache.org/schema/shardingsphere/database-discovery"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
@@ -42,6 +43,8 @@
http://shardingsphere.apache.org/schema/shardingsphere/encrypt/encrypt.xsd
http://shardingsphere.apache.org/schema/shardingsphere/shadow
http://shardingsphere.apache.org/schema/shardingsphere/shadow/shadow.xsd
+
http://shardingsphere.apache.org/schema/shardingsphere/database-discovery
+
http://shardingsphere.apache.org/schema/shardingsphere/database-discovery/database-discovery.xsd
">
<context:annotation-config />
<context:component-scan
base-package="org.apache.shardingsphere.example.${feature?replace('-',
'.')}.${framework?replace('-', '.')}"/>
@@ -63,7 +66,7 @@
<bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory" />
<tx:annotation-driven />
</#if>
-
+<#if feature!="db-discovery">
<bean id="demo_ds_0" class="com.zaxxer.hikari.HikariDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3307/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
@@ -77,6 +80,7 @@
<property name="username" value="root"/>
<property name="password" value="123456"/>
</bean>
+</#if>
<#if feature=="sharding">
<#include "shardingApplication.ftl">
<#elseif feature=="readwrite-splitting">
@@ -85,6 +89,8 @@
<#include "encryptApplication.ftl">
<#elseif feature=="shadow">
<#include "shadowApplication.ftl">
+<#elseif feature=="db-discovery">
+ <#include "db-discovery.ftl">
</#if>
<#if framework=="spring-namespace-mybatis">
diff --git
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/db-discovery.ftl
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/db-discovery.ftl
new file mode 100644
index 0000000..3c37fd6
--- /dev/null
+++
b/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/db-discovery.ftl
@@ -0,0 +1,56 @@
+<#--
+ ~ 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.
+ -->
+
+ <bean id="ds_0" class="com.zaxxer.hikari.HikariDataSource"
destroy-method="close">
+ <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
+ <property name="jdbcUrl"
value="jdbc:mysql://172.72.0.15:3306/ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
+ <property name="username" value="root"/>
+ <property name="password" value="root"/>
+ </bean>
+
+ <bean id="ds_0_replica_0" class="com.zaxxer.hikari.HikariDataSource"
destroy-method="close">
+ <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
+ <property name="jdbcUrl"
value="jdbc:mysql://172.72.0.16:3306/ds_0_replica_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
+ <property name="username" value="root"/>
+ <property name="password" value="root"/>
+ </bean>
+
+ <bean id="ds_0_replica_1" class="com.zaxxer.hikari.HikariDataSource"
destroy-method="close">
+ <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
+ <property name="jdbcUrl"
value="jdbc:mysql://172.72.0.17:3306/ds_0_replica_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
+ <property name="username" value="root"/>
+ <property name="password" value="root"/>
+ </bean>
+
+ <database-discovery:rule id="databaseDiscoveryRule">
+ <database-discovery:data-source-rule id="db"
data-source-names="ds_0,ds_0_replica_1,ds_0_replica_1"
discovery-heartbeat-name="mgr-heartbeat"
+ discovery-type-name="mgr"/>
+ <database-discovery:discovery-heartbeat id="mgr-heartbeat">
+ <props>
+ <prop key="keep-alive-cron">0/5 * * * * ?</prop>
+ </props>
+ </database-discovery:discovery-heartbeat>
+ </database-discovery:rule>
+
+ <database-discovery:discovery-type id="mgr" type="MGR">
+ <props>
+ <prop key="keep-alive-cron">0/5 * * * * ?</prop>
+ <prop key="group-name">aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa</prop>
+ </props>
+ </database-discovery:discovery-type>
+
+ <shardingsphere:data-source id="dataSource"
data-source-names="ds_0,ds_0_replica_1,ds_0_replica_1"
rule-refs="databaseDiscoveryRule" />
diff --git
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/pom.xml
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/pom.xml
index 8af3fdf..f293626 100644
---
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/pom.xml
+++
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/pom.xml
@@ -34,6 +34,7 @@
<module>shardingsphere-jdbc-memory-local-db-discovery-spring-boot-starter-jdbc-example</module>
<module>shardingsphere-jdbc-memory-local-db-discovery-spring-boot-starter-jpa-example</module>
<module>shardingsphere-jdbc-memory-local-db-discovery-spring-boot-starter-mybatis-example</module>
+
<module>shardingsphere-jdbc-memory-local-db-discovery-spring-namespace-jdbc-example</module>
</modules>
</project>
diff --git
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/pom.xml
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-namespace-jdbc-example/pom.xml
similarity index 68%
copy from
examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/pom.xml
copy to
examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-namespace-jdbc-example/pom.xml
index 8af3fdf..0b64f94 100644
---
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/pom.xml
+++
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-namespace-jdbc-example/pom.xml
@@ -20,20 +20,24 @@
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-local-example</artifactId>
+
<artifactId>shardingsphere-jdbc-memory-local-db-discovery-example</artifactId>
<groupId>org.apache.shardingsphere.example</groupId>
<version>${revision}</version>
</parent>
<modelVersion>4.0.0</modelVersion>
- <packaging>pom</packaging>
-
<artifactId>shardingsphere-jdbc-memory-local-db-discovery-example</artifactId>
+
<artifactId>shardingsphere-jdbc-memory-local-db-discovery-spring-namespace-jdbc-example</artifactId>
<name>${project.artifactId}</name>
- <modules>
-
<module>shardingsphere-jdbc-memory-local-db-discovery-jdbc-example</module>
-
<module>shardingsphere-jdbc-memory-local-db-discovery-spring-boot-starter-jdbc-example</module>
-
<module>shardingsphere-jdbc-memory-local-db-discovery-spring-boot-starter-jpa-example</module>
-
<module>shardingsphere-jdbc-memory-local-db-discovery-spring-boot-starter-mybatis-example</module>
- </modules>
+ <dependencies>
+ <dependency>
+ <groupId>org.apache.shardingsphere</groupId>
+ <artifactId>shardingsphere-jdbc-core-spring-namespace</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>org.springframework</groupId>
+ <artifactId>spring-context</artifactId>
+ <version>5.1.5.RELEASE</version>
+ </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-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/db/discovery/spring/namespace/jdbc/MemoryLocalDbDiscoverySpringNamespaceJdbcExample.java
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc
[...]
new file mode 100644
index 0000000..de6676f
--- /dev/null
+++
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/db/discovery/spring/namespace/jdbc/MemoryLocalDbDiscoverySpringNamespaceJdbcExample.java
@@ -0,0 +1,32 @@
+/*
+ * 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.db.discovery.spring.namespace.jdbc;
+
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.context.support.ClassPathXmlApplicationContext;
+import java.sql.SQLException;
+
+public class MemoryLocalDbDiscoverySpringNamespaceJdbcExample {
+
+ public static void main(final String[] args) throws SQLException {
+ try (ConfigurableApplicationContext applicationContext = new
ClassPathXmlApplicationContext("application.xml")) {
+ MemoryLocalDbDiscoverySpringNamespaceJdbcExampleService
exampleService =
applicationContext.getBean(MemoryLocalDbDiscoverySpringNamespaceJdbcExampleService.class);
+ exampleService.run();
+ }
+ }
+}
diff --git
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/db/discovery/spring/namespace/jdbc/MemoryLocalDbDiscoverySpringNamespaceJdbcExampleService.java
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphe [...]
new file mode 100644
index 0000000..24b7b85
--- /dev/null
+++
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/db/discovery/spring/namespace/jdbc/MemoryLocalDbDiscoverySpringNamespaceJdbcExampleService.java
@@ -0,0 +1,227 @@
+/*
+ * 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.db.discovery.spring.namespace.jdbc;
+
+import lombok.AllArgsConstructor;
+import
org.apache.shardingsphere.example.db.discovery.spring.namespace.jdbc.entity.Order;
+import
org.apache.shardingsphere.example.db.discovery.spring.namespace.jdbc.entity.OrderItem;
+import org.springframework.stereotype.Service;
+
+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;
+
+@Service
+@AllArgsConstructor
+public final class MemoryLocalDbDiscoverySpringNamespaceJdbcExampleService {
+
+ 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.
+ * @throws SQLException
+ */
+ private void initEnvironment() throws SQLException {
+ String createOrderTableSql = "CREATE TABLE IF NOT EXISTS t_order
(order_id BIGINT NOT NULL AUTO_INCREMENT, user_id INT NOT NULL, address_id
BIGINT NOT NULL, status VARCHAR(50), PRIMARY KEY (order_id))";
+ String createOrderItemTableSql = "CREATE TABLE IF NOT EXISTS
t_order_item "
+ + "(order_item_id BIGINT NOT NULL AUTO_INCREMENT, order_id
BIGINT NOT NULL, user_id INT NOT NULL, status VARCHAR(50), PRIMARY KEY
(order_item_id))";
+ String createAddressTableSql = "CREATE TABLE IF NOT EXISTS t_address "
+ + "(address_id BIGINT NOT NULL, address_name VARCHAR(100) NOT
NULL, PRIMARY KEY (address_id))";
+ String truncateOrderTable = "TRUNCATE TABLE t_order";
+ String truncateOrderItemTable = "TRUNCATE TABLE t_order_item";
+ String truncateAddressTableSql = "TRUNCATE TABLE t_address";
+
+ try (Connection connection = dataSource.getConnection();
+ Statement statement = connection.createStatement()) {
+ statement.executeUpdate(createOrderTableSql);
+ statement.executeUpdate(createOrderItemTableSql);
+ statement.executeUpdate(createAddressTableSql);
+ statement.executeUpdate(createAddressTableSql);
+ statement.executeUpdate(truncateOrderTable);
+ statement.executeUpdate(truncateOrderItemTable);
+ statement.executeUpdate(truncateAddressTableSql);
+ }
+ }
+
+ private void processSuccess() throws SQLException {
+ System.out.println("-------------- Process Success Begin
---------------");
+ List<Long> orderIds = insertData();
+ printData();
+ deleteData(orderIds);
+ printData();
+ System.out.println("-------------- Process Success Finish
--------------");
+ }
+
+ private void processFailure() throws SQLException {
+ System.out.println("-------------- Process Failure Begin
---------------");
+ insertData();
+ System.out.println("-------------- Process Failure Finish
--------------");
+ throw new RuntimeException("Exception occur for transaction test.");
+ }
+
+ private List<Long> insertData() throws SQLException {
+ System.out.println("---------------------------- Insert Data
----------------------------");
+ List<Long> result = new ArrayList<>(10);
+ for (int i = 1; i <= 10; i++) {
+ Order order = insertOrder(i);
+ insertOrderItem(i, order);
+ result.add(order.getOrderId());
+ }
+ return result;
+ }
+
+ private Order insertOrder(final int i) throws SQLException {
+ Order order = new Order();
+ order.setUserId(i);
+ order.setAddressId(i);
+ order.setStatus("INSERT_TEST");
+ String sql = "INSERT INTO t_order (user_id, address_id, status) VALUES
(?, ?, ?)";
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement preparedStatement =
connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
+ preparedStatement.setInt(1, order.getUserId());
+ preparedStatement.setLong(2, order.getAddressId());
+ preparedStatement.setString(3, order.getStatus());
+ preparedStatement.executeUpdate();
+ try (ResultSet resultSet = preparedStatement.getGeneratedKeys()) {
+ if (resultSet.next()) {
+ order.setOrderId(resultSet.getLong(1));
+ }
+ }
+ }
+ return order;
+ }
+
+ private void insertOrderItem(final int i, final Order order) throws
SQLException {
+ OrderItem orderItem = new OrderItem();
+ orderItem.setOrderId(order.getOrderId());
+ orderItem.setUserId(i);
+ orderItem.setStatus("INSERT_TEST");
+ String sql = "INSERT INTO t_order_item (order_id, user_id, status)
VALUES (?, ?, ?)";
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement preparedStatement =
connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS)) {
+ preparedStatement.setLong(1, orderItem.getOrderId());
+ preparedStatement.setInt(2, orderItem.getUserId());
+ preparedStatement.setString(3, orderItem.getStatus());
+ preparedStatement.executeUpdate();
+ try (ResultSet resultSet = preparedStatement.getGeneratedKeys()) {
+ if (resultSet.next()) {
+ orderItem.setOrderItemId(resultSet.getLong(1));
+ }
+ }
+ }
+ }
+
+ private void deleteData(final List<Long> orderIds) throws SQLException {
+ System.out.println("---------------------------- Delete Data
----------------------------");
+ for (Long each : orderIds) {
+ String deleteOrderSql = "DELETE FROM t_order WHERE order_id=?";
+ String deleteOrderItemSql = "DELETE FROM t_order_item WHERE
order_id=?";
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement orderPreparedStatement =
connection.prepareStatement(deleteOrderSql);
+ PreparedStatement orderItemPreparedStatement =
connection.prepareStatement(deleteOrderItemSql)) {
+ orderPreparedStatement.setLong(1, each);
+ orderItemPreparedStatement.setLong(1, each);
+ orderPreparedStatement.executeUpdate();
+ orderItemPreparedStatement.executeUpdate();
+ }
+ }
+ }
+
+ private void printData() throws SQLException {
+ System.out.println("---------------------------- Print Order Data
-----------------------");
+ for (Object each : this.getOrders()) {
+ System.out.println(each);
+ }
+ System.out.println("---------------------------- Print OrderItem Data
-------------------");
+ for (Object each : this.getOrderItems()) {
+ System.out.println(each);
+ }
+ }
+
+ protected List<OrderItem> getOrderItems() throws SQLException {
+ String sql = "SELECT i.* FROM t_order o, t_order_item i WHERE
o.order_id = i.order_id";
+ List<OrderItem> result = new LinkedList<>();
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement preparedStatement =
connection.prepareStatement(sql);
+ ResultSet resultSet = preparedStatement.executeQuery()) {
+ while (resultSet.next()) {
+ OrderItem orderItem = new OrderItem();
+ orderItem.setOrderItemId(resultSet.getLong(1));
+ orderItem.setOrderId(resultSet.getLong(2));
+ orderItem.setUserId(resultSet.getInt(3));
+ orderItem.setStatus(resultSet.getString(4));
+ result.add(orderItem);
+ }
+ }
+ return result;
+ }
+
+ protected List<Order> getOrders() throws SQLException {
+ String sql = "SELECT * FROM t_order";
+ List<Order> result = new LinkedList<>();
+ try (Connection connection = dataSource.getConnection();
+ PreparedStatement preparedStatement =
connection.prepareStatement(sql);
+ ResultSet resultSet = preparedStatement.executeQuery()) {
+ while (resultSet.next()) {
+ Order order = new Order();
+ order.setOrderId(resultSet.getLong(1));
+ order.setUserId(resultSet.getInt(2));
+ order.setAddressId(resultSet.getLong(3));
+ order.setStatus(resultSet.getString(4));
+ result.add(order);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Restore the environment.
+ * @throws SQLException
+ */
+ private void cleanEnvironment() throws SQLException {
+ String dropOrderSql = "DROP TABLE t_order";
+ String dropOrderItemSql = "DROP TABLE t_order_item";
+ String dropAddressSql = "DROP TABLE t_address";
+ try (Connection connection = dataSource.getConnection();
+ Statement statement = connection.createStatement()) {
+ statement.executeUpdate(dropOrderSql);
+ statement.executeUpdate(dropOrderItemSql);
+ statement.executeUpdate(dropAddressSql);
+ }
+ }
+}
diff --git
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/db/discovery/spring/namespace/jdbc/entity/Order.java
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-
[...]
new file mode 100644
index 0000000..534f78a
--- /dev/null
+++
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/db/discovery/spring/namespace/jdbc/entity/Order.java
@@ -0,0 +1,70 @@
+/*
+ * 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.db.discovery.spring.namespace.jdbc.entity;
+
+import java.io.Serializable;
+
+public class Order implements Serializable {
+
+ private static final long serialVersionUID = 8306802022239174861L;
+
+ private long orderId;
+
+ private int userId;
+
+ private long addressId;
+
+ private String status;
+
+ public long getOrderId() {
+ return orderId;
+ }
+
+ public void setOrderId(final long orderId) {
+ this.orderId = orderId;
+ }
+
+ public int getUserId() {
+ return userId;
+ }
+
+ public void setUserId(final int userId) {
+ this.userId = userId;
+ }
+
+ public String getStatus() {
+ return status;
+ }
+
+ public void setStatus(final String status) {
+ this.status = status;
+ }
+
+ public long getAddressId() {
+ return addressId;
+ }
+
+ public void setAddressId(final long addressId) {
+ this.addressId = addressId;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("order_id: %s, user_id: %s, address_id: %s,
status: %s", orderId, userId, addressId, status);
+ }
+}
diff --git
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/db/discovery/spring/namespace/jdbc/entity/OrderItem.java
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-j
[...]
new file mode 100644
index 0000000..a20234f
--- /dev/null
+++
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-namespace-jdbc-example/src/main/java/org/apache/shardingsphere/example/db/discovery/spring/namespace/jdbc/entity/OrderItem.java
@@ -0,0 +1,70 @@
+/*
+ * 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.db.discovery.spring.namespace.jdbc.entity;
+
+import java.io.Serializable;
+
+public class OrderItem implements Serializable {
+
+ private static final long serialVersionUID = 1332162822494069342L;
+
+ private long orderItemId;
+
+ private long orderId;
+
+ private int userId;
+
+ private String status;
+
+ public long getOrderItemId() {
+ return orderItemId;
+ }
+
+ public void setOrderItemId(final long orderItemId) {
+ this.orderItemId = orderItemId;
+ }
+
+ public long getOrderId() {
+ return orderId;
+ }
+
+ public void setOrderId(final long orderId) {
+ this.orderId = orderId;
+ }
+
+ public int getUserId() {
+ return userId;
+ }
+
+ public void setUserId(final int userId) {
+ this.userId = userId;
+ }
+
+ public String getStatus() {
+ return status;
+ }
+
+ public void setStatus(final String status) {
+ this.status = status;
+ }
+
+ @Override
+ public String toString() {
+ return String.format("order_item_id:%s, order_id: %s, user_id: %s,
status: %s", orderItemId, orderId, userId, status);
+ }
+}
diff --git
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/application.ftl
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-namespace-jdbc-example/src/main/resources/application.xml
similarity index 56%
copy from
examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/application.ftl
copy to
examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-namespace-jdbc-example/src/main/resources/application.xml
index 3da5275..3cc72fe 100644
---
a/examples/shardingsphere-sample/shardingsphere-example-engine/src/main/resources/template/xml/application.ftl
+++
b/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-namespace-jdbc-example/src/main/resources/application.xml
@@ -26,6 +26,7 @@
xmlns:readwrite-splitting="http://shardingsphere.apache.org/schema/shardingsphere/readwrite-splitting"
xmlns:encrypt="http://shardingsphere.apache.org/schema/shardingsphere/encrypt"
xmlns:shadow="http://shardingsphere.apache.org/schema/shardingsphere/shadow"
+
xmlns:database-discovery="http://shardingsphere.apache.org/schema/shardingsphere/database-discovery"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
@@ -42,65 +43,50 @@
http://shardingsphere.apache.org/schema/shardingsphere/encrypt/encrypt.xsd
http://shardingsphere.apache.org/schema/shardingsphere/shadow
http://shardingsphere.apache.org/schema/shardingsphere/shadow/shadow.xsd
+
http://shardingsphere.apache.org/schema/shardingsphere/database-discovery
+
http://shardingsphere.apache.org/schema/shardingsphere/database-discovery/database-discovery.xsd
">
<context:annotation-config />
- <context:component-scan
base-package="org.apache.shardingsphere.example.${feature?replace('-',
'.')}.${framework?replace('-', '.')}"/>
-<#if framework=="spring-namespace-jpa">
- <bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="jpaVendorAdapter">
- <bean
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
p:database="MYSQL" />
- </property>
- <property name="packagesToScan"
value="org.apache.shardingsphere.example.${feature?replace('-',
'.')}.${framework?replace('-', '.')}.entity" />
- <property name="jpaProperties">
- <props>
- <prop
key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
- <prop key="hibernate.hbm2ddl.auto">create-drop</prop>
- <prop key="hibernate.show_sql">false</prop>
- </props>
- </property>
- </bean>
- <bean id="transactionManager"
class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory" />
- <tx:annotation-driven />
-</#if>
+ <context:component-scan
base-package="org.apache.shardingsphere.example.db.discovery.spring.namespace.jdbc"/>
- <bean id="demo_ds_0" class="com.zaxxer.hikari.HikariDataSource"
destroy-method="close">
+ <bean id="ds_0" class="com.zaxxer.hikari.HikariDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
- <property name="jdbcUrl"
value="jdbc:mysql://localhost:3307/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
+ <property name="jdbcUrl"
value="jdbc:mysql://172.72.0.15:3306/ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
- <property name="password" value="123456"/>
+ <property name="password" value="root"/>
</bean>
- <bean id="demo_ds_1" class="com.zaxxer.hikari.HikariDataSource"
destroy-method="close">
+ <bean id="ds_0_replica_0" class="com.zaxxer.hikari.HikariDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
- <property name="jdbcUrl"
value="jdbc:mysql://localhost:3307/demo_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
+ <property name="jdbcUrl"
value="jdbc:mysql://172.72.0.16:3306/ds_0_replica_0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
- <property name="password" value="123456"/>
+ <property name="password" value="root"/>
</bean>
-<#if feature=="sharding">
- <#include "shardingApplication.ftl">
-<#elseif feature=="readwrite-splitting">
- <#include "readwriteSplittingApplication.ftl">
-<#elseif feature=="encrypt">
- <#include "encryptApplication.ftl">
-<#elseif feature=="shadow">
- <#include "shadowApplication.ftl">
-</#if>
-<#if framework=="spring-namespace-mybatis">
- <bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>
- <tx:annotation-driven />
-
- <bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource"/>
- <property name="mapperLocations" value="classpath*:mappers/*.xml"/>
- </bean>
-
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage"
value="org.apache.shardingsphere.example.${feature?replace('-',
'.')}.${framework?replace('-', '.')}.repository"/>
- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
+ <bean id="ds_0_replica_1" class="com.zaxxer.hikari.HikariDataSource"
destroy-method="close">
+ <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
+ <property name="jdbcUrl"
value="jdbc:mysql://172.72.0.17:3306/ds_0_replica_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8"/>
+ <property name="username" value="root"/>
+ <property name="password" value="root"/>
</bean>
-</#if>
+
+ <database-discovery:rule id="databaseDiscoveryRule">
+ <database-discovery:data-source-rule id="db"
data-source-names="ds_0,ds_0_replica_1,ds_0_replica_1"
discovery-heartbeat-name="mgr-heartbeat"
+ discovery-type-name="mgr"/>
+ <database-discovery:discovery-heartbeat id="mgr-heartbeat">
+ <props>
+ <prop key="keep-alive-cron">0/5 * * * * ?</prop>
+ </props>
+ </database-discovery:discovery-heartbeat>
+ </database-discovery:rule>
+
+ <database-discovery:discovery-type id="mgr" type="MGR">
+ <props>
+ <prop key="keep-alive-cron">0/5 * * * * ?</prop>
+ <prop key="group-name">aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa</prop>
+ </props>
+ </database-discovery:discovery-type>
+
+ <shardingsphere:data-source id="dataSource"
data-source-names="ds_0,ds_0_replica_1,ds_0_replica_1"
rule-refs="databaseDiscoveryRule" />
+
</beans>
diff --git
a/examples/shardingsphere-sample/shardingsphere-jdbc-sample/shardingsphere-jdbc-memory-example/shardingsphere-jdbc-memory-local-example/shardingsphere-jdbc-memory-local-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-namespace-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-db-discovery-exa
[...]
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-db-discovery-example/shardingsphere-jdbc-memory-local-db-discovery-spring-namespace-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>