sharding-jdbc如何配置分析Configuration

发布时间:2021-10-20 16:51:28 作者:柒染
来源:亿速云 阅读:166

sharding-jdbc如何配置分析Configuration,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

Sharding核心配置主要如下(官网):

接下来对各个核心配置进行分析:

  1. 以多主多从读写分离、表分片为例

    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;
        }
    }


  2. 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;
    }


     

  3. 分析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);
        }
    }


     

  4. 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());
        }


     

  5. 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;
        }


     

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. Cisco mds 9700 configuration 基本配置
  2. Mybatis源码分析[05.Configuration]

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

sharding-jdbc configuration

上一篇:是什么阻碍了Linux桌面的发展

下一篇:怎么进行echart Coordinate内容解码

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》