diff --git a/DysonNetwork.Develop/Program.cs b/DysonNetwork.Develop/Program.cs index 7b75b06..4d003cb 100644 --- a/DysonNetwork.Develop/Program.cs +++ b/DysonNetwork.Develop/Program.cs @@ -16,9 +16,6 @@ builder.ConfigureAppKestrel(builder.Configuration); builder.Services.AddAppServices(builder.Configuration); builder.Services.AddAppAuthentication(); builder.Services.AddDysonAuth(); -builder.Services.AddSphereService(); -builder.Services.AddAccountService(); -builder.Services.AddDriveService(); builder.AddSwaggerManifest( "DysonNetwork.Develop", diff --git a/DysonNetwork.Drive/Program.cs b/DysonNetwork.Drive/Program.cs index 21cb457..7099d0c 100644 --- a/DysonNetwork.Drive/Program.cs +++ b/DysonNetwork.Drive/Program.cs @@ -19,8 +19,6 @@ builder.ConfigureAppKestrel(builder.Configuration, maxRequestBodySize: long.MaxV builder.Services.AddAppServices(builder.Configuration); builder.Services.AddAppAuthentication(); builder.Services.AddDysonAuth(); -builder.Services.AddRingService(); -builder.Services.AddAccountService(); builder.Services.AddAppFlushHandlers(); builder.Services.AddAppBusinessServices(); diff --git a/DysonNetwork.Insight/Program.cs b/DysonNetwork.Insight/Program.cs index 726448d..e420aba 100644 --- a/DysonNetwork.Insight/Program.cs +++ b/DysonNetwork.Insight/Program.cs @@ -21,8 +21,6 @@ builder.Services.AddAppBusinessServices(); builder.Services.AddAppScheduledJobs(); builder.Services.AddDysonAuth(); -builder.Services.AddAccountService(); -builder.Services.AddSphereService(); builder.Services.AddThinkingServices(builder.Configuration); builder.AddSwaggerManifest( diff --git a/DysonNetwork.Insight/Thought/ThoughtProvider.cs b/DysonNetwork.Insight/Thought/ThoughtProvider.cs index 812a6da..9151d98 100644 --- a/DysonNetwork.Insight/Thought/ThoughtProvider.cs +++ b/DysonNetwork.Insight/Thought/ThoughtProvider.cs @@ -27,7 +27,7 @@ public class ThoughtProvider private readonly PostService.PostServiceClient _postClient; private readonly AccountService.AccountServiceClient _accountClient; private readonly IConfiguration _configuration; - private readonly ILogger _logger; + private readonly ServiceRegistrar _registrar; private readonly Dictionary _kernels = new(); private readonly Dictionary _serviceProviders = new(); @@ -39,10 +39,10 @@ public class ThoughtProvider IConfiguration configuration, PostService.PostServiceClient postServiceClient, AccountService.AccountServiceClient accountServiceClient, - ILogger logger + ServiceRegistrar registrar ) { - _logger = logger; + _registrar = registrar; _postClient = postServiceClient; _accountClient = accountServiceClient; _configuration = configuration; @@ -105,8 +105,8 @@ public class ThoughtProvider // Add gRPC clients for Thought Plugins builder.Services.AddServiceDiscoveryCore(); builder.Services.AddServiceDiscovery(); - builder.Services.AddAccountService(); - builder.Services.AddSphereService(); + builder.Services.AddAccountService(_registrar); + builder.Services.AddSphereService(_registrar); builder.Plugins.AddFromObject(new SnAccountKernelPlugin(_accountClient)); builder.Plugins.AddFromObject(new SnPostKernelPlugin(_postClient)); diff --git a/DysonNetwork.Pass/Program.cs b/DysonNetwork.Pass/Program.cs index b054d44..bcc9852 100644 --- a/DysonNetwork.Pass/Program.cs +++ b/DysonNetwork.Pass/Program.cs @@ -16,9 +16,6 @@ builder.ConfigureAppKestrel(builder.Configuration); // Add application services builder.Services.AddAppServices(builder.Configuration); builder.Services.AddAppAuthentication(); -builder.Services.AddRingService(); -builder.Services.AddDriveService(); -builder.Services.AddDevelopService(); builder.Services.AddAppFlushHandlers(); builder.Services.AddAppBusinessServices(builder.Configuration); diff --git a/DysonNetwork.Pass/Startup/ServiceCollectionExtensions.cs b/DysonNetwork.Pass/Startup/ServiceCollectionExtensions.cs index e2d352f..80e4939 100644 --- a/DysonNetwork.Pass/Startup/ServiceCollectionExtensions.cs +++ b/DysonNetwork.Pass/Startup/ServiceCollectionExtensions.cs @@ -48,8 +48,6 @@ public static class ServiceCollectionExtensions }); services.AddGrpcReflection(); - services.AddRingService(); - // Register OIDC services services.AddScoped(); services.AddScoped(); diff --git a/DysonNetwork.Ring/Program.cs b/DysonNetwork.Ring/Program.cs index 9aa3044..83895f1 100644 --- a/DysonNetwork.Ring/Program.cs +++ b/DysonNetwork.Ring/Program.cs @@ -18,7 +18,6 @@ builder.ConfigureAppKestrel(builder.Configuration); builder.Services.AddAppServices(builder.Configuration); builder.Services.AddAppAuthentication(); builder.Services.AddDysonAuth(); -builder.Services.AddAccountService(); builder.Services.AddAppFlushHandlers(); builder.Services.AddAppBusinessServices(); diff --git a/DysonNetwork.Shared/Auth/Startup.cs b/DysonNetwork.Shared/Auth/Startup.cs index df1b562..8dc6e31 100644 --- a/DysonNetwork.Shared/Auth/Startup.cs +++ b/DysonNetwork.Shared/Auth/Startup.cs @@ -9,8 +9,6 @@ public static class DysonAuthStartup this IServiceCollection services ) { - services.AddAuthService(); - services.AddAuthentication(options => { options.DefaultAuthenticateScheme = AuthConstants.SchemeName; diff --git a/DysonNetwork.Shared/Extensions.cs b/DysonNetwork.Shared/Extensions.cs index c7b4b12..19388f7 100644 --- a/DysonNetwork.Shared/Extensions.cs +++ b/DysonNetwork.Shared/Extensions.cs @@ -7,6 +7,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Diagnostics.HealthChecks; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.ServiceDiscovery; using NodaTime; using OpenTelemetry; using OpenTelemetry.Metrics; @@ -52,11 +53,20 @@ public static class Extensions // options.AllowedSchemes = ["https"]; // }); + var etcdClient = new EtcdClient(builder.Configuration.GetConnectionString("Registrar")); + var registrar = new ServiceRegistrar(etcdClient); builder.Services.AddSingleton(SystemClock.Instance); - builder.Services.AddSingleton(new EtcdClient(builder.Configuration.GetConnectionString("Registrar"))); - builder.Services.AddSingleton(); + builder.Services.AddSingleton(etcdClient); + builder.Services.AddSingleton(registrar); builder.Services.AddHostedService(); + builder.Services.AddRingService(registrar); + builder.Services.AddAuthService(registrar); + builder.Services.AddAccountService(registrar); + builder.Services.AddSphereService(registrar); + builder.Services.AddDriveService(registrar); + builder.Services.AddDevelopService(registrar); + builder.AddNatsClient("Queue"); builder.AddRedisClient("Cache", configureOptions: opts => { opts.AbortOnConnectFail = false; }); diff --git a/DysonNetwork.Shared/Registry/ServiceInjectionHelper.cs b/DysonNetwork.Shared/Registry/ServiceInjectionHelper.cs index 9f587ea..7f3ed7c 100644 --- a/DysonNetwork.Shared/Registry/ServiceInjectionHelper.cs +++ b/DysonNetwork.Shared/Registry/ServiceInjectionHelper.cs @@ -1,14 +1,17 @@ using DysonNetwork.Shared.Proto; +using Grpc.Net.ClientFactory; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; namespace DysonNetwork.Shared.Registry; public static class ServiceInjectionHelper { - public static IServiceCollection AddRingService(this IServiceCollection services) + public static IServiceCollection AddRingService(this IServiceCollection services, ServiceRegistrar registrar) { + var instance = registrar.GetServiceInstanceAsync("ring", "grpc").GetAwaiter().GetResult(); services - .AddGrpcClient(o => o.Address = new Uri("https://_grpc.ring")) + .AddGrpcClient(o => o.Address = new Uri(instance)) .ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler() { ServerCertificateCustomValidationCallback = (_, _, _, _) => true } ); @@ -16,16 +19,17 @@ public static class ServiceInjectionHelper return services; } - public static IServiceCollection AddAuthService(this IServiceCollection services) + public static IServiceCollection AddAuthService(this IServiceCollection services, ServiceRegistrar registrar) { + var instance = registrar.GetServiceInstanceAsync("pass", "grpc").GetAwaiter().GetResult(); services - .AddGrpcClient(o => o.Address = new Uri("https://_grpc.pass")) + .AddGrpcClient(o => o.Address = new Uri(instance)) .ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler() { ServerCertificateCustomValidationCallback = (_, _, _, _) => true } ); services - .AddGrpcClient(o => o.Address = new Uri("https://_grpc.pass")) + .AddGrpcClient(o => o.Address = new Uri(instance)) .ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler() { ServerCertificateCustomValidationCallback = (_, _, _, _) => true } ); @@ -33,10 +37,11 @@ public static class ServiceInjectionHelper return services; } - public static IServiceCollection AddAccountService(this IServiceCollection services) + public static IServiceCollection AddAccountService(this IServiceCollection services, ServiceRegistrar registrar) { + var instance = registrar.GetServiceInstanceAsync("pass", "grpc").GetAwaiter().GetResult(); services - .AddGrpcClient(o => o.Address = new Uri("https://_grpc.pass")) + .AddGrpcClient(o => o.Address = new Uri(instance)) .ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler() { ServerCertificateCustomValidationCallback = (_, _, _, _) => true } ); @@ -44,42 +49,42 @@ public static class ServiceInjectionHelper services .AddGrpcClient(o => - o.Address = new Uri("https://_grpc.pass") + o.Address = new Uri(instance) ) .ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler() { ServerCertificateCustomValidationCallback = (_, _, _, _) => true } ); - services.AddGrpcClient(o => o.Address = new Uri("https://_grpc.pass")) + services.AddGrpcClient(o => o.Address = new Uri(instance)) .ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler() { ServerCertificateCustomValidationCallback = (_, _, _, _) => true } ); - services.AddGrpcClient(o => o.Address = new Uri("https://_grpc.pass")) + services.AddGrpcClient(o => o.Address = new Uri(instance)) .ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler() { ServerCertificateCustomValidationCallback = (_, _, _, _) => true } ); - services.AddGrpcClient(o => o.Address = new Uri("https://_grpc.pass")) + services.AddGrpcClient(o => o.Address = new Uri(instance)) .ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler() { ServerCertificateCustomValidationCallback = (_, _, _, _) => true } ); services - .AddGrpcClient(o => o.Address = new Uri("https://_grpc.pass")) + .AddGrpcClient(o => o.Address = new Uri(instance)) .ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler() { ServerCertificateCustomValidationCallback = (_, _, _, _) => true } ); services.AddSingleton(); - + services - .AddGrpcClient(o => o.Address = new Uri("https://_grpc.pass")) + .AddGrpcClient(o => o.Address = new Uri(instance)) .ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler() { ServerCertificateCustomValidationCallback = (_, _, _, _) => true } ); - + services - .AddGrpcClient(o => o.Address = new Uri("https://_grpc.pass")) + .AddGrpcClient(o => o.Address = new Uri(instance)) .ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler() { ServerCertificateCustomValidationCallback = (_, _, _, _) => true } ); @@ -87,9 +92,10 @@ public static class ServiceInjectionHelper return services; } - public static IServiceCollection AddDriveService(this IServiceCollection services) + public static IServiceCollection AddDriveService(this IServiceCollection services, ServiceRegistrar registrar) { - services.AddGrpcClient(o => o.Address = new Uri("https://_grpc.drive")) + var instance = registrar.GetServiceInstanceAsync("drive", "grpc").GetAwaiter().GetResult(); + services.AddGrpcClient(o => o.Address = new Uri(instance)) .ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler() { ServerCertificateCustomValidationCallback = (_, _, _, _) => true } ); @@ -103,32 +109,33 @@ public static class ServiceInjectionHelper return services; } - public static IServiceCollection AddSphereService(this IServiceCollection services) + public static IServiceCollection AddSphereService(this IServiceCollection services, ServiceRegistrar registrar) { + var instance = registrar.GetServiceInstanceAsync("drive", "grpc").GetAwaiter().GetResult(); services - .AddGrpcClient(o => o.Address = new Uri("https://_grpc.sphere")) + .AddGrpcClient(o => o.Address = new Uri(instance)) .ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler() { ServerCertificateCustomValidationCallback = (_, _, _, _) => true } ); services - .AddGrpcClient(o => o.Address = new Uri("https://_grpc.sphere")) + .AddGrpcClient(o => o.Address = new Uri(instance)) .ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler() { ServerCertificateCustomValidationCallback = (_, _, _, _) => true } ); services.AddSingleton(); - return services; } - public static IServiceCollection AddDevelopService(this IServiceCollection services) + public static IServiceCollection AddDevelopService(this IServiceCollection services, ServiceRegistrar registrar) { + var instance = registrar.GetServiceInstanceAsync("develop", "grpc").GetAwaiter().GetResult(); services.AddGrpcClient(o => - o.Address = new Uri("https://_grpc.develop")) + o.Address = new Uri(instance)) .ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler() { ServerCertificateCustomValidationCallback = (_, _, _, _) => true } ); return services; } -} +} \ No newline at end of file diff --git a/DysonNetwork.Shared/Registry/ServiceRegistrar.cs b/DysonNetwork.Shared/Registry/ServiceRegistrar.cs index 77aa6d7..3aa3c90 100644 --- a/DysonNetwork.Shared/Registry/ServiceRegistrar.cs +++ b/DysonNetwork.Shared/Registry/ServiceRegistrar.cs @@ -85,14 +85,15 @@ public class ServiceRegistrar(EtcdClient etcd) public async Task> GetServiceInstancesAsync(string serviceName, string servicePart) { var prefix = $"/services/{serviceName}/{servicePart}/"; - var request = new RangeRequest + while (true) { - Key = ByteString.CopyFromUtf8(prefix), - RangeEnd = ByteString.CopyFromUtf8(prefix + "\0") - }; - var response = await etcd.GetAsync(request); - var instances = response.Kvs.Select(kv => kv.Value.ToStringUtf8()).ToList(); - return instances; + var response = await etcd.GetRangeAsync(prefix); + var instances = response.Kvs.Select(kv => kv.Value.ToStringUtf8()).ToList(); + if (instances.Count > 0) + return instances; + Console.WriteLine($"No instances found for service '{serviceName}/{servicePart}', retrying..."); + await Task.Delay(1000); + } } /// @@ -104,8 +105,7 @@ public class ServiceRegistrar(EtcdClient etcd) if (instances.Count == 0) throw new InvalidOperationException($"No instances found for service '{serviceName}' part '{servicePart}'"); var key = $"{serviceName}/{servicePart}"; - if (!_roundRobinCounters.ContainsKey(key)) - _roundRobinCounters[key] = 0; + _roundRobinCounters.TryAdd(key, 0); var instance = instances[_roundRobinCounters[key] % instances.Count]; _roundRobinCounters[key] = (_roundRobinCounters[key] + 1) % int.MaxValue; return instance; diff --git a/DysonNetwork.Sphere/Program.cs b/DysonNetwork.Sphere/Program.cs index c66ab61..83dbfac 100644 --- a/DysonNetwork.Sphere/Program.cs +++ b/DysonNetwork.Sphere/Program.cs @@ -19,9 +19,6 @@ builder.ConfigureAppKestrel(builder.Configuration); builder.Services.AddAppServices(); builder.Services.AddAppAuthentication(); builder.Services.AddDysonAuth(); -builder.Services.AddAccountService(); -builder.Services.AddRingService(); -builder.Services.AddDriveService(); builder.Services.AddAppFlushHandlers(); builder.Services.AddAppBusinessServices(builder.Configuration); diff --git a/DysonNetwork.Zone/Program.cs b/DysonNetwork.Zone/Program.cs index 873e241..51061e6 100644 --- a/DysonNetwork.Zone/Program.cs +++ b/DysonNetwork.Zone/Program.cs @@ -24,9 +24,6 @@ builder.Services.AddAppBusinessServices(builder.Configuration); builder.Services.AddAppScheduledJobs(); builder.Services.AddDysonAuth(); -builder.Services.AddRingService(); -builder.Services.AddAccountService(); -builder.Services.AddSphereService(); builder.Services.Configure(options => { options.LowercaseUrls = true; }); diff --git a/DysonNetwork.sln.DotSettings.user b/DysonNetwork.sln.DotSettings.user index 3d019cd..6bad741 100644 --- a/DysonNetwork.sln.DotSettings.user +++ b/DysonNetwork.sln.DotSettings.user @@ -35,6 +35,7 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded @@ -67,6 +68,11 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded + ForceIncluded + ForceIncluded + ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded @@ -76,6 +82,7 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded @@ -90,6 +97,8 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded @@ -151,6 +160,7 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded ForceIncluded @@ -171,8 +181,10 @@ ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded + ForceIncluded ForceIncluded ForceIncluded <AssemblyExplorer>