avatar

目录
服务容错&服务跟踪&容器部署

第9章 服务容错

服务容错和Hystrix

问题引出:

​ 雪崩效应:A —> B —> C ,B调用C不可用,B一直重试,A调用B也不可用了,这样,资源耗尽,整个系统不可用

Spring Cloud Hystrix

  • 防雪崩利器
  • 服务降级
    • 场景:“服务开小差,请稍后重试”
    • (在高流量场景下)优先核心服务,非核心服务不可用或弱可用
    • 通过HystrixCommand注解指定
    • fallbackMethod中具体实现降级逻辑
  • 服务熔断
  • 依赖隔离
  • 监控(Hystrix Dashboard)

触发降级

pom

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

启动类

java
1
2
3
4
@SpringCloudApplication //代替以下3个
//@EnableCircuitBreaker
//@SpringBootApplication
//@EnableDiscoveryClient

触发降级

java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.mxx.order.controller;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
@DefaultProperties(defaultFallback = "defaultFallback")
@RestController
public class HystrixController {

@HystrixCommand(fallbackMethod = "fallback")
@RequestMapping("/getProductInfo")
public String getProductInfo(){
// ...

/*
抛异常,就会触发降级,跳转执行fallbackMetho
若并发太高或数据库连接太多,可以手动抛异常,触发降级
*/
/* if(true)
throw new RuntimeException("主动抛异常");*/
return "success : productInfo";
}

@HystrixCommand //使用默认fallbackMethod
@RequestMapping("/getProductInfo2")
public String getProductInfo2(){
// ...

if(true)
throw new RuntimeException("主动抛异常");
return "success : productInfo2";
}

private String fallback(){

return "太拥挤了,请稍后重试";
}

private String defaultFallback(){

return "默认提示:太拥挤了,请稍后重试";
}
}

超时设置

超过指定时间还没有访问成功,就降级处理

java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@HystrixCommand(fallbackMethod = "fallback",
commandProperties = @HystrixProperty(
name = "execution.isolation.thread.timeoutInMilliseconds", value = "3000"
)) // 默认是1000ms
@RequestMapping("/getProductInfo")
public String getProductInfo(){
// ...
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "success : productInfo";
}

依赖隔离??

  • 线程池隔离
  • Hystrix自动实现了依赖隔离

服务熔断

探讨断路器模式

Code
1
2
Circuit Breaker:断路器(电流过大时会烧坏保险丝,保护电路)
断路器模式:当某个服务发生故障,通过circuitBreaker的故障监控,切断原来的主逻辑(跳闸),返回错误

断路器状态图:

![屏幕快照 2018-12-28 下午4.07.56](20181227220131330/屏幕快照 2018-12-28 下午4.07.56.png)

解释:

  • 熔断器开始处于close状态,当调用失败次数累计到了阈值(比例),就会启动熔断机制(open),此时对服务都返回错误,但设置了个时钟,到了时钟后进入半熔断状态(Half Open),允许定量服务请求。
  • 在Half Open下,若服务都调用成功(或者满足成功比例),则认为恢复了,就会关闭(closed),否则又回到open状态

关键参数

Code
1
2
3
4
circuitBreaker.enabled 	//开启熔断服务
circuitBreaker.requestVolumeThreshold //在滚动时间窗口中,断路器最小请求数。请求数达到后才计算。
circuitBreaker.sleepWindowInMilliseconds //休眠时间窗口
circuitBreaker.errorThresholdPercentage //断路器打开的错误百分比条件

解释:

  • 当调用失败次数达到 “错误百分比条件” ,断路器从“closed”进入“half open”。

  • 当断路器“open”后,Hysticx会启动“休眠时间窗口”,在这个时间内,降级逻辑(fallbackMethod)成为临时主逻辑,当时间到期,断路器进入“half open”,释放一次请求到原来的逻辑上,若此次请求返回正常,则断路器“close” ,主逻辑恢复。否则,断路器“open”,“休眠时间窗口”重置计时。


操作:

java
1
2
3
4
5
6
7
8
9
10
@HystrixCommand(commandProperties = {
@HystrixProperty(name = "circuitBreaker.enabled", value = "true"), //设置熔断
@HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"), //请求数达到后才计算
@HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds", value = "10000"), //休眠时间窗
@HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "60"), //错误率
})
@RequestMapping("/getProductInfo")
public String getProductInfo(){

}

