Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Common/src/Common/Discovery/IServiceInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ public interface IServiceInstance
/// </summary>
string ServiceId { get; }

/// <summary>
/// Gets the instance ID as registered by the discovery client.
/// </summary>
string InstanceId { get; }

/// <summary>
/// Gets the hostname of the registered service instance.
/// </summary>
Expand Down
1 change: 1 addition & 0 deletions src/Common/src/Common/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
Steeltoe.Common.Discovery.IServiceInstance.InstanceId.get -> string!
Original file line number Diff line number Diff line change
Expand Up @@ -373,8 +373,8 @@ public void UpdateSettingsFromDiscovery_UpdatesSettingsCorrectly()

List<IServiceInstance> instances =
[
new TestServiceInstance("i1", new Uri("https://foo.bar:8888/"), metadata1),
new TestServiceInstance("i2", new Uri("https://foo.bar.baz:9999/"), metadata2)
new TestServiceInstance("s", "i1", new Uri("https://foo.bar:8888/"), metadata1),
new TestServiceInstance("s", "i2", new Uri("https://foo.bar.baz:9999/"), metadata2)
];

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

private sealed class TestServiceInstance(string serviceId, Uri uri, IReadOnlyDictionary<string, string?> metadata) : IServiceInstance
private sealed class TestServiceInstance(string serviceId, string instanceId, Uri uri, IReadOnlyDictionary<string, string?> metadata) : IServiceInstance
{
public string ServiceId { get; } = serviceId;
public string InstanceId { get; } = instanceId;
public string Host { get; } = uri.Host;
public int Port { get; } = uri.Port;
public bool IsSecure { get; } = uri.Scheme == Uri.UriSchemeHttps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public sealed class ConfigurationServiceInstance : IServiceInstance
[Required]
public string? ServiceId { get; set; }

/// <inheritdoc />
public string InstanceId => string.Empty;

/// <inheritdoc />
[Required]
public string? Host { get; set; }
Expand Down
1 change: 1 addition & 0 deletions src/Discovery/src/Configuration/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#nullable enable
Steeltoe.Discovery.Configuration.ConfigurationServiceInstance.InstanceId.get -> string!
4 changes: 4 additions & 0 deletions src/Discovery/src/Consul/ConsulServiceInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ internal sealed class ConsulServiceInstance : IServiceInstance
/// <inheritdoc />
public string ServiceId { get; }

/// <inheritdoc />
public string InstanceId { get; }

/// <inheritdoc />
public string Host { get; }

Expand Down Expand Up @@ -48,6 +51,7 @@ internal ConsulServiceInstance(ServiceEntry serviceEntry)
Metadata = serviceEntry.Service.Meta.AsReadOnly();
IsSecure = serviceEntry.Service.Meta != null && serviceEntry.Service.Meta.TryGetValue("secure", out string? secureString) && bool.Parse(secureString);
ServiceId = serviceEntry.Service.Service;
InstanceId = serviceEntry.Service.ID;
Port = serviceEntry.Service.Port;
Uri = new Uri($"{(IsSecure ? "https" : "http")}://{Host}:{Port}");
}
Expand Down
4 changes: 1 addition & 3 deletions src/Discovery/src/Consul/Registry/ConsulRegistration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ internal sealed class ConsulRegistration : IServiceInstance
/// <inheritdoc />
public string ServiceId { get; }

/// <summary>
/// Gets the instance ID as registered by the Consul server.
/// </summary>
/// <inheritdoc />
public string InstanceId { get; }

/// <inheritdoc />
Expand Down
4 changes: 4 additions & 0 deletions src/Discovery/src/Consul/ThisServiceInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ internal sealed class ThisServiceInstance : IServiceInstance
/// <inheritdoc />
public string ServiceId { get; }

/// <inheritdoc />
public string InstanceId { get; }

/// <inheritdoc />
public string Host { get; }

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

