Microservices with Spring Boot. Part 5. Using the Eureka name server

In this final part of our series of microservice architectures, we will learn how to enable the Eureka name server and allow microservices to interact with it.

This article is part of the Spring Boot Microservices series:


In this series of articles, you will become familiar with the concept of microservices and learn how to create microservices using Spring Boot and Spring Cloud.

This guide will help you learn the basics of microservice architectures. We will also begin to look at the basic microservice implementation with Spring Boot.

We will create a couple of microservices and make them communicate with each other using the Eureka (Eureka Naming Server) and Ribbon name servers to balance the load on the client side.

In this part, we will concentrate on turning on the Eureka Naming Server and ensure the interaction of microservices with it.

You will learn:


  • What is a name server for?
  • What is Eureka?
  • How does a name server provide location transparency between microservices?

In parts 2 and 3, we created two microservices and established a connection between them.



In part 4, we used Ribbon to distribute the load between two instances of the Forex service. However, we hardcode the URLs of both instances of the Forex service in CCS. This means that every time a new instance of FS appears, we will need to change the CCS configuration. Is not cool.

In this part, we will use the Eureka Naming Server to solve this problem.



You will need:


  • Maven 3.0+ - your build tool
  • Your favorite IDE. We use Eclipse.
  • JDK 1.8+

Ready Maven Project with Code Samples


The Github repository has all the code examples.

Create an Eureka name server boot project using Spring Initializr


Creating an Eureka name server using the Spring Initializr is very simple. Spring Initializr: start.spring.io is a great tool for quickly creating your Spring Boot projects.

With the help of Spring Initializr, you can create a variety of projects.

To create a web services project, you must complete the following steps:

1. Launch Spring Initializr and select the following:

  • Type com.in28minutes.springboot.microservice.eureka.naming.server as a group
  • Type spring-boot-microservice-eureka-naming-server as an artifact
  • Select the following dependencies:

- Eureka
- DevTools

2. Click Generate Project .

3. Import the project into Eclipse: File -> Import -> Existing Maven Project.

Remember to list Eureka in the dependencies.

Turn on Eureka


Enable EurekaServer in SpringBootMicroserviceEurekaNamingServerApplication .

@SpringBootApplication
@EnableEurekaServer
public class SpringBootMicroserviceEurekaNamingServerApplicatio

Configure the application name and port for the Eureka server:

/spring-boot-microservice-eureka-naming-server/src/main/resources/application.properties


spring.application.name=netflix-eureka-naming-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

Starting the Eureka Name Server


Launch SpringBootMicroserviceEurekaNamingServerApplication as a Java application.

You can see the status of the running Eureka server at
http://localhost:8761

You will see that not a single instance has yet been connected to Eureka:



Connect FS and CCS Microservices to Eureka


Make these changes for both microservices:

  • Add the following dependency to pom.xml:

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

  • Configure the Eureka URL in application.properties:
    eureka.client.service-url.default-zone=http://localhost:8761/eureka

Restart all instances of CCS and FS. You will see that the CCS and FS microservices are registered on the Eureka name server. This is cool!



This screenshot shows how to start an additional instance of the Forex service on 8081.



You can see that one instance of CCS and two instances of FS microservices are registered on the Eureka name server.



Routing Ribbon requests through Eureka


All you have to do is remove the following configuration
in application.properties:

forex-service.ribbon.listOfServers=localhost:8000,localhost:8001

Restart the CCS instance.

Eureka in action


Currently we have the following services:

  • Currency conversion microservice (CCS) on 8100
  • Two instances of Forex microservice on 8000 and 8001
  • Eureka Server is running

Now you will see that requests for CCS will be distributed between two instances of the Forex microservice using Ribbon via Eureka.

Request 1


GET to http://localhost:8100/currency-converter-feign/from/EUR/to/INR/quantity/10000

{
  id: 10002,
  from: "EUR",
  to: "INR",
  conversionMultiple: 75,
  quantity: 10000,
  totalCalculatedAmount: 750000,
  port: 8000,
}

Request 2


GET to http://localhost:8100/currency-converter-feign/from/EUR/to/INR/quantity/10000

{
  id: 10002,
  from: "EUR",
  to: "INR",
  conversionMultiple: 75,
  quantity: 10000,
  totalCalculatedAmount: 750000,
  port: 8001,
}

You may notice that the port numbers in the two answers are different.

Exercise : Run another instance of the Forex service at 8002. You will see that the load is automatically redirected to it.

Wow! That's cool, isn't it?

Summary


We created two microservices and established a connection between them.



We use the tape to distribute the load between two instances of the Forex service and Eureka as a name server. When we launch new instances of the Forex service, you will see that the load is automatically distributed between them.

The idea of ​​this five-part series was to give Microservices the capabilities of Spring Boot and Spring Cloud.

There are many possibilities for using microservices. Until next time.

A complete code sample for this project can be found in the GitHub repository .

Source: https://habr.com/ru/post/undefined/


All Articles