spring-cloud / spring-cloud-netflix

Integration with Netflix OSS components
http://cloud.spring.io/spring-cloud-netflix/
Apache License 2.0
4.87k stars 2.44k forks source link

Unable to configure Eureka port #3889

Open dlvenable opened 4 years ago

dlvenable commented 4 years ago

We need to be able to run our services with dynamic ports in Amazon ECS. We have an internal Spring Boot starter which is able to successfully get the ECS port and set it on a new EurekaInstanceConfigBean bean.

However, the EurekaAutoServiceRegistration class is overriding this value.

Here are our log files. You can see that our starter is finding port 32768. However, you can also see where EurekaAutoServiceRegistration sets it afterward.

2020-09-09 16:18:58.021  INFO [,] 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8001 (http)
2020-09-09 16:18:58.987 DEBUG [,] 1 --- [           main] c.a.s.cloud.boot.EurekaAutoConfigure     : Received host port 32768 from ECS container metadata.
2020-09-09 16:18:58.988  INFO [,] 1 --- [           main] c.a.s.cloud.boot.EurekaAutoConfigure     : Setting non-secure port to 32768
2020-09-09 16:19:02.370  INFO [,] 1 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8001 (http) with context path ''
2020-09-09 16:19:02.375  INFO [,] 1 --- [           main] .s.c.n.e.s.EurekaAutoServiceRegistration : Updating port to 8001 

I believe the relevant code in Spring Cloud Eureka is this: https://github.com/spring-cloud/spring-cloud-netflix/blob/master/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/serviceregistry/EurekaAutoServiceRegistration.java#L139-L150

It seems that this registration should not be attempting to override an existing configuration. Also, since it running based on a, WebServerInitializedEvent, we can't use Spring Boot's configure-after controls.

spencergibb commented 4 years ago

yeah, if the port is set by properties and it is greater than zero we should use that.

dlvenable commented 4 years ago

Thank you for the response. For additional information, here is a snippet from where we do our configuration.

You can see that our log line "Setting non-secure port to {}" is being reached in the logs above.

   @Value("${server.port:#{null}}")
    private Integer port;

    @Bean
    @Profile("production")
    public EurekaInstanceConfigBean eurekaInstanceConfig(InetUtils inetUtils)
    {
        EurekaInstanceConfigBean eurekaConfig = new EurekaInstanceConfigBean(inetUtils);
        AmazonInfo awsInfo = AmazonInfo.Builder.newBuilder().autoBuild("eureka");
        eurekaConfig.setDataCenterInfo(awsInfo);
        eurekaConfig.setIpAddress(awsInfo.get(AmazonInfo.MetaDataKey.localIpv4));
        eurekaConfig.setHostname(awsInfo.get(AmazonInfo.MetaDataKey.localHostname));

        Integer port = getInstancePort();  // This is just a method to get the port from AWS ECS
        if(port != null)
        {
            log.info("Setting non-secure port to {}", port);
            eurekaConfig.setNonSecurePort(port);
        }

        return eurekaConfig;
    }