StowayNet.Extensions.Snowflake
StowayNet.Extensions.Snowflake是针对雪花算法开发的.net core框架扩展组件。
入门
NuGet
你可以运行以下命令在你的项目中安装 StowayNet.Extensions.Snowflake
.
PM> Install-Package StowayNet.Extensions.Snowflake
配置
首先配置 StowayNet.Extensions.Snowflake
到 Startup.cs
文件中,如下:
......
using StowayNet;
......
public void ConfigureServices(IServiceCollection services)
{
......
services.AddStowayNet();
......
}
你也可以通过配置选项 StowayNet.Extensions.Snowflake.SnowflakeIdOptions
来修改默认配置:
public void ConfigureServices(IServiceCollection services)
{
......
services.AddStowayNet();
services.Configure<StowayNet.Extensions.Snowflake.SnowflakeIdOptions>(options =>
{
options.ServerId = serverId;
});
......
}
示例
using StowayNet.Extensions.Snowflake;
class TestSample
{
private readonly ILogger<TestSample> _logger;
private readonly ISnowflakeIdWorker _idWorker;
public TestSample(ILogger<TestSample> logger, ISnowflakeIdWorker idWorker)
{
_logger = logger;
_idWorker = idWorker;
}
public void Execute()
{
_logger.LogDebug($"newid:{_idWorker.NewId()}");
}
}