-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Microsoft.Extensions.ServiceDiscovery support (#317)
* Microsoft.Extensions.ServiceDiscovery support * comment fix * comment fix
- Loading branch information
Showing
11 changed files
with
433 additions
and
8 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,12 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<Nullable>enable</Nullable> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Nacos.AspNetCore\Nacos.AspNetCore.csproj" /> | ||
<ProjectReference Include="..\..\src\Nacos.Microsoft.Extensions.ServiceDiscovery\Nacos.Microsoft.Extensions.ServiceDiscovery.csproj" /> | ||
</ItemGroup> | ||
</Project> |
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
41 changes: 41 additions & 0 deletions
41
....Microsoft.Extensions.ServiceDiscovery/Nacos.Microsoft.Extensions.ServiceDiscovery.csproj
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,41 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
<Import Project="../../build/version.props" /> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
<PackageId>nacos-sdk-csharp.Extensions.ServiceDiscovery</PackageId> | ||
<VersionPrefix>$(NugetVersion)</VersionPrefix> | ||
<VersionSuffix></VersionSuffix> | ||
<Authors>nacos-sdk-csharp Contributors</Authors> | ||
<Description>nacos csharp sdk</Description> | ||
<PackageTags>nacos,csharp,sdk,servicediscovery</PackageTags> | ||
<PackageProjectUrl>https://github.com/nacos-group/nacos-sdk-csharp</PackageProjectUrl> | ||
<RepositoryUrl>https://github.com/nacos-group/nacos-sdk-csharp</RepositoryUrl> | ||
<ProjectUrl>https://github.com/nacos-group/nacos-sdk-csharp</ProjectUrl> | ||
<PackageLicenseFile>LICENSE</PackageLicenseFile> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
<PackageReleaseNotes> | ||
</PackageReleaseNotes> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<GenerateDocumentationFile>true</GenerateDocumentationFile> | ||
<NoWarn>$(NoWarn);1591</NoWarn> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="../../LICENSE" Pack="true" Visible="false" PackagePath="" /> | ||
<None Include="README.md" Pack="true" Visible="false" PackagePath="" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.ServiceDiscovery" Version="8.2.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Nacos\Nacos.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
19 changes: 19 additions & 0 deletions
19
src/Nacos.Microsoft.Extensions.ServiceDiscovery/NacosServiceDiscoveryExtensions.cs
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,19 @@ | ||
#pragma warning disable SA1200 | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.ServiceDiscovery; | ||
#pragma warning restore SA1200 | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.Extensions.Hosting; | ||
|
||
public static class NacosServiceDiscoveryExtensions | ||
{ | ||
public static IServiceCollection AddNacosSrvServiceEndpointProvider(this IServiceCollection services) | ||
{ | ||
ArgumentNullException.ThrowIfNull(services); | ||
|
||
services.AddServiceDiscoveryCore(); | ||
services.AddSingleton<IServiceEndpointProviderFactory, NacosServiceEndPointProviderFactory>(); | ||
return services; | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/Nacos.Microsoft.Extensions.ServiceDiscovery/NacosServiceEndPointProvider.cs
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,71 @@ | ||
#pragma warning disable SA1200 | ||
using Nacos.Microsoft.Extensions.ServiceDiscovery; | ||
using System.Net; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Extensions.ServiceDiscovery; | ||
using Nacos.V2; | ||
#pragma warning restore SA1200 | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.Extensions.Hosting; | ||
|
||
/// <summary> | ||
/// NacosServiceEndpointProvider | ||
/// </summary> | ||
/// <param name="query">query</param> | ||
/// <param name="hostName">hostName</param> | ||
/// <param name="options">options</param> | ||
/// <param name="logger">logger</param> | ||
/// <param name="namingService">namingService</param> | ||
/// <param name="timeProvider">timeProvider</param> | ||
internal sealed partial class NacosServiceEndpointProvider( | ||
ServiceEndpointQuery query, | ||
string hostName, | ||
IOptionsMonitor<NacosServiceEndpointProviderOptions> options, | ||
ILogger<NacosServiceEndpointProvider> logger, | ||
INacosNamingService namingService, | ||
TimeProvider timeProvider) : NacosServiceEndpointProviderBase(query, logger, timeProvider), IHostNameFeature | ||
{ | ||
protected override double RetryBackOffFactor => options.CurrentValue.RetryBackOffFactor; | ||
|
||
protected override TimeSpan MinRetryPeriod => options.CurrentValue.MinRetryPeriod; | ||
|
||
protected override TimeSpan MaxRetryPeriod => options.CurrentValue.MaxRetryPeriod; | ||
|
||
protected override TimeSpan DefaultRefreshPeriod => options.CurrentValue.DefaultRefreshPeriod; | ||
|
||
string IHostNameFeature.HostName => hostName; | ||
|
||
/// <inheritdoc/> | ||
public override string ToString() => "Nacos"; | ||
|
||
protected override async Task ResolveAsyncCore() | ||
{ | ||
var endpoints = new List<ServiceEndpoint>(); | ||
var ttl = DefaultRefreshPeriod; | ||
Log.AddressQuery(logger, ServiceName, hostName); | ||
var selectInstances = await namingService.SelectInstances(hostName, true).ConfigureAwait(false); | ||
foreach (var instance in selectInstances) | ||
{ | ||
var ipAddress = new IPAddress(instance.Ip.Split('.').Select(a => Convert.ToByte(a)).ToArray()); | ||
var ipPoint = new IPEndPoint(ipAddress, instance.Port); | ||
var serviceEndpoint = ServiceEndpoint.Create(ipPoint); | ||
serviceEndpoint.Features.Set<IServiceEndpointProvider>(this); | ||
if (options.CurrentValue.ShouldApplyHostNameMetadata(serviceEndpoint)) | ||
{ | ||
serviceEndpoint.Features.Set<IHostNameFeature>(this); | ||
} | ||
|
||
endpoints.Add(serviceEndpoint); | ||
} | ||
|
||
if (endpoints.Count == 0) | ||
{ | ||
throw new InvalidOperationException($"No records were found for service '{ServiceName}' ( name: '{hostName}')."); | ||
} | ||
|
||
SetResult(endpoints, ttl); | ||
} | ||
} | ||
|
38 changes: 38 additions & 0 deletions
38
src/Nacos.Microsoft.Extensions.ServiceDiscovery/NacosServiceEndPointProviderFactory.cs
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,38 @@ | ||
#pragma warning disable SA1200 | ||
using System.Diagnostics.CodeAnalysis; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Extensions.ServiceDiscovery; | ||
using Nacos.Microsoft.Extensions.ServiceDiscovery; | ||
using Nacos.V2; | ||
#pragma warning restore SA1200 | ||
|
||
// ReSharper disable once CheckNamespace | ||
namespace Microsoft.Extensions.Hosting; | ||
|
||
/// <summary> | ||
/// NacosServiceEndPointProviderFactory | ||
/// </summary> | ||
/// <param name="options">options</param> | ||
/// <param name="logger">logger</param> | ||
/// <param name="timeProvider">timeProvider</param> | ||
/// <param name="namingService">nacos namingService</param> | ||
internal sealed partial class NacosServiceEndPointProviderFactory( | ||
IOptionsMonitor<NacosServiceEndpointProviderOptions> options, | ||
ILogger<NacosServiceEndpointProvider> logger, | ||
TimeProvider timeProvider, | ||
INacosNamingService namingService) : IServiceEndpointProviderFactory | ||
{ | ||
/// <summary> | ||
/// Tries to create an <see cref="T:Microsoft.Extensions.ServiceDiscovery.IServiceEndpointProvider" /> instance for the specified <paramref name="query" />. | ||
/// </summary> | ||
/// <param name="query">The service to create the provider for.</param> | ||
/// <param name="provider">The provider.</param> | ||
/// <returns><see langword="true" /> if the provider was created, <see langword="false" /> otherwise.</returns> | ||
public bool TryCreateProvider(ServiceEndpointQuery query, [NotNullWhen(true)] out IServiceEndpointProvider? provider) | ||
{ | ||
provider = new NacosServiceEndpointProvider(query, query.ServiceName, options, logger, namingService, timeProvider); | ||
|
||
return true; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Nacos.Microsoft.Extensions.ServiceDiscovery/NacosServiceEndpointProviderBase.Log.cs
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,23 @@ | ||
#pragma warning disable SA1200 | ||
using Microsoft.Extensions.Logging; | ||
#pragma warning restore SA1200 | ||
|
||
namespace Nacos.Microsoft.Extensions.ServiceDiscovery; | ||
|
||
/// <summary> | ||
/// log base | ||
/// </summary> | ||
internal partial class NacosServiceEndpointProviderBase | ||
{ | ||
/// <summary> | ||
/// log | ||
/// </summary> | ||
internal static partial class Log | ||
{ | ||
[LoggerMessage(1, LogLevel.Trace, "Resolving endpoints for service '{ServiceName}' using host lookup for name '{RecordName}'.", EventName = "AddressQuery")] | ||
public static partial void AddressQuery(ILogger logger, string serviceName, string recordName); | ||
|
||
[LoggerMessage(2, LogLevel.Debug, "Skipping endpoint resolution for service '{ServiceName}': '{Reason}'.", EventName = "SkippedResolution")] | ||
public static partial void SkippedResolution(ILogger logger, string serviceName, string reason); | ||
} | ||
} |
Oops, something went wrong.