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.executor.kernel.ExecutorEngine;
import org.apache.shardingsphere.infra.metadata.ShardingSphereMetaData;
import org.apache.shardingsphere.infra.optimize.context.OptimizerContext;
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.spring.boot.util.BeanUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
@Configuration
public class ShardingSphereConfig {
@Bean
public DataSource dataSource() throws SQLException {
// 配置真实数据源
Map<String, DataSource> dataSourceMap = new HashMap<>();
// 配置第一个数据源
dataSourceMap.put("ds0", ...);
// 配置第二个数据源
dataSourceMap.put("ds1", ...);
// 配置分片规则
ShardingSphereRule rule = ...;
// 配置属性
Properties props = new Properties();
props.setProperty("query.with.cipher.column", "true");
ConfigurationProperties configurationProperties = new ConfigurationProperties(props);
// 构建MetaDataContexts
MetaDataContexts metaDataContexts = new MetaDataContextsBuilder(
dataSourceMap,
Collections.singletonMap("sharding_db", rule),
new HashMap<>(),
configurationProperties
).build();
// 获取执行引擎
ExecutorEngine executorEngine = ...;
// 创建ContextManager
ContextManager contextManager = new ContextManager(
metaDataContexts,
new TransactionContexts(...),
executorEngine,
...
);
//
评论已关闭