Skip to content

Commit

Permalink
feat(openchallenges): update the Eureka config of the challenge and i…
Browse files Browse the repository at this point in the history
…mage services (#2768)
  • Loading branch information
tschaffter authored Aug 22, 2024
1 parent 2199038 commit 14b1889
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,24 +16,35 @@ 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 <a
* href="https://docs.spring.io/spring-cloud-netflix/docs/current/reference/html/#using-eureka-on-aws">Using
* Eureka on AWS</a>
* 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</a>
*/
@Bean
@ConditionalOnProperty(
value = "openchallenges-challenge-service.is-deployed-on-aws",
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;
}
}
Original file line number Diff line number Diff line change
@@ -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 <a
* 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</a>
*/
@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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 14b1889

Please sign in to comment.