Sunday, 28 July 2019

Client Side Load Balancing with Ribbon and Spring Cloud

Microservice architecture is of no use if there is no inter-process or service communication. The Ribbon application provides this feature. Ribbon works with Eureka for load balancing and with Hystrix for fault tolerance or circuit breaker operations.

Ribbon also supports TCP and UDP protocols, apart from HTTP. It provides these protocol supports in both asynchronous and reactive models. It also provides the caching and batching capabilities.

In this Tutorial We are going to see the client side loadbalacing with ribbon and spring cloud. In this I have three projects as say-hello and other project named profileService, and profileService2 which is running with the profile related services. Now we will access this profileServie and return the response.

Follow the below steps.

1.Create a project named 'say-hello'.
2.Add the gradle dependencies.

buildscript {
ext {
springBootVersion = '2.1.6.RELEASE'
}
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

bootJar {
baseName = 'user'
version = '0.0.1-SNAPSHOT'
}
sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
mavenCentral()
}


dependencies {
compile('org.springframework.cloud:spring-cloud-starter-netflix-ribbon')
compile('org.springframework.boot:spring-boot-starter-web')
testCompile('org.springframework.boot:spring-boot-starter-test')
}

dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:Finchley.SR2"
}
}

eclipse {
classpath {
containers.remove('org.eclipse.jdt.launching.JRE_CONTAINER')
containers 'org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8'
}
}

3. Create a main class with the Following content.

package com.example.sayhello;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@SpringBootApplication
@RestController
@RibbonClient(name = "say-hello",configuration = RibbonConfiguration.class)
public class SayHelloApplication {
@Bean
@LoadBalanced
  RestTemplate restTemplate(){
    return new RestTemplate();
  }

  @Autowired
  RestTemplate restTemplate;
private static Logger log = LoggerFactory.getLogger(SayHelloApplication.class);

@RequestMapping("/callService")
  public Object invoke() {
    Object obj = this.restTemplate.getForObject("http://say-hello/user/name/sarath", String.class);
    return obj;
  }

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

}

4.Now we have created a service which Invokes my service and returns the Object. Next is to define the Configuration.

/say-hello/src/main/resources/application.yml

spring:
  application:
    name: say-hello

server:
  port: 8091
  
say-hello:
  ribbon:
    eureka:
      enabled: false
    listOfServers: localhost:8080,localhost:8180
    ServerListRefreshInterval: 15000
5.We should also define the ribbon configuration class.

RibbonConfiguration.class

package com.example.sayhello;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.IPing;
import com.netflix.loadbalancer.PingUrl;

public class RibbonConfiguration {

  @Autowired
  IClientConfig ribbonClientConfig;

  @Bean
  public IPing ribbonPing(IClientConfig config) {
    return new PingUrl();
  }

 }


Testing the Url.

When you call the http://localhost:8091/callService

It responds from 8080 and 8081 service respectively.

Congratulations we have implemented the Loadbalancer for the Application. The Implementation may differs calling directly or from another api.

Happy Learning !!!

1 comment:
Write comments