Part 5 – Eureka Naming Server in Microservices

eureka

Eureka Naming Server in Microservices

What have we learned so far,

PART 1 – MICROSERVICES INTRODUCTION – In this tutorial, we discussed what microservices architecture is and how its different from monolithic architecture.

 PART 2 – SCENARIO TO DEVELOP – In this tutorial, we discussed the scenario to develop.

PART 3 – FEIGN CLIENT – In this tutorial, we discussed how feign client simplifies the rest client consumption.

PART 4 – RIBBON CLIENT – In this tutorial, we discussed how ribbon client handles the client side load balancing.

What is Eureka Naming Server?

Eureka is a REST (Representational State Transfer) based service that is primarily used in the AWS cloud for locating services for the purpose of load balancing and failover of middle-tier servers. We call this service, the Eureka Server. Eureka also comes with a Java-based client component,the Eureka Client, which makes interactions with the service much easier.

Advantages of eureka naming server

Dynamic load balancing : In AWS cloud, because of its inherent nature, servers come and go. Unlike the traditional load balancers which work with servers with well known IP addresses and host names, in AWS, load balancing requires much more sophistication in registering and de-registering servers with load balancer on the fly. Since AWS does not yet provide a middle tier load balancer, Eureka fills a big gap in the area of mid-tier load balancing.
Handling Fail over of service instances : It will automatically remove the failed service and balance the load to available services.
Lets see how our components will connect with each other after adding the Zuul gateway.
Eureka
 
Lets add the naming server into our application :
1. Create new project with below details

Part 41 - Capture

2. Add @EnableEurekaServer into initializer class

package com.onlyfullstack.eurekanamingservice;

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

@SpringBootApplication
@EnableEurekaServer
public class EurekaNamingServiceApplication {

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

3. Add below properties into application.properties file of eureka-naming-server project

spring.application.name=eureka-naming-service
server.port=8761

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

4. Start the EurekaNamingServiceApplication and open http://localhost:8761 , it will show the registered services with it. As of now we don’t have any service registered with naming service so its showing as No instances available.

Part2B422B 2BCapture

5. Add below maven dependency in accounts-service and customer-service.

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

6. Add @EnableDiscoveryClient into CustomerServiceApplication and AccountServiceApplication as below
AccountServiceApplication

package com.onlyfullstack.accountservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class AccountServiceApplication {

 public static void main(String[] args) {
  SpringApplication.run(AccountServiceApplication.class, args);
 }
 
/*  @Bean //Part 2
  public RestTemplate restTemplate() {
   return new RestTemplate();
  }*/
}

CustomerServiceApplication

package com.onlyfullstack.customerservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class CustomerServiceApplication {

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

7. Add below link to accounts-service and customer-services application.properties files
account–service -> application.properties
Remove or comment the static entries of service.ribbon.listOfServers

server.port=8100
spring.application.name=account-service

#Part 4
#customer-service.ribbon.listOfServers=http://localhost:8200,http://localhost:8201

#Part 5
eureka.client.service-url.default-zone=http://localhost:8761/eureka

customer-service -> application.properties

server.port=8200
spring.application.name=customer-service

eureka.client.service-url.default-zone=http://localhost:8761/eureka

8. Now start the accounts-service with default port specified in properties file and the start customer-services on 8200 and 8201 ports. We have already discussed on starting same application on two different ports in last tutorial. Please make sure that the eureka-naming-service is up so that rest of the services will register themselves on it.

9. Lets see eureka console after starting all the projects

Part2B432B 2BCapture

10. Now with the help of eureka naming server we can dynamically register the services and retrieve the response.

Source Code
Download source code of microservice with spring cloud from below git repository :

Lets go to our next tutorial where we will discuss

Part 6 – Zuul Gateway

– In this tutorial we will understand below topics
– What is Zuul?
– What is the need of Zuul Gateway?
– How to implement the Zuul Gateway in microservices?
PART 6 – MICROSERVICES WITH SPRING CLOUD : ZUUL GATEWAY

Microservices Tutorial

Microservices Tutorial

Related Posts

Microservices Interview Questions