Spring Boot Application Configured As Eureka Server

Posted By : Deepak Maurya | 10-Sep-2020

In Microservice architecture eureka server is an application that holds the information about all client-service applications. In microservice  each 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.
 

Let's start coding.

Add Eureka server dependency 

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>

Add all dependencies and artifacts in pom.xml file

<?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>2.2.2.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>Registry-Service</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>Registry-Service</name>
	<description>Demo project for Spring Boot</description>
	<properties>
		<java.version>1.8</java.version>
		<spring-cloud.version>Hoxton.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>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</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>
	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
		</repository>
	</repositories>
</project>

Add Main Class for Eureka server 

@SpringBootApplication
@EnableEurekaServer
public class RegistryServiceApplication {

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

}

@EnableEurekaServer annotation , This annotation is used to make your Spring Boot application acts as a Eureka Server. In Eureka server all other Microservices can register themselves by using  service URL.

Add aplication.properties file

spring.application.name=eureka-server

server.port=10062
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
  • spring.application.name:  This property has been added to define a unique name for your application.
  • server.port: eureka server running at 10062 port
  • eureka.client.fetch-registry: Registry Service  doesn't register itself in eureka server.
  • eureka.client.register-with-eureka : this property is determines service register itself as a client in eureka server.

Testing

Test the Application 
http://localhost:10062/

Conclusion

I hope this post helps you understand about Eureka.There is a lot to learn. I only covered the basics in this post. In this post we implemented Eureka server as Discovery Server. each service  will register into the Eureka server.Eureka server knows all the services host and port.

I plan to write more posts on the Microservices, focusing on more complex features.

 

We are a 360-degree SaaS app development company that provides end-to-end software development services with a focus on next-gen technologies. Our development team specializes in using JavaScript technologies like Angular, Node, and ReactJS to build scalable, responsive, and feature-rich web applications.

About Author

Author Image
Deepak Maurya

Deepak is a backend developer and has knowledge of Java and Android. His hobbies are travelling and playing cricket.

Request for Proposal

Name is required

Comment is required

Sending message..