Skip to content

Commit

Permalink
For apache#12986, Migrate the sharding-readwrite-splitting example. (a…
Browse files Browse the repository at this point in the history
  • Loading branch information
yx9o authored Oct 13, 2021
1 parent c6ec965 commit c4b6b04
Show file tree
Hide file tree
Showing 22 changed files with 403 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,17 @@
<version>5.0.0-RC1-SNAPSHOT</version>
</parent>
<artifactId>sharding-readwrite-splitting-raw-jdbc-example</artifactId>
<packaging>pom</packaging>
<name>${project.artifactId}</name>

<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>
Original file line number Diff line number Diff line change
@@ -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.sharding.readwrite.splitting.raw;

import org.apache.shardingsphere.example.core.api.ExampleExecuteTemplate;
import org.apache.shardingsphere.example.core.api.service.ExampleService;
import org.apache.shardingsphere.example.core.jdbc.service.OrderServiceImpl;
import org.apache.shardingsphere.example.sharding.readwrite.splitting.raw.factory.DataSourceFactory;
import org.apache.shardingsphere.example.type.ShardingType;

import javax.sql.DataSource;
import java.sql.SQLException;

/*
* Please make sure primary replica data replication sync on MySQL is running correctly. Otherwise this example will query empty data from replica.
*/
public final class ShardingReadwriteSplittingRawJavaConfigurationExample {

private static ShardingType shardingType = ShardingType.SHARDING_READWRITE_SPLITTING;

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 OrderServiceImpl(dataSource);
}
}
Original file line number Diff line number Diff line change
@@ -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.
*/

package org.apache.shardingsphere.example.sharding.readwrite.splitting.raw;

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.AddressRepositoryImpl;
import org.apache.shardingsphere.example.core.jdbc.repository.OrderItemRepositoryImpl;
import org.apache.shardingsphere.example.core.jdbc.repository.RangeOrderRepositoryImpl;
import org.apache.shardingsphere.example.core.jdbc.service.OrderServiceImpl;
import org.apache.shardingsphere.example.sharding.readwrite.splitting.raw.factory.RangeDataSourceFactory;
import org.apache.shardingsphere.example.type.ShardingType;

import javax.sql.DataSource;
import java.sql.SQLException;

