纵横研究院微服务&容器专题社区Spring Cloud

熔断器Hystrix

2019-05-12  本文已影响15人  董二弯

微服务中服务之间的相互依赖性错综复杂,一个网络请求通常需要调用多个服务实例才能完成。如果一个服务不可用,如网络延迟或故障,会影响依赖于这个服务的其它服务,最后导致整个系统处于瘫痪的状态。为解决这种雪崩效应,引进熔断组件Hystrix,当出现故障后会自动剔除服务实列。Hystrix除了一些基本的熔断器,还能实现服务降级、服务限流的功能,并提供了界面展示健康状态的功能。本章讲诉如何使用RestTemplate和Feign消费服务是使用Hystrix,并介绍Hystrix的工作原理。

Hystrix的设计原则

Hystrix的工作机制

当服务的某个API接口的失败次数在一定时间内小于设定的阈值时,熔断器处于关闭状态,该API接口正常提供服务。当该API接口处理请求的失败次数大于设定的阈值时,Hystrix判定该API接口出现了故障,打开熔断器,这时该API接口会执行快速失败的逻辑,不执行业务逻辑,请求的线程不会处于阻塞状态。处于打开状态的熔断器在一定时间后会处于半打开状态,并将一定数量的请求执行正常逻辑,剩余的请求会执行快速失败。若执行正常逻辑的请求失败了,则熔断器继续打开,若成功了,则熔断器关闭。这样熔断器就具有了自我修复的功能。

在RestTemplate上使用熔断器

本章案例在前面章节的案例基础之上进行改造。在讲述ribbon章节时,eureka-ribbon-client工程中我们使用RestTemplate调用了eureka-client的“/hi”接口,并用Ribbon做了负载均衡,现在在此基础上加Hystrix熔断器的功能。

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableHystrixDashboard
public class EurekaRibbonClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaRibbonClientApplication.class, args);
    }
}
public class RibbonServiceHystrix {
    @Autowired
    private RestTemplate restTemplate;

    @HystrixCommand(fallbackMethod = "hiError")
    public String hiFromClient() {
        return restTemplate.getForObject("http://eureka-client/hi?name=dzy", String.class);
    }
    public String hiError(){
        return "error";
    }
}

在Feign上使用熔断器

feign:
  hystrix:
    enabled: true
@FeignClient(value = "eureka-client", configuration = FeignClientConfig.class,fallback = HiFailBack.class)
public interface HiFeignClient {
    @GetMapping("/hi")
    String hi(@RequestParam(value = "name") String name);
}
@Component
public class HiFailBack implements HiFeignClient {

    @Override
    public String hi(String name) {
        return "hystrix error";
    }
}

使用Hystrix Dashboard监控熔断器的状态

Hystrix Dashboard 是监控Hystrix的熔断状况的一个组件,提供了数据监控和友好的图形化展示界面。

在RestTemplate中使用Hystrix Dashboard

在eureka-ribbon-client的基础上改造。

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
@SpringBootApplication
@EnableEurekaClient
@EnableHystrix
@EnableHystrixDashboard
public class EurekaRibbonClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaRibbonClientApplication.class, args);
    }
}

在feign中使用Hystrix Dashboard

 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
@EnableHystrixDashboard
@EnableHystrix
public class EurekaFeignClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaFeignClientApplication.class, args);
    }
}

只需要上述两步就可以在Feign中开启Hystrix Dashboard的功能。

使用Turbine聚合监控

在使用Hystrix Dashboard组件监控服务的熔断状况时,每个服务都有一个Hystrix Dashboard主页,当服务数量很多时,监控非常不方便。为了同时监控多个服务的熔断器的状况,Netflix开源了Hystrix另一个组件Turbine。Turbine用于聚合多个Hystrix Dashboard,将多个Hystrix Dashboard组件的数据放在一个页面上展示,进行集中监控。

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
</dependency>

<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-turbine</artifactId>
</dependency>
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
 </dependency>
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
 </dependency>
server:
  port: 8770
spring:
  application:
    name: turbine-server
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
management:
  security:
    enabled: false
turbine:
  app-config: eureka-ribbon-client,eureka-feign-client
  cluster-name-expression: new String("default")
  aggregator:
    clusterConfig: default

其中turbine.app-config配置了需要监控的服务名,turbine.cluster-name-expression默认为服务器的集群,此时用默认的即可。turbine.aggregator.clusterConfig可以不写,因为默认就是default。

@SpringBootApplication
@EnableEurekaClient
@EnableTurbine
public class TurbineServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(TurbineServerApplication.class, args);
    }

}

总结

在这一章学习了熔断器Hystrix,其中包括什么是Hystrix,Hystrix的工作原理,TestTemplate、Feign结合Hystrix的使用。最后介绍使用图形化监控组件Hystrix Dashboard,以及监控聚合组件Turbine。在下一章中介绍路由网关Zuul。

PS:项目github地址:https://github.com/dzydzydzy/spring-cloud-example.git

上一篇 下一篇

猜你喜欢

热点阅读