From 29d007ae4e88eadbd795da522afb12c0ab48595f Mon Sep 17 00:00:00 2001 From: IvanM <87393011+Ivan-Montes@users.noreply.github.com> Date: Mon, 13 Jan 2025 08:51:55 +0100 Subject: [PATCH] Set Eureka InstanceInfo to endpoint attribute Related: #6056 Motivation: Users might want to use the metadata from the Eureka `InstanceInfo` but currently, there's no way to retrieve it. Modifications: - Add a helper class to hide the implementation detail. - Set the `InstanceInfo` to the `Endpoint` as an attribute. Result: - Closes #6056 - Now users can retrieve it. --- .../client/eureka/EurekaEndpointGroup.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/eureka/src/main/java/com/linecorp/armeria/client/eureka/EurekaEndpointGroup.java b/eureka/src/main/java/com/linecorp/armeria/client/eureka/EurekaEndpointGroup.java index 631817fd1a7..59c6becab2a 100644 --- a/eureka/src/main/java/com/linecorp/armeria/client/eureka/EurekaEndpointGroup.java +++ b/eureka/src/main/java/com/linecorp/armeria/client/eureka/EurekaEndpointGroup.java @@ -66,6 +66,7 @@ import com.linecorp.armeria.server.eureka.EurekaUpdatingListener; import io.netty.channel.EventLoop; +import io.netty.util.AttributeKey; import io.netty.util.concurrent.ScheduledFuture; /** @@ -399,6 +400,24 @@ public List apply(byte[] content) { } } + private static final class EurekaInstanceInfoUtil { + + private static final AttributeKey INSTANCE_INFO = AttributeKey.valueOf( + EurekaInstanceInfoUtil.class, "INSTANCE_INFO"); + + @Nullable + static InstanceInfo get(Endpoint endpoint) { + requireNonNull(endpoint, "endpoint"); + return endpoint.attr(INSTANCE_INFO); + } + + static Endpoint with(Endpoint endpoint, InstanceInfo instanceInfo) { + requireNonNull(endpoint, "endpoint"); + requireNonNull(instanceInfo, "instanceInfo"); + return endpoint.withAttr(INSTANCE_INFO, instanceInfo); + } + } + private static Endpoint endpoint(InstanceInfo instanceInfo, boolean secureVip) { final String hostname = instanceInfo.getHostName(); final PortWrapper portWrapper = instanceInfo.getPort(); @@ -415,7 +434,7 @@ private static Endpoint endpoint(InstanceInfo instanceInfo, boolean secureVip) { if (ipAddr != null && hostname != ipAddr) { endpoint = endpoint.withIpAddr(ipAddr); } - return endpoint; + return EurekaInstanceInfoUtil.with(endpoint, instanceInfo); } @Override