1、Hystrix簡介
Hystrix是由Nefflix開源的一個延遲和容錯庫,用於隔離訪問遠程系統、服務或第三方庫,防止級聯失敗,從而提升系統的可用性、容錯性與局部應用的彈性,是一個實現了超時機制和熔斷器模式的工具類庫。
2、Hystrix設計原則
- 防止任何單獨的依賴耗盡資源(線程),過載立即切斷並快速失敗,防止排隊。
- 盡可能提供回退以保護用戶免受故障。
- 使用隔離技術(例如隔板、泳道和斷路器模式)來限制任何一個依賴的影響。
- 通過近實時的指標,監控和告警,確保故障被及時發現。
- 通過動態修改配置屬性,確保故障及時恢復。
- 防止整個依賴客戶端執行失敗,而不僅僅是網絡通信。
3、Hystrix工作原理
- 使用命令模式將所有對外部服務(或依賴關系)的調用包裝再HystrixCommand或HystrixObservableCommand對象中,並將該對象放在單獨的線程中執行。
- 每個依賴都維護一個線程池(或信號量),線程池被耗盡則拒絕請求(而不是讓請求排隊)。
- 記錄請求成功,失敗,超時和線程拒絕。
- 服務錯誤百分比超過閥值,熔斷器開關自動打開,一段時間內停止對該服務的所有請求。
- 請求失敗,被拒絕,超時或熔斷時執行降級邏輯。
- 近實時地講課指標和配置的修改。
當使用Hystrix封裝每個基礎依賴項時,每個依賴項彼此隔離,受到延遲時發生飽和的資源的限制,並包含回退邏輯,該邏輯決定了在依賴項中發生任何類型的故障時做出什么響應。
4、Hystrix整合
1)實現eureka-server
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"> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.2.2.RELEASE</version> <relativePath/> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>eureka-server</artifactId> <version>1.0</version> <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>Hoxton.RELEASE</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </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.apache.maven.plugins</groupId> <artifactId>maven-jar-plugin</artifactId> <version>2.6</version> <configuration> <excludes> <exclude>*.**</exclude> <exclude>*/*.xml</exclude> </excludes> <archive> <manifest> <addClasspath>true</addClasspath> <classpathPrefix>lib/</classpathPrefix> <useUniqueVersions>false</useUniqueVersions> <mainClass>cn.kenlab.org.EurekaServerApplication</mainClass> </manifest> <manifestEntries> <Class-Path>./resources/</Class-Path> </manifestEntries> </archive> <outputDirectory>${project.build.directory}</outputDirectory> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.0.1</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib/</outputDirectory> </configuration> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <id>copy-resources</id> <phase>package</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <resources> <resource> <directory>src/main/resources</directory> </resource> </resources> <outputDirectory>${project.build.directory}/resources</outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build> </project>
編寫啟動類,添加@EnableEurekaServer和@SpringBootApplication注解:
@EnableEurekaServer @SpringBootApplication public class EurekaServerApplication { public static void main(String[] args) { SpringApplication.run(EurekaServerApplication.class,args); } }
配置文件application.properties內容:
server.port=8761 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false
2)創建服務提供者
pob文件添加依賴
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
實現啟動類,添加@EnableEurekaClient和@SpringBootApplication注解。
@SpringBootApplication @EnableEurekaClient public class UserServicerApplication { public static void main(String[] args) { SpringApplication.run(UserServicerApplication.class,args); } }
實現UserController接口:
@RestController @RequestMapping("/User") public class UserController { static Map<Integer, User> userMap = new HashMap<>(); static { //模擬數據庫 User user1 = new User(1, "張三", "123456"); userMap.put(1, user1); User user2 = new User(2, "李四", "123123"); userMap.put(2, user2); } @RequestMapping(value = "/getUser",method = RequestMethod.GET) public User getUser(int id) { User user = userMap.get(id); return user; } }
配置文件application.properties內容:
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/ eureka.client.register-with-eureka=true eureka.client.fetch-registry=true server.port=8082 spring.application.name=service
3)在Ribbon中使用熔斷器
pom中添加依賴,熔斷器在spring-cloud-starter-netflix-hystrix包中。
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <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>
啟動類中加上@EnableHystrix注解,開啟熔斷器功能。
@EnableHystrix //在啟動類上添加@EnableHystrix注解開啟Hystrix的熔斷器功能。 @SpringBootApplication @EnableEurekaClient public class ConsumerRibbonApplication { //當添加@LoadBalanced注解,就代表啟動Ribbon,進行負載均衡 @LoadBalanced @Bean public RestTemplate restTemplate() { return new RestTemplate(); } public static void main(String[] args) { SpringApplication.run(ConsumerRibbonApplication.class,args); } }
實現HystrixRibbonController類,在需要有熔斷機制的方法上添加@HystrixCommand,屬性fallbackMethod是熔斷時返回的方法。
@RestController @RequestMapping("/Hystrix/User") public class HystrixRibbonController { @Autowired private RestTemplate restTemplate; /** * 調用 user微服務 */ @HystrixCommand(fallbackMethod = "getDefaultUser") @RequestMapping(value = "getUser",method = RequestMethod.GET) public String getUser(Integer id) { String url = "http://service/User/getUser?id=" + id; return restTemplate.getForObject(url, String.class); } public String getDefaultUser(Integer id) { System.out.println("熔斷,默認回調函數"); return "{\"id\":-1,\"name\":\"熔斷用戶\",\"password\":\"123456\"}"; } }
測試。正常情況下,在postman中訪問192.168.1.136:8080/Hystrix/User/getUser?id=1,服務正常。如下圖所示:
然后停掉service服務,再次訪問192.168.1.136:8080/Hystrix/User/getUser?id=1,此時會觸發熔斷。運行結果如下所示:
4)在Feign中使用熔斷器
pom文件中添加依賴。由於Feign依賴中已經加入了Hystrix依賴,因此,項目中添加Feign依賴后,無需添加Hstrix的其它依賴。只需要在application.properties文件中添加feign.hystrix.enabled=true,開啟熔斷機制即可,默認為false。
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency>
啟動類與普通feign啟動類實現方式相同。即加上@EnableFeignClients。
@SpringBootApplication @EnableEurekaClient @EnableFeignClients public class ConsumerFeignApplication { public static void main(String[] args) { SpringApplication.run(ConsumerFeignApplication.class,args); } }
配置文件application.properties中加入feign.hystrix.enabled=true,開啟熔斷機制。
eureka.client.service-url.defaultZone=http://127.0.0.1:8761/eureka/ eureka.client.register-with-eureka=true eureka.client.fetch-registry=true server.port=8081 spring.application.name=Feign feign.hystrix.enabled=true
實現feign客戶端,調用服務提供者service模塊的getUser接口,並配置fallback快速失敗處理類UserFeignBackImpl。
//表示"service"的服務,指定fallback @FeignClient(value = "service",fallback = UserFeignBackImpl.class) public interface UserInterface { @RequestMapping(value = "/User/getUser",method = RequestMethod.GET) public String getUser(@RequestParam("id") int id); }
實現UserFeignBackImpl類。
@Component public class UserFeignBackImpl implements UserInterface { @Override public String getUser(int id) { return "{\"id\":-1,\"name\":\"熔斷用戶\",\"msg\":\"請求異常,返回熔斷用戶!\"}"; } }
實現外部訪問接口。
@RestController @RequestMapping("/HystrixFeign/User") public class FeignHystrixController { @Autowired private UserInterface userInterface; @RequestMapping(value = "getUser",method = RequestMethod.GET) public String getUser(int id) { return userInterface.getUser(id); } }
測試方法同上。