SpringCloud-断路降级

SpringCloud文章系列

  1. SpringCloud
  2. SpringCloud-注册中心
  3. SpringCloud-配置中心
  4. SpringCloud-链路跟踪
  5. SpringCloud-消息总线
  6. SpringCloud-API网关
  7. SpringCloud-异步消息
  8. SpringCloud-同步调用
  9. 【当前文章】SpringCloud-断路降级
  10. SpringCloud-监控管理
  11. SpringCloud-番外篇-临时任务
  12. SpringCloud-番外篇-文档生成
  13. SpringCloud-番外篇-源码解析

接入说明

  1. 目前采用最新的1.xRelease版本:1.5.15.RELEASE
  2. SpringCloud项目可以在start.spring.io下载,不过更方便的应该是通过idea新建项目,建立Spring Initializr项目
  3. 我一般习惯将具体实现服务用具体的服务名,而公共组件用service-xxx来命名,当然还有一些比较固定名字的公共组件

断路监控大盘

1. 新建项目

新建artifactId为hystrix-dashboard的服务

2. 导入依赖

依赖说明

  1. actuator用于暴露监控的接口
  2. eureka-server是Eureka服务器的依赖
  3. config是Config客户端,从配置中心拉取配置
  4. netflix-hystrix-dashboard是hystrix监控大盘的依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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-netflix-hystrix-dashboard</artifactId>
</dependency>

3. 编写启动类

打开HystrixDashboardApplication,在class上加入@EnableHystrixDashboard注解

4. 修改配置

1
2
3
4
5
6
7
server.port=8030
spring.application.name=hystrix-dashboard
# public
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka,http://localhost:8762/eureka
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=service-config

5. 启动main

6. 验证

访问 http://localhost:8761/ 看到hystrix-dashboard已经注册了一台
访问 http://localhost:8030 可以看到hystrix监控的页面,上面需要配置一个xxx.stream的http地址,客户度集成后就有这个地址了

客户端使用

基于刚同步调用章节business-service服务调用account-service服务接口的例子

1. 添加依赖

1
2
3
4
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>

2. 开启注解

BusinessServiceApplication加入注解 @EnableCircuitBreaker 打开断路器功能

3. 修改代码

基于feign远程调用的降级非常简单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@RestController
class AccountController {
@Autowired
private AccountRead accountRead;
public String fallback() {
return "掛了";
}
@HystrixCommand(fallbackMethod = "fallback")
@RequestMapping("/account-msg")
public String read() {
return accountRead.read();
}
}

增加@HystrixCommand注解,注解种指定fallbackMethod对应同参数与返回值的方法即可

其他的方法也可以在方法上面标注@HystrixCommand(fallbackMethod = “xxx”)注解就实现降级了,比如用restTempldate请求的方法上加上

监控大盘集成

只需将客户的的http://localhost:8010/hystrix.stream填入hystrix-dashboard的url中点击下面按钮注册就可以看到监控数据
但是这样只能看到一台机器的,有时候我们想关注整个集群的情况,多个业务服务的链路情况,则需要使用数据聚合服务turbine-service

turbine服务

导入依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</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-turbine</artifactId>
</dependency>

添加bootstrap.properties

1
2
3
4
5
6
7
8
9
10
11
12
13
server.port=8001
spring.application.name=turbine-service
# private
turbine.app-config=api-gateway,business-service
turbine.aggregator.clusterConfig=default
turbine.cluster-name-expression="default"
turbine.combine-host-port=true
# public
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka,http://localhost:8762/eureka
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=service-config

turbine.app-config用于指定具体聚合哪些服务,这里聚合2个有调用请求的服务网关+business服务

在TurbineServiceApplication类上加入@EnableTurbine

将地址 http://localhost:8001/hystrix.stream 配置在hystrix-dashboard上就可以看到服务网关+business服务2个集群的调用情况了

疑问

如果触发了断路器一定是调用服务不可用么?

不是,因为断路器触发不止远程服务不可行,包括调用异常的情况,只要是异常都会触发断路,比如bug导致的

如果fallback代码报错了呢?

fallback报错了会抛出一个异常,而fallback一般都是简单的返回默认值固定值的逻辑,如果担心这里处理也有问题,可以在fallback上再加上@HystrixCommand降级
可以这样一直降级下去

其他注册中心

其他更多的是限流的方案与设置调用超时与trycatch的简单处理方案,相对不是太灵活,这里不展开了

------ 本文结束 ------

版权声明

dawell's Notes by Dawell is licensed under a Creative Commons BY-NC-ND 4.0 International License.
Dawell创作并维护的dawell's Notes博客采用创作共用保留署名-非商业-禁止演绎4.0国际许可证
本文首发于dawell's Notes 博客( http://dawell.cc ),版权所有,侵权必究。

坚持原创技术分享,您的支持将鼓励我继续创作!