forked from anjoy8/Blog.Core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1.统一使用 Yitter.IdGenerator 生成雪花Id、 2.使用 SnowflakeId.AutoRegister 自动注册WorkerId 静态使用:IdGeneratorUtility.NextId()
- Loading branch information
1 parent
0d5fb83
commit 2d73502
Showing
10 changed files
with
119 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using Blog.Core.Common.DB; | ||
using Blog.Core.Common.Option; | ||
using Microsoft.Extensions.Hosting; | ||
using Serilog; | ||
using SnowflakeId.AutoRegister.Builder; | ||
using SnowflakeId.AutoRegister.Interfaces; | ||
using SqlSugar; | ||
using Yitter.IdGenerator; | ||
|
||
namespace Blog.Core.Common.Utlilty; | ||
|
||
public class IdGeneratorUtility | ||
{ | ||
private static readonly Lazy<IAutoRegister> AutoRegister = new(() => | ||
{ | ||
var builder = new AutoRegisterBuilder() | ||
// Register Option | ||
// Use the following line to set the identifier. | ||
// Recommended setting to distinguish multiple applications on a single machine | ||
.SetExtraIdentifier(App.Configuration["urls"] ?? string.Empty) | ||
// Use the following line to set the WorkerId scope. | ||
.SetWorkerIdScope(1, 30) | ||
// Use the following line to set the register option. | ||
// .SetRegisterOption(option => {}) | ||
; | ||
var redisOptions = App.GetOptions<RedisOptions>(); | ||
if (redisOptions.Enable) | ||
// Use the following line to use the Redis store. | ||
builder.UseRedisStore(redisOptions.ConnectionString); | ||
else if (BaseDBConfig.LogConfig != null && BaseDBConfig.LogConfig.DbType == DbType.SqlServer) | ||
// Use the following line to use the SQL Server store. | ||
builder.UseSqlServerStore(BaseDBConfig.LogConfig.ConnectionString); | ||
else | ||
// Use the following line to use the default store. | ||
// Only suitable for standalone use, local testing, etc. | ||
builder.UseDefaultStore(); | ||
|
||
App.GetService<IHostApplicationLifetime>(false).ApplicationStopping.Register(UnRegister); | ||
return builder.Build(); | ||
}); | ||
|
||
private static readonly Lazy<IIdGenerator> _idGenInstance = new(() => | ||
{ | ||
var config = AutoRegister.Value.Register(); | ||
|
||
//WorkerId DataCenterId 取值 1-31 | ||
var options = new IdGeneratorOptions | ||
{ | ||
WorkerId = (ushort)config.WorkerId, | ||
}; | ||
IIdGenerator idGenInstance = new DefaultIdGenerator(options); | ||
return idGenInstance; | ||
}); | ||
|
||
private static IIdGenerator IdGenInstance => _idGenInstance.Value; | ||
|
||
public static long NextId() | ||
{ | ||
return IdGenInstance.NewLong(); | ||
} | ||
|
||
public static void UnRegister() | ||
{ | ||
if (!AutoRegister.IsValueCreated) return; | ||
|
||
AutoRegister.Value.UnRegister(); | ||
Log.Information("Snowflake Id Unregistered"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters