Skip to content

Commit a034de5

Browse files
author
liguoliang
committed
修复非默认命名空间无法发现的问题
1 parent 6683dd2 commit a034de5

6 files changed

Lines changed: 34 additions & 12 deletions

File tree

demo/ApiGatewayDemo/ApiGatewayDemo.csproj

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77

88
<ItemGroup>
99
<PackageReference Include="Ocelot" Version="17.0.0" />
10-
<PackageReference Include="Ocelot.Provider.Nacos" Version="1.0.0" />
10+
</ItemGroup>
11+
12+
<ItemGroup>
13+
<ProjectReference Include="..\..\src\Ocelot.Provider.Nacos\Ocelot.Provider.Nacos.csproj" />
1114
</ItemGroup>
1215
<ItemGroup>
1316
<Content Update="ocelotconfig.json">

demo/ApiGatewayDemo/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"ApiGatewayDemo": {
44
"commandName": "Project",
55
"launchBrowser": true,
6-
"applicationUrl": "http://*:5001",
6+
"applicationUrl": "http://0.0.0.0:5001",
77
"environmentVariables": {
88
"ASPNETCORE_ENVIRONMENT": "Development"
99
}

demo/ProductApi/Properties/launchSettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"ProductApi": {
1919
"commandName": "Project",
2020
"launchBrowser": true,
21-
"applicationUrl": "http://*:5002",
21+
"applicationUrl": "http://0.0.0.0:5002",
2222
"environmentVariables": {
2323
"ASPNETCORE_ENVIRONMENT": "Development"
2424
}

src/Ocelot.Provider.Nacos/Nacos.cs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,29 +5,44 @@
55
using Ocelot.Provider.Nacos.NacosClient;
66
using Ocelot.ServiceDiscovery.Providers;
77
using Ocelot.Values;
8+
using Nacos;
9+
using Microsoft.Extensions.Options;
810

911
namespace Ocelot.Provider.Nacos
1012
{
1113
public class Nacos : IServiceDiscoveryProvider
1214
{
13-
private readonly INacosServerManager _client;
15+
private readonly INacosNamingClient _client;
1416
private readonly string _serviceName;
17+
private readonly string _groupName;
18+
private readonly string _clusters;
19+
private readonly string _namespaceId;
1520

16-
public Nacos(string serviceName, INacosServerManager client)
21+
public Nacos(string serviceName, INacosNamingClient client, IOptions<NacosAspNetCoreOptions> options)
1722
{
18-
_client = client;
1923
_serviceName = serviceName;
24+
_client = client;
25+
_groupName = string.IsNullOrWhiteSpace(options.Value.GroupName) ?
26+
"DEFAULT_GROUP" : options.Value.GroupName;
27+
_clusters = string.IsNullOrWhiteSpace(options.Value.ClusterName) ? "DEFAULT" : options.Value.ClusterName;
28+
_namespaceId = string.IsNullOrWhiteSpace(options.Value.Namespace) ? "public" : options.Value.Namespace;
2029
}
2130

2231
public async Task<List<Service>> Get()
2332
{
2433
var services = new List<Service>();
2534

26-
var instances = await _client.GetServerAsync(_serviceName);
35+
var instances = await _client.ListInstancesAsync(new ListInstancesRequest
36+
{
37+
ServiceName = _serviceName,
38+
GroupName = _groupName,
39+
NamespaceId = _namespaceId,
40+
HealthyOnly = true,
41+
});
2742

28-
if (instances != null && instances.Any())
43+
if (instances != null && instances.Hosts!=null && instances.Hosts.Any())
2944
{
30-
services.AddRange(instances.Select(i => new Service(i.InstanceId, new ServiceHostAndPort(i.Ip, i.Port), "", "", new List<string>())));
45+
services.AddRange(instances.Hosts.Select(i => new Service(i.InstanceId, new ServiceHostAndPort(i.Ip, i.Port), "", "", new List<string>())));
3146
}
3247

3348
return await Task.FromResult(services);

src/Ocelot.Provider.Nacos/NacosProviderFactory.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,21 @@
22
using Ocelot.Provider.Nacos.NacosClient;
33
using Ocelot.ServiceDiscovery;
44
using Microsoft.Extensions.DependencyInjection;
5+
using Nacos.V2;
6+
using Microsoft.Extensions.Options;
7+
using Nacos;
58

69
namespace Ocelot.Provider.Nacos
710
{
811
public static class NacosProviderFactory
912
{
1013
public static ServiceDiscoveryFinderDelegate Get = (provider, config, route) =>
1114
{
12-
var client = provider.GetService<INacosServerManager>();
15+
var client = provider.GetService<INacosNamingClient>();
1316
if (config.Type?.ToLower() == "nacos" && client != null)
1417
{
15-
return new Nacos(route.ServiceName, client);
18+
var option = provider.GetService<IOptions<NacosAspNetCoreOptions>>();
19+
return new Nacos(route.ServiceName, client, option);
1620
}
1721
return null;
1822
};

src/Ocelot.Provider.Nacos/Ocelot.Provider.Nacos.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
<ItemGroup>
1414
<PackageReference Include="Ocelot" Version="17.0.0" />
15-
<PackageReference Include="nacos-sdk-csharp-unofficial" Version="0.8.0" />
15+
<PackageReference Include="nacos-sdk-csharp" Version="1.1.1" />
1616
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="5.0.0" />
1717
<PackageReference Include="EasyCaching.InMemory" Version="1.1.0" />
1818
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.1" />

0 commit comments

Comments
 (0)