- Spring Boot Tutorial
- Spring Boot - Home
- Spring Boot - Introduction
- Spring Boot - Quick Start
- Spring Boot - Bootstrapping
- Spring Tool Suite
- Spring Boot - Tomcat Deployment
- Spring Boot - Build Systems
- Spring Boot - Code Structure
- Spring Beans & Dependency Injection
- Spring Boot - Runners
- Spring Boot - Starters
- Spring Boot - Application Properties
- Spring Boot - Configuration
- Spring Boot - Annotations
- Spring Boot - Logging
- Building RESTful Web Services
- Spring Boot - Exception Handling
- Spring Boot - Interceptor
- Spring Boot - Servlet Filter
- Spring Boot - Tomcat Port Number
- Spring Boot - Rest Template
- Spring Boot - File Handling
- Spring Boot - Service Components
- Spring Boot - Thymeleaf
- Consuming RESTful Web Services
- Spring Boot - CORS Support
- Spring Boot - Internationalization
- Spring Boot - Scheduling
- Spring Boot - Enabling HTTPS
- Spring Boot - Eureka Server
- Service Registration with Eureka
- Gateway Proxy Server and Routing
- Spring Cloud Configuration Server
- Spring Cloud Configuration Client
- Spring Boot - Actuator
- Spring Boot - Admin Server
- Spring Boot - Admin Client
- Spring Boot - Enabling Swagger2
- Spring Boot - Using SpringDoc OpenAPI
- Spring Boot - Creating Docker Image
- Tracing Micro Service Logs
- Spring Boot - Flyway Database
- Spring Boot - Sending Email
- Spring Boot - Hystrix
- Spring Boot - Web Socket
- Spring Boot - Batch Service
- Spring Boot - Apache Kafka
- Spring Boot - Twilio
- Spring Boot - Unit Test Cases
- Rest Controller Unit Test
- Spring Boot - Database Handling
- Securing Web Applications
- Spring Boot - OAuth2 with JWT
- Spring Boot - Google Cloud Platform
- Spring Boot - Google OAuth2 Sign-In
- Spring Boot Resources
- Spring Boot - Quick Guide
- Spring Boot - Useful Resources
- Spring Boot - Discussion
Spring Boot - Eureka Server
Eureka Server is an application that holds the information about all client-service applications. Every Micro service will register into the Eureka server and Eureka server knows all the client applications running on each port and IP address. Eureka Server is also known as Discovery Server.
In this chapter, we will learn in detail about How to build a Eureka server.
Building a Eureka Server
Eureka Server comes with the bundle of Spring Cloud. For this, we need to develop the Eureka server and run it on the default port 8761.
Visit the Spring Initializer homepage https://start.spring.io/ and download the Spring Boot project with Eureka server dependency. It is shown in the screenshot below −
After downloading the project in main Spring Boot Application class file, we need to add @EnableEurekaServer annotation. The @EnableEurekaServer annotation is used to make your Spring Boot application acts as a Eureka Server.
The code for main Spring Boot application class file is as shown below −
package com.tutorialspoint.eurekaserver; 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); } }
Make sure Spring cloud Eureka server dependency is added in your build configuration file.
The code for Maven user dependency is shown below −
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency>
The code for Gradle user dependency is given below −
compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')
The complete build configuration file is given below −
Maven - pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.3.3</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.tutorialspoint</groupId> <artifactId>eurekaserver</artifactId> <version>0.0.1-SNAPSHOT</version> <name>eurekaserver</name> <description>Demo project for Spring Boot</description> <url/> <licenses> <license/> </licenses> <developers> <developer/> </developers> <scm> <connection/> <developerConnection/> <tag/> <url/> </scm> <properties> <java.version>21</java.version> <spring-cloud.version>2023.0.3</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
Gradle – build.gradle
buildscript { ext { springBootVersion = '3.3.3' } repositories { mavenCentral() } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}") } } apply plugin: 'java' apply plugin: 'eclipse' apply plugin: 'org.springframework.boot' group = 'com.tutorialspoint' version = '0.0.1-SNAPSHOT' sourceCompatibility = 21 repositories { mavenCentral() } ext { springCloudVersion = '2023.0.3' } dependencies { compile('org.springframework.cloud:spring-cloud-starter-eureka-server') testCompile('org.springframework.boot:spring-boot-starter-test') } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
By default, the Eureka Server registers itself into the discovery. You should add the below given configuration into your application.properties file or application.yml file.
application.properties file is given below −
eureka.client.registerWithEureka = false eureka.client.fetchRegistry = false server.port = 8761
The application.yml file is given below −
eureka: client: registerWithEureka: false fetchRegistry: false server: port: 8761
Compilation and Execution
Now, you can create an executable JAR file, and run the Spring Boot application by using the Maven or Gradle commands shown below −
For Maven, use the command as shown below −
mvn clean install
After "BUILD SUCCESS", you can find the JAR file under the target directory.
For Gradle, you can use the command shown below −
gradle clean build
After "BUILD SUCCESSFUL", you can find the JAR file under the build/libs directory.
Now, run the JAR file by using the following command −
java –jar <JARFILE>
You can find that the application has started on the Tomcat port 8761 as shown below −
Standard Commons Logging discovery in action with spring-jcl: please remove commons-logging.jar from classpath in order to avoid potential conflicts . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ [32m :: Spring Boot :: [39m [2m (v3.3.3)[0;39m [2m2024-09-10T17:40:49.095+05:30[0;39m [32m INFO[0;39m [35m19372[0;39m [2m---[0;39m [2m[eurekaserver] [ main][0;39m [2m[0;39m[36mc.t.e.EurekaserverApplication [0;39m [2m:[0;39m Starting EurekaserverApplication using Java 21.0.3 with PID 19372 (E:\Dev\eurekaserver\target\classes started by Tutorialspoint in E:\Dev\eurekaserver) [2m2024-09-10T17:40:49.098+05:30[0;39m [32m INFO[0;39m [35m19372[0;39m [2m---[0;39m [2m[eurekaserver] [ main][0;39m [2m[0;39m[36mc.t.e.EurekaserverApplication [0;39m [2m:[0;39m No active profile set, falling back to 1 default profile: "default" ... [2m2024-09-10T17:40:52.658+05:30[0;39m [32m INFO[0;39m [35m19372[0;39m [2m---[0;39m [2m[eurekaserver] [ main][0;39m [2m[0;39m[36mc.n.eureka.cluster.PeerEurekaNodes [0;39m [2m:[0;39m Adding new peer nodes [http://localhost:8761/eureka/] ... [2m2024-09-10T17:40:52.971+05:30[0;39m [32m INFO[0;39m [35m19372[0;39m [2m---[0;39m [2m[eurekaserver] [ Thread-9][0;39m [2m[0;39m[36me.s.EurekaServerInitializerConfiguration[0;39m [2m:[0;39m Started Eureka Server [2m2024-09-10T17:40:52.991+05:30[0;39m [32m INFO[0;39m [35m19372[0;39m [2m---[0;39m [2m[eurekaserver] [ main][0;39m [2m[0;39m[36mo.s.b.w.embedded.tomcat.TomcatWebServer [0;39m [2m:[0;39m Tomcat started on port 8761 (http) with context path '/' [2m2024-09-10T17:40:52.992+05:30[0;39m [32m INFO[0;39m [35m19372[0;39m [2m---[0;39m [2m[eurekaserver] [ main][0;39m [2m[0;39m[36m.s.c.n.e.s.EurekaAutoServiceRegistration[0;39m [2m:[0;39m Updating port to 8761 [2m2024-09-10T17:40:53.012+05:30[0;39m [32m INFO[0;39m [35m19372[0;39m [2m---[0;39m [2m[eurekaserver] [ main][0;39m [2m[0;39m[36mc.t.e.EurekaserverApplication [0;39m [2m:[0;39m Started EurekaserverApplication in 4.406 seconds (process running for 5.362) [2m2024-09-10T17:40:54.031+05:30[0;39m [32m INFO[0;39m [35m19372[0;39m [2m---[0;39m [2m[eurekaserver] [on(4)-127.0.0.1][0;39m [2m[0;39m[36mo.a.c.c.C.[Tomcat].[localhost].[/] [0;39m [2m:[0;39m Initializing Spring DispatcherServlet 'dispatcherServlet' [2m2024-09-10T17:40:54.031+05:30[0;39m [32m INFO[0;39m [35m19372[0;39m [2m---[0;39m [2m[eurekaserver] [on(4)-127.0.0.1][0;39m [2m[0;39m[36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m Initializing Servlet 'dispatcherServlet' [2m2024-09-10T17:40:54.033+05:30[0;39m [32m INFO[0;39m [35m19372[0;39m [2m---[0;39m [2m[eurekaserver] [on(4)-127.0.0.1][0;39m [2m[0;39m[36mo.s.web.servlet.DispatcherServlet [0;39m [2m:[0;39m Completed initialization in 1 ms
Now, hit the URL http://localhost:8761/ in your web browser and you can find the Eureka Server running on the port 8761 as shown below −