Hi All,

Greeting of the day!

Today we will explore the below points in spring microservices.

  • What is Service Registry & Eureka Server? 
  • What is Service Discovery & Eureka Client? 
So let's begin,

Service Registry - This is a microservice that will work as a registry where all microservices will register themselves and will look for (discover) other microservice which are registered on the service registry.

We will be using Eureka as Service Registry.

So service registry will be Eureka Server. And all microservice which register themselves and discover other microservices to call will be Eureka Client.

So let's take an example,

We will be building 3 microservices.
  1. Eureka Server Microservices.
  2. Fixed Deposit Service [Eureka Cleint]
  3. Saving Account Service
Note-Both Fixed deposit service and saving account service will register themselves to Eureka Server. Now let's say customers want to open a fixed deposit, so in fixed deposit service, we need to fetch some customers details like PAN No, Nominee which will be provided by saving account service.

So for that Fixed deposit service will go to Eureka Server for service discovery of saving account service.

We will be developing this basic example.



So let's begin
  • First, let's build Eureka Server.
Go to Spring Initializr and create a project with the following dependency and import generated project in IDE.



Add below properties in the application.properties

spring.application.name=eureka-server
server.port=8761
eureka.client.register-with-eureka=false //Tells not to register since application itself is Eureka Registry.
eureka.client.fetch-registry=false   //Tells not to fetch registry since application itself is registry. 

Also, add the below annotation on the main class i.e EurekaServerApplication.java
@SpringBootApplication
@EnableEurekaServer

and run Eureka Server.
You will be able to access Eureka Server at http://localhost:8761/



Now our Eureka server is up and running.

So now let's build a saving account service and register it to Eureka Server.

Go to Spring Initializr and create a project with the following dependency and import generated project in IDE.


 Now add the below properties in the application.properties 

eureka.client.service-url.default-zone=http://localhost:8761/
spring.application.name=saving-account-service
server.port=8999

Here we have given eureka.client.service-url.default-zone which is URL of Eureka Server for service registration.

Now add the below annotation on the main class i.e on SavingAccountServiceApplication.java 
@SpringBootApplication
@EnableEurekaClient

That's all, now run SavingAccountService and then go to Eureka Server you will find saving account service is registered in Eureka Server.

  
Now we will be adding some REST endpoints in the saving account service which will be used by the fixed deposits service.

Create model class SavingAccount.java as below.


Now let's create 1 Rest Endpoint which will be used by fix-deposit-service

Here we are not connecting to a database we have just taken 1 list and returned data from that list as shown below. 

I have attached the project structure of the saving-account-service for reference.


Now let's develop a fixed deposit service 
  
Go to Spring Initialize and create FixedDepositService with the below dependency and import it to IDE.


Now we will add code for Service Discovery in fixed deposit service, it will go to EurekaServer and find saving account service.

Add below properties in the application.properties.

eureka.client.service-url.default-zone=http://localhost:8761/
spring.application.name=fixed-deposit-service
server.port=8998

Now add the below annotation on the main class i.e on FixedDepositServiceApplication

@SpringBootApplication
@EnableEurekaClient

Now create two classes FixedDeposti.java and SavingAccount.java




Now we will create the controller class FixedDepositController where we will add code for service discovery of SavingAccountService.


As you can see we have used http://saving-account-service/saving-account/{accountId} is Rest API call, so it will look in Eureka Server for saving-account-service and Eureka server will pass the request to instances of saving account service which is registered in Eureka service registry. 

I have attached the project structure of fixed-deposit-service for reference.


Now you have to run Eureka-server,fixed-deposit-service,saving-account-service and then go and hit below URL,

URL localhost:8998/fixed-deposit/open
Method Type- POST

Payload 
{
"fdAmount":150000,
"fdMaturityDate":"18-08-2020",
"savingAccountNo":"34343"
}
 
And you will find the response below which contains data we retrieve from saving-account-service

Output

{
    "fdAmount": 5000,
    "fdMaturityDate": "18-08-2020",
    "savingAccountNo": 34343,
    "panNo": "AWGKB3433K",
    "customerName": "William",
    "nomineeName": "James"
}

So we have completed basic examples of a service registry, eureka server, and service discovery.

If you have any questions let me know.


For other spring boot tutorials please visit Spring Tutorial

Thank you 
Happy Learning.