改为 使用yml配置项:

yaml
1
2
3
4
5
6
7
8
9
10
11
12
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1000
getProductInfo:
execution:
isolation:
thread:
timeoutInMilliseconds: 3000

hystrix-dashboard

pom

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

yml

yaml
1
2
3
4
5
management:
endpoints:
web:
exposure:
include: hystrix.stream

启动类

Code
1
@EnableHystrixDashboard

访问:http://localhost:8081/hystrix

dashboard:

![屏幕快照 2018-12-28 下午5.24.36](20181227220131330/屏幕快照 2018-12-28 下午5.24.36.png)

再看熔断器状态:

  • closed –> open:当请求数达到“断路器最小请求数”,且失败率大于“错误百分比条件”

  • closed –> half open:当调用失败次数达到 “错误百分比条件”

  • open –> half open:当断路器“open”后,Hysticx会启动“休眠时间窗口”,在这个时间内,降级逻辑(fallbackMethod)成为临时主逻辑,当时间到期,断路器进入“half open”

  • half open –> closed:进入“half open”后,释放一次请求到原来的逻辑上,若此次请求返回正常,则断路器“close” ,主逻辑恢复

  • half open –> open:进入“half open”后,释放一次请求到原来的逻辑上,若此次请求返回失败,则断路器“open” ,“休眠时间窗口”重置计时。

Zuul:超时配置

问题引出:通过zuul第一次访问服务容易超时

解释:由于懒加载配置,第一次访问时会加载很多类,导致超过默认时间

如何修改默认超时时间?zuul使用的是hystrix的超时组件

yaml
1
2
3
4
5
6
7
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 3000

第10章 服务跟踪

实操:Sleuth+Zipkin

链路监控

  • Spring Cloud Sleuth

    xml
    1
    2
    3
    4
    5
    <!-- order 和 product 都要包含-->
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-sleuth</artifactId>
    </dependency>
  • 可视化工具:zipkin

    xml
    1
    2
    3
    4
    5
    <!-- 包含以上两个依赖-->
    <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-zipkin</artifactId>
    </dependency>
    • 配置
    yaml
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    # order
    spring:
    zipkin:
    base-url: 10.211.55.6:9411
    sender:
    type: web
    sleuth:
    sampler:
    probability: 1 # 抽样比100%
    # order | product
    logging:
    level:
    org.springframework.cloud.openfeign: debug
    • ui展示

    ![屏幕快照 2019-01-22 上午10.43.15](20181227220131330/屏幕快照 2019-01-22 上午10.43.15.png)

    可查看某个调用链的请求时间都花在哪一步了。

理论:分布式追踪系统

分布式追踪系统

核心步骤

  • 数据采集
  • 数据存储
  • 查询展示

说明

  • 不同系统api不兼容,改动起来工作量很大
  • OpenTracing:解决不同分布式系统api不兼容问题,是一种标准
  • Annotation:事件类型…
  • Zipkin:遵循OpenTracing的产品,Twitter开源

zipkin原理图

![屏幕快照 2019-01-22 上午10.52.37](20181227220131330/屏幕快照 2019-01-22 上午10.52.37.png)

zipkin关键概念

  • traceId
  • spanId
  • parentId

第11章 容器部署

Eureka使用Docker部署

Dockerfile

Code
1
2
3
4
5
6
7
8
9
FROM hub.c.163.com/library/java:8-alpine

MAINTAINER XXX XXX@imooc.com

ADD target/*.jar app.jar

EXPOSE 8761

ENTRYPOINT ["java", "-jar", "/app.jar"]

运行$ docker build -t myspringcloud/eureka .制作镜像

rancher

  • 开源企业级全栈化容器部署及管理平台[更方便地管理Docker]

  • https://www.cnrancher.com/

  • 安装rancher docker run -d --restart=unless-stopped -p 80:80 -p 443:443 rancher/rancher:stable

  • 登录后主界面

![屏幕快照 2019-01-22 上午11.42.29](20181227220131330/屏幕快照 2019-01-22 上午11.42.29.png)

课程推荐:[Docker+Kubernetes微服务容器化实践]

方法总结:rancher+docker+网易云远程仓库 实现分布式部署

文章作者: Machine
文章链接: https://machine4869.gitee.io/2018/12/27/20181227220131330/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 哑舍
打赏
  • 微信
    微信
  • 支付宝
    支付宝

评论