Sunday, 28 July 2019

Circuit breaker – Hystrix

Hystrix tool is for circuit breaker operations, that is, latency and fault tolerance.Therefore, Hystrix stops cascading failures. Hystrix performs the real-time operations for monitoring the services and property changes, and supports concurrency.

Gradle Dependency.

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-hystrix')
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'
}
}

Main Class:

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.circuitbreaker.EnableCircuitBreaker;
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
@EnableCircuitBreaker
public class SayHelloApplication {
@Bean
  RestTemplate restTemplate(){
    return new RestTemplate();
  }

  @Autowired
  RestTemplate restTemplate;
  
  @Autowired
  ProfileService profileService;
@Bean
public ProfileService getBookService() {
return new  ProfileService(restTemplate);
}

private static Logger log = LoggerFactory.getLogger(SayHelloApplication.class);

@RequestMapping("/callService")
  public Object invoke() {
    Object obj = getBookService().readProfileList();
    return obj;
  }

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

}

Service Class:

package com.example.sayhello;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

@Service
public class ProfileService {

  private final RestTemplate restTemplate;

  public ProfileService(RestTemplate rest) {
    this.restTemplate = rest;
  }

  @HystrixCommand(fallbackMethod = "adminProfile")
  public String readProfileList() {
    URI uri = URI.create("http://localhost:8180/user/name/sarath");
    return this.restTemplate.getForObject(uri, String.class);
  }

  public String adminProfile() {
  URI uri = URI.create("http://localhost:8080/user/name/Syed");
    return this.restTemplate.getForObject(uri, String.class);
  }

}

From the above class if the localhost:8180 is not accessible then It will access the localhost:8080. 
Thanks God We end up in seeing the alternate when the service is down.

No comments:
Write comments