In this tutorials, we are going to see Spring Boot Eureka/ Spring Cloud Eureka.
Spring Boot Eureka:
Spring Cloud Eureka is a wrapper of Eureka which build from Netflix.
Doc: Spring Cloud Eureka is a Client-side service discovery allows each service to find and communicate with each other without hard coding hostname and port. The only ‘fixed point’ in such an architecture consists of a service registry with which each service has to register.
Spring Boot Eureka Server Example:
Here I am going to create two different micro-services, both are individual applications one will act as a server and another one is a client. Intermediately I am going to create a Eureka server as service registry it allows both client and servers to communicate with each other without knowing the hostname and port number.
Technologies Used:
- Spring Boot Starter 2.0.5
- Spring-cloud-starter-netflix-eureka-server
- Spring-cloud-dependencies
- Java 8
- IntelliJ Idea
Application Structure:
Dependencies for Eureka Server:
To implement eureka server as service registry we have to add the below dependencies in pom.xml
<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>
Complete 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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.onlinetutorialspoint</groupId>
<artifactId>SpringBoot_Eureka_Server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringBoot_Eureka_Server</name>
<description>Spring Boot Eureka Server Example</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</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>
Service registry configuration in application.yml file.
spring:
application:
name: eureka_server
server:
port: 8090
eureka:
client:
registerWithEureka: false
fetchRegistry: false
server:
waitTimeInMsWhenSyncEmpty: 0
Here, I am mentioning application name as eureka_server and port as 8090. As this application act as a eureka server, it should not be registered with default eureka client so that we should make use of reisterWithEureka:false and fetchRegisty:false.
Enabling eureka server in spring boot application.
package com.onlinetutorialspoint;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;
@EnableEurekaServer
@SpringBootApplication
public class SpringBootEurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootEurekaServerApplication.class, args);
}
}
Enabling Eureka Server with Spring Boot as simple as annotating @EnableEurekaServer annotation on top of @SprinBootApplication.
Run the application:
mvn spring-boot:run
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.0.5.RELEASE)
2018-10-07 20:41:23.423 INFO 2036 --- [ main] c.o.SpringBootEurekaServerApplication : No active profile set, falling back to default profiles: default
2018-10-07 20:41:23.457 INFO 2036 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@53bc1328: startup date [Sun Oct 07 20:41:23 IST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@39d9314d
2018-10-07 20:41:26.040 INFO 2036 --- [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=e1e941da-179e-3162-9b2c-a5223c94cb1f
.........
.........
After running the application, we can see the eureka dashboard like below by accessing our application’s url.
http://localhost:8090/
On the above eureka dashboard, we can see the basic information about system status and current instances. Currently, we do not have any registered instances with eureka service.
References:
Happy Learning 🙂