From 4c65602465a8910c3207f7be20b0ce0ca1805a4b Mon Sep 17 00:00:00 2001 From: LittleSheep Date: Sun, 21 Dec 2025 19:59:53 +0800 Subject: [PATCH] :recycle: Update service discovery settings --- DysonNetwork.Develop/appsettings.json | 3 -- DysonNetwork.Gateway/Program.cs | 2 +- DysonNetwork.Shared/Extensions.cs | 44 ++++++++++++++++----------- DysonNetwork.sln.DotSettings.user | 1 + 4 files changed, 28 insertions(+), 22 deletions(-) diff --git a/DysonNetwork.Develop/appsettings.json b/DysonNetwork.Develop/appsettings.json index 4e9d68f..456e48c 100644 --- a/DysonNetwork.Develop/appsettings.json +++ b/DysonNetwork.Develop/appsettings.json @@ -21,8 +21,5 @@ }, "Cache": { "Serializer": "MessagePack" - }, - "Etcd": { - "Insecure": true } } diff --git a/DysonNetwork.Gateway/Program.cs b/DysonNetwork.Gateway/Program.cs index ce16a28..f785e81 100644 --- a/DysonNetwork.Gateway/Program.cs +++ b/DysonNetwork.Gateway/Program.cs @@ -131,7 +131,7 @@ var clusters = serviceNames.Select(serviceName => new ClusterConfig Timeout = TimeSpan.FromSeconds(5), Path = "/health" }, - Passive = new() + Passive = new PassiveHealthCheckConfig { Enabled = true } diff --git a/DysonNetwork.Shared/Extensions.cs b/DysonNetwork.Shared/Extensions.cs index da8f1e9..d4c9c06 100644 --- a/DysonNetwork.Shared/Extensions.cs +++ b/DysonNetwork.Shared/Extensions.cs @@ -24,7 +24,8 @@ public static class Extensions private const string HealthEndpointPath = "/health"; private const string AlivenessEndpointPath = "/alive"; - public static TBuilder AddServiceDefaults(this TBuilder builder) where TBuilder : IHostApplicationBuilder + public static TBuilder AddServiceDefaults(this TBuilder builder) + where TBuilder : IHostApplicationBuilder { // Allow unencrypted grpc AppContext.SetSwitch("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true); @@ -34,6 +35,7 @@ public static class Extensions builder.AddDefaultHealthChecks(); builder.Services.AddServiceDiscovery(); + builder.Services.AddServiceDiscoveryCore(); builder.Services.ConfigureHttpClientDefaults(http => { @@ -44,16 +46,16 @@ public static class Extensions http.AddServiceDiscovery(); }); - // Uncomment the following to restrict the allowed schemes for service discovery. - // builder.Services.Configure(options => - // { - // options.AllowedSchemes = ["https"]; - // }); - builder.Services.AddSingleton(SystemClock.Instance); builder.AddNatsClient("queue"); - builder.AddRedisClient("cache", configureOptions: opts => { opts.AbortOnConnectFail = false; }); + builder.AddRedisClient( + "cache", + configureOptions: opts => + { + opts.AbortOnConnectFail = false; + } + ); // Setup cache service builder.Services.AddStackExchangeRedisCache(options => @@ -84,16 +86,19 @@ public static class Extensions logging.IncludeScopes = true; }); - builder.Services.AddOpenTelemetry() + builder + .Services.AddOpenTelemetry() .WithMetrics(metrics => { - metrics.AddAspNetCoreInstrumentation() + metrics + .AddAspNetCoreInstrumentation() .AddHttpClientInstrumentation() .AddRuntimeInstrumentation(); }) .WithTracing(tracing => { - tracing.AddSource(builder.Environment.ApplicationName) + tracing + .AddSource(builder.Environment.ApplicationName) .AddAspNetCoreInstrumentation(tracing => // Exclude health check requests from tracing tracing.Filter = context => @@ -112,7 +117,9 @@ public static class Extensions private static TBuilder AddOpenTelemetryExporters(this TBuilder builder) where TBuilder : IHostApplicationBuilder { - var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); + var useOtlpExporter = !string.IsNullOrWhiteSpace( + builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"] + ); if (useOtlpExporter) { @@ -132,7 +139,8 @@ public static class Extensions public static TBuilder AddDefaultHealthChecks(this TBuilder builder) where TBuilder : IHostApplicationBuilder { - builder.Services.AddHealthChecks() + builder + .Services.AddHealthChecks() // Add a default liveness check to ensure app is responsive .AddCheck("self", () => HealthCheckResult.Healthy(), ["live"]); @@ -145,11 +153,11 @@ public static class Extensions app.MapHealthChecks(HealthEndpointPath); // Only health checks tagged with the "live" tag must pass for app to be considered alive - app.MapHealthChecks(AlivenessEndpointPath, new HealthCheckOptions - { - Predicate = r => r.Tags.Contains("live") - }); + app.MapHealthChecks( + AlivenessEndpointPath, + new HealthCheckOptions { Predicate = r => r.Tags.Contains("live") } + ); return app; } -} \ No newline at end of file +} diff --git a/DysonNetwork.sln.DotSettings.user b/DysonNetwork.sln.DotSettings.user index 6cd788c..528df8e 100644 --- a/DysonNetwork.sln.DotSettings.user +++ b/DysonNetwork.sln.DotSettings.user @@ -152,6 +152,7 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded