SpringCloud微服务(学习笔记):降级,熔断 Hystrix

降级,熔断 Hystrix

  • 测试用项目环境
  • 降级,熔断
    • 熔断
    • 降级
  • Hystrix
    • Hystrix解决了什么问题:
    • 结合Feign开发Hystrix断路器
      • maven依赖
      • 启动类注解
      • 降级
      • 熔断
        • 1.加入feign支持hystrix
        • 2.编写FallBack类
    • Hystrix配置
  • 加餐
    • 断路器Dashboard监控仪表盘
      • maven依赖
      • 启动类注解
    • 仪表盘各参数含义

降级,熔断,是微服务或分布式系统中系统负载过高,突发流量或网络问题等各种异常情况的常用解决方案。

测试用项目环境

框架 版本
Idea 2019.2
JDK 1.8
SpringBoot 2.1.6.RELEASE
SpringCloud Greenwich.SR2

降级,熔断

  • 两者从可用性和可靠性触发,为了防止系统崩溃
  • 最终让用户体验到的是某些功能暂时不能用
  • 服务熔断一般是下游服务故障导致的,而服务降级一般是从整体系统负荷考虑,由调用方控制

熔断

类似保险丝,为了防止整个系统故障,保护子服务和下游服务。

降级

抛弃一些非核心的接口和数据,保留核心数据。

Hystrix

英文翻译豪猪
Netflix的开源项目
github:https://github.com/Netflix/Hystrix
wiki:https://github.com/Netflix/Hystrix/wiki

Hystrix解决了什么问题:

99.9930 = 99.7% uptime
0.3% of 1 billion requests = 3,000,000 failures
2+ hours downtime/month even if all dependencies have excellent uptime.

假如微服务系统中每个服务的可用性为99.99%
一次请求中会经过30个服务的调用
那么每次请求的可用性为99.7%
1亿次请求中请求失败的次数达到3百万
转换为服务器宕机超过两小时
这就是Hystrix的重要性

结合Feign开发Hystrix断路器

maven依赖

使用maven依赖管理,使用最新的springcloud Greenwich.SR2版本

<dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-dependenciesartifactId>
        <version>Greenwich.SR2version>
        <type>pomtype>
        <scope>importscope>
      dependency>
    dependencies>
dependencyManagement>

feign依赖里面已经有了hystrix但不是和springcloud结合的,没有**@HystrixCommand**注解
所以需要再加入结合后的hystrix依赖

<dependency>
      <groupId>org.springframework.cloudgroupId>
      <artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>

启动类注解

@EnableCircuitBreaker

也可以使用

@SpringCloudApplication

该注解默认集成了微服务主要组件的启动注解

@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker

降级

在可能抛出异常的方法上加上
@HystrixCommand

@HystrixCommand(fallbackMethod = "testFail")
public Object test(int user){ 
	return null;
}
    
// 方法名指定 业务方法出现异常则会进入指定的fallbackMethod
public Object testFail(int user){
	// 封装errMsg
	return null;
}

熔断

1.加入feign支持hystrix

# application.yml
feign:
  hystrix:
    enabled: true

2.编写FallBack类

fallback指向的必须是实现Feign调用的接口类的实现类

@FeignClient(name = "TEST-SERVER",fallback = TestServiceFallBack.class)
public interface TestService {
}
// 实现类
@Component // 交给spring管理
public class TestServiceFallBack implements TestService{
}

在调用服务出现异常时,feign便会执行fallback指定类的对应方法。可以进行日志记录,短信通知维护人员等。
建议写入消息队列等待消费,或者写入线程异步执行,提高性能。

Hystrix配置

Hystrix相关配置在
com.netflix.hystrix.HystrixCommandProperties
默认配置feign调用超过一秒认为访问超时
可以在**@HystrixCmomand**中配置
注解中的配置不需要加hystrix前缀,name就为HystrixCommandProperties中指定的key

@HystrixCommand(fallbackMethod = "testFail",commandProperties ={
	@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "2000")
})
@GetMapping("/")
public Object test(int user){
	return "success";
}

yml的配置方式需要在key前加hystrix.command.default

hystrix:
  command:
    defalut:
      execution:
        isolation:
          thread:
            timeoutInMilliseconds: 3000

加餐

断路器Dashboard监控仪表盘

maven依赖


<dependency>
	<groupId>org.springframework.cloudgroupId>
	<artifactId>spring-cloud-starter-netflix-hystrix-dashboardartifactId>
	dependency>

<dependency>
	<groupId>org.springframework.bootgroupId>
	<artifactId>spring-boot-starter-actuatorartifactId>
dependency>

启动类注解

@EnableHystrixDashboard

然后访问项目根路径下的/hystrix
比如:http://localhost:8091/hystrix
然后你会看到
SpringCloud微服务(学习笔记):降级,熔断 Hystrix_第1张图片
在第一栏中输入项目根目录下路径/actuator/hystrix.stream
比如:http://localhost:8091/actuator/hystrix.stream
其他不用输入,点击Monitor Stream进入监控仪表

如果SpringBoot版本比较高2.x的话进入后会看到这样的页面
SpringCloud微服务(学习笔记):降级,熔断 Hystrix_第2张图片
因为后面的SpringBoot版本默认关闭了监控断点
配置中开启监控断点:

# 开启监控断点
management:
  endpoints:
    web:
      exposure:
        include: "*"

配置好后开启服务
然后请求服务的任意一个接口,监控器便可以正常访问
SpringCloud微服务(学习笔记):降级,熔断 Hystrix_第3张图片
打断一下!
这个DashBoard监视仪表是使用SSE后台推送技术返回的,flux流式返回!

仪表盘各参数含义

SpringCloud微服务(学习笔记):降级,熔断 Hystrix_第4张图片

你可能感兴趣的:(微服务,笔记)