服务熔断与熔断监控

作者: AaronSimon | 来源:发表于2018-09-26 23:52 被阅读36次

在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign来调用。为了保证其高可用,单个服务通常会集群部署。由于网络原因或者自身的原因,服务并不能保证100%可用,如果单个服务出现问题,调用这个服务就会出现线程阻塞,此时若有大量的请求涌入,Servlet容器的线程资源会被消耗完毕,导致服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统造成灾难性的严重后果,这就是服务故障的“雪崩”效应。
本文主要内容如下:

  • Feign使用Hystrix
  • Hystrix Dashboard监控

一、Hystrix的工作机制

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

二、Hystrix使用与Hystrix Dashboard监控

2.1 示例介绍

服务名 端口号 服务说明
eureka 8761 服务注册于发现
provider 8800 服务提供者
consumer 8801 服务消费者(feign使用Hystrix)

2.2 provider服务提供者

1. 服务接口
@RestController
public class HelloController {
    @RequestMapping(value = "/getBookByName")
    public String getNameById(@RequestParam("id") String id){
        String bookName = "";
        switch (id){
            case "1" :
                bookName = "Java";
                break;
            case "2" :
                bookName = "C++";
                break;
            default:
                bookName = "phython";
        }
        return bookName;
    }
}
2. 配置文件
server:
  port: 8800
  servlet:
    context-path: /provider
spring:
  application:
    name: provider
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

2.3 consumer服务消费者

1. 依赖添加
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- hystrix仪表盘 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
2. 配置文件
server:
  port: 8801
  servlet:
    context-path: /consumer

spring:
  application:
    name: consumer
#开启feign hystrix   
feign:
  hystrix:
    enabled: true

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  # 由于配置了context-path,如果想要访问所有端点,需添加一下配置    
  instance:
    metadata-map:
      management:
        context-path: /consumer/actuator
    health-check-url: http://localhost:${server.port}/consumer/actuator/health
    status-page-url: http://localhost:${server.port}/consumer/actuator/info
    home-page-url: http://localhost:${server.port}

management:
  endpoint:
    health:
      #查看详细的应用健康信息需要配置
      show-details: ALWAYS
  endpoints:
    web:
      exposure:
        #要公开所有(已启用)网络端点,Spring Boot 2.0默认只开启info,health两个端点
        include: '*'
3. FeignClient接口
@FeignClient(name = "provider",path = "/provider",fallback = FeignFallback.class)
public interface FeignService {
    @RequestMapping(value = "/getBookByName",method = RequestMethod.GET)
    String getBookByName(@RequestParam("id") String id);
}
4. 服务回退
@Component
public class FeignFallback implements FeignService {
    @Override
    public String getBookByName(String id) {
        return "error";
    }
}
5. 服务接口
@RestController
public class HelloController {
    @Autowired
    private FeignService feignService;

    @RequestMapping(value = "getBookName")
    public String getBookName(@RequestParam("id") String id){
        return feignService.getBookByName(id);
    }
}
6. 启动类
@SpringBootApplication
@EnableEurekaClient
@EnableFeignClients
//开启熔断
@EnableHystrix
//开启熔断监控
@EnableHystrixDashboard
public class ConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }
}

2.4 测试

依次启动服务eureka->provider->consumer

1. 访问http://localhost:8801/consumer/getBookName?id=1

请求返回内容:

Java
2. 访问http://localhost:8801/consumer/hystrix, 输入http://localhost:8801/consumer/actuator/hystrix.stream,点击Monitor Stream按钮
hystrix监控主页
hystrix监控详情
3. 停止provider服务,访问访问http://localhost:8801/consumer/getBookName?id=1

请求返回内容:

error
4. 再次查看hystrix监控详情
hystrix监控详情

2.5 hystrix仪表盘各项指标的说明

hystrix监控详情

相关文章

  • SpringCloud-笔记10-Hystrix防雪崩利器

    服务降级 依赖隔离 服务熔断-监控(Hystrix Dashboard) 服务熔断 Hystrix Dashboard

  • 服务熔断与熔断监控

    在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(RPC),在Spring Cloud可以...

  • SpringCloud(五):服务熔断与熔断监控

    一、概念部分 1.什么是熔断器?熔断,就是断开与服务器的连接,熔断器是在服务不可用的时候主动断开,以免造成更多的雪...

  • SOA和RPC区别

    RPC SOA SOA又叫服务治理,入springcloud,服务进行监控,熔断,链路跟踪。

  • 3.6:服务熔断设计

    本文将梳理微服务架构下,服务熔断原理与设计。整体包含以下两部分: 为什么需要服务熔断 Hystrix熔断的设计 为...

  • 2022-06-30 Springcloud基础知识(7)- S

    Spring Cloud Hystrix (二) | Hystrix 全局/解耦降级、服务熔断、故障监控 查看内容...

  • 【五】Spring Cloud Hystrix

    Hystrix具备了服务降级、服务熔断、线程隔离、请求缓存、请求合并以及服务监控等强大功能。 1.Hystrix服...

  • Alibaba Sentinel超详细整理

    Alibaba Sentinel 是面向云原生微服务的流量控制,熔断降级组件,监控保护你的微服务 github[h...

  • 服务降级熔断 - 熔断降级

    Hystrix实现原理-熔断机制 熔断是参考电路而产生的一种保护性机制,即系统中如果存在某个服务失败率过高时,将开...

  • Gateway

    功能:Gateway提供了如下功能:路由、隔离、限流、熔断、返回、监控熔断。 路由:路由是核心功能,需要根据各种条...

网友评论

    本文标题:服务熔断与熔断监控

    本文链接:https://www.haomeiwen.com/subject/larsoftx.html