import org.apache.shardingsphere.infra.config.properties.ConfigurationProperties;
import org.apache.shardingsphere.infra.context.metadata.MetaDataContexts;
import org.apache.shardingsphere.infra.context.runtime.RuntimeContext;
import org.apache.shardingsphere.infra.database.DefaultSchema;
import org.apache.shardingsphere.infra.database.type.DatabaseType;
import org.apache.shardingsphere.infra.datanode.DataNode;
import org.apache.shardingsphere.infra.metadata.model.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.metadata.model.logic.LogicSchemaMetaData;
import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.metadata.MetaDataContextsBuilder;
import org.apache.shardingsphere.mode.repository.StandalonePersistRepository;
import org.apache.shardingsphere.readwrite.route.engine.ReadwriteSplittingDataSourceRouter;
import org.apache.shardingsphere.readwrite.rule.ReadwriteSplittingRule;
import org.apache.shardingsphere.readwrite.rule.builder.ReadwriteSplittingRuleBuilder;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class ReadWriteSplitExample {
public static void main(final String[] args) throws SQLException {
// 配置读写分离规则
ReadwriteSplittingRule readwriteSplittingRule = ReadwriteSplittingRuleBuilder.create()
.defaultDataSourceRule(
new DataSourceRule(new MockedDataSource(), new MockedDataSource()), "readwrite-ds")
.build();
// 构建ShardingSphereMetaData
ShardingSphereMetaData metaData = new ShardingSphereMetaData(
"ds",
new HashMap<>(),
new DataNode("ds"),
Collections.emptyList(),
new ReadwriteSplittingRule(readwriteSplittingRule, Collections.emptyMap()),
new ConfigurationProperties(new Properties())
);
// 构建逻辑SchemaMetaData
LogicSchemaMetaData logicSchemaMetaData = new LogicSchemaMetaData(
"logic_db",
Collections.singletonMap("readwrite-ds", metaData),
new ReadwriteSplittingRule(readwriteSplittingRu
评论已关闭