1.  引言


一般而言,一个服务都是部署了多台机器的,那么在这种情况下,当其中一个服务挂了以后Hystrix是怎么处理的呢?

为了验证这个问题,我们准备两个服务:user-apiapp-gateway,再加一个Eureka Server

2.  服务搭建


2.1.  注册中心

关于这一部分,参见《SpringCloud学习笔记(1)——Eureka

2.2.  服务提供方

  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.  
  6. <groupId>com.cjs.example</groupId>
  7. <artifactId>user-api</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10.  
  11. <name>user-api</name>
  12. <description></description>
  13.  
  14. <parent>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-parent</artifactId>
  17. <version>2.0.5.RELEASE</version>
  18. <relativePath/> <!-- lookup parent from repository -->
  19. </parent>
  20.  
  21. <properties>
  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  24. <java.version>1.8</java.version>
  25. <spring-cloud.version>Finchley.SR1</spring-cloud.version>
  26. </properties>
  27.  
  28. <dependencies>
  29. <dependency>
  30. <groupId>org.springframework.cloud</groupId>
  31. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  32. </dependency>
  33.  
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-web</artifactId>
  37. </dependency>
  38. <dependency>
  39. <groupId>org.springframework.boot</groupId>
  40. <artifactId>spring-boot-starter-actuator</artifactId>
  41. </dependency>
  42. </dependencies>
  43.  
  44. <dependencyManagement>
  45. <dependencies>
  46. <dependency>
  47. <groupId>org.springframework.cloud</groupId>
  48. <artifactId>spring-cloud-dependencies</artifactId>
  49. <version>${spring-cloud.version}</version>
  50. <type>pom</type>
  51. <scope>import</scope>
  52. </dependency>
  53. </dependencies>
  54. </dependencyManagement>
  55.  
  56. <build>
  57. <plugins>
  58. <plugin>
  59. <groupId>org.springframework.boot</groupId>
  60. <artifactId>spring-boot-maven-plugin</artifactId>
  61. </plugin>
  62. </plugins>
  63. </build>
  64.  
  65. </project>
  1. server:
  2. port: 8081
  3. spring:
  4. application:
  5. name: user-api
  6.  
  7. eureka:
  8. instance:
  9. instance-id: 192.168.1.1:${server.port}
  10. hostname: 192.168.1.1
  11. client:
  12. service-url:
  13. defaultZone: http://192.168.1.1:8761/eureka/
  1. package com.cjs.example;
  2. import org.springframework.boot.SpringApplication;
  3. import org.springframework.boot.autoconfigure.SpringBootApplication;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @RestController
  7. @SpringBootApplication
  8. public class UserApiApplication {
  9. public static void main(String[] args) {
  10. SpringApplication.run(UserApiApplication.class, args);
  11. }
  12. @RequestMapping("/sayHello")
  13. public String sayHello() {
  14. System.out.println("lalala");
  15. return "hello";
  16. }
  17. }

这里通过控制打印日志来观察调用的是哪个机器上的该服务

2.3.  服务消费方

pom.xml

  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.  
  6. <groupId>com.cjs.example</groupId>
  7. <artifactId>app-gatewate</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10.  
  11. <name>app-gatewate</name>
  12. <description></description>
  13.  
  14. <parent>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-parent</artifactId>
  17. <version>2.0.5.RELEASE</version>
  18. <relativePath/> <!-- lookup parent from repository -->
  19. </parent>
  20.  
  21. <properties>
  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  24. <java.version>1.8</java.version>
  25. <spring-cloud.version>Finchley.SR1</spring-cloud.version>
  26. </properties>
  27.  
  28. <dependencies>
  29. <dependency>
  30. <groupId>org.springframework.cloud</groupId>
  31. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>org.springframework.cloud</groupId>
  35. <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  36. </dependency>
  37. <dependency>
  38. <groupId>org.springframework.cloud</groupId>
  39. <artifactId>spring-cloud-starter-openfeign</artifactId>
  40. </dependency>
  41.  
  42. <dependency>
  43. <groupId>org.springframework.boot</groupId>
  44. <artifactId>spring-boot-starter-web</artifactId>
  45. </dependency>
  46. <dependency>
  47. <groupId>org.springframework.boot</groupId>
  48. <artifactId>spring-boot-starter-actuator</artifactId>
  49. </dependency>
  50. </dependencies>
  51.  
  52. <dependencyManagement>
  53. <dependencies>
  54. <dependency>
  55. <groupId>org.springframework.cloud</groupId>
  56. <artifactId>spring-cloud-dependencies</artifactId>
  57. <version>${spring-cloud.version}</version>
  58. <type>pom</type>
  59. <scope>import</scope>
  60. </dependency>
  61. </dependencies>
  62. </dependencyManagement>
  63.  
  64. <build>
  65. <plugins>
  66. <plugin>
  67. <groupId>org.springframework.boot</groupId>
  68. <artifactId>spring-boot-maven-plugin</artifactId>
  69. </plugin>
  70. </plugins>
  71. </build>
  72.  
  73. </project>

