|
1 |
| -using Consul; |
2 |
| -using Ocelot.Infrastructure.Extensions; |
| 1 | +using Ocelot.Infrastructure.Extensions; |
3 | 2 | using Ocelot.Logging;
|
4 | 3 | using Ocelot.ServiceDiscovery.Providers;
|
5 | 4 | using Ocelot.Values;
|
6 | 5 |
|
7 |
| -namespace Ocelot.Provider.Consul |
8 |
| -{ |
9 |
| - public class Consul : IServiceDiscoveryProvider |
10 |
| - { |
11 |
| - private readonly ConsulRegistryConfiguration _config; |
12 |
| - private readonly IOcelotLogger _logger; |
13 |
| - private readonly IConsulClient _consul; |
14 |
| - private const string VersionPrefix = "version-"; |
| 6 | +namespace Ocelot.Provider.Consul; |
15 | 7 |
|
16 |
| - public Consul(ConsulRegistryConfiguration config, IOcelotLoggerFactory factory, IConsulClientFactory clientFactory) |
17 |
| - { |
18 |
| - _config = config; |
19 |
| - _logger = factory.CreateLogger<Consul>(); |
20 |
| - _consul = clientFactory.Get(_config); |
21 |
| - } |
| 8 | +public class Consul : IServiceDiscoveryProvider |
| 9 | +{ |
| 10 | + private const string VersionPrefix = "version-"; |
| 11 | + private readonly ConsulRegistryConfiguration _config; |
| 12 | + private readonly IConsulClient _consul; |
| 13 | + private readonly IOcelotLogger _logger; |
22 | 14 |
|
23 |
| - public async Task<List<Service>> Get() |
24 |
| - { |
25 |
| - var consulAddress = (_consul as ConsulClient)?.Config.Address; |
26 |
| - _logger.LogDebug($"Querying Consul {consulAddress} about a service: {_config.KeyOfServiceInConsul}"); |
| 15 | + public Consul(ConsulRegistryConfiguration config, IOcelotLoggerFactory factory, IConsulClientFactory clientFactory) |
| 16 | + { |
| 17 | + _logger = factory.CreateLogger<Consul>(); |
| 18 | + _config = config; |
| 19 | + _consul = clientFactory.Get(_config); |
| 20 | + } |
27 | 21 |
|
28 |
| - var queryResult = await _consul.Health.Service(_config.KeyOfServiceInConsul, string.Empty, true); |
| 22 | + public async Task<List<Service>> Get() |
| 23 | + { |
| 24 | + var queryResult = await _consul.Health.Service(_config.KeyOfServiceInConsul, string.Empty, true); |
29 | 25 |
|
30 |
| - var services = new List<Service>(); |
| 26 | + var services = new List<Service>(); |
31 | 27 |
|
32 |
| - foreach (var serviceEntry in queryResult.Response) |
| 28 | + foreach (var serviceEntry in queryResult.Response) |
| 29 | + { |
| 30 | + if (IsValid(serviceEntry)) |
33 | 31 | {
|
34 |
| - var address = serviceEntry.Service.Address; |
35 |
| - var port = serviceEntry.Service.Port; |
36 |
| - |
37 |
| - if (IsValid(serviceEntry)) |
| 32 | + var nodes = await _consul.Catalog.Nodes(); |
| 33 | + if (nodes.Response == null) |
38 | 34 | {
|
39 |
| - var nodes = await _consul.Catalog.Nodes(); |
40 |
| - if (nodes.Response == null) |
41 |
| - { |
42 |
| - services.Add(BuildService(serviceEntry, null)); |
43 |
| - } |
44 |
| - else |
45 |
| - { |
46 |
| - var serviceNode = nodes.Response.FirstOrDefault(n => n.Address == address); |
47 |
| - services.Add(BuildService(serviceEntry, serviceNode)); |
48 |
| - } |
49 |
| - |
50 |
| - _logger.LogDebug($"Consul answer: Address: {address}, Port: {port}"); |
| 35 | + services.Add(BuildService(serviceEntry, null)); |
51 | 36 | }
|
52 | 37 | else
|
53 | 38 | {
|
54 |
| - _logger.LogWarning($"Unable to use service Address: {address} and Port: {port} as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"); |
| 39 | + var serviceNode = nodes.Response.FirstOrDefault(n => n.Address == serviceEntry.Service.Address); |
| 40 | + services.Add(BuildService(serviceEntry, serviceNode)); |
55 | 41 | }
|
56 | 42 | }
|
57 |
| - |
58 |
| - return services.ToList(); |
| 43 | + else |
| 44 | + { |
| 45 | + _logger.LogWarning( |
| 46 | + $"Unable to use service Address: {serviceEntry.Service.Address} and Port: {serviceEntry.Service.Port} as it is invalid. Address must contain host only e.g. localhost and port must be greater than 0"); |
| 47 | + } |
59 | 48 | }
|
60 | 49 |
|
61 |
| - private static Service BuildService(ServiceEntry serviceEntry, Node serviceNode) |
62 |
| - { |
63 |
| - return new Service( |
64 |
| - serviceEntry.Service.Service, |
65 |
| - new ServiceHostAndPort(serviceNode == null ? serviceEntry.Service.Address : serviceNode.Name, serviceEntry.Service.Port), |
66 |
| - serviceEntry.Service.ID, |
67 |
| - GetVersionFromStrings(serviceEntry.Service.Tags), |
68 |
| - serviceEntry.Service.Tags ?? Enumerable.Empty<string>()); |
69 |
| - } |
| 50 | + return services.ToList(); |
| 51 | + } |
70 | 52 |
|
71 |
| - private static bool IsValid(ServiceEntry serviceEntry) |
72 |
| - { |
73 |
| - if (string.IsNullOrEmpty(serviceEntry.Service.Address) || serviceEntry.Service.Address.Contains("http://") || serviceEntry.Service.Address.Contains("https://") || serviceEntry.Service.Port <= 0) |
74 |
| - { |
75 |
| - return false; |
76 |
| - } |
| 53 | + private static Service BuildService(ServiceEntry serviceEntry, Node serviceNode) |
| 54 | + { |
| 55 | + return new Service( |
| 56 | + serviceEntry.Service.Service, |
| 57 | + new ServiceHostAndPort(serviceNode == null ? serviceEntry.Service.Address : serviceNode.Name, |
| 58 | + serviceEntry.Service.Port), |
| 59 | + serviceEntry.Service.ID, |
| 60 | + GetVersionFromStrings(serviceEntry.Service.Tags), |
| 61 | + serviceEntry.Service.Tags ?? Enumerable.Empty<string>()); |
| 62 | + } |
77 | 63 |
|
78 |
| - return true; |
79 |
| - } |
| 64 | + private static bool IsValid(ServiceEntry serviceEntry) |
| 65 | + { |
| 66 | + return !string.IsNullOrEmpty(serviceEntry.Service.Address) |
| 67 | + && !serviceEntry.Service.Address.Contains("http://") |
| 68 | + && !serviceEntry.Service.Address.Contains("https://") |
| 69 | + && serviceEntry.Service.Port > 0; |
| 70 | + } |
80 | 71 |
|
81 |
| - private static string GetVersionFromStrings(IEnumerable<string> strings) |
82 |
| - => strings? |
83 |
| - .FirstOrDefault(x => x.StartsWith(VersionPrefix, StringComparison.Ordinal)) |
84 |
| - .TrimStart(VersionPrefix); |
| 72 | + private static string GetVersionFromStrings(IEnumerable<string> strings) |
| 73 | + { |
| 74 | + return strings |
| 75 | + ?.FirstOrDefault(x => x.StartsWith(VersionPrefix, StringComparison.Ordinal)) |
| 76 | + .TrimStart(VersionPrefix); |
85 | 77 | }
|
86 | 78 | }
|
0 commit comments