开发手册 欢迎您!
软件开发者资料库

Spring Boot - Hystrix

Spring Boot Hystrix - 从简单和简单的步骤学习Spring Boot,从基本到高级概念,包括简介,快速入门,引导,Tomcat部署,构建系统,代码结构,Spring Bean和依赖注入,Runners,Application Properties,Logging,构建RESTful Web服务,异常处理,拦截器,Servlet过滤器,Tomcat端口号,Rest模板,文件处理,服务组件,Thymeleaf,使用RESTful Web服务,CORS支持,国际化,调度,启用HTTPS,Eureka服务器,使用Eureka进行服务注册,Zuul代理服务器和路由,Spring云配置服务器,Spring云配置客户端,执行器,管理服务器,管理客户端,启用Swagger2,创建Docker镜像,跟踪微服务日志,Flyway数据库,发送电子邮件,Hystrix,Web套接字,批量服务,Spring for Apache Kafka,Twilio,单元测试用例,静态控制器单元测试,数据库处理,保护Web应用程序ations,OAuth2 with JWT,Google Cloud Platform,Google OAuth2 Sign-In。

Hystrix是Netflix的一个库. Hystrix隔离了服务之间的访问点,阻止了它们之间的级联故障,并提供了后备选项.

例如,当您调用3 rd 方时应用程序,发送响应需要更多时间.那么在那个时候,控件转到了回退方法并将自定义响应返回给你的应用程序.

在本章中,您将看到如何在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.it1352.hystrixapp; import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.netflix.hystrix.EnableHystrix;@SpringBootApplication@EnableHystrixpublic 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.it1352.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@RestControllerpublic 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文件

      4.0.0   com.it1352   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.it1352'version = '0.0.1-SNAPSHOT'sourceCompatibility = 1.8repositories {   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"之后",你可以在目标目录下找到JAR文件.

对于Gradle,使用如下所示的命令 :

gradle clean build

在"BUILD SUCCESSFUL"之后,你可以在build/libs目录下找到JAR文件.

现在,使用下面给出的命令运行JAR文件 :

java -jar 

这将在Tomcat端口8080上启动应用程序,如下所示 :

Tomcat应用程序命令提示符

现在,从网络浏览器点击URL http://localhost:8080/,并看到Hystrix的回应. API需要3秒钟才能响应,但Hystrix超时为1秒.

请求失败Hystrix超时