title: Override spring-boot EurekaInstanceConfigBean
body: <p>I am building a dockerized spring-cloud based microservice that registers with eureka. Part of the registration process is asking the host for the port mapped to the container so docker can choose a free host port for the containerized service.</p>
  
  <p>I have a host based service the dockerized service can ask for the port mapping and am now trying to register the microservice with eureka using the external port. </p>
  
  <p>I get the right port inside my microservice but am unable to override the <code>EurekaInstanceConfig</code>. </p>
  
  <p>What i have tried:</p>
  
  <pre><code>@SpringBootApplication
  @EnableEurekaClient
  public class ApplicationBootstrapper {
  
      @Value("${containerIp}")
      private String containerIp;
  
      @Bean
      public EurekaInstanceConfigBean eurekaInstanceConfigBean() {
          EurekaInstanceConfigBean config = new EurekaInstanceConfigBean();
          String hostPort = new RestTemplate().getForObject(
              "http://{hostname}:7691/container/{id}/hostPort", 
              String.class, 
              containerIp, 
              config.getHostname());
  
          config.setPreferIpAddress(true);
          config.setIpAddress(containerIp);
          config.setNonSecurePort(Integer.valueOf(hostPort));
  
          return config;
  }
  </code></pre>
  
  <p>My custom <code>EurekaInstanceConfigBean</code> gets created but the configuration is not picked up (the service registers with its internal container port). </p>
  
  <p>The question is: How do i override the <code>EurekaInstanceConfigBean</code>?</p>
  
  <p><strong>EDIT (2):</strong></p>
  
  <p>As Steve pointed out and now as spring-cloud-1.0.0.RELEASE is available most of my previous solution is now obsolete. I've attached my final solution in case anyone is trying something similar:</p>
  
  <pre><code>@Configuration
  public class EurekaConfig {
  
      private static final Log logger = LogFactory.getLog(EurekaConfig.class);
  
      @Value("${containerIp}")
      private String containerIp;
  
      @Value("${kompositPort:7691}")
      private String kompositPort;
  
      @Bean
      public EurekaInstanceConfigBean eurekaInstanceConfigBean() {
          Integer hostPort = new RestTemplate().getForObject(
              "http://{containerIp}:{port}/container/{instanceId}/hostPort",
              Integer.class, 
              containerIp, 
              kompositPort, 
              getHostname());
  
          EurekaInstanceConfigBean config = new EurekaInstanceConfigBean();
          config.setNonSecurePort(hostPort);
          config.setPreferIpAddress(true);
          config.setIpAddress(containerIp);
          config.getMetadataMap().put("instanceId", getHostname());
          return config;
      }
  
      private static String getHostname() {
          String hostname = null;
          try {
              hostname = InetAddress.getLocalHost().getHostName();
          } catch (UnknownHostException e) {
              logger.error("Cannot get host info", e);
          }
          return hostname;
      }
  
  }
  </code></pre>