在一个分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,如何能够保证在一个依赖出问题的情况下,不会导致整体服务失败,这个就是Hystrix需要做的事情。Hystrix提供了熔断、隔离、Fallback、cache、监控等功能,能够在一个、或多个依赖同时出现问题时保证系统依然可用。

在分布式系统架构中多个系统之间通常是通过远程RPC调用进行通信,也就是 A 系统调用 B 系统服务,B 系统调用 C 系统的服务。当尾部应用 C 发生故障而系统 B 没有服务降级时候可能会导致 B,甚至系统 A 瘫痪,这种现象被称为雪崩现象。所以在系统设计时候要使用一定的降级策略,来保证当服务提供方服务不可用时候,服务调用方可以切换到降级后的策略进行执行。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.xm.cloud</groupId>
  6. <artifactId>cl_hello_consumer_hy</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>cl_hello_consumer_hy</name>
  10. <description>This is a Web about springcloud</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.6.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. <spring-cloud.version>Finchley.SR2</spring-cloud.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.cloud</groupId>
  34. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.cloud</groupId>
  38. <artifactId>spring-cloud-starter-openfeign</artifactId>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework.boot</groupId>
  42. <artifactId>spring-boot-starter-test</artifactId>
  43. <scope>test</scope>
  44. </dependency>
  45. <dependency>
  46. <groupId>org.springframework.boot</groupId>
  47. <artifactId>spring-boot-starter-actuator</artifactId>
  48. </dependency>
  49. </dependencies>
  50. <dependencyManagement>
  51. <dependencies>
  52. <dependency>
  53. <groupId>org.springframework.cloud</groupId>
  54. <artifactId>spring-cloud-dependencies</artifactId>
  55. <version>${spring-cloud.version}</version>
  56. <type>pom</type>
  57. <scope>import</scope>
  58. </dependency>
  59. </dependencies>
  60. </dependencyManagement>
  61. <build>
  62. <plugins>
  63. <plugin>
  64. <groupId>org.springframework.boot</groupId>
  65. <artifactId>spring-boot-maven-plugin</artifactId>
  66. </plugin>
  67. </plugins>
  68. </build>
  69. </project>
  1. eureka.client.service-url.defaultZone=http://127.0.0.1:7001/eureka/
  2. eureka.client.register-with-eureka=false
  3. feign.hystrix.enabled=true
  1. package com.xm.cloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6. import org.springframework.cloud.openfeign.EnableFeignClients;
  7. @EnableDiscoveryClient
  8. @EnableFeignClients
  9. @SpringBootApplication
  10. public class ClHelloConsumerHyApplication {
  11. public static void main(String[] args) {
  12. SpringApplication.run(ClHelloConsumerHyApplication.class, args);
  13. }
  14. }
  1. package com.xm.cloud.fallback;
  2. import org.springframework.stereotype.Component;
  3. import com.xm.cloud.service.HelloService;
  4. import feign.hystrix.FallbackFactory;
  5. @Component
  6. public class HelloServiceFallbackFactory implements FallbackFactory<HelloService> {
  7. @Override
  8. public HelloService create(Throwable cause) {
  9. return new HelloService() {
  10. @Override
  11. public String sayHello() {
  12. return "HelloService 异常!";
  13. }
  14. };
  15. }
  16. }
  1. package com.xm.cloud.service;
  2. import org.springframework.cloud.openfeign.FeignClient;
  3. import org.springframework.web.bind.annotation.GetMapping;
  4. import com.xm.cloud.fallback.HelloServiceFallbackFactory;
  5. @FeignClient(value="CL-HELLO-PRODUCER",fallbackFactory=HelloServiceFallbackFactory.class)
  6. public interface HelloService {
  7. @GetMapping("/hello")
  8. public String sayHello();
  9. }
  1. package com.xm.cloud.controller;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.web.bind.annotation.GetMapping;
  6. import org.springframework.web.bind.annotation.RestController;
  7. import com.xm.cloud.service.HelloService;
  8. @RestController
  9. public class HelloController {
  10. @Autowired
  11. private HelloService helloService;
  12. @GetMapping("/hello")
  13. public List<String> sayHello() {
  14. List<String> list = new ArrayList<String>();
  15. for(int i=0;i<10;i++) {
  16. list.add(helloService.sayHello());
  17. }
  18. return list;
  19. }
  20. }
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <groupId>com.xm.cloud</groupId>
  6. <artifactId>cl_hello_producer_hy</artifactId>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <packaging>jar</packaging>
  9. <name>cl_hello_producer_hy</name>
  10. <description>This is a Web about springcloud</description>
  11. <parent>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-parent</artifactId>
  14. <version>2.0.6.RELEASE</version>
  15. <relativePath/> <!-- lookup parent from repository -->
  16. </parent>
  17. <properties>
  18. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  19. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  20. <java.version>1.8</java.version>
  21. <spring-cloud.version>Finchley.SR2</spring-cloud.version>
  22. </properties>
  23. <dependencies>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.cloud</groupId>
  30. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  31. </dependency>
  32. <dependency>
  33. <groupId>org.springframework.cloud</groupId>
  34. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  35. </dependency>
  36. <dependency>
  37. <groupId>org.springframework.boot</groupId>
  38. <artifactId>spring-boot-starter-test</artifactId>
  39. <scope>test</scope>
  40. </dependency>
  41. </dependencies>
  42. <dependencyManagement>
  43. <dependencies>
  44. <dependency>
  45. <groupId>org.springframework.cloud</groupId>
  46. <artifactId>spring-cloud-dependencies</artifactId>
  47. <version>${spring-cloud.version}</version>
  48. <type>pom</type>
  49. <scope>import</scope>
  50. </dependency>
  51. </dependencies>
  52. </dependencyManagement>
  53. <build>
  54. <plugins>
  55. <plugin>
  56. <groupId>org.springframework.boot</groupId>
  57. <artifactId>spring-boot-maven-plugin</artifactId>
  58. </plugin>
  59. </plugins>
  60. </build>
  61. </project>
  1. server.port=8001
  2. spring.application.name=cl-hello-producer
  3. eureka.client.service-url.defaultZone=http://127.0.0.1:7001/eureka/
  1. package com.xm.cloud;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
  5. import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
  6. @EnableDiscoveryClient
  7. @EnableCircuitBreaker
  8. @SpringBootApplication
  9. public class ClHelloProducerHyApplication {
  10. public static void main(String[] args) {
  11. SpringApplication.run(ClHelloProducerHyApplication.class, args);
  12. }
  13. }
  1. package com.xm.cloud.controller;
  2. import org.springframework.web.bind.annotation.GetMapping;
  3. import org.springframework.web.bind.annotation.RestController;
  4. import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
  5. @RestController
  6. public class HelloController {
  7. @GetMapping("/hello")
  8. @HystrixCommand
  9. public String sayHello() {
  10. if(Math.random()>0.5) {
  11. throw new RuntimeException();
  12. } else {
  13. return "Hello spring cloud!";
  14. }
  15. }
  16. }

运行:localhost:8080/hello

0 “HelloService 异常!”
1 “Hello spring cloud!”
2 “HelloService 异常!”
3 “Hello spring cloud!”
4 “Hello spring cloud!”
5 “Hello spring cloud!”
6 “Hello spring cloud!”
7 “Hello spring cloud!”
8 “HelloService 异常!”
9 “Hello spring cloud!”

版权声明:本文为TimerHotel原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/TimerHotel/p/springcloud_05.html