fbpx

Server Side Load Balancing With Netflix Zuul and Eureka

Server Side Load balancing With Zuul & Eureka

Server Side Load Balancing With Netflix Zuul and Eureka:-

In this post, we will talk about server-side load balancing With Zuul and Eureka. Let us take a while and understand what is meant by server-side load balancing. In simple word, we distribute our user requests.In a Spring Cloud MicroServices ecosystem load balancing is an important and common functionality .

Zuul acts as a gateway for requests from websites, mobile devices to the backend of your service. Zuul is an edge service that provides dynamic routing, monitoring, resiliency, security, and many more features.

1. Why Do We Need Zuul:-

The amount of high traffic sometimes results in complex production issues. We want a system that can rapidly react to these changes. This technique was used when a new page from the website needed tuning for a smaller set of mobile devices or a group of customers.




If Performance problems, as well as unexplained errors, were observed. It was difficult to debug the issues because the problems were only happening for a small set of customers or a small set of devices. By diverting the traffic to a single instance, it becomes easier to debug in real time. It is done using Zuul filters in a netflix Zuul Load balancer. Zuul uses different types of filters that we can implement for various purpose, read more about this here.

2.Project Setup:-

We are going to set up and run three projects.

  1. Eureka Service Discovery – Service Registry for Our Backend Services. Please have a look at our previous example about Creating Eureka Service Discovery.
  2. Customer Service – A rest API which is basically a backend service in a real-time scenario. Visit my previous post about Writing  Service Rest With Spring Boot.
  3. Zuul Gateway – API Gateway to serve requests and call our internal backend services.in this case, it acts as a proxy and calls our customer service Internally.

3.Configure Zuul Communicating With Eureka

Now we have to Configure our  Zuul to Communicate with Eureka Server and mark our application as Zuul Proxy . To do this we need to use @EnableZuulProxy  annotation  in our main application file.

Add the following configuration in your  application.yml file.

spring:
  application:
    name: proxy-service

server:
  port: 8060

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

logging:
  pattern: 
    console: "%d{yyyy-MM-dd HH:mm:ss} ${LOG_LEVEL_PATTERN:-%5p} %m%n"
              
