十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
sharding-jdbc如何配置分析Configuration,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。
抚顺县网站制作公司哪家好,找成都创新互联公司!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设等网站项目制作,到程序开发,运营维护。成都创新互联公司于2013年开始到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选成都创新互联公司。
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 ListgetMasterSlaveRuleConfigurations() { 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 createDataSourceMap() { final Map 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 CollectiontableRuleConfigs = new LinkedList<>(); //绑定表配置 private Collection bindingTableGroups = new LinkedList<>(); //广播表配置 private Collection broadcastTables = new LinkedList<>(); //默认数据源名称 private String defaultDataSourceName; //默认分库策略 private ShardingStrategyConfiguration defaultDatabaseShardingStrategyConfig; //默认分片策略 private ShardingStrategyConfiguration defaultTableShardingStrategyConfig; //默认主键生成工具类 private KeyGeneratorConfiguration defaultKeyGeneratorConfig; //主从规则配置 private Collection 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 MapdataSourceMap, 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 CollectiondataSourceNames) { 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 ListdataNodes = 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; }
看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。