Skip to content

Commit 6f7dfa2

Browse files
authored
Service Discovery: Expose Instance ID in IServiceInstance (#1597)
1 parent a8c3a98 commit 6f7dfa2

File tree

16 files changed

+41
-6
lines changed

16 files changed

+41
-6
lines changed

src/Common/src/Common/Discovery/IServiceInstance.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ public interface IServiceInstance
1111
/// </summary>
1212
string ServiceId { get; }
1313

14+
/// <summary>
15+
/// Gets the instance ID as registered by the discovery client.
16+
/// </summary>
17+
string InstanceId { get; }
18+
1419
/// <summary>
1520
/// Gets the hostname of the registered service instance.
1621
/// </summary>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
#nullable enable
2+
Steeltoe.Common.Discovery.IServiceInstance.InstanceId.get -> string!

src/Configuration/test/ConfigServer.Test/ConfigServerConfigurationProviderTest.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,8 @@ public void UpdateSettingsFromDiscovery_UpdatesSettingsCorrectly()
373373

374374
List<IServiceInstance> instances =
375375
[
376-
new TestServiceInstance("i1", new Uri("https://foo.bar:8888/"), metadata1),
377-
new TestServiceInstance("i2", new Uri("https://foo.bar.baz:9999/"), metadata2)
376+
new TestServiceInstance("s", "i1", new Uri("https://foo.bar:8888/"), metadata1),
377+
new TestServiceInstance("s", "i2", new Uri("https://foo.bar.baz:9999/"), metadata2)
378378
];
379379

380380
provider.UpdateSettingsFromDiscovery(instances, options);
@@ -416,9 +416,10 @@ private static string GetEncodedUserPassword(string user, string password)
416416
return Convert.ToBase64String(Encoding.ASCII.GetBytes($"{user}:{password}"));
417417
}
418418

419-
private sealed class TestServiceInstance(string serviceId, Uri uri, IReadOnlyDictionary<string, string?> metadata) : IServiceInstance
419+
private sealed class TestServiceInstance(string serviceId, string instanceId, Uri uri, IReadOnlyDictionary<string, string?> metadata) : IServiceInstance
420420
{
421421
public string ServiceId { get; } = serviceId;
422+
public string InstanceId { get; } = instanceId;
422423
public string Host { get; } = uri.Host;
423424
public int Port { get; } = uri.Port;
424425
public bool IsSecure { get; } = uri.Scheme == Uri.UriSchemeHttps;

src/Discovery/src/Configuration/ConfigurationServiceInstance.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public sealed class ConfigurationServiceInstance : IServiceInstance
2222
[Required]
2323
public string? ServiceId { get; set; }
2424

25+
/// <inheritdoc />
26+
public string InstanceId => string.Empty;
27+
2528
/// <inheritdoc />
2629
[Required]
2730
public string? Host { get; set; }
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
#nullable enable
2+
Steeltoe.Discovery.Configuration.ConfigurationServiceInstance.InstanceId.get -> string!

src/Discovery/src/Consul/ConsulServiceInstance.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ internal sealed class ConsulServiceInstance : IServiceInstance
1616
/// <inheritdoc />
1717
public string ServiceId { get; }
1818

19+
/// <inheritdoc />
20+
public string InstanceId { get; }
21+
1922
/// <inheritdoc />
2023
public string Host { get; }
2124

@@ -48,6 +51,7 @@ internal ConsulServiceInstance(ServiceEntry serviceEntry)
4851
Metadata = serviceEntry.Service.Meta.AsReadOnly();
4952
IsSecure = serviceEntry.Service.Meta != null && serviceEntry.Service.Meta.TryGetValue("secure", out string? secureString) && bool.Parse(secureString);
5053
ServiceId = serviceEntry.Service.Service;
54+
InstanceId = serviceEntry.Service.ID;
5155
Port = serviceEntry.Service.Port;
5256
Uri = new Uri($"{(IsSecure ? "https" : "http")}://{Host}:{Port}");
5357
}

src/Discovery/src/Consul/Registry/ConsulRegistration.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ internal sealed class ConsulRegistration : IServiceInstance
2222
/// <inheritdoc />
2323
public string ServiceId { get; }
2424

25-
/// <summary>
26-
/// Gets the instance ID as registered by the Consul server.
27-
/// </summary>
25+
/// <inheritdoc />
2826
public string InstanceId { get; }
2927

3028
/// <inheritdoc />

src/Discovery/src/Consul/ThisServiceInstance.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ internal sealed class ThisServiceInstance : IServiceInstance
1515
/// <inheritdoc />
1616
public string ServiceId { get; }
1717

18+
/// <inheritdoc />
19+
public string InstanceId { get; }
20+
1821
/// <inheritdoc />
1922
public string Host { get; }
2023

@@ -35,6 +38,7 @@ public ThisServiceInstance(ConsulRegistration registration)
3538
ArgumentNullException.ThrowIfNull(registration);
3639

3740
ServiceId = registration.ServiceId;
41+
InstanceId = registration.InstanceId;
3842
Host = registration.Host;
3943
IsSecure = registration.IsSecure;
4044
Port = registration.Port;

src/Discovery/src/Eureka/EurekaServiceInstance.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ namespace Steeltoe.Discovery.Eureka;
1313
internal sealed class EurekaServiceInstance : IServiceInstance
1414
{
1515
public string ServiceId { get; }
16+
public string InstanceId { get; }
1617
public string Host { get; }
1718
public int Port { get; }
1819
public bool IsSecure { get; }
@@ -24,6 +25,7 @@ public EurekaServiceInstance(InstanceInfo instance)
2425
ArgumentNullException.ThrowIfNull(instance);
2526

2627
ServiceId = instance.AppName;
28+
InstanceId = instance.InstanceId;
2729
Host = instance.HostName;
2830
Port = GetPort(instance);
2931
IsSecure = instance.IsSecurePortEnabled;

src/Discovery/src/HttpClients/LoadBalancers/ServiceInstancesResolver.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ private sealed class JsonSerializableServiceInstance : IServiceInstance
138138
// Trust that deserialized instances meet the IServiceInstance contract, so suppress nullability warnings.
139139

140140
public string ServiceId { get; set; } = null!;
141+
public string InstanceId { get; set; } = null!;
141142
public string Host { get; set; } = null!;
142143
public int Port { get; set; }
143144
public bool IsSecure { get; set; }
@@ -151,6 +152,7 @@ public static JsonSerializableServiceInstance CopyFrom(IServiceInstance instance
151152
return new JsonSerializableServiceInstance
152153
{
153154
ServiceId = instance.ServiceId,
155+
InstanceId = instance.InstanceId,
154156
Host = instance.Host,
155157
Port = instance.Port,
156158
IsSecure = instance.IsSecure,

0 commit comments

Comments
 (0)