暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

SpringCloud-3-Hystrix服务容错_保护断路器

荒凉 技术路 2020-04-25
341

前言

Github:https://github.com/HealerJean

博客:http://blog.healerjean.com

前面做了一个简单的服务 注册。服务发现,服务提供者和消费者的项目,现在我们还是准备之前的项目代码

1、 Hystrix
:容错保护 (消费者:3001)

hystrix
对应的中文名字是“豪猪”,豪猪周身长满了刺,能保护自己不受天敌的伤害,代表了一种防御机制,这与 hystrix
本身的功能不谋而合,因此 Netflix
团队将该框架命名为 Hystrix
,并使用了对应的卡通形象做作为logo。


在一个分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,如何能够保证在一个依赖出问题的情况下,不会导致整体服务失败,这个就是 Hystrix
需要做的事情。

Hystrix
提供了熔断、隔离、 Fallback
cache
、监控等功能,能够在一个、或多个依赖同时出现问题时保证系统依然可用。

1.1、 pom.xml
添加依赖

  1. <!--hystrix 容错保护-->

  2. <dependency>

  3. <groupId>org.springframework.cloud</groupId>

  4. <artifactId>spring-cloud-starter-hystrix</artifactId>

  5. </dependency>

  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 https://maven.apache.org/xsd/maven-4.0.0.xsd">

  4. <modelVersion>4.0.0</modelVersion>

  5. <parent>

  6. <groupId>com.healerjean.proj</groupId>

  7. <artifactId>hlj-parent</artifactId>

  8. <version>1.0.0-SNAPSHOT</version>

  9. </parent>


  10. <artifactId>hlj-server-consumer-3001</artifactId>

  11. <version>${project.healerjean.version}</version>


  12. <properties>

  13. <java.version>1.8</java.version>

  14. </properties>


  15. <dependencies>


  16. <!--eureka 客户端,处理服务的注册和发现-->

  17. <dependency>

  18. <groupId>org.springframework.cloud</groupId>

  19. <artifactId>spring-cloud-starter-eureka</artifactId>

  20. </dependency>


  21. <!--ribbon 消费服务,它同时也作为辅助均衡器-->

  22. <dependency>

  23. <groupId>org.springframework.cloud</groupId>

  24. <artifactId>spring-cloud-starter-ribbon</artifactId>

  25. </dependency>



  26. <!--hystrix 容错保护-->

  27. <dependency>

  28. <groupId>org.springframework.cloud</groupId>

  29. <artifactId>spring-cloud-starter-hystrix</artifactId>

  30. </dependency>


  31. <!--swagger-->

  32. <dependency>

  33. <groupId>io.springfox</groupId>

  34. <artifactId>springfox-swagger2</artifactId>

  35. </dependency>

  36. <dependency>

  37. <groupId>io.springfox</groupId>

  38. <artifactId>springfox-swagger-ui</artifactId>

  39. </dependency>


  40. <!--lombok-->

  41. <dependency>

  42. <groupId>org.projectlombok</groupId>

  43. <artifactId>lombok</artifactId>

  44. </dependency>


  45. <!--StringUtils-->

  46. <dependency>

  47. <groupId>org.apache.commons</groupId>

  48. <artifactId>commons-lang3</artifactId>

  49. </dependency>


  50. </dependencies>


  51. <build>

  52. <plugins>

  53. <plugin>

  54. <groupId>org.springframework.boot</groupId>

  55. <artifactId>spring-boot-maven-plugin</artifactId>

  56. </plugin>

  57. </plugins>

  58. </build>


  59. </project>

1.2、启动类:

@EnableCircuitBreaker
开启断路器功能

@SpringCloudApplication
:可以代替如下三个

@EnableCircuitBreaker
//开启断路器功能@EnableDiscoveryClient
//开启服务发现客户端,@SpringBootApplication

  1. package com.healerjean.proj;


  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.client.loadbalancer.LoadBalanced;

  7. import org.springframework.context.annotation.Bean;

  8. import org.springframework.web.client.RestTemplate;


  9. @EnableCircuitBreaker //开启断路器功能

  10. @EnableDiscoveryClient //支持服务发现

  11. @SpringBootApplication

  12. ////@SpringCloudApplication //可以取代上面三个

  13. public class ServerConsumer_3001_Application {


  14. //开启客户端负载均衡

  15. @Bean

  16. @LoadBalanced

  17. RestTemplate restTemplate() {

  18. return new RestTemplate();

  19. }


  20. public static void main(String[] args) {

  21. SpringApplication.run(ServerConsumer_3001_Application.class, args);

  22. }


  23. }

1.3、 @HystrixCommand
:服务降级

@HystrixCommand
方法内部报错之后,不会抛出异常,自动去找回滚的方法

1.3.1、 ConsumeService

  1. public interface ConsumeService {


  2. String testFallBack();

  3. }

1.3.2、 ConsumeServiceImpl

  1. @Service

  2. public class ConsumeServiceImpl implements ConsumeService {


  3. @Autowired

  4. private RestTemplate restTemplate;

  5. @Value("${hlj.server.providerName}")

  6. private String serverProviderName;


  7. @Override

  8. @HystrixCommand(fallbackMethod = "fallBack")

  9. public String testFallBack() {

  10. return restTemplate.getForEntity(

  11. "http://" + serverProviderName + "/api/provider/connect/",

  12. String.class).getBody();


  13. // int i = 1/0;

  14. // return "success";

  15. }


  16. public String fallBack() {

  17. return "testFallBack 方法不可用,服务降级";

  18. }


  19. }

1.4、访问接口 :启动所有的服务测试

服务注册中心:hlj-eureka-server-1111
服务注册中心:hlj-eureka-server-1112

服务提供者:hlj-server-provider-2001
服务提供者:hlj-server-provider-2002

服务消费者:hlj-server-consumer-3001

1.4.1、访问接口

  1. http://127.0.0.1:3001/api/consumer/hystrix/fallBack

接口返回

服务提供者:2002

  1. {

  2. "host": "localhost",

  3. "port": 2002,

  4. "metadata": {},

  5. "serviceId": "hlj-server-provider",

  6. "secure": false,

  7. "uri": "http://localhost:2002"

  8. }

服务提供者:2001

  1. {

  2. "host": "localhost",

  3. "port": 2001,

  4. "metadata": {},

  5. "secure": false,

  6. "serviceId": "hlj-server-provider",

  7. "uri": "http://localhost:2001"

  8. }

1.5、断路器测试

1.5.1、这个时候断开服务提供者:2002

这个时候我们将2002 的服务提供者挂掉,继续访问,发现一会正常,一会显示错误信息。

但是时间长了,就还是只会显示正常 ,出现这种情况,应该是注册中心没有及时检测到挂掉了2002,还继续提供给消费者服务。但是时间长了,就肯定原型毕露

接口返回

服务提供者:2002

  1. testFallBack 方法不可用,服务降级

服务提供者:2001

  1. {

  2. "host": "localhost",

  3. "port": 2001,

  4. "metadata": {},

  5. "secure": false,

  6. "serviceId": "hlj-server-provider",

  7. "uri": "http://localhost:2001"

  8. }

15.2、 Hystrix
默认超时2000毫秒

如果某个服务提供者超时了,就会进行容错保护,按照上面的测试



文章转载自荒凉 技术路,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论