Back to: Spring Boot Tutorials
Spring Boot Eureka Server with Examples
In this article, I am going to discuss Spring Boot Eureka Server with Examples. Please read our previous article where we discussed Spring Boot HTTPS with Examples.
What is a Eureka Server?
A Eureka server is an application that holds information about all client service applications. In other words, every microservice from Spring Boot shall register on this Eureka server. Hence, the Eureka server knows which application is running on which port and which IP address. Because of this, the Eureka server is also known as the discovery server.
How to Implement Eureka Server in Spring Boot?
To run a Eureka server in Spring Boot, follow these steps:
Step 1: Create a new project using Spring Initializr in VS Code. Include Eureka Server dependency.
Ensure that the dependencies are correct:
Step 2: Add @EnableEurekaServer annotation to EurekaApplication.java class in src/main/java/com/dotnet/eureka directory. Also, import the required package. This makes it a Eureka server.
Step 3: Configure the application to perform as a Eureka server. Modify the application.properties file in the src/main/resources directory as follows:
Alternatively, we may use YAML to set up the configuration. To do this, create a file called application.yml in the same directory as the application.properties file. Populate the file with the following lines:
This is going to have the same effect as modifying the application.properties file. Since YAML is more readable, YAML should be used for larger configuration files. Ensure that the application.properties and application.yml files do not clash with each other. This may create discrepancies in the project.
Step 4: Compile and execute the application. Ensure the compilation is successful.
Step 5: Log on to http://localhost:8761 to check if the Eureka server is functioning. You should see the following page:
As can be seen in the above screenshot, the Eureka server is up and running. Since no other application is running, the application table is left empty.
Congratulations! You now know how to run a Eureka server in Spring Boot.
The Complete Example Code
EurekaApplication.java
package com.dotnet.eureka; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer public class EurekaApplication { public static void main(String[] args) { SpringApplication.run(EurekaApplication.class, args); } }
In the next article, I will discuss Spring Boot Eureka Client with Examples. In this article, I try to explain Spring Boot Eureka Server with Examples. I hope you enjoy this Spring Boot Eureka Server article.