How to set up a Service Registry in Spring Boot?

Let’s say you have created few microservices.

They talk to each other.

You manually configure the URL of each microservice which each microservice talks to.

You add them in application.yml files.

Maintaining them can be difficult.

And if there are multiple instances of each microservice , you may need to configure each instance URL or the URL of a load balancer which handles the routing.

Even if you are using a load balancer every time a new instance is added you need to manually configure that instance in the load balancer.

As you scale your applications this could be a headache.

What if you could invoke a microservice using just a name?

What if you could automatically register new instances to a load balancer?

Service Registry provides the solution to these questions.

It is a microservice design pattern which lets you invoke a microservice using just an id.

New instances of a microservice will automatically get registered to the registry and you never need to worry about the hostname and port of the microservice you are calling.

Let’s implement this.

In Spring Boot you can implement a service registry using a Netflix library “eureka”.

To implement it you need to follow the below steps:

  1. Set up a Service Registry (Eureka server)
  2. Configure microservice to talk to the service registry (Eureka clients).

Let’s first set up the service registry:

Setting up a service registry

To set up a service registry follow the below steps:

STEP1: Add Eureka server dependency

STEP2: Add an annotation

STEP3: Configure application.yml file

Advertisements

STEP1: Add Eureka server dependency

Create a spring boot project and add the below dependency:

Here is the pom.xml after adding the dependency:

<?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.6.6</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>eurekaserver</artifactId>
	<version>1.0.0</version>
	<name>eurekaserver</name>
	<description>Demo project for  Eureka Server</description>
	<properties>
		<java.version>11</java.version>
		<spring-cloud.version>2021.0.1</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>

STEP2: Add an annotation

Add the annotation @EnableEurekaServer to the main class:

package com.example.eureka.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class EurekaserverApplication {

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

}

STEP3: Configure application.yml file

Specify port number in which the eureka server should run. All Eureka clients expect the eureka server to be running on the same server at port 8761 by default.

So it is better to configure this port number.

Also the server registers itself as a client by default . To disable this set the property register-with-eureka to false:




server:
  port: 8761
  
eureka:
  client:
    register-with-eureka: false

That’s it ! Start the server and load the url http://localhost:8761 in your browser:

As you see there are no services registered to the server now.

Our Service Registry is now ready.

Let’s create a service and register it to this registry.

Advertisements

Setting up a microservice client:

To set up a microservice client , follow the below steps:

1.Add Eureka Discovery client dependency

2. Add application name in application.yml file

STEP1: Add Eureka client dependency:

Create a Spring Boot project and add Eureka Client as a dependency:

Also add spring starter web dependency.

Here is the pom.xml with the required dependencies:

<?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.6.6</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.example</groupId>
	<artifactId>eurekaclient</artifactId>
	<version>1.0.0</version>
	<name>eurekaclient</name>
	<description>Demo project for  Eureka Client</description>
	<properties>
		<java.version>11</java.version>
		<spring-cloud.version>2021.0.1</spring-cloud.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
	<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			
		</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>

STEP2: Add application name in application.yml file


spring:
  application:
    name: eureka-client

This is the name by which the service will register itself to the service registry.

That’s it!

Start the client and then refresh the service registry home page:

The client just auto registered itself to the service registry!

How did it happen?

We didn’t configure the service registry on the client!

This is because by default eureka clients look for eureka server running at port 8761 on the same server. Since we deployed the server on the same machine and on port 8761 , it found the server by itself and registered itself to it!

If we want to configure a different eureka server (for example on port 8762) we can do that using this property in client application.yml file:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8762/eureka
Advertisements

I created one more microservice as an eureka client in the same away as above and it registered itself automatically too:

I ran one more instance of one of the clients (eureka-client) and it got registered as well:

That’s it!

We set up a service registry and registered instances in it using Netflix Eureka.

You also get a bird eye view of all the microservices running in your project if you register them on a service registry like above.

Code:

https://github.com/vijaysrj/eurekaserver

https://github.com/vijaysrj/eurekaclient

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s