sharding-jdbc如何配置分析Configuration,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
撫順縣網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營(yíng)維護(hù)。成都創(chuàng)新互聯(lián)公司于2013年開始到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)公司。
Sharding核心配置主要如下(官網(wǎng)):
分片規(guī)則
分片規(guī)則配置的總?cè)肟?。包含?shù)據(jù)源配置、表配置、綁定表配置以及讀寫分離配置等
數(shù)據(jù)源配置
真實(shí)數(shù)據(jù)源列表
表配置
邏輯表名稱、數(shù)據(jù)節(jié)點(diǎn)與分表規(guī)則的配置
數(shù)據(jù)節(jié)點(diǎn)配置
用于配置邏輯表與真實(shí)表的映射關(guān)系??煞譃榫鶆蚍植己妥远x分布兩種形式
分片策略配置
對(duì)于分片策略存有數(shù)據(jù)源分片策略和表分片策略兩種維度
數(shù)據(jù)源分片策略:
對(duì)應(yīng)于DatabaseShardingStrategy。用于配置數(shù)據(jù)被分配的目標(biāo)數(shù)據(jù)源
表分片策略
對(duì)應(yīng)于TableShardingStrategy。用于配置數(shù)據(jù)被分配的目標(biāo)表,該目標(biāo)表存在與該數(shù)據(jù)的目標(biāo)數(shù)據(jù)源內(nèi)。故表分片策略是依賴與數(shù)據(jù)源分片策略的結(jié)果的
自增主鍵生成策略
通過在客戶端生成自增主鍵替換以數(shù)據(jù)庫原生自增主鍵的方式,做到分布式主鍵無重復(fù)。
接下來對(duì)各個(gè)核心配置進(jìn)行分析:
以多主多從讀寫分離、表分片為例
public final class ShardingMasterSlaveConfigurationPrecise implements ExampleConfiguration { @Override public DataSource getDataSource() throws SQLException { ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration(); //分片表規(guī)則配置 shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration()); shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration()); //綁定分片表,主要用來路由 shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item"); //設(shè)置默認(rèn)數(shù)據(jù)源分片策略 shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new StandardShardingStrategyConfiguration("user_id", new PreciseModuloShardingDatabaseAlgorithm())); //設(shè)置默認(rèn)表分片策略 shardingRuleConfig.setDefaultTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("order_id", new PreciseModuloShardingTableAlgorithm())); //主從配置,支持多主多從 shardingRuleConfig.setMasterSlaveRuleConfigs(getMasterSlaveRuleConfigurations()); //創(chuàng)建ShardingDataSource數(shù)據(jù)源 return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, new Properties()); } private static TableRuleConfiguration getOrderTableRuleConfiguration() { //分片表配置 TableRuleConfiguration result = new TableRuleConfiguration(/*邏輯表*/"t_order", /*數(shù)據(jù)源名.真實(shí)表*/"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分片規(guī)則核心配置
@Getter @Setter public final class ShardingRuleConfiguration implements RuleConfiguration { //表規(guī)則配置 private Collection<TableRuleConfiguration> tableRuleConfigs = new LinkedList<>(); //綁定表配置 private Collection<String> bindingTableGroups = new LinkedList<>(); //廣播表配置 private Collection<String> broadcastTables = new LinkedList<>(); //默認(rèn)數(shù)據(jù)源名稱 private String defaultDataSourceName; //默認(rèn)分庫策略 private ShardingStrategyConfiguration defaultDatabaseShardingStrategyConfig; //默認(rèn)分片策略 private ShardingStrategyConfiguration defaultTableShardingStrategyConfig; //默認(rèn)主鍵生成工具類 private KeyGeneratorConfiguration defaultKeyGeneratorConfig; //主從規(guī)則配置 private Collection<MasterSlaveRuleConfiguration> masterSlaveRuleConfigs = new LinkedList<>(); //數(shù)據(jù)脫敏規(guī)則配置 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 { //創(chuàng)建ShardingDataSource數(shù)據(jù)源,同時(shí)創(chuàng)建sharding rule配置 return new ShardingDataSource(dataSourceMap, new ShardingRule(shardingRuleConfig, dataSourceMap.keySet()), props); } }
ShardingRule分片規(guī)則
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數(shù)據(jù)源名稱 shardingDataSourceNames = new ShardingDataSourceNames(shardingRuleConfig, dataSourceNames); //創(chuàng)建表規(guī)則集合 tableRules = createTableRules(shardingRuleConfig); //創(chuàng)建分組綁定表 //1.從tableRules查找 //2.是否廣播表,如果是廣播表,dataSourceNames.size() == 1 ? dataSourceNames.iterator().next() : shardingRuleConfig.getDefaultDataSourceName(); //3.已上兩種情況都不滿足,則使用默認(rèn)數(shù)據(jù)源創(chuàng)建tableRule bindingTableRules = createBindingTableRules(shardingRuleConfig.getBindingTableGroups()); //廣播表 broadcastTables = shardingRuleConfig.getBroadcastTables(); //默認(rèn)分庫策略 defaultDatabaseShardingStrategy = createDefaultShardingStrategy(shardingRuleConfig.getDefaultDatabaseShardingStrategyConfig()); //默認(rèn)表分片策略 defaultTableShardingStrategy = createDefaultShardingStrategy(shardingRuleConfig.getDefaultTableShardingStrategyConfig()); //默認(rèn)主鍵生成工具類 defaultShardingKeyGenerator = createDefaultKeyGenerator(shardingRuleConfig.getDefaultKeyGeneratorConfig()); //創(chuàng)建主從規(guī)則 masterSlaveRules = createMasterSlaveRules(shardingRuleConfig.getMasterSlaveRuleConfigs()); //數(shù)據(jù)脫敏規(guī)則 encryptRule = createEncryptRule(shardingRuleConfig.getEncryptRuleConfig()); }
TableRule表規(guī)則
public TableRule(final TableRuleConfiguration tableRuleConfig, final ShardingDataSourceNames shardingDataSourceNames, final String defaultGenerateKeyColumn) { //獲取邏輯表 logicTable = tableRuleConfig.getLogicTable().toLowerCase(); //inline表達(dá)式解析出真實(shí)表,比如: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); //真實(shí)表數(shù)據(jù)節(jié)點(diǎn) //1.如果沒有配置真實(shí)表,則根據(jù)邏輯表、數(shù)據(jù)源來生成對(duì)應(yīng)真實(shí)表數(shù)據(jù)節(jié)點(diǎn) //2.否則根據(jù)inline表達(dá)式解析出來的真實(shí)表來生成真實(shí)數(shù)據(jù)節(jié)點(diǎn) actualDataNodes = isEmptyDataNodes(dataNodes) ? generateDataNodes(tableRuleConfig.getLogicTable(), shardingDataSourceNames.getDataSourceNames()) : generateDataNodes(dataNodes, shardingDataSourceNames.getDataSourceNames()); //真實(shí)表 actualTables = getActualTables(); //數(shù)據(jù)源->分片策略 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; }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
網(wǎng)頁標(biāo)題:sharding-jdbc如何配置分析Configuration
文章路徑:http://jinyejixie.com/article28/gcecjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App設(shè)計(jì)、搜索引擎優(yōu)化、全網(wǎng)營(yíng)銷推廣、網(wǎng)站設(shè)計(jì)、動(dòng)態(tài)網(wǎng)站、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)