Spring Boot Hystrix

Hystrix is ​​a library of Netflix. Hystrix isolation between the access point of service, preventing cascading failures among themselves and provide a fallback option.

For example, when calling a third-party application that takes more time to send a response. So at that time, a fallback method to control and customize the response back to your application.

In this chapter, you will see how Hystrix in Spring Boot application.

First, we need to add Spring Cloud Starter Hystrix dependencies in build configuration file.

Maven users can pom.xml to add the following file dependencies -

<dependency>
   <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> 
XML

Gradle users can build.gradle add the following file dependencies -

compile('org.springframework.cloud:spring-cloud-starter-hystrix')

Now, @EnableHystrixadd comments to the main Spring Boot application class file. @EnableHystrixAnnotations are used to enable functions Hystrix Spring Boot application.

Spring Boot main application class file code is as follows -

package com.yiibai.hystrixapp;

import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.hystrix.EnableHystrix; @SpringBootApplication @EnableHystrix public class HystrixappApplication { public static void main(String[] args) { SpringApplication.run(HystrixappApplication.class, args); } } 
Java

Write a simple Rest Controller now, so that in the time of the request 3to return after the second String.

@RequestMapping(value = "/") public String hello() throws InterruptedException { Thread.sleep(3000); return "Welcome Hystrix"; } 
Java

Now, add Rest API @Hystrixcommand and @HystrixProperty, in milliseconds and define a timeout value.

@HystrixCommand(fallbackMethod = "fallback_hello", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000") }) 
Java

Next, if the request takes a long time to respond, please define fallback method fallback_hello().

private String fallback_hello() { return "Request fails. It takes long time to response"; } 
Java

Here Show full Rest Controller class file contains the REST API and Hystrix attributes -

@RequestMapping(value = "/") @HystrixCommand(fallbackMethod = "fallback_hello", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000") }) public String hello() throws InterruptedException { Thread.sleep(3000); return "Welcome Hystrix"; } private String fallback_hello() { return "Request fails. It takes long time to response"; } 
Java

In this example, REST API written in the Main Spring Boot application class file itself.

package com.yiibai.hystrixapp;

import org.springframework.boot.SpringApplication; import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; @SpringBootApplication @EnableHystrix @RestController public class HystrixappApplication { public static void main(String[] args) { SpringApplication.run(HystrixappApplication.class, args); } @RequestMapping(value = "/") @HystrixCommand(fallbackMethod = "fallback_hello", commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000") }) public String hello() throws InterruptedException { Thread.sleep(3000); return "Welcome Hystrix"; } private String fallback_hello() { return "Request fails. It takes long time to response"; } } 
Java

Construction of the full profile as shown in FIG.

Maven build file - pom.xml content:

<?xml version = "1.0" encoding = "UTF-8"?>
<project xmlns = "http://maven.apache.org/POM/4.0.0" 
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 
   http://maven.apache.org/xsd/maven-4.0.0.xsd">

   <modelVersion>4.0.0</modelVersion> <groupId>com.yiibai</groupId> <artifactId>hystrixapp</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>hystrixapp</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.9.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> <spring-cloud.version>Edgware.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 
XML

Gradle build file - build.gradle

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

group = 'com.yiibai'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8

repositories {
   mavenCentral()
}
ext {
   springCloudVersion = 'Edgware.RELEASE'
}
dependencies {
   compile('org.springframework.cloud:spring-cloud-starter-hystrix')
   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:${springCloudVersion}"
   }
}

You can create an executable JAR file, and run Spring Boot application uses the following Gradle or Maven command -

For Maven, using a command such as -

mvn clean install
Shell

After "BUILD SUCCESS", you can targetfind the JAR file directory.

For Gradle, using a command such as -

gradle clean build

After the "BUILD SUCCESSFUL", can build/libsfind the JAR file directory. Now, run the JAR file using the command given below -

java –jar <JARFILE>

This will launch the application in Tomcat on port 8080.

Now, click from a Web browser = the URL of> http://localhost:8080/, and then view Hystrix response. API requires 3seconds to respond, but Hystrix timeout 1seconds.

Guess you like

Origin www.cnblogs.com/borter/p/12423870.html