|
5 | 5 | using Ocelot.Provider.Nacos.NacosClient; |
6 | 6 | using Ocelot.ServiceDiscovery.Providers; |
7 | 7 | using Ocelot.Values; |
| 8 | +using Nacos; |
| 9 | +using Microsoft.Extensions.Options; |
8 | 10 |
|
9 | 11 | namespace Ocelot.Provider.Nacos |
10 | 12 | { |
11 | 13 | public class Nacos : IServiceDiscoveryProvider |
12 | 14 | { |
13 | | - private readonly INacosServerManager _client; |
| 15 | + private readonly INacosNamingClient _client; |
14 | 16 | private readonly string _serviceName; |
| 17 | + private readonly string _groupName; |
| 18 | + private readonly string _clusters; |
| 19 | + private readonly string _namespaceId; |
15 | 20 |
|
16 | | - public Nacos(string serviceName, INacosServerManager client) |
| 21 | + public Nacos(string serviceName, INacosNamingClient client, IOptions<NacosAspNetCoreOptions> options) |
17 | 22 | { |
18 | | - _client = client; |
19 | 23 | _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; |
20 | 29 | } |
21 | 30 |
|
22 | 31 | public async Task<List<Service>> Get() |
23 | 32 | { |
24 | 33 | var services = new List<Service>(); |
25 | 34 |
|
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 | + }); |
27 | 42 |
|
28 | | - if (instances != null && instances.Any()) |
| 43 | + if (instances != null && instances.Hosts!=null && instances.Hosts.Any()) |
29 | 44 | { |
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>()))); |
31 | 46 | } |
32 | 47 |
|
33 | 48 | return await Task.FromResult(services); |
|
0 commit comments