Biwen.Microsoft.Extensions.ServiceDiscovery
1.0.2
dotnet add package Biwen.Microsoft.Extensions.ServiceDiscovery --version 1.0.2
NuGet\Install-Package Biwen.Microsoft.Extensions.ServiceDiscovery -Version 1.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Biwen.Microsoft.Extensions.ServiceDiscovery" Version="1.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Biwen.Microsoft.Extensions.ServiceDiscovery --version 1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Biwen.Microsoft.Extensions.ServiceDiscovery, 1.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Biwen.Microsoft.Extensions.ServiceDiscovery as a Cake Addin #addin nuget:?package=Biwen.Microsoft.Extensions.ServiceDiscovery&version=1.0.2 // Install Biwen.Microsoft.Extensions.ServiceDiscovery as a Cake Tool #tool nuget:?package=Biwen.Microsoft.Extensions.ServiceDiscovery&version=1.0.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Microsoft.Extensions.ServiceDiscovery集成Consul
使用方式
1.安装Nuget包
dotnet add package Biwen.Microsoft.Extensions.ServiceDiscovery.Consul
2 Enjoy
builder.Services.AddServiceDiscovery().AddConsulServiceEndpointProvider();
using Consul.AspNetCore;
using Microsoft.AspNetCore.Antiforgery;
using Microsoft.Extensions.ServiceDiscovery.Abstractions;
using System.Text.Json.Serialization;
var builder = WebApplication.CreateSlimBuilder(args);
//健康监测
builder.Services.AddHealthChecks().AddCheck("default", () =>
{
return new Microsoft.Extensions.Diagnostics.HealthChecks.HealthCheckResult(
Microsoft.Extensions.Diagnostics.HealthChecks.HealthStatus.Healthy, "Healthy");
});
//注册 Consul服务和发现
builder.Services.AddConsul().AddConsulServiceRegistration(cfg =>
{
cfg.Meta = new Dictionary<string, string>() { { "Weight", "1" } };
cfg.ID = "SVC1";
cfg.Port = 5124;
cfg.Name = "todo";
cfg.Address = "http://127.0.0.1";
cfg.Tags = ["MicroSvc"];
cfg.Check = new Consul.AgentServiceCheck
{
DeregisterCriticalServiceAfter = TimeSpan.FromSeconds(15),//服务启动多久后注册
Interval = TimeSpan.FromSeconds(15),//健康检查时间间隔,或者称为心跳间隔
HTTP = "http://127.0.0.1:5124/health",//健康检查地址
Timeout = TimeSpan.FromSeconds(5),
Method = "GET",
};
});
// 使用Microsoft.Extensions.ServiceDiscovery实现负载均衡 & Consul
builder.Services.AddServiceDiscovery().AddConsulServiceEndpointProvider();
builder.Services.ConfigureHttpClientDefaults(static http =>
{
http.UseServiceDiscovery();
});
//使用IHttpClientFactory
builder.Services.AddHttpClient("todo", cfg =>
{
cfg.BaseAddress = new("http://todo");
});
builder.Services.ConfigureHttpJsonOptions(options =>
{
options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
});
var app = builder.Build();
// Consul健康监测
app.UseHealthChecks("/health");
#region apis
var sampleTodos = new Todo[] {
new(1, "Walk the dog"),
new(2, "Do the dishes", DateOnly.FromDateTime(DateTime.Now)),
new(3, "Do the laundry", DateOnly.FromDateTime(DateTime.Now.AddDays(1))),
new(4, "Clean the bathroom"),
new(5, "Clean the car", DateOnly.FromDateTime(DateTime.Now.AddDays(2)))
};
var todosApi = app.MapGroup("/todos");
todosApi.MapGet("/", () => sampleTodos);
todosApi.MapGet("/{id}", (int id) =>
sampleTodos.FirstOrDefault(a => a.Id == id) is { } todo
? Results.Ok(todo)
: Results.NotFound());
#endregion
#region 测试服务发现和负载
app.MapGet("/test", async (IHttpClientFactory clientFactory) =>
{
var client = clientFactory.CreateClient("todo");
var response = await client.GetAsync("/todos");
var todos = await response.Content.ReadAsStringAsync();
return Results.Content(todos, contentType: "application/json");
});
#endregion
app.Run();
public record Todo(int Id, string? Title, DateOnly? DueBy = null, bool IsComplete = false);
[JsonSerializable(typeof(Todo[]))]
internal partial class AppJsonSerializerContext : JsonSerializerContext
{
}
3 More
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- Consul.AspNetCore (>= 1.7.14.4)
- Microsoft.Extensions.ServiceDiscovery (>= 8.2.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.2 | 79 | 10/22/2024 |
1.0.1 | 118 | 7/19/2024 |
1.0.0 | 104 | 5/22/2024 |
1.0.0-pre6 | 56 | 4/29/2024 |
1.0.0-pre5 | 65 | 4/11/2024 |
1.0.0-pre4 | 84 | 3/22/2024 |
1.0.0-pre1 | 93 | 1/19/2024 |
Upgrade to Microsoft.Extensions.ServiceDiscovery 8.2.1