Hystrix是Netflix的一个库。 Hystrix隔离了服务之间的访问点,阻止了它们之间的级联故障并提供了后备选项。
例如,当呼叫第三方应用程序时,传送响应需要更多时间。所以在那个时候,控制元件转到了回退方法并将自定义响应返回给你的应用程序。
在本章中,将看到如何在Spring Boot应用程序中实现Hystrix。
首先,需要在构建配置档案中新增Spring Cloud Starter Hystrix依赖项。
Maven使用者可以在pom.xml 档案中新增以下依赖项 -
org.springframework.cloud
spring-cloud-starter-hystrix
Gradle使用者可以在build.gradle 档案中新增以下依赖项 -
compile(\'org.springframework.cloud:spring-cloud-starter-hystrix\')
现在,将@EnableHystrix注释新增到主Spring Boot应用程序类档案中。 @EnableHystrix注释用于将Hystrix功能启用到Spring Boot应用程序中。
主 Spring Boot 应用程序类档案程式码如下 -
package com.felix.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);
}
}
现在编写一个简单的Rest Controller,使其在请求的时间后3秒后返回String。
@RequestMapping(value = "/")
public String hello() throws InterruptedException {
Thread.sleep(3000);
return "Welcome Hystrix";
}
现在,为Rest API新增@Hystrix命令和@HystrixProperty,并以毫秒为单位定义超时值。
@HystrixCommand(fallbackMethod = "fallback_hello", commandProperties = {
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
})
接下来,如果请求需要很长时间来响应,请定义回退方法fallback_hello()。
private String fallback_hello() {
return "Request fails. It takes long time to response";
}
此处显示包含REST API和Hystrix属性的完整Rest Controller类档案 -
@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";
}
在此示例中,REST API编写在主Spring Boot应用程序类档案本身中。
package com.felix.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";
}
}
完整的构建配置档案如下所示。
Maven构建档案 - pom.xml 的内容:
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">
4.0.0
com.felix
hystrixapp
0.0.1-SNAPSHOT
jar
hystrixapp
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.9.RELEASE
UTF-8
UTF-8
1.8
Edgware.RELEASE
org.springframework.cloud
spring-cloud-starter-hystrix
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-test
test
org.springframework.cloud
spring-cloud-dependencies
${spring-cloud.version}
pom
import
org.springframework.boot
spring-boot-maven-plugin
Gradle构建档案 – 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.felix\'
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}"
}
}
可以建立可执行的JAR档案,并使用以下Maven或Gradle命令执行Spring Boot应用程序 -
对于Maven,使用如下所示的命令 -
mvn clean install
在“BUILD SUCCESS”之后,可以在target目录下找到JAR档案。
对于Gradle,使用如下所示的命令 -
gradle clean build
在“BUILD SUCCESSFUL”之后,可以在build/libs 目录下找到JAR档案。现在,使用下面给出的命令执行JAR档案 -
java –jar
这将在Tomcat埠8080上启动应用程序。
现在,从Web浏览器中点选URL => http://localhost:8080/,然后检视Hystrix响应。 API需要3秒钟才能响应,但Hystrix超时为1秒。






