ServiceId = registration.ServiceId;
InstanceId = registration.InstanceId;
Host = registration.Host;
IsSecure = registration.IsSecure;
Port = registration.Port;
Expand Down
2 changes: 2 additions & 0 deletions src/Discovery/src/Eureka/EurekaServiceInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Steeltoe.Discovery.Eureka;
internal sealed class EurekaServiceInstance : IServiceInstance
{
public string ServiceId { get; }
public string InstanceId { get; }
public string Host { get; }
public int Port { get; }
public bool IsSecure { get; }
Expand All @@ -24,6 +25,7 @@ public EurekaServiceInstance(InstanceInfo instance)
ArgumentNullException.ThrowIfNull(instance);

ServiceId = instance.AppName;
InstanceId = instance.InstanceId;
Host = instance.HostName;
Port = GetPort(instance);
IsSecure = instance.IsSecurePortEnabled;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ private sealed class JsonSerializableServiceInstance : IServiceInstance
// Trust that deserialized instances meet the IServiceInstance contract, so suppress nullability warnings.

public string ServiceId { get; set; } = null!;
public string InstanceId { get; set; } = null!;
public string Host { get; set; } = null!;
public int Port { get; set; }
public bool IsSecure { get; set; }
Expand All @@ -151,6 +152,7 @@ public static JsonSerializableServiceInstance CopyFrom(IServiceInstance instance
return new JsonSerializableServiceInstance
{
ServiceId = instance.ServiceId,
InstanceId = instance.InstanceId,
Host = instance.Host,
Port = instance.Port,
IsSecure = instance.IsSecure,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public async Task AddInstancesToListAsync_AddsExpected()
Service = new AgentService
{
Service = "ServiceId",
ID = "Instance1",
Address = "foo.bar.com",
Port = 1234,
Meta = new Dictionary<string, string>
Expand All @@ -46,6 +47,7 @@ public async Task AddInstancesToListAsync_AddsExpected()
Service = new AgentService
{
Service = "ServiceId",
ID = "Instance2",
Address = "foo1.bar1.com",
Port = 5678,
Meta = new Dictionary<string, string>
Expand Down Expand Up @@ -79,6 +81,7 @@ await discoveryClient.AddInstancesToListAsync(serviceInstances, "ServiceId", Que

serviceInstances[0].Host.Should().Be("foo.bar.com");
serviceInstances[0].ServiceId.Should().Be("ServiceId");
serviceInstances[0].InstanceId.Should().Be("Instance1");
serviceInstances[0].IsSecure.Should().BeTrue();
serviceInstances[0].Port.Should().Be(1234);
serviceInstances[0].Metadata.Should().HaveCount(2);
Expand All @@ -88,6 +91,7 @@ await discoveryClient.AddInstancesToListAsync(serviceInstances, "ServiceId", Que

serviceInstances[1].Host.Should().Be("foo1.bar1.com");
serviceInstances[1].ServiceId.Should().Be("ServiceId");
serviceInstances[1].InstanceId.Should().Be("Instance2");
serviceInstances[1].IsSecure.Should().BeFalse();
serviceInstances[1].Port.Should().Be(5678);
serviceInstances[1].Metadata.Should().HaveCount(2);
Expand Down Expand Up @@ -169,6 +173,7 @@ public async Task GetAllInstances_ReturnsExpected()
Service = new AgentService
{
Service = "ServiceId",
ID = "Instance1",
Address = "foo.bar.com",
Port = 1234,
Meta = new Dictionary<string, string>
Expand All @@ -183,6 +188,7 @@ public async Task GetAllInstances_ReturnsExpected()
Service = new AgentService
{
Service = "ServiceId",
ID = "Instance2",
Address = "foo1.bar1.com",
Port = 5678,
Meta = new Dictionary<string, string>
Expand Down Expand Up @@ -215,6 +221,7 @@ public async Task GetAllInstances_ReturnsExpected()

serviceInstances[0].Host.Should().Be("foo.bar.com");
serviceInstances[0].ServiceId.Should().Be("ServiceId");
serviceInstances[0].InstanceId.Should().Be("Instance1");
serviceInstances[0].IsSecure.Should().BeTrue();
serviceInstances[0].Port.Should().Be(1234);
serviceInstances[0].Metadata.Should().HaveCount(2);
Expand All @@ -224,6 +231,7 @@ public async Task GetAllInstances_ReturnsExpected()

serviceInstances[1].Host.Should().Be("foo1.bar1.com");
serviceInstances[1].ServiceId.Should().Be("ServiceId");
serviceInstances[1].InstanceId.Should().Be("Instance2");
serviceInstances[1].IsSecure.Should().BeFalse();
serviceInstances[1].Port.Should().Be(5678);
serviceInstances[1].Metadata.Should().HaveCount(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public void Constructor_Initializes()
Service = new AgentService
{
Service = "ServiceId",
ID = "Instance1",
Address = "foo.bar.com",
Port = 1234,
Tags =
Expand All @@ -35,6 +36,7 @@ public void Constructor_Initializes()

serviceInstance.Host.Should().Be("foo.bar.com");
serviceInstance.ServiceId.Should().Be("ServiceId");
serviceInstance.InstanceId.Should().Be("Instance1");
serviceInstance.IsSecure.Should().BeTrue();
serviceInstance.Port.Should().Be(1234);
serviceInstance.Tags.Should().HaveCount(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public void Constructor_Initializes()

instance.Host.Should().Be("test.foo.bar");
instance.ServiceId.Should().Be("foobar");
instance.InstanceId.Should().Be("ID");
instance.IsSecure.Should().BeFalse();
instance.Port.Should().Be(1234);
instance.Metadata.Should().ContainSingle();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ public async Task Constructor_Initializes_Correctly()
thisService.Metadata.Should().BeEmpty();
thisService.Port.Should().Be(5000);
thisService.ServiceId.Should().Be("DEMO");
thisService.InstanceId.Should().Be($"{instanceOptions.HostName}:demo:5000");
thisService.Uri.Should().Be(new Uri($"http://{instanceOptions.HostName}:5000"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public void InstanceWithBothPorts()
var serviceInstance = new EurekaServiceInstance(instance);

serviceInstance.ServiceId.Should().Be(instance.AppName);
serviceInstance.InstanceId.Should().Be("id");
serviceInstance.Host.Should().Be(instance.HostName);
serviceInstance.Port.Should().Be(instance.SecurePort);
serviceInstance.IsSecure.Should().BeTrue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ private static IDistributedCache GetCache()
private sealed class TestServiceInstance(Uri uri) : IServiceInstance
{
public string ServiceId => throw new NotImplementedException();
public string InstanceId => throw new NotImplementedException();
public string Host => throw new NotImplementedException();
public int Port => throw new NotImplementedException();
public bool IsSecure => throw new NotImplementedException();
Expand Down
Loading