
- Spring Boot 教程
- Spring Boot - 首頁
- Spring Boot - 簡介
- Spring Boot - 快速入門
- Spring Boot - 引導
- Spring Tool Suite
- Spring Boot - Tomcat 部署
- Spring Boot - 構建系統
- Spring Boot - 程式碼結構
- Spring Bean & 依賴注入
- Spring Boot - 執行器
- Spring Boot - 啟動器
- Spring Boot - 應用程式屬性
- Spring Boot - 配置
- Spring Boot - 註解
- Spring Boot - 日誌記錄
- 構建 RESTful Web 服務
- Spring Boot - 異常處理
- Spring Boot - 攔截器
- Spring Boot - Servlet 過濾器
- Spring Boot - Tomcat 埠號
- Spring Boot - Rest 模板
- Spring Boot - 檔案處理
- Spring Boot - 服務元件
- Spring Boot - Thymeleaf
- 使用 RESTful Web 服務
- Spring Boot - CORS 支援
- Spring Boot - 國際化
- Spring Boot - 排程
- Spring Boot - 啟用 HTTPS
- Spring Boot - Eureka 伺服器
- 使用 Eureka 進行服務註冊
- 閘道器代理伺服器和路由
- Spring Cloud 配置伺服器
- Spring Cloud 配置客戶端
- Spring Boot - Actuator
- Spring Boot - Admin 伺服器
- Spring Boot - Admin 客戶端
- Spring Boot - 啟用 Swagger2
- Spring Boot - 使用 SpringDoc OpenAPI
- Spring Boot - 建立 Docker 映象
- 跟蹤微服務日誌
- Spring Boot - Flyway 資料庫
- Spring Boot - 傳送電子郵件
- Spring Boot - Hystrix
- Spring Boot - WebSocket
- Spring Boot - 批處理服務
- Spring Boot - Apache Kafka
- Spring Boot - Twilio
- Spring Boot - 單元測試用例
- Rest Controller 單元測試
- Spring Boot - 資料庫處理
- 保護 Web 應用程式
- Spring Boot - 使用 JWT 的 OAuth2
- Spring Boot - Google Cloud Platform
- Spring Boot - Google OAuth2 登入
- Spring Boot 資源
- Spring Boot - 快速指南
- Spring Boot - 有用資源
- Spring Boot - 討論
Spring Boot - Hystrix
Hystrix 是 Netflix 的一個庫。Hystrix 隔離服務之間的訪問點,阻止服務之間的級聯故障,並提供回退選項。
注意*− 從 Spring Boot 3 開始,Hystrix 已棄用且不再受支援。我們使用舊版本來展示其功能。
例如,當您呼叫第三方應用程式時,傳送響應需要更多時間。因此,此時控制權將轉移到回退方法,並向您的應用程式返回自定義響應。
在本章中,您將瞭解如何在 Spring Boot 應用程式中實現 Hystrix。
首先,我們需要在構建配置檔案中新增 Spring Cloud Starter Hystrix 依賴項。
Maven 使用者可以在 pom.xml 檔案中新增以下依賴項:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency>
Gradle 使用者可以在 build.gradle 檔案中新增以下依賴項:
compile('org.springframework.cloud:spring-cloud-starter-hystrix')
現在,將 @EnableHystrix 註解新增到您的主 Spring Boot 應用程式類檔案中。@EnableHystrix 註解用於在您的 Spring Boot 應用程式中啟用 Hystrix 功能。
主 Spring Boot 應用程式類檔案程式碼如下所示:
package com.tutorialspoint.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 "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 應用程式類檔案中。
HystrixappApplication.java
package com.tutorialspoint.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 檔案
<?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.tutorialspoint</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>
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.tutorialspoint' 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 <JARFILE>
這將在 Tomcat 埠 8080 上啟動應用程式,如下所示:

現在,從您的 Web 瀏覽器訪問 URL https://:8080/,並檢視 Hystrix 響應。API 需要 3 秒才能響應,但 Hystrix 超時時間為 1 秒。
