I have introduced you to Microservices and the great benefits it brings. The process of building a Microservices-style application will have many challenges, and one of those challenges is how to manage the services created, including their maintenance, scalability, and high availability. The larger the application, the more difficult it will be to manage these services.
To solve this problem, a concept called Service registration and discovery was introduced, which defines a server that can be used for services to register and manage their information.
This server will be responsible for storing and providing information about services in a Microservices system. When a service registers information with the server, it will provide information such as its host, port, and status. And it also regularly sends heartbeat messages to notify the server of its status. Therefore, this server can easily provide information about services when they are called from other services.
These are the basic points about the Eureka Server of Netflix OSS (Open Source Software) that I want to mention in this tutorial!
In this tutorial, I will guide you on how to set up to run an Eureka Server using Spring Cloud Netflix!
First, create a Spring Boot project with the Eureka Server dependency.
My result is as follows:
Next, go to the main class of our Spring Boot application and declare an annotation named @EnableEurekaServer as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
package com.huongdanjava.springcloud.netflix.eurekaserver; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @EnableEurekaServer @SpringBootApplication public class SpringCloudEurekaServerApplication { public static void main(String[] args) { SpringApplication.run(SpringCloudEurekaServerApplication.class, args); } } |
The @EnableEurekaServer annotation will let Spring Boot know that we need to run an Eureka Server using Netflix OSS’s Eureka Server, exposing endpoints so that services can register, discover, and send heartbeats.
Now run the application, request to http://localhost:8080/, you will see the following result:
The “Instances currently registered with Eureka” section will contain information about all services registered with this Eureka Server. Since there are currently no services registered, you will not see anything here.
If you pay attention to the console log, you will see the following error:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
2025-07-06T17:47:11.712+08:00 INFO 49060 --- [foReplicator-%d] c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error. endpoint=DefaultEndpoint{ serviceUrl='http://localhost:8761/eureka/}, exception=java.net.ConnectException: Connection refused stacktrace=jakarta.ws.rs.ProcessingException: java.net.ConnectException: Connection refused at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:288) at org.glassfish.jersey.client.ClientRuntime.invoke(ClientRuntime.java:300) at org.glassfish.jersey.client.JerseyInvocation.lambda$invoke$0(JerseyInvocation.java:674) at org.glassfish.jersey.client.JerseyInvocation.call(JerseyInvocation.java:709) at org.glassfish.jersey.client.JerseyInvocation.lambda$runInScope$3(JerseyInvocation.java:703) at org.glassfish.jersey.internal.Errors.process(Errors.java:292) at org.glassfish.jersey.internal.Errors.process(Errors.java:274) at org.glassfish.jersey.internal.Errors.process(Errors.java:205) at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:391) at org.glassfish.jersey.client.JerseyInvocation.runInScope(JerseyInvocation.java:703) at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:673) at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:439) at org.glassfish.jersey.client.JerseyInvocation$Builder.post(JerseyInvocation.java:345) at com.netflix.discovery.shared.transport.jersey3.AbstractJersey3EurekaHttpClient.register(AbstractJersey3EurekaHttpClient.java:99) at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$1.execute(EurekaHttpClientDecorator.java:59) at com.netflix.discovery.shared.transport.decorator.MetricsCollectingEurekaHttpClient.execute(MetricsCollectingEurekaHttpClient.java:70) at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$1.execute(EurekaHttpClientDecorator.java:59) at com.netflix.discovery.shared.transport.decorator.RedirectingEurekaHttpClient.executeOnNewServer(RedirectingEurekaHttpClient.java:121) at com.netflix.discovery.shared.transport.decorator.RedirectingEurekaHttpClient.execute(RedirectingEurekaHttpClient.java:80) at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$1.execute(EurekaHttpClientDecorator.java:59) at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:120) at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$1.execute(EurekaHttpClientDecorator.java:59) at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:76) at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.register(EurekaHttpClientDecorator.java:56) at com.netflix.discovery.DiscoveryClient.register(DiscoveryClient.java:828) at com.netflix.discovery.InstanceInfoReplicator.run(InstanceInfoReplicator.java:125) at com.netflix.discovery.InstanceInfoReplicator$2.run(InstanceInfoReplicator.java:105) at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:572) at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317) at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642) at java.base/java.lang.Thread.run(Thread.java:1583) Caused by: java.net.ConnectException: Connection refused at java.base/sun.nio.ch.Net.pollConnect(Native Method) at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:682) at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:549) at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:592) at java.base/java.net.Socket.connect(Socket.java:751) at java.base/sun.net.NetworkClient.doConnect(NetworkClient.java:178) at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:531) at java.base/sun.net.www.http.HttpClient.openServer(HttpClient.java:636) at java.base/sun.net.www.http.HttpClient.<init>(HttpClient.java:280) at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:386) at java.base/sun.net.www.http.HttpClient.New(HttpClient.java:408) at java.base/sun.net.www.protocol.http.HttpURLConnection.getNewHttpClient(HttpURLConnection.java:1304) at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect0(HttpURLConnection.java:1237) at java.base/sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:1123) at java.base/sun.net.www.protocol.http.HttpURLConnection.connect(HttpURLConnection.java:1052) at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1675) at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1599) at java.base/java.net.HttpURLConnection.getResponseCode(HttpURLConnection.java:531) at org.glassfish.jersey.client.internal.HttpUrlConnector.handleException(HttpUrlConnector.java:624) at org.glassfish.jersey.client.internal.HttpUrlConnector._apply(HttpUrlConnector.java:450) at org.glassfish.jersey.client.internal.HttpUrlConnector.apply(HttpUrlConnector.java:286) ... 35 more |
The reason is that by default, this Eureka Server also acts as a service, also known as an Eureka Client. This Eureka Client will automatically, every 30 seconds, call the address http://localhost:8761/eureka/ to register itself with the Eureka Server if it has not registered yet and to discover other services.
The above error occurs because the default port of Eureka Server is 8761, but here, my application is using the default port of Tomcat, which is 8080. When calling port 8761, there will be an error.
To fix this error, please configure the port to be the same as the default port of the Eureka Server by adding the server.port property to the application’s configuration file, application.properties.
1 |
server.port=8761 |
Rerun the application and go to http://localhost:8761/, you will see a registered service:
This service is Eureka Server!
If you do not want Eureka Server to automatically register itself and discover other services, you can disable this default configuration by adding the following 2 properties to the application configuration file, application.properties:
1 2 |
eureka.client.register-with-eureka=false eureka.client.fetch-registry=false |
Then, restart the application, you will not see the Eureka Server not automatically do that anymore!
So we have successfully set up the Eureka Server of Netflix OSS using Spring Cloud Netflix!