Yzl.Extensions.SpringBoot.Admin.Client.Net
0.1.10
dotnet add package Yzl.Extensions.SpringBoot.Admin.Client.Net --version 0.1.10
NuGet\Install-Package Yzl.Extensions.SpringBoot.Admin.Client.Net -Version 0.1.10
<PackageReference Include="Yzl.Extensions.SpringBoot.Admin.Client.Net" Version="0.1.10" />
<PackageVersion Include="Yzl.Extensions.SpringBoot.Admin.Client.Net" Version="0.1.10" />
<PackageReference Include="Yzl.Extensions.SpringBoot.Admin.Client.Net" />
paket add Yzl.Extensions.SpringBoot.Admin.Client.Net --version 0.1.10
#r "nuget: Yzl.Extensions.SpringBoot.Admin.Client.Net, 0.1.10"
#:package Yzl.Extensions.SpringBoot.Admin.Client.Net@0.1.10
#addin nuget:?package=Yzl.Extensions.SpringBoot.Admin.Client.Net&version=0.1.10
#tool nuget:?package=Yzl.Extensions.SpringBoot.Admin.Client.Net&version=0.1.10
Yzl.Extensions.SpringBoot.Admin.Client.Net 总体架构设计
安装:
dotnet add package Yzl.Extensions.SpringBoot.Admin.Client.Net(会自动带入Yzl.Extensions.Actuator)本文档同时作为 nuget.org 详情页的说明:第 5 节是可直接复制的配置与代码示例, 第 7 节是从旧版本升级的迁移说明。完整历史变更见源码仓库的
ReleaseNotes.md。
1. 设计目标
Yzl.Extensions.SpringBoot.Admin.Client.Net 是一个 仿照 Spring Boot Admin Client 的 .NET 8 应用注册与心跳组件,主要目标:
- 自动注册 .NET 应用到 Spring Boot Admin Server
- 自动采集 Actuator 暴露的端点信息
- 定时发送健康心跳与元数据
- 支持日志管理、指标上报和多节点管理
等价于 Java 世界里的:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
2. 总体架构图
┌────────────────────────────────────────────────────────────┐
│ Business Service (.NET 8) │
│ │
│ ┌──────────────────────────────────────────────────────┐ │
│ │ Yzl.Extensions.SpringBoot.Admin.Client.Net │ │
│ │ │ │
│ │ ┌────────────────────────────────────────────────┐ │ │
│ │ │ Registration Layer │ │ │
│ │ │ │ │ │
│ │ │ • SbaRegistrationService │ │ │
│ │ │ • Actuator Endpoints Metadata │ │ │
│ │ │ • HTTP Payload Construction │ │ │
│ │ └────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌────────────────────────────────────────────────┐ │ │
│ │ │ Heartbeat Layer │ │ │
│ │ │ │ │ │
│ │ │ • SbaHeartbeatService │ │ │
│ │ │ • Periodic Health & Status Reporting │ │ │
│ │ └────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌────────────────────────────────────────────────┐ │ │
│ │ │ HttpClient Factory │ │ │
│ │ │ │ │ │
│ │ │ • SbaHttpClientFactory │ │ │
│ │ │ • Basic Auth Support │ │ │
│ │ │ • HttpClient Caching / Reuse │ │ │
│ │ └────────────────────────────────────────────────┘ │ │
│ │ │ │
│ │ ┌────────────────────────────────────────────────┐ │ │
│ │ │ Core Services Layer │ │ │
│ │ │ │ │ │
│ │ │ • ActuatorUrlResolver │ │ │
│ │ │ • IOptions<SbaClientOptions> │ │ │
│ │ │ • Registration & Heartbeat Orchestration │ │ │
│ │ └────────────────────────────────────────────────┘ │ │
│ └──────────────────────────────────────────────────────┘ │
│ │
│ ⚠ Integrates with Yzl.Extensions.Actuator to fetch │
│ exposed endpoints automatically │
│ │
└────────────────────────────────────────────────────────────┘
│
│ HTTP / REST
▼
┌────────────────────────────────────────────────────────────┐
│ Spring Boot Admin Server │
│ │
│ • Node Registration │
│ • Health Monitoring │
│ • Metrics Visualization │
│ • Logger Level Management │
│ • Multi-node Management │
└────────────────────────────────────────────────────────────┘
3. 核心组件说明
层级 组件 功能
Configuration SbaClientOptions Java ClientProperties,前缀 spring:boot:admin:client
(连接 SBA Server:url / api-path / period / username / password / auto-*)
Configuration SbaInstanceOptions Java InstanceProperties,前缀 spring:boot:admin:client:instance
(注册成什么样:name / service-url / management-url / health-url / metadata)
Registration SbaRegistrationService 注册应用到 SBA Server,构建 metadata payload
Heartbeat SbaHeartbeatService 定时发送健康心跳,保持节点活跃
HttpClient SbaHttpClientFactory 创建 HttpClient,支持 Basic Auth,避免频繁创建
Core Services ActuatorUrlResolver 先用宿主绑定地址推导默认值,再按 instance.* 优先级覆盖
配置类与 Java 一一对应:
ClientProperties与InstanceProperties是两个独立类, .NET 侧同样是SbaClientOptions与SbaInstanceOptions两个独立类, 只是共用同一个配置根spring:boot:admin:client。
4. 方法调用流程
[Program.cs]
│
▼
AddSpringBootAdminClient() --> 注册 SBA Client 服务 + 绑定两个配置类
│
▼
SbaRegistrationService.Resolve() --> 构建 metadata payload
│
▼
SbaHttpClientFactory.Create() --> 获取 HttpClient (缓存)
│
▼
HTTP POST /instances --> SBA Server 注册应用
│
▼
SbaHeartbeatService.PeriodicTick() --> 健康心跳上报
5. 配置示例
{
"spring": {
"application": {
"name": "sba.c.c.test"
},
"boot": {
"admin": {
"client": {
"url": "http://10.1.24.141:16000",
"username": "admin",
"password": "1",
"period": "15s",
"instance": {
"name": "sba.c.c.test",
"metadata": {
"environment": "prod",
"user.name": "sba",
"user.password": "sba-secret"
}
}
}
}
}
},
"management": {
"server": {
"port": 27002
},
"endpoints": {
"web": {
"exposure": {
"basePath": "/actuator",
"include_": [ "health", "info", "metrics", "loggers" ],
"exclude_": [ "env" ]
}
}
},
"security": {
"enabled": true,
"type": "Basic",
"permit-endpoints": [ "health" ],
"basic": {
"users": [ { "name": "sba", "password": "{noop}sba-secret", "roles": [ "ACTUATOR" ] } ]
}
}
}
}
5.1 两个配置类的职责
| 配置节 | .NET 类 | Java 类 | 说明 |
|---|---|---|---|
spring.boot.admin.client |
SbaClientOptions |
ClientProperties |
连接 SBA Server:url、api-path、period/refreshInterval、username、password、auto-registration、auto-deregistration |
spring.boot.admin.client.instance |
SbaInstanceOptions |
InstanceProperties |
注册身份与地址:name、service-url、service-base-url、service-path、management-url、management-base-url、health-url、service-host-type、metadata |
配置键统一使用 Java 的 kebab-case 写法;旧版(0.1.x)的 PascalCase 键
(Url / RefreshInterval / AutoRegistration / AutoDeregistration / ServiceUrl 等)
仍然可用,但 service-url / management-url / health-url 三个 URL 项已迁移到
instance 节并标记 [Obsolete]。
5.2 URL 解析优先级(对齐 Java DefaultApplicationFactory)
| 输出 | 优先级(高 → 低) |
|---|---|
serviceUrl |
instance.service-url → 旧版 client.service-url → service-base-url + service-path |
managementUrl |
instance.management-url → 旧版 client.management-url → management-base-url + management.endpoints.web.basePath |
healthUrl |
instance.health-url → 旧版 client.health-url → management-base-url + basePath + /health |
name |
instance.name → spring.application.name → 入口程序集名 |
service-base-url / management-base-url 未配置时,由 Kestrel 实际绑定地址推导
(独立管理端口取 management.server.port)。注意 health-url 由 management-base-url
推导而不是 management-url——只覆盖 management-url 不会改变健康检查地址,这与 Java 一致。
5.3 metadata
instance.metadata 的合并顺序与 Java 相同:框架先写入自动生成的键
(management.context-path、startup、Actuator Basic 凭据等),再合并用户配置,
用户配置优先。带点的键(如 user.name)在 JSON 中直接写作 "user.name",
不要写成嵌套对象:
"metadata": { "user.name": "sba", "user.password": "sba-secret" }
开启 Actuator 鉴权后(
management:security:enabled=true):SBA Server 回访实例时也需要凭据。 本客户端会自动把user.name/user.password写进实例注册 metadata(仅限明文或{noop}口令), 对应 SBA 的BasicAuthHttpHeaderProvider。若口令是{pbkdf2}哈希,无法还原,会输出Warning, 此时请在instance.metadata显式提供,或改在 SBA Server 端配置spring.boot.admin.instance-auth。
SBA Server 端解析凭据的顺序(BasicAuthHttpHeaderProvider,命中即止):
1. 实例注册 metadata 的 user.name / user.password ← 本客户端自动写入的路径
2. spring.boot.admin.instance-auth.service-map.<服务名>.username / .password
3. spring.boot.admin.instance-auth.default-user-name / .default-password
注意区分两组凭据,不要混用:
| 凭据 | 配置位置 | 用途 |
|---|---|---|
client.username / client.password |
spring:boot:admin:client:username / password |
应用 → SBA Server 的注册/心跳接口认证 |
user.name / user.password(metadata) |
自动注入,或 instance.metadata 显式覆盖 |
SBA Server → 应用 /actuator 的认证 |
using Yzl.Extensions.Actuator;
using Yzl.Extensions.SpringBoot.Admin.Client.Net;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
// ===== Yzl.Extensions.SpringBoot.Admin.Client.Net =====
builder.Services.AddSpringBootAdminClient(builder.Configuration);
var app = builder.Build();
app.MapGet("/", () => "Hello .NET 8 + SBA.Net.Client + Actuator!");
// ===== Yzl.Extensions.Actuator Endpoints(复用业务端口时,必须配置) =====
app.UseSpringNetActuatorMapEndpoints(builder.Configuration);
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();
6. 与Java对比
- 完全对应 Spring Boot Admin Clien
- 与 Yzl.Extensions.Actuator 紧密集成,减少重复配置
7. 版本升级(0.1.x → 0.1.10)
完整变更见源码仓库的 ReleaseNotes.md,这里只列升级时需要动手的部分。
7.1 配置迁移对照表
| 0.1.x 旧字段 | 0.1.10 新字段 | 兼容性 |
|---|---|---|
client:Url |
client:url |
✅ 绑定不区分大小写 |
client:Username / Password |
client:username / password |
✅ 同上 |
client:RefreshInterval("00:00:15") |
client:period("15s") |
✅ 两者都读,period 优先 |
client:AutoRegistration |
client:auto-registration |
✅ 旧键由 LegacyClientKeyCompat 回退读取 |
client:AutoDeregistration |
client:auto-deregistration |
✅ 同上 |
client:ServiceUrl |
client:instance:service-url |
⚠️ 仍读但已 [Obsolete](CS0618),instance.* 优先 |
client:ManagementUrl |
client:instance:management-url |
⚠️ 同上 |
client:HealthUrl |
client:instance:health-url |
⚠️ 同上 |
client:instanceMetadata(从未发布) |
client:instance:metadata |
❌ 已移除 |
7.2 0.1.10 新增配置(无旧对应)
instance:name、instance:service-base-url、instance:service-path、
instance:management-base-url、instance:service-host-type、instance:prefer-ip、
client:api-path(默认 instances)、client:period(支持 10s / 1m / 500ms / PT30S)。
7.3 升级检查清单
- 只用了
Url/Username/Password/RefreshInterval→ 不用改,旧键继续可用。 - 用了
client.service-url/management-url/health-url→ 迁移到client.instance.*对应键,消除CS0618警告。 - 开启 Actuator 鉴权(
management:security:enabled=true)→ 本客户端会自动把明文或{noop}口令写进实例 metadata;若口令是{pbkdf2}哈希则无法还原,需在instance.metadata显式提供user.name/user.password。 - 依赖升级 → 0.1.10 客户端需要
Yzl.Extensions.Actuator0.1.10(新增的management:security类型来自该包)。
如有使用问题或更好建议,请与作者联系。EMail:306337420@QQ.com。
| 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net8.0
- Yzl.Extensions.Actuator (>= 0.1.10)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.