spring boot hystrix——迹忆客-ag捕鱼王app官网
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
注解用于在 spring boot 应用程序中启用 hystrix 功能。
下面给出主要的 spring boot 应用程序类文件代码
package com.jiyik.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 秒后返回字符串。
@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 "请求失败。 需要很长时间才能响应";
}
包含 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 "请求失败。 需要很长时间才能响应";
}
在此示例中,rest api 编写在主 spring boot 应用程序类文件本身中。
package com.jiyik.hystrixapp;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.cloud.netflix.hystrix.enablehystrix;
import org.springframework.web.bind.annotation.requestmapping;
@springbootapplication
@enablehystrix
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 "请求失败。 需要很长时间才能响应";
}
}
现在,我们可以创建一个可执行的 jar 文件,并使用以下 maven 或 gradle 命令运行 spring boot 应用程序。
对于 maven,使用下面给出的命令
$ mvn clean install
成功之后,我们可以在 target 目录下找到 jar 文件。
对于 gradle,使用下面给出的命令
$ gradle clean build
这里我们使用 idea 来启动服务(读者也可以使用上面两种方式中的一种生成可执行 jar 包)
现在,从web 浏览器中点击 url http://localhost:8080/
,然后查看 hystrix 响应。 api 需要 3 秒响应,但 hystrix 超时为 1 秒。