application.yml

  1. server:
  2. port: 8080
  3. spring:
  4. application:
  5. name: app-gateway
  6. eureka:
  7. instance:
  8. instance-id: 192.168.1.1:${server.port}
  9. hostname: 192.168.1.1
  10. client:
  11. service-url:
  12. defaultZone: http://192.168.1.1:8761/eureka/
  13. feign:
  14. hystrix:
  15. enabled: true

AppGatewayApplication.java

  1. package com.cjs.example;
  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.openfeign.EnableFeignClients;
  6. @EnableCircuitBreaker
  7. @EnableFeignClients
  8. @SpringBootApplication
  9. public class AppGatewayApplication {
  10. public static void main(String[] args) {
  11. SpringApplication.run(AppGatewayApplication.class, args);
  12. }
  13. }

FeignClient

  1. package com.cjs.example.service;
  2. import com.cjs.example.fallback.UserClientFallback;
  3. import org.springframework.cloud.openfeign.FeignClient;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. @FeignClient(value = "user-api", fallback = UserClientFallback.class)
  6. public interface UserClient {
  7. @RequestMapping("/sayHello")
  8. String sayHello();
  9. }

Hystrix

  1. package com.cjs.example.fallback;
  2. import com.cjs.example.service.UserClient;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. public class UserClientFallback implements UserClient {
  6. @Override
  7. public String sayHello() {
  8. return "Oh! Error!!!";
  9. }
  10. }

UserController.java

  1. package com.cjs.example.controller;
  2. import com.cjs.example.service.UserClient;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @RestController
  7. @RequestMapping("/user")
  8. public class UserController {
  9. @Autowired
  10. private UserClient userClient;
  11. @RequestMapping("/hello")
  12. public String hello() {
  13. return userClient.sayHello();
  14. }
  15. }

目录结构

3.  演示


1、依次启动各服务:eureka-demo-server,user-api(两台),app-gateway

2、浏览器查看 http://192.168.1.1:8761/

3、通过请求 http://localhost:8080/user/hello 令app-gateway调用user-api服务(使用FeignClient调用,并集成Hystrix)

4、观察控制打印“lalala”,同时页面输出“hello”

5、停掉其中一台user-api,发现在短时间内会执行回退操作(即页面返回“Oh! Error!!!”),随后恢复正常(因为请求打到另一台机器上了)

 

由此,可以看出

首先,一般Hystrix 和 Ribbon一起使用的,而且必要有注册中心,在这种情况下,如果调用某个服务失败(或者拒绝、超时等),会尝试调用其它机器上的该服务,但这种切换需要一定的时间,并不是无缝切换的(也就是说,并不是在服务调用失败或拒绝的时候理解换到另一台机器,在所有机器都尝试过以后都失败的情况下才执行回退逻辑,不是这样的,可以想象一下,单纯依靠Hystrix的能力是不能做到这一点的,况且,Hystrix本身设计就不是用来做这个事情的,它设计是用来阻止级联失败,用快速失败来代替请求排队的,但是有了客户端负载均衡器以后,当其中一台机器挂了以后,请求会转发到其它机器上。因此,切换是负载均衡器做的,跟断路器没有关系)

 

4.  小结


Ribbon是一个客户端的负载均衡器,它决定选择哪一台机器来调用

Hystrix是一个断路器,它将服务调用进行隔离,用快速失败来代替排队,阻止级联调用失败。它的目的是不让服务挂掉。

二者之间没有关系,因为设计它们的目的不一样,解决的问题也不一样

再回到开头提到的问题,即使没有Hystrix,一台机器挂掉以后,后续的请求也会转到其它机器上。只是有了Hystrix以后,遇到这种失败的时候会执行回退操作。

5.  其它


Hystrix介绍

Hystrix是如何工作的

Spring Boot 集成 Hystrix

SpringCloud学习笔记(3)——Hystrix

SpringCloud学习笔记(2)——Ribbon

SpringCloud学习笔记(1)——Eureka

 

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