From 14b18892874f8f13985b40de02bdfc818b73ae86 Mon Sep 17 00:00:00 2001 From: Thomas Schaffter Date: Thu, 22 Aug 2024 14:23:43 -0700 Subject: [PATCH] feat(openchallenges): update the Eureka config of the challenge and image services (#2768) --- .../EurekaInstanceConfiguration.java | 25 +++++++--- .../EurekaInstanceConfiguration.java | 50 +++++++++++++++++++ .../EurekaInstanceConfiguration.java | 2 +- 3 files changed, 70 insertions(+), 7 deletions(-) create mode 100644 apps/openchallenges/image-service/src/main/java/org/sagebionetworks/openchallenges/image/service/configuration/EurekaInstanceConfiguration.java diff --git a/apps/openchallenges/challenge-service/src/main/java/org/sagebionetworks/openchallenges/challenge/service/configuration/EurekaInstanceConfiguration.java b/apps/openchallenges/challenge-service/src/main/java/org/sagebionetworks/openchallenges/challenge/service/configuration/EurekaInstanceConfiguration.java index 4e734328d7..18c39b670e 100644 --- a/apps/openchallenges/challenge-service/src/main/java/org/sagebionetworks/openchallenges/challenge/service/configuration/EurekaInstanceConfiguration.java +++ b/apps/openchallenges/challenge-service/src/main/java/org/sagebionetworks/openchallenges/challenge/service/configuration/EurekaInstanceConfiguration.java @@ -1,8 +1,10 @@ package org.sagebionetworks.openchallenges.challenge.service.configuration; -import com.netflix.appinfo.AmazonInfo; +import java.net.InetAddress; +import java.net.UnknownHostException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.cloud.commons.util.InetUtils; import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean; @@ -14,13 +16,16 @@ public class EurekaInstanceConfiguration { private static final Logger LOGGER = LoggerFactory.getLogger(EurekaInstanceConfiguration.class); + @Value("${server.port}") + private String port; + /** * If the application is planned to be deployed to an AWS cloud, the Eureka instance must be * configured to be AWS-aware. This can be done by customizing the EurekaInstanceConfigBean. * * @see Using - * Eureka on AWS + * href="https://medium.com/@harsugangwar/microservices-with-eureka-on-aws-ecs-a-guide-to-service-discovery-and-ci-with-aws-codebuild-0478a4a71ba2">Microservices + * with Eureka on AWS ECS: A Guide to Service Discovery and CI with AWS CodeBuild */ @Bean @ConditionalOnProperty( @@ -28,10 +33,18 @@ public class EurekaInstanceConfiguration { havingValue = "true", matchIfMissing = false) public EurekaInstanceConfigBean eurekaInstanceConfig(InetUtils inetUtils) { - LOGGER.info("Configuring the Eureka instance to be AWS-aware."); + LOGGER.info("Configuring the Eureka instance for AWS."); EurekaInstanceConfigBean bean = new EurekaInstanceConfigBean(inetUtils); - AmazonInfo info = AmazonInfo.Builder.newBuilder().autoBuild("eureka"); - bean.setDataCenterInfo(info); + String ip = null; + try { + ip = InetAddress.getLocalHost().getHostAddress(); + } catch (UnknownHostException e) { + LOGGER.error("Unable to get the host address.", e); + } + bean.setIpAddress(ip); + bean.setPreferIpAddress(true); + bean.setNonSecurePortEnabled(true); + bean.setNonSecurePort(Integer.parseInt(port)); return bean; } } diff --git a/apps/openchallenges/image-service/src/main/java/org/sagebionetworks/openchallenges/image/service/configuration/EurekaInstanceConfiguration.java b/apps/openchallenges/image-service/src/main/java/org/sagebionetworks/openchallenges/image/service/configuration/EurekaInstanceConfiguration.java new file mode 100644 index 0000000000..ba944aab4d --- /dev/null +++ b/apps/openchallenges/image-service/src/main/java/org/sagebionetworks/openchallenges/image/service/configuration/EurekaInstanceConfiguration.java @@ -0,0 +1,50 @@ +package org.sagebionetworks.openchallenges.image.service.configuration; + +import java.net.InetAddress; +import java.net.UnknownHostException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.cloud.commons.util.InetUtils; +import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class EurekaInstanceConfiguration { + + private static final Logger LOGGER = LoggerFactory.getLogger(EurekaInstanceConfiguration.class); + + @Value("${server.port}") + private String port; + + /** + * If the application is planned to be deployed to an AWS cloud, the Eureka instance must be + * configured to be AWS-aware. This can be done by customizing the EurekaInstanceConfigBean. + * + * @see Microservices + * with Eureka on AWS ECS: A Guide to Service Discovery and CI with AWS CodeBuild + */ + @Bean + @ConditionalOnProperty( + value = "openchallenges-image-service.is-deployed-on-aws", + havingValue = "true", + matchIfMissing = false) + public EurekaInstanceConfigBean eurekaInstanceConfig(InetUtils inetUtils) { + LOGGER.info("Configuring the Eureka instance for AWS."); + EurekaInstanceConfigBean bean = new EurekaInstanceConfigBean(inetUtils); + String ip = null; + try { + ip = InetAddress.getLocalHost().getHostAddress(); + } catch (UnknownHostException e) { + LOGGER.error("Unable to get the host address.", e); + } + bean.setIpAddress(ip); + bean.setPreferIpAddress(true); + bean.setNonSecurePortEnabled(true); + bean.setNonSecurePort(Integer.parseInt(port)); + return bean; + } +} diff --git a/apps/openchallenges/organization-service/src/main/java/org/sagebionetworks/openchallenges/organization/service/configuration/EurekaInstanceConfiguration.java b/apps/openchallenges/organization-service/src/main/java/org/sagebionetworks/openchallenges/organization/service/configuration/EurekaInstanceConfiguration.java index 934a252a0a..29798c18b6 100644 --- a/apps/openchallenges/organization-service/src/main/java/org/sagebionetworks/openchallenges/organization/service/configuration/EurekaInstanceConfiguration.java +++ b/apps/openchallenges/organization-service/src/main/java/org/sagebionetworks/openchallenges/organization/service/configuration/EurekaInstanceConfiguration.java @@ -33,7 +33,7 @@ public class EurekaInstanceConfiguration { havingValue = "true", matchIfMissing = false) public EurekaInstanceConfigBean eurekaInstanceConfig(InetUtils inetUtils) { - LOGGER.info("Configuring the Eureka instance to be AWS-aware."); + LOGGER.info("Configuring the Eureka instance for AWS."); EurekaInstanceConfigBean bean = new EurekaInstanceConfigBean(inetUtils); String ip = null; try {