7

一款针对EF Core轻量级分表分库、读写分离的开源项目 - chingho

 1 year ago
source link: https://www.cnblogs.com/chingho/p/17106687.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

在项目开发中,如果数据量比较大,比如日志记录,我们往往会采用分表分库的方案;为了提升性能,把数据库查询与更新操作分开,这时候就要采用读写分离的方案。

分表分库通常包含垂直分库、垂直分表、水平分库和水平分表方案,包括分表分库方案,实现每一个方案都需要花费一定的时间;所以,今天给大家推荐一个基于EF Code实现的分表分库、读写分离开源库,可以让我们零成本的接入。

项目简介

这是一个针对EF Code高性能、轻量级分表分库、读写分离开源项目,可以让我们方便快速接入、或者改造原有项目,几乎零成本接入。

项目特性

1、分表:时间分表、自定义分表、多表查询更新删除。

2、分库:支持自定义分库、分库查询更新删除。

3、分表分库:支持部分表格分表、部分表格分库。

4、读写分离:支持一主多从的读写分离的方案。

5、其他:支持动态分表、分库,支持高性能查询,支持事务等。

技术架构

1、跨平台:基于.NetCore开发,支持Windows、Mono、Liunx、Windows Azure。

2、支持 Standard 2.0、.NetCore 2.0+。

3、数据库:支持MySql、Oracle、SqlServer。

项目结构

图片

该项目包含不同数据库的例子,src为不同版本的源码。

快速上手

1、表的定义

public class SysUserMod:IAge
{
/// <summary>
/// 用户Id用于分表
/// </summary>
public string Id { get; set; }
/// <summary>
/// 用户名称
/// </summary>
public string Name { get; set; }
/// <summary>
/// 用户姓名
/// </summary>
public int Age { get; set; }
}

2、分表规则

public class SysUserModVirtualTableRoute : AbstractSimpleShardingModKeyStringVirtualTableRoute<SysUserMod>
{
    //根据ID取模3,分为2张表
public SysUserModVirtualTableRoute() : base(2,3)
    {
    }

public override void Configure(EntityMetadataTableBuilder<SysUserMod> builder)
    {
        builder.ShardingProperty(o => o.Id);
    }

}

3、Startup配置

public void ConfigureServices(IServiceCollection services)
{
    ......
    services.AddShardingDbContext<DefaultShardingDbContext>()
        .UseRouteConfig(o =>
        {
//定义分表路由
            o.AddShardingDataSourceRoute<SysUserModVirtualDataSourceRoute>();
        }).UseConfig((sp,o) =>
        {
            ......
//指定主从数据库
            o.AddDefaultDataSource("ds0",
"server=127.0.0.1;port=3306;database=db1;userid=root;password=root;");
            o.AddExtraDataSource(sp => new Dictionary<string, string>()
            {
                { "ds1", "server=127.0.0.1;port=3306;database=db2;userid=root;password=root;" },
                { "ds2", "server=127.0.0.1;port=3306;database=db3;userid=root;password=root;" }
            });
            o.UseShardingMigrationConfigure(b =>
            {
                b.ReplaceService<IMigrationsSqlGenerator, ShardingMySqlMigrationsSqlGenerator>();
            });
        }).ReplaceService<IModelCacheLockerProvider,DicModelCacheLockerProvider>()
        .AddShardingCore();
}

项目地址

Github:

https://github.com/dotnetcore/sharding-core

Gitee:

https://gitee.com/xuejm/sharding-core

更多开源项目请查看一个专注推荐优秀.Net开源项目的榜单

- End -

文章首发于公众号【编程乐趣】,欢迎大家关注。

图片

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK