您好,登錄后才能下訂單哦!
sharding-jdbc如何配置分析Configuration,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
Sharding核心配置主要如下(官網):
分片規則
分片規則配置的總入口。包含數據源配置、表配置、綁定表配置以及讀寫分離配置等
數據源配置
真實數據源列表
表配置
邏輯表名稱、數據節點與分表規則的配置
數據節點配置
用于配置邏輯表與真實表的映射關系。可分為均勻分布和自定義分布兩種形式
分片策略配置
對于分片策略存有數據源分片策略和表分片策略兩種維度
數據源分片策略:
對應于DatabaseShardingStrategy。用于配置數據被分配的目標數據源
表分片策略
對應于TableShardingStrategy。用于配置數據被分配的目標表,該目標表存在與該數據的目標數據源內。故表分片策略是依賴與數據源分片策略的結果的
自增主鍵生成策略
通過在客戶端生成自增主鍵替換以數據庫原生自增主鍵的方式,做到分布式主鍵無重復。
接下來對各個核心配置進行分析:
以多主多從讀寫分離、表分片為例
public final class ShardingMasterSlaveConfigurationPrecise implements ExampleConfiguration { @Override public DataSource getDataSource() throws SQLException { ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); //分片表規則配置 shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration()); shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration()); //綁定分片表,主要用來路由 shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item"); //設置默認數據源分片策略 shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new StandardShardingStrategyConfiguration("user_id", new PreciseModuloShardingDatabaseAlgorithm())); //設置默認表分片策略 shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new PreciseModuloShardingTableAlgorithm())); //主從配置,支持多主多從 shardingRuleConfig.setMasterSlaveRuleConfigs(getMasterSlaveRuleConfigurations()); //創建ShardingDataSource數據源 return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, new Properties()); } private static TableRuleConfiguration getOrderTableRuleConfiguration() { //分片表配置 TableRuleConfiguration result = new TableRuleConfiguration(/*邏輯表*/"t_order", /*數據源名.真實表*/"ds_${0..1}.t_order_${[0, 1]}"); //自定義主鍵生成配置 result.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "order_id", getProperties())); return result; } private static TableRuleConfiguration getOrderItemTableRuleConfiguration() { TableRuleConfiguration result = new TableRuleConfiguration("t_order_item", "ds_${0..1}.t_order_item_${[0, 1]}"); result.setKeyGeneratorConfig(new KeyGeneratorConfiguration("SNOWFLAKE", "order_item_id", getProperties())); return result; } private static List<MasterSlaveRuleConfiguration> getMasterSlaveRuleConfigurations() { MasterSlaveRuleConfiguration masterSlaveRuleConfig1 = new MasterSlaveRuleConfiguration("ds_0", "demo_ds_master_0", Arrays.asList("demo_ds_master_0_slave_0", "demo_ds_master_0_slave_1")); MasterSlaveRuleConfiguration masterSlaveRuleConfig2 = new MasterSlaveRuleConfiguration("ds_1", "demo_ds_master_1", Arrays.asList("demo_ds_master_1_slave_0", "demo_ds_master_1_slave_1")); return Lists.newArrayList(masterSlaveRuleConfig1, masterSlaveRuleConfig2); } private static Map<String, DataSource> createDataSourceMap() { final Map<String, DataSource> result = new HashMap<>(); result.put("demo_ds_master_0", DataSourceUtil.createDataSource("demo_ds_master_0")); result.put("demo_ds_master_0_slave_0", DataSourceUtil.createDataSource("demo_ds_master_0_slave_0")); result.put("demo_ds_master_0_slave_1", DataSourceUtil.createDataSource("demo_ds_master_0_slave_1")); result.put("demo_ds_master_1", DataSourceUtil.createDataSource("demo_ds_master_1")); result.put("demo_ds_master_1_slave_0", DataSourceUtil.createDataSource("demo_ds_master_1_slave_0")); result.put("demo_ds_master_1_slave_1", DataSourceUtil.createDataSource("demo_ds_master_1_slave_1")); return result; } private static Properties getProperties() { Properties result = new Properties(); result.setProperty("worker.id", "123"); return result; } }
ShardingRuleConfiguration分片規則核心配置
@Getter @Setter public final class ShardingRuleConfiguration implements RuleConfiguration { //表規則配置 private Collection<TableRuleConfiguration> tableRuleConfigs = new LinkedList<>(); //綁定表配置 private Collection<String> bindingTableGroups = new LinkedList<>(); //廣播表配置 private Collection<String> broadcastTables = new LinkedList<>(); //默認數據源名稱 private String defaultDataSourceName; //默認分庫策略 private ShardingStrategyConfiguration defaultDatabaseShardingStrategyConfig; //默認分片策略 private ShardingStrategyConfiguration defaultTableShardingStrategyConfig; //默認主鍵生成工具類 private KeyGeneratorConfiguration defaultKeyGeneratorConfig; //主從規則配置 private Collection<MasterSlaveRuleConfiguration> masterSlaveRuleConfigs = new LinkedList<>(); //數據脫敏規則配置 private EncryptRuleConfiguration encryptRuleConfig; }
分析ShardingDataSourceFactory#createDataSource中的ShardingRule配置
/** * Sharding data source factory. * * @author zhangliang */ @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class ShardingDataSourceFactory { /** * Create sharding data source. * * @param dataSourceMap data source map * @param shardingRuleConfig rule configuration for databases and tables sharding * @param props properties for data source * @return sharding data source * @throws SQLException SQL exception */ public static DataSource createDataSource( final Map<String, DataSource> dataSourceMap, final ShardingRuleConfiguration shardingRuleConfig, final Properties props) throws SQLException { //創建ShardingDataSource數據源,同時創建sharding rule配置 return new ShardingDataSource(dataSourceMap, new ShardingRule(shardingRuleConfig, dataSourceMap.keySet()), props); } }
ShardingRule分片規則
public ShardingRule(final ShardingRuleConfiguration shardingRuleConfig, final Collection<String> dataSourceNames) { Preconditions.checkArgument(null != shardingRuleConfig, "ShardingRuleConfig cannot be null."); Preconditions.checkArgument(null != dataSourceNames && !dataSourceNames.isEmpty(), "Data sources cannot be empty."); this.shardingRuleConfig = shardingRuleConfig; //sharding數據源名稱 shardingDataSourceNames = new ShardingDataSourceNames(shardingRuleConfig, dataSourceNames); //創建表規則集合 tableRules = createTableRules(shardingRuleConfig); //創建分組綁定表 //1.從tableRules查找 //2.是否廣播表,如果是廣播表,dataSourceNames.size() == 1 ? dataSourceNames.iterator().next() : shardingRuleConfig.getDefaultDataSourceName(); //3.已上兩種情況都不滿足,則使用默認數據源創建tableRule bindingTableRules = createBindingTableRules(shardingRuleConfig.getBindingTableGroups()); //廣播表 broadcastTables = shardingRuleConfig.getBroadcastTables(); //默認分庫策略 defaultDatabaseShardingStrategy = createDefaultShardingStrategy(shardingRuleConfig.getDefaultDatabaseShardingStrategyConfig()); //默認表分片策略 defaultTableShardingStrategy = createDefaultShardingStrategy(shardingRuleConfig.getDefaultTableShardingStrategyConfig()); //默認主鍵生成工具類 defaultShardingKeyGenerator = createDefaultKeyGenerator(shardingRuleConfig.getDefaultKeyGeneratorConfig()); //創建主從規則 masterSlaveRules = createMasterSlaveRules(shardingRuleConfig.getMasterSlaveRuleConfigs()); //數據脫敏規則 encryptRule = createEncryptRule(shardingRuleConfig.getEncryptRuleConfig()); }
TableRule表規則
public TableRule(final TableRuleConfiguration tableRuleConfig, final ShardingDataSourceNames shardingDataSourceNames, final String defaultGenerateKeyColumn) { //獲取邏輯表 logicTable = tableRuleConfig.getLogicTable().toLowerCase(); //inline表達式解析出真實表,比如:ds_${0..1}.t_order_${[0, 1]} //解析出來為: //ds_0.t_order_0 //ds_0.t_order_1 //ds_1.t_order_0 //ds_1.t_order_1 List<String> dataNodes = new InlineExpressionParser(tableRuleConfig.getActualDataNodes()).splitAndEvaluate(); dataNodeIndexMap = new HashMap<>(dataNodes.size(), 1); //真實表數據節點 //1.如果沒有配置真實表,則根據邏輯表、數據源來生成對應真實表數據節點 //2.否則根據inline表達式解析出來的真實表來生成真實數據節點 actualDataNodes = isEmptyDataNodes(dataNodes) ? generateDataNodes(tableRuleConfig.getLogicTable(), shardingDataSourceNames.getDataSourceNames()) : generateDataNodes(dataNodes, shardingDataSourceNames.getDataSourceNames()); //真實表 actualTables = getActualTables(); //數據源->分片策略 databaseShardingStrategy = null == tableRuleConfig.getDatabaseShardingStrategyConfig() ? null : ShardingStrategyFactory.newInstance(tableRuleConfig.getDatabaseShardingStrategyConfig()); //表->分片策略 tableShardingStrategy = null == tableRuleConfig.getTableShardingStrategyConfig() ? null : ShardingStrategyFactory.newInstance(tableRuleConfig.getTableShardingStrategyConfig()); //主鍵字段 generateKeyColumn = getGenerateKeyColumn(tableRuleConfig.getKeyGeneratorConfig(), defaultGenerateKeyColumn); //生成主鍵工具類,比如SNOWFLAKE shardingKeyGenerator = containsKeyGeneratorConfiguration(tableRuleConfig) ? new ShardingKeyGeneratorServiceLoader().newService(tableRuleConfig.getKeyGeneratorConfig().getType(), tableRuleConfig.getKeyGeneratorConfig().getProperties()) : null; }
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。