美文网首页
sharding-jdbc笔记(2)-分片策略

sharding-jdbc笔记(2)-分片策略

作者: 兴浩 | 来源:发表于2018-05-13 20:20 被阅读55次

    参考:
    https://www.cnblogs.com/mr-yang-localhost/p/8313360.html

    分表示例

        private static TableRuleConfiguration getOrderTableRuleConfiguration() {
            TableRuleConfiguration orderTableRuleConfig = new TableRuleConfiguration();
            orderTableRuleConfig.setLogicTable("t_order");
            orderTableRuleConfig.setActualDataNodes("demo_ds.t_order_${[0, 1]}");
            orderTableRuleConfig.setKeyGeneratorColumnName("order_id");
            orderTableRuleConfig.setTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("user_id", ModuloShardingTableAlgorithm.class.getName()));
            return orderTableRuleConfig;
        }
        
        private static TableRuleConfiguration getOrderItemTableRuleConfiguration() {
            TableRuleConfiguration orderItemTableRuleConfig = new TableRuleConfiguration();
            orderItemTableRuleConfig.setLogicTable("t_order_item");
            orderItemTableRuleConfig.setActualDataNodes("demo_ds.t_order_item_${[0, 1]}");
            orderItemTableRuleConfig.setTableShardingStrategyConfig(new StandardShardingStrategyConfiguration("user_id", ModuloShardingTableAlgorithm.class.getName()));
            return orderItemTableRuleConfig;
        }
    

    以上分表策略数据库的存储为

    分库示例

        private static DataSource getShardingDataSource() throws SQLException {
            ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
            shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
            shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
            shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig
                    (new InlineShardingStrategyConfiguration("user_id", "demo_ds_${user_id % 2}"));
            return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, 
                    new HashMap<String, Object>(), new Properties());
        }
    

    InlineShardingStrategyConfiguration是通过表达式的方式来分库

    分库结果:

    分库分表示例

        private static DataSource getShardingDataSource() 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 InlineShardingStrategyConfiguration("user_id", "demo_ds_${user_id % 2}"));
            shardingRuleConfig.setDefaultTableShardingStrategyConfig(
    new StandardShardingStrategyConfiguration("user_id", ModuloShardingTableAlgorithm.class.getName()));
            return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, new HashMap<String, Object>(), new Properties());
        }
    

    分库分表结果:

    相关文章

      网友评论

          本文标题:sharding-jdbc笔记(2)-分片策略

          本文链接:https://www.haomeiwen.com/subject/lytrdftx.html