Spring Cloud Netflix | Eureka not registering when

2019-08-28 06:39发布

问题:

I am trying to deploy Spring Netflix Eureka and related microservice application using ECS and Cloudformation.

Eureka is not able to register the related microservices because the docker images are not able to link on hostname.

Please suggest what should be the best solution to handle this.

回答1:

You should use EC2 instance's host ip instead of docker container host's. In your microservices (if those are spring boot applications), put this code:

@Bean
@Profile("docker")
public EurekaInstanceConfigBean eurekaInstanceConfig() {
    EurekaInstanceConfigBean config = new EurekaInstanceConfigBean();
    AmazonInfo info = AmazonInfo.Builder.newBuilder().autoBuild("eureka");
    config.setDataCenterInfo(info);
    info.getMetadata().put(AmazonInfo.MetaDataKey.publicHostname.getName(), info.get(AmazonInfo.MetaDataKey.publicIpv4));
    config.setHostname(info.get(AmazonInfo.MetaDataKey.publicHostname));
    config.setIpAddress(info.get(AmazonInfo.MetaDataKey.publicIpv4));
    config.setNonSecurePort(port);
    return config;
}

Remember to enable 'docker' profile in spring boot configuration.