Spring Boot Hystrix

  • Hystrix

    Hystrix是Netflix的库。Hystrix隔离服务之间的访问点,停止服务之间的级联故障,并提供后备选项。
    例如,当你调用一个第三方应用程序,它需要更多的时间来发送响应。因此,Hystrix控件将转到后备方法,并将自定义响应返回到您的应用程序。
    在本章中,您将看到如何在Spring Boot应用程序中实现Hystrix。
    首先,我们需要在构建配置文件中添加Spring Cloud Starter Hystrix依赖项。
    Maven用户可以在pom.xml文件中添加以下依赖项-
    Maven用户可以将以下依赖项添加到pom.xml文件中。
    
    <dependency>
       <groupId>org.springframework.cloud</groupId>
       <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
    </dependency>
    
    对于Gradle用户,在build.gradle文件中添加以下依赖项。
    
    implementation 'org.springframework.cloud:spring-cloud-starter-hystrix' 
    
    现在,将@EnableHystrix注解添加到主Spring Boot应用程序类文件中,@EnableHystrix注解用于将Hystrix功能启用到您的Spring Boot应用程序中。
    下面给出了主要的Spring Boot应用程序类文件代码-
    
    package com.jc2182.demo;
    
    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.jc2182.demo;
    
    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
    
    <?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
            <modelVersion>4.0.0</modelVersion>
            <parent>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-parent</artifactId>
                    <version>2.2.6.RELEASE</version>
                    <relativePath/> <!-- lookup parent from repository -->
            </parent>
            <groupId>com.jc2182</groupId>
            <artifactId>demo</artifactId>
            <version>0.0.1-SNAPSHOT</version>
            <name>demo</name>
            <description>Demo project for Spring Boot</description>
    
            <properties>
                    <java.version>1.8</java.version>
                    <spring-cloud.version>Hoxton.SR3</spring-cloud.version>
            </properties>
    
            <dependencies>
                    <dependency>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-starter-web</artifactId>
                    </dependency>
                    <dependency>
                            <groupId>org.springframework.cloud</groupId>
                            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
                    </dependency>
    
                    <dependency>
                            <groupId>org.springframework.boot</groupId>
                            <artifactId>spring-boot-starter-test</artifactId>
                            <scope>test</scope>
                            <exclusions>
                                    <exclusion>
                                            <groupId>org.junit.vintage</groupId>
                                            <artifactId>junit-vintage-engine</artifactId>
                                    </exclusion>
                            </exclusions>
                    </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>
    
    Gradle – build.gradle
    
    plugins {
            id 'org.springframework.boot' version '2.2.6.RELEASE'
            id 'io.spring.dependency-management' version '1.0.9.RELEASE'
            id 'java'
    }
    
    group = 'com.jc2182'
    version = '0.0.1-SNAPSHOT'
    sourceCompatibility = '1.8'
    
    repositories {
            mavenCentral()
    }
    
    ext {
            set('springCloudVersion', "Hoxton.SR3")
    }
    
    dependencies {
            implementation 'org.springframework.boot:spring-boot-starter-web'
            implementation 'org.springframework.cloud:spring-cloud-starter-netflix-hystrix'
            testImplementation('org.springframework.boot:spring-boot-starter-test') {
                    exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
            }
    }
    
    dependencyManagement {
            imports {
                    mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
            }
    }
    
    test {
            useJUnitPlatform()
    }
    
    您可以创建一个可执行的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 <JARFILE>
    
    现在,该应用程序已使用https在Tomcat端口8080上启动,如下所示-
    springboot hystrix
    现在,从您的Web浏览器访问URL http://localhost:8080/,然后查看Hystrix响应。该API需要3秒钟来响应,但是Hystrix超时是1秒(调用了回调函数)。
    springboot hystrix