zuul:
  routes:
    customer:
      path: /customer/**
      serviceId: customer-service

Here we are registering our proxy server to Eureka service Registry assuming eureka running on 8761 port. Our Proxy Server will run on 8060.

We also configured routes for our Zuul proxy. We already have our customer service registered with Eureka Registry. Hence we now have just configured the routes to access the customer service.customer-service the name of the service we registered.

Once Zuul receives a request for a particular service , it internally uses Netflix Ribbon to lookup for services available from Eureka Service Discovery.

We can run multiple instances of our rest service and our API gateway will use both of the instances in round robin fashion.

Testing Zuul Eureka Service Discovery

Download Code

How to Use Spring Cloud Eureka Service Discovery

How to Use Spring Cloud Eureka Service Discovery

In this article, we are going to learn How to User Spring Cloud Eureka Service Discovery. We are going to explore and understand how different microservices can register themselves and discover other services and call their API.

Spring cloud eureka service discovery is going to solve one of the core problems developers face while implementing a microservices architecture.

Lets try and understand this, Suppose we have a set of Micro services running on different ports. How do we manage these? Maintenance and managing different ports and their addresses are fairly complex, as the volume of Micro services increases. Once your services become distributed across regions it becomes much harder.

In a distributed environment this concept is called service registration and discovery. We build a system where we maintain a registry of all the microservices deployed. All other microservices can act as a client and register themselves with this system, they can also discover other microservices registered in the system.

How this system keeps a track of Working and functional Micro services that are UP and Running?

This system will receive periodic heartbeats from the services registered in this system , if any of the services fails to send a heartbeat it de-registers that.

  • This way it maintains a well-balanced ecosystem of microservices. This system discussed above is called  Service Discovery Server.
  • We are going to use Eureka as our Service discovery which is well integrated inside Spring Cloud.

We are going to Create Our Eureka Service Discovery Server and register microservices. let’s take a step forward and start coding our Eureka Service Discovery.

Eureka Service Discovery Server:-

Create a Spring boot project from Spring Boot initializer. Add the dependencies showed below. Extract project and open using eclipse or any other IDE.

SpringCloudEureka

Now open your main file and add annotation@EnableEurekaServer on the class level. This annotation tells Spring Cloud that we Setup a registry, So that other services can talk to the registry. In short, this annotation enables the Service Registry.

When a client registers with Eureka, it provides meta-data about itself such as host and port, health indicator URL, home page etc. Eureka receives heartbeat messages from each instance belonging to a service. If the heartbeat fails over a configurable timetable, the instance is normally removed from the registry.

package com.frugalis.EurekaServer;

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);
    }
}

Now let’s add some properties to our application.properties.

server.port=8761

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

logging.level.com.netflix.eureka=OFF
logging.level.com.netflix.discovery=OFF

Spring Cloud Eureka Service can have one or more replica of Registry server as well. If you don’t have one or more replica the spring cloud logs some error messages which we suppressed in our properties file.

In a typical prod environment, we will have multiple instances of our registry server.  Eureka Service Registry server tries to register themselves as well, hence we suppressed that as well in our properties file.

Run the Eureka Registry Server:-

$ mvn clean install

$ mvn spring-boot:run

Navigate to http://localhost:8761/eureka.If you see a Screen like this below, Then you have successfully set up a basic Eureka Registry Server.

Spring Eureka Server

How To Register Our Microservices With Eureka Service Registry

Now that Our Eureka Service Registry is up and running, we will use our existing microservice that we created earlier in this post and register the microservice with Eureka Service Registry.

  • There are several implementations of service discovery like Zookeeper and Consul, We are using Eureka Discovery. Please add eureka discovery client dependency.spring-cloud-starter-netflix-eureka-client.
  • Now we need to add @EnableDiscoveryClient in our Spring Boot application to enable and register our service with Eureka Service Registry. We also need to add few properties inapplication.properties

We configure default-zone as the address and port of our Eureka server.

spring.application.name=customer-service
eureka.client.serviceUrl.defaultZone=http://127.0.0.1:8761/eureka/
management.security.enabled=false
eureka.instance.leaseRenewalIntervalInSeconds=1
eureka.instance.leaseExpirationDurationInSeconds=2
  • Add your service name as application name and add zones for your service.
  • We are disabling security as of now but you can always configure security like we do it using spring security.
  • Here “defaultzone” is a fallback value that provides the service URL for any client that doesn’t express a preference .

If anyone of you wants to code with security please lemme know. We can configure multiple instances for our microservices in a production environment and register with Eureka.

[sociallocker id=”1183″]

Download Code

[/sociallocker]

How to Create Spring Cloud Config Server

How to Create Spring Cloud Config Server

This is one of the first steps to build Spring based microservices with Spring Cloud by using a Config Server .

This section emphasizes on building centralized configurations. We are going to create a Spring Cloud Config Server and centralize configurations needed for microservices.




In a traditional application, our configurations reside inside a file in the same project. We package the files and deploy them on a server.

Now, if we need to change any configurations we need to repackage the code and redeploy.

This becomes a tedious task in the microservices world as there could be multiple deployment and repackaging needed in a real-world scenario.

So the solution to above problem is Create Spring Cloud Config Server.

The Config server gets the configuration from Github or File System and all microservices uses the configuration from the config server as shown below. Let’s see how can we achieve this?

Technologies Used:-

  • Java 8
  • Spring Boot
  • Maven 3
  • Git

Project Setup for Spring Cloud Config Server :-

Now create a simple Spring Boot app from Spring initializer as below and click on generate a project. We are going to choose Config Server dependency.

Spring-Cloud-Config-Project

@Enableconfigserver:-

We need to use this annotation while we Create Spring Cloud Config Server which says its a cloud config server and serve the configuration. Using this annotation, our app will be treated like a spring config server.

package com.frugalis.config.ConfigServer;

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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




How Spring Cloud Config Server Stores Configuration:-

The Spring cloud config server stores configuration in two ways

  1. Git or SVN
  2. Using local File System

We are going to use a Github repository and store configuration in a properties file. We are going to create a properties file for each microservice. Let’s say I am going to build a service known as  user-service .So we are creating a user-service.properties for our service user-service and push it in our GitHub repository.

Let’s create a GitHub repository for Storing of Data by our Spring Cloud Config Server. You can fork my repository as well here.

Spring Cloud Config Server

Let’s create a file known as bootstrap.properties in resources folder.

#Server port
server.port = 8888
#spring.cloud.config.server.svn.uri=//path to SVN
#spring.cloud.config.server.svn.order=1 // Specify order if you want to use both git and svn
#spring.cloud.config.server.git.order=2
#Git repo location.
spring.cloud.config.server.git.uri=https://github.com/Sanjay007/spring-cloud-config-server.git
management.security.enabled=false

Now let’s start Our Spring Cloud Config Server.

$ cd ConfigServer
$ mvnw spring-boot:run
$ curl localhost:8888/user-service.properties

In the next post, we will see how we can create a Spring Cloud Config Client.

Download Code




Microservices Using Spring Boot and Spring Cloud

Microservices Using Spring Boot and Spring Cloud:-

Let’s welcome another word “Microservices” on the crowded Street of Software. Let’s define microservice in a  simple and best way. We will learn why industry is building Microservices using Spring Boot and Spring Cloud.

A Micro Service is a design pattern or an architectural pattern to develop complex applications into simple reusable components.A monolithic application is built as a single unit whereas a micro service application is built in several independent units and easily deploy-able.

Let’s explain this by an example of an e-commerce application .

In a traditional application, there are several domain components such as Account Component,User Component and Order Component which collaborates with each other and packaged in a single deployable war. That’s is the monolithic approach illustrated below.

Monolithic Application Approach :-

Monolithic Architecture

Micro services Architecture:-

Microservices Architecture

In this above pattern we are deploying each components as a distributed and separated app.These different components can interact each other. So in order to build large applications , we need to identify the different sub domains under the domain and build each sub domain as a MicroService.

Advantages Of Micro services:-

  • Being independent deployable microservices app .Our components becomes loosely coupled.
  • We can have multiple teams working together seamlessly.
  • Code base becomes pretty much small.
  • Maintaining release becomes easier.

Why to Build Microservices Using Spring Boot and Spring Cloud:-

Let me tell you in Short, Netflix (one of the most popular on-demand video streaming service ) is the place where it all started for microservices, they decoupled their applications and implemented the microservices architecture. Netflix realized that many of their innovations are useful and can be used for a variety of purposes, So they decided to open source these technologies.

The approach or the architecture followed by Netflix helped them to deliver software fast, solved issues of distributed computing and industry loved it. So industry started accepting this architecture.

We followed the same architecture and started developing software into a small set of independent modules. But While doing this we made code that is independently deployable which implies we are into the world of distributed applications. The pain is now managing these set of distributed applications, So Spring Cloud comes to the rescue.

Spring team was alert enough to discover that the Netflix OSS projects have addressed a number of key architectural problems about the Cloud environment. There was a need to improve consumer ability of the Netflix projects , hence they Started Spring Cloud and reinvented a lot of stuff.



Spring Cloud is a new Project Spring.io familyand built on top of Spring Boot. Spring cloud solves a very basic need for cloud-based applications. These applications are distributed applications running in a  distributed environment. Spring cloud is the result of an integration of libraries from Netflix and provides easy access to these Netflix libraries. Spring Cloud takes features and libraries from Netflix cloud components and integrates inside spring family in a well designed and easily consumable way.

Let’s have a look at few of the Spring Cloud Projects that we can leverage to solve distributed applications issues:-

  • Spring Cloud Config Server.
  • Spring Cloud NetFlix.
  • Eureka
  • Hystrix
  • Ribbon
  • Feign
  • Spring Cloud Bus
  • Spring Cloud Service Broker.
  • Spring Cloud Cluster.




Microservices using Spring Boot And Spring Cloud

We have designed a cloud architecture for our Micro-services Project  . Each of these above components solves one or other challenge, So let’s check these by one in coming Series of the post. We are going to build few services and use the above  Spring Cloud components. Stay tune !

Keep In Mind , do not unnecessarily overuse the micro services pattern in your applications . Please understand first what problems it solves then try and implement .

Create Spring Cloud Config Server

Deploy Spring Boot App On Heroku

Spring MVC Basic Setup and Hello World

How to Upload File Using Spring Boot and REST

CRUD Rest API With Spring Web-Flux Reactive Programming with Spring 5

Spring Boot MongoDB+MLAB

Creating REST Service with Spring Boot