title: Eureka - works on localhost, but problems when on different hosts
body: <p>I am attempting to use Netflix Eureka to help with the management of services. I started by developing a simple eureka server and then two services: a client and a server. The server provides REST endpoints that the client will call.</p>
  
  <p>When I run everything on localhost, it all works beautifully. The services register themselves with the eureka server and the client successfully invokes the server. When I open <code>http://localhost:8761</code> in a browser I get what I would expect to see:</p>
  
  <p><a href="https://i.stack.imgur.com/wLASk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wLASk.png" alt="enter image description here"></a></p>
  
  <p>I then decided to make it more realistic by separating these applications onto separate machines (these are Linux VMs managed by vagrant). As far as I can tell, the server and client are registering themselves with the eureka-server, but I cannot get the client to successfully invoke the server.</p>
  
  <p>From what I've been able to find in my searching, the URL passed to a <code>RestTemplate</code> instance to make the REST call needs to only specify the "ID" of the service to be called. There's no need for a host name or IP address or port - which is the point, yes?</p>
  
  <p>It seems so close, but is not working. I'd like to know why (and what I've got to do to <em>get</em> it working).</p>
  
  <p>Here's the code.</p>
  
  <p><strong>Eureka Server</strong></p>
  
  <p>EurekaServiceApplication.java</p>
  
  <pre><code>@EnableEurekaServer
  @SpringBootApplication
  public class EurekaServiceApplication {
    public static void main(String[] args) {
      SpringApplication.run(EurekaServiceApplication.class, args);
    }
  }
  </code></pre>
  
  <p>application.yml:</p>
  
  <pre><code>server:
    port: 8761
  
  eureka:
    client:
      register-with-eureka: false
      fetch-registry: false
  
  logging:
    level:
      com:
        netflix:
          eureka: OFF
          discovery: OFF
  </code></pre>
  
  <p>Can't get much simpler than that.</p>
  
  <p><strong>Server</strong></p>
  
  <p>ApplicationMain.java:</p>
  
  <pre><code>@EnableDiscoveryClient
  @SpringBootApplication
  public class ApplicationMain {
    public static void main(String[] args) {
      SpringApplication.run(ApplicationMain.class, args);
    }
  }
  </code></pre>
  
  <p>Server.java:</p>
  
  <pre><code>@Service
  public class Server {
    private final Logger                   logger = LogManager.getLogger();
    @Autowired
    private ConfigurableApplicationContext context;
  
    public String getID() {
      return "server";
    }
  }
  </code></pre>
  
  <p>ServerController.java:</p>
  
  <pre><code>@RestController
  @RequestMapping("/v1/server")
  public class ServerController {
  
    @Autowired
    private Server          service;
    @Autowired
    private DiscoveryClient dicoveryClient;
  
    @GetMapping(value = "/id", produces = {MediaType.APPLICATION_JSON_VALUE})
    public DeferredResult&lt;ResponseEntity&lt;GeneralResponse&gt;&gt; getID() {
      DeferredResult&lt;ResponseEntity&lt;GeneralResponse&gt;&gt; result = new DeferredResult&lt;&gt;();
      GeneralResponse genResp = new GeneralResponse();
      genResp.setMessage(service.getID());
      result.setResult(ResponseEntity.ok(genResp));
      return result;
    }
  
    @RequestMapping("/instances")
    public List&lt;ServiceInstance&gt; instances() {
      return dicoveryClient.getInstances("SERVER");
    }
  
  }
  </code></pre>
  
  <p>bootstrap.yml:</p>
  
  <pre><code>spring:
    application:
      name: server
  </code></pre>
  
  <p>application.yml:</p>
  
  <pre><code>server:
    port: 8091
  
  eureka:
    client:
      serviceUrl:
        defaultZone: http://localhost:8761/eureka
  </code></pre>
  
  <p><strong>Client</strong></p>
  
  <p>ApplicationMain.java:</p>
  
  <pre><code>@EnableDiscoveryClient
  @SpringBootApplication
  public class ApplicationMain {
    public static void main(String[] args) {
      SpringApplication.run(ApplicationMain.class, args);
    }
  }
  </code></pre>
  
  <p>Client.java:</p>
  
  <pre><code>@Service
  public class Client {
  
    private final Logger logger = LogManager.getLogger();
  
    @Autowired
    private RestTemplate restTemplate;
  
    @Scheduled(fixedRateString = "10000")
    public void callServer() {
      String url = "http://server/v1/server/id";
      ResponseEntity&lt;GeneralResponse&gt; response =
          restTemplate.getForEntity(url, GeneralResponse.class);
      logger.info("response: {}", response.getBody().getMessage());
    }
  
  }
  </code></pre>
  
  <p>bootstrap.yml:</p>
  
  <pre><code>spring:
    application:
      name: client
  </code></pre>
  
  <p>application.yml:</p>
  
  <pre><code>server:
    port: 8092
  
  eureka:
    client:
      serviceUrl:
        defaultZone: http://localhost:8761/eureka
  </code></pre>
  
  <p>For the case where I run these applications on three separate machines, I edit the <code>application.yml</code> for the server and client to change <code>localhost</code> to the IP address of the machine the eureka server is running on.</p>
  
  <p>Here is the exception thrown on the client:</p>
  
  <pre><code>2019-11-27 14:34:11.991  INFO 11807 --- [   scheduling-1] c.n.l.BaseLoadBalancer                   : Client: server instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=server,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
  2019-11-27 14:34:12.000  INFO 11807 --- [   scheduling-1] c.n.l.DynamicServerListLoadBalancer      : Using serverListUpdater PollingServerListUpdater
  2019-11-27 14:34:12.038  INFO 11807 --- [   scheduling-1] c.n.c.ChainedDynamicProperty             : Flipping property: server.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
  2019-11-27 14:34:12.041  INFO 11807 --- [   scheduling-1] c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client server initialized: DynamicServerListLoadBaActive connections count: 0;servCircuit breaker tripped count: 0;host:80Active connections per server: 0.0;]{defaultzone=[Zone:defaultzone;     Instance },Server stats: average resp time:0.0;1;90 percentile resp time:0.0;tion95 percentile resp time:0.0;ST 1min resp time:0.0;nectiomax resp time:0.0;ure costddev resp time:0.0]
  ]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@af0e207
  2019-11-27 14:34:12.098 ERROR 11807 --- [   scheduling-1] o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task.
  
  org.springframework.web.client.ResourceAccessException: I/O error on GET request for "http://server/v1/server/id": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
          at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:744) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
          at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:670) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
          at org.springframework.web.client.RestTemplate.getForEntity(RestTemplate.java:338) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
          at com.example.Client.callServer(Client.java:66) ~[client-0.0.1-SNAPSHOT.jar:0.0.1-SNAPSHOT]
          at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_232]
          at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_232]
          at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_232]
          at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_232]
          at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
          at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) [spring-context-5.1.8.RELEASE.jar:5.1.8.RELEASE]
          at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_232]
          at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) [?:1.8.0_232]
          at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [?:1.8.0_232]
          at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [?:1.8.0_232]
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_232]
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_232]
          at java.lang.Thread.run(Thread.java:748) [?:1.8.0_232]
  Caused by: java.net.ConnectException: Connection refused (Connection refused)
          at java.net.PlainSocketImpl.socketConnect(Native Method) ~[?:1.8.0_232]
          at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[?:1.8.0_232]
          at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[?:1.8.0_232]
          at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[?:1.8.0_232]
          at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[?:1.8.0_232]
          at java.net.Socket.connect(Socket.java:607) ~[?:1.8.0_232]
          at java.net.Socket.connect(Socket.java:556) ~[?:1.8.0_232]
          at sun.net.NetworkClient.doConnect(NetworkClient.java:180) ~[?:1.8.0_232]
          at sun.net.www.http.HttpClient.openServer(HttpClient.java:463) ~[?:1.8.0_232]
          at sun.net.www.http.HttpClient.openServer(HttpClient.java:558) ~[?:1.8.0_232]
          at sun.net.www.http.HttpClient.&lt;init&gt;(HttpClient.java:242) ~[?:1.8.0_232]
          at sun.net.www.http.HttpClient.New(HttpClient.java:339) ~[?:1.8.0_232]
          at sun.net.www.http.HttpClient.New(HttpClient.java:357) ~[?:1.8.0_232]
          at sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1226) ~[?:1.8.0_232]
          at sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1162) ~[?:1.8.0_232]
          at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1056) ~[?:1.8.0_232]
          at sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:990) ~[?:1.8.0_232]
          at org.springframework.http.client.SimpleBufferingClientHttpRequest.executeInternal(SimpleBufferingClientHttpRequest.java:76) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
          at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
          at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
          at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:108) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
          at org.springframework.cloud.client.loadbalancer.LoadBalancerRequestFactory.lambda$createRequest$0(LoadBalancerRequestFactory.java:59) ~[spring-cloud-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE]
          at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:112) ~[spring-cloud-netflix-ribbon-2.0.2.RELEASE.jar:2.0.2.RELEASE]
          at org.springframework.cloud.netflix.ribbon.RibbonLoadBalancerClient.execute(RibbonLoadBalancerClient.java:94) ~[spring-cloud-netflix-ribbon-2.0.2.RELEASE.jar:2.0.2.RELEASE]
          at org.springframework.cloud.client.loadbalancer.LoadBalancerInterceptor.intercept(LoadBalancerInterceptor.java:55) ~[spring-cloud-commons-2.0.2.RELEASE.jar:2.0.2.RELEASE]
          at org.springframework.http.client.InterceptingClientHttpRequest$InterceptingRequestExecution.execute(InterceptingClientHttpRequest.java:92) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
          at org.springframework.http.client.InterceptingClientHttpRequest.executeInternal(InterceptingClientHttpRequest.java:76) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
          at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(AbstractBufferingClientHttpRequest.java:48) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
          at org.springframework.http.client.AbstractClientHttpRequest.execute(AbstractClientHttpRequest.java:53) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
          at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:735) ~[spring-web-5.1.8.RELEASE.jar:5.1.8.RELEASE]
          ... 16 more
  </code></pre>
  
  <p><strong>UPDATE</strong>:</p>
  
  <p>ClientConfig.java:</p>
  
  <pre><code>@Configuration
  @EnableScheduling
  public class ClientConfig {
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
      return new RestTemplate();
    }
  }
  </code></pre>
  
  <p><strong>UPDATE</strong>:</p>
  
  <p>My work environment has a web proxy in place which has been known to cause connectivity issues in the past. After a little more research I discovered there are some eureka properties that allow for the specification of a proxy server.</p>
  
  <p>So, I set the following eureka properties to see if it would help. It did not.</p>
  
  <pre><code>eureka:
    client:
      register-with-eureka: false
      fetch-registry: false
      proxy-host: &lt;proxy server&gt;
      proxy-port: &lt;proxy port&gt;
  </code></pre>