/*
* Please make sure primary replica data replication sync on MySQL is running correctly. Otherwise this example will query empty data from replica.
*/
public final class ShardingReadwriteSplittingRawJavaRangeConfigurationExample {

private static ShardingType shardingType = ShardingType.SHARDING_READWRITE_SPLITTING;

public static void main(final String[] args) throws SQLException {
DataSource dataSource = RangeDataSourceFactory.newInstance(shardingType);
ExampleExecuteTemplate.run(getExampleService(dataSource));
}

private static ExampleService getExampleService(final DataSource dataSource) {
return new OrderServiceImpl(new RangeOrderRepositoryImpl(dataSource), new OrderItemRepositoryImpl(dataSource), new AddressRepositoryImpl(dataSource));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.sharding.readwrite.splitting.raw;

import org.apache.shardingsphere.example.core.api.ExampleExecuteTemplate;
import org.apache.shardingsphere.example.core.jdbc.service.AccountServiceImpl;
import org.apache.shardingsphere.example.core.jdbc.service.OrderServiceImpl;
import org.apache.shardingsphere.example.sharding.readwrite.splitting.raw.factory.YamlDataSourceFactory;
import org.apache.shardingsphere.example.type.ShardingType;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.SQLException;

/*
* Please make sure primary replica data replication sync on MySQL is running correctly. Otherwise this example will query empty data from replica.
*/
public final class ShardingReadwriteSplittingRawYamlConfigurationExample {

private static ShardingType shardingType = ShardingType.SHARDING_READWRITE_SPLITTING;

public static void main(final String[] args) throws SQLException, IOException {
DataSource dataSource = YamlDataSourceFactory.newInstance(shardingType);
ExampleExecuteTemplate.run(new OrderServiceImpl(dataSource));
ExampleExecuteTemplate.run(new AccountServiceImpl(dataSource));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.sharding.readwrite.splitting.raw;

import org.apache.shardingsphere.example.core.api.ExampleExecuteTemplate;
import org.apache.shardingsphere.example.core.jdbc.service.AccountServiceImpl;
import org.apache.shardingsphere.example.core.jdbc.service.OrderServiceImpl;
import org.apache.shardingsphere.example.sharding.readwrite.splitting.raw.factory.YamlRangeDataSourceFactory;
import org.apache.shardingsphere.example.type.ShardingType;

import javax.sql.DataSource;
import java.io.IOException;
import java.sql.SQLException;

/*
* Please make sure primary replica data replication sync on MySQL is running correctly. Otherwise this example will query empty data from replica.
*/
public final class ShardingReadwriteSplittingRawYamlRangeConfigurationExample {

private static ShardingType shardingType = ShardingType.SHARDING_READWRITE_SPLITTING;

public static void main(final String[] args) throws SQLException, IOException {
DataSource dataSource = YamlRangeDataSourceFactory.newInstance(shardingType);
ExampleExecuteTemplate.run(new OrderServiceImpl(dataSource));
ExampleExecuteTemplate.run(new AccountServiceImpl(dataSource));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.example.sharding.raw.jdbc.config;
package org.apache.shardingsphere.example.sharding.readwrite.splitting.raw.config;

import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
import org.apache.shardingsphere.example.config.ExampleConfiguration;
Expand All @@ -40,7 +40,8 @@ public final class ShardingReadwriteSplittingConfigurationPrecise implements Exa

@Override
public DataSource getDataSource() throws SQLException {
return ShardingSphereDataSourceFactory.createDataSource(createDataSourceMap(), Arrays.asList(createShardingRuleConfiguration(), createReadwriteSplittingConfiguration()), new Properties());
return ShardingSphereDataSourceFactory.createDataSource(createDataSourceMap(),
Arrays.asList(createShardingRuleConfiguration(), createReadwriteSplittingConfiguration()), new Properties());
}

private static Map<String, DataSource> createDataSourceMap() {
Expand All @@ -62,8 +63,8 @@ private ShardingRuleConfiguration createShardingRuleConfiguration() {
result.getBroadcastTables().add("t_address");
result.setDefaultDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "standard_test_db"));
result.setDefaultTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "standard_test_tbl"));
result.getShardingAlgorithms() .put("standard_test_db", new ShardingSphereAlgorithmConfiguration("STANDARD_TEST_DB", new Properties()));
result.getShardingAlgorithms() .put("standard_test_tbl", new ShardingSphereAlgorithmConfiguration("STANDARD_TEST_TBL", new Properties()));
result.getShardingAlgorithms().put("standard_test_db", new ShardingSphereAlgorithmConfiguration("STANDARD_TEST_DB", new Properties()));
result.getShardingAlgorithms().put("standard_test_tbl", new ShardingSphereAlgorithmConfiguration("STANDARD_TEST_TBL", new Properties()));
result.getKeyGenerators().put("snowflake", new ShardingSphereAlgorithmConfiguration("SNOWFLAKE", getProperties()));
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.example.sharding.raw.jdbc.config;
package org.apache.shardingsphere.example.sharding.readwrite.splitting.raw.config;

import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
import org.apache.shardingsphere.example.config.ExampleConfiguration;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.sharding.readwrite.splitting.raw.factory;

import org.apache.shardingsphere.example.sharding.readwrite.splitting.raw.config.ShardingReadwriteSplittingConfigurationPrecise;
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 SHARDING_READWRITE_SPLITTING:
return new ShardingReadwriteSplittingConfigurationPrecise().getDataSource();
default:
throw new UnsupportedOperationException(shardingType.name());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.sharding.readwrite.splitting.raw.factory;

import org.apache.shardingsphere.example.sharding.readwrite.splitting.raw.config.ShardingReadwriteSplittingConfigurationRange;
import org.apache.shardingsphere.example.type.ShardingType;

import javax.sql.DataSource;
import java.sql.SQLException;

public final class RangeDataSourceFactory {

public static DataSource newInstance(final ShardingType shardingType) throws SQLException {
switch (shardingType) {
case SHARDING_READWRITE_SPLITTING:
return new ShardingReadwriteSplittingConfigurationRange().getDataSource();
default:
throw new UnsupportedOperationException(shardingType.name());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.sharding.readwrite.splitting.raw.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 SHARDING_READWRITE_SPLITTING:
return YamlShardingSphereDataSourceFactory.createDataSource(getFile("/META-INF/sharding-readwrite-splitting.yaml"));
default:
throw new UnsupportedOperationException(shardingType.name());
}
}

private static File getFile(final String fileName) {
return new File(YamlDataSourceFactory.class.getResource(fileName).getFile());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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.sharding.readwrite.splitting.raw.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 YamlRangeDataSourceFactory {

public static DataSource newInstance(final ShardingType shardingType) throws SQLException, IOException {
switch (shardingType) {
case SHARDING_READWRITE_SPLITTING:
return YamlShardingSphereDataSourceFactory.createDataSource(getFile("/META-INF/sharding-readwrite-splitting-range.yaml"));
default:
throw new UnsupportedOperationException(shardingType.name());
}
}

private static File getFile(final String fileName) {
return new File(YamlRangeDataSourceFactory.class.getResource(fileName).getFile());
}
}
Loading

0 comments on commit c4b6b04

Please sign in to comment.