Describe the bug I have two applications on my local, one runs on 8080 and another runs on 9090. both port is defined in application.yml. they both register themself to a eureka server. however the app runs on 9090 will always register itself running on port 8080 on eureka server. I put a breakpoint in class EurekaClientAutoConfiguration https://github.com/spring-cloud/spring-cloud-netflix/blob/aee83fb1132f33ae4bb798536c6e0cabf368c880/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.java#L140 and find that the serverport is always falling back to 8080. I then try to move the port setting to bootstrap.yml and it's no luck. I have to fix it by passing the server.port from running command or hardcoding System.setProperty("port", "9090"); in main method. then it changes to 9090.

On the other hand, the app running on port 8080, I can easily change its port registering on eureka server by just change the server.port property of application.yml.

Is this a bug or did I do something wrong?

Sample The settings:

Application8080.java:

@EnableEurekaClient
public class Application8080 {

    public static void main(String[] args) {
        SpringApplication.run(Application8080.class, args);
    }
}

application.yml:

server:
  port: 8080
eureka:
  client:
    enabled: true
    serviceUrl:
      defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
  instance:
    preferIpAddress: true
    lease-renewal-interval-in-seconds: 10

for Application8080, it is also using LoadBalancer

Application9090.java is the same application.yml:

eureka:
  client:
    enabled: true
    serviceUrl:
      defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}
  instance:
    preferIpAddress: true
    instance-id: ${spring.cloud.client.hostname}:${spring.application.name}:${spring.application.instance_id:${server.port}}

bootstrap.yml:

server:
  port: 9090

Comment From: wunaidage

found the reason. if you have the same issue, check if somewhere in your code defines an Environment bean. if it is incorrectly defined and initialized, it will wipe all environment variables that eureka need to initialize the bean. so everything fallback to default: 8080.