3
votes

I am trying to create a simple eureka server which will detect other microservices and all other microservice can talk through it. I am using spring boot 2.0.3.

My eureka server

EurekaServerApplication.java file (eureka server)

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

HelloServerApplication application.property (eureka server)

spring.application.name=eureka-server
server.port=8070

application.yml (eureka server)

eureka:
  client:
    registerWithEureka: false
    fetchRegistry: false
    server:
      waitTimeInMsWhenSyncEmpty: 0

My eureka client

HelloServerApplication.java (eureka client)

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


@EnableDiscoveryClient
@SpringBootApplication
public class HelloServerApplication {

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

application.yml (eureka client)

spring:
  application:
    name: hello-server

server:
  port: 8071

eureka:
  client:
    registerWithEureka: true
    fetchRegistry: true
    serviceUrl:
      defaultZone: http://localhost:8070/eureka/
  instance:
    hostname: localhost

When I run the eureka server, I can access the server at http://localhost:8070/ but when I try to run a eureka client HelloServerApplication.java the eureka server shows the following error and the eureka client shuts down-

Error on eureka server

2018-06-25 17:26:30.250 ERROR 4635 --- [get_localhost-3] c.n.e.cluster.ReplicationTaskProcessor   : Network level connection to peer localhost; retrying after delay

com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused (Connection refused)
    at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187) ~[jersey-apache-client4-1.19.1.jar:1.19.1]
    at com.netflix.eureka.cluster.DynamicGZIPContentEncodingFilter.handle(DynamicGZIPContentEncodingFilter.java:48) ~[eureka-core-1.9.2.jar:1.9.2]
    at com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFilter.java:27) ~[eureka-client-1.9.2.jar:1.9.2]
    at com.sun.jersey.api.client.Client.handle(Client.java:652) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:570) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.netflix.eureka.transport.JerseyReplicationClient.submitBatchUpdates(JerseyReplicationClient.java:116) ~[eureka-core-1.9.2.jar:1.9.2]
    at com.netflix.eureka.cluster.ReplicationTaskProcessor.process(ReplicationTaskProcessor.java:80) ~[eureka-core-1.9.2.jar:1.9.2]
    at com.netflix.eureka.util.batcher.TaskExecutors$BatchWorkerRunnable.run(TaskExecutors.java:187) [eureka-core-1.9.2.jar:1.9.2]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_171]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_171]
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_171]
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_171]
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_171]
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_171]
    at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_171]
    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:121) ~[httpclient-4.5.5.jar:4.5.5]
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180) ~[httpclient-4.5.5.jar:4.5.5]
    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:144) ~[httpclient-4.5.5.jar:4.5.5]
    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:134) ~[httpclient-4.5.5.jar:4.5.5]
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610) ~[httpclient-4.5.5.jar:4.5.5]
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445) ~[httpclient-4.5.5.jar:4.5.5]
    at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:835) ~[httpclient-4.5.5.jar:4.5.5]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:118) ~[httpclient-4.5.5.jar:4.5.5]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56) ~[httpclient-4.5.5.jar:4.5.5]
    at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:173) ~[jersey-apache-client4-1.19.1.jar:1.19.1]
    ... 10 common frames omitted

I tried moving all properties on both the sides to bootstrap.application file, that did not work.

I tried this solution: https://github.com/spring-cloud/spring-cloud-netflix/issues/2893, but it did not work.

3
are you missing ` defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/` on the server?Craig Smith
@CraigSmith No, I have added it in the application.yml file of HelloServer as serviceUrl: defaultZone: http://localhost:8070/eureka/Sunil
please check out stackoverflow.com/questions/45615866/… looks like you need to have it on both client and server.Craig Smith
@CraigSmith I added the serviceUrl: defaultZone: http://localhost:8070/eureka/ in both the yml files (server/client). Now the server does not show huge exception message but shows - 2018-06-25 18:50:27.352 INFO 9171 --- [nio-8070-exec-2] c.n.e.registry.AbstractInstanceRegistry : Registered instance HELLO-SERVER/192.168.1.201:hello-server:8071 with status UP (replication=false) 2018-06-25 18:50:27.414 INFO 9171 --- [nio-8070-exec-3] c.n.e.registry.AbstractInstanceRegistry : Registered instance HELLO-SERVER/192.168.1.201:hello-server:8071 with status DOWN (replication=false)Sunil
why do you have both application.properties and application.yml? You should probably choose one or the other.Craig Smith

3 Answers

2
votes

The eureka configuration in both the server and client side is very sensitive. The service url and default zone configuration should be written as follow :

 service-url (Not serviceUrl):
   defaultZone(Not default-zone) : http://localhost:portNumberHere/eureka
0
votes

It seems that eureka tries to connect to itself despite the below settings: eureka.client.register-with-eureka=false eureka.client.fetch-registry=false you have two options to fix that 1-Keep the default port of eureka 2-Add below to your application.properties of your eureka server eureka.server.maxThreadsForPeerReplication=0

0
votes

Following configuration in YAML and application.properties file worked for me.

naming-server : name of the configuration in YAML file.

application.yml

eureka.client.service-url.defaultZone: http://naming-server:8761/eureka

Also, in Eureka property file make sure you have put below things as stated in above answer

application.property

eureka.client.register-with-eureka = false
eureka.client.fetch-registry = false