SpringCloud 详解(八)

Hystrix

一个用于处理分布式系统的延迟容错的开源库。在分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,Hystrix 能够保证在一个依赖出问题的情况下,不会导致整体服务失败,避免级联故障,以提高分布式系统的弹性

“断路器”本身是一种开关装置,当某个服务单元发生故障之后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个符合预期的、可处理的备选响应(FallBack),而不是长时间的等待或者抛出调用方无法处理的异常,这样就保证了服务调用方的线程不会被长时间不必要地占用,从而避免了故障在分布式系统中的蔓延,乃至雪崩。

服务降级

系统将某些不重要的业务或接口的功能降低,可以只提供部分功能,也可以完全停止所有所有不重要的功能。服务器忙,请稍后再试,不让客户端等待并立刻返回一个友好提示。降级的思想是丢车保帅。

哪些情况会触发降级:

  • 程序运行导常
  • 超时
  • 服务熔断触发服务降级
  • 线程池或信号量打满

常见降级方式:

  • 系统后门降级:系统预留后门用于降级,比如提供一个降级 URL,访问 URL 时就执行降级指令。缺点:如果服务器数量多,需要一台一台去操作,效率低。
  • 独立系统降级:将降级操作独立到一个单独的系统中,可以实现复杂的权限管理、批量操作等功能。

服务熔断

降级是应对系统自身的故障,而熔断的目的是应对外部系统的故障。类比保险丝达到最大服务访问后,直接拒绝访问,拉闸限电,然后调用服务降级的方法并返回友好提示。当检测到该节点微服务调用响应正常后,恢复调用链路。实现思路:需要系统有一个统一的 API 调用层,由 API 来进行采样或者统计。

服务限流

只允许系统能够承受的访问量进来,超出的会被丢弃。降级从系统功能优先级角度考虑如何应对故障,而限流则从用户访问压力的角度来考虑如何应对故障。

常见限流方式:

  • 基于请求限流:指从外部请求的角度考虑限流。
  • 基于资源限流:指从系统内部考虑,找到影响性能的关键资源,对其使用上限限制。

支付模块

准备工作

创建一个 Module,一个空的 Maven 项目。并导入下面的依赖。

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
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>ml.guest997</groupId>
<artifactId>Commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

编写配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
server:
port: 8005

spring:
application:
name: payment

eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://eureka7001.com:7001/eureka

主启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
package ml.guest997;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class Payment5 {
public static void main(String[] args) {
SpringApplication.run(Payment5.class, args);
}
}

Controller 层

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
package ml.guest997.Controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class PaymentController {
@GetMapping("/payment/ok/{id}")
public String paymentOK(@PathVariable("id") Integer id) {
String s = "当前线程:" + Thread.currentThread().getName() + " paymentOK id:" + id;
log.info("result:" + s);
return s;
}

@GetMapping("/payment//timeout/{id}")
public String paymentTimeOut(@PathVariable("id") Integer id) {
String s = null;
try {
TimeUnit.MILLISECONDS.sleep(3000);
s = "当前线程:" + Thread.currentThread().getName() + " paymentTimeOut id:" + id + " 耗时3秒";
} catch (InterruptedException e) {
e.printStackTrace();
}
log.info("result:" + s);
return s;
}
}

测试

分别启动 Eureka-Server 和 Payment5 模块后,浏览器访问:127.0.0.1:8005/payment/ok/997 和 127.0.0.1:8005/payment/timeout/997,能够正常返回数据,控制台也能正常打印信息。

JMeter 高并发压测

设置20000个并发请求,然后再在浏览器测试,访问之前的两个网址,会发现响应时间都有所增加甚至出现卡顿。这是因为 Tomcat 默认的工作线程数被打满了,没有多余的线程来分担压力和处理。

订单模块

准备工作

创建一个 Module,一个空的 Maven 项目。并导入下面的依赖。

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
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>ml.guest997</groupId>
<artifactId>Commons</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

编写配置文件

1
2
3
4
5
6
7
8
server:
port: 80

eureka:
client:
register-with-eureka: false
service-url:
defaultZone: http://eureka7001.com:7001/eureka/

主启动类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package ml.guest997;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableFeignClients
@EnableHystrix //启动 Hystrix
public class Order3 {
public static void main(String[] args) {
SpringApplication.run(Order3.class, args);
}
}

Service 层

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package ml.guest997.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

@Component
@FeignClient("PAYMENT")
public interface PaymentService {
@GetMapping("/payment/ok/{id}")
String paymentOK(@PathVariable("id") Integer id);

@GetMapping("/payment/timeout/{id}")
String paymentTimeOut(@PathVariable("id") Integer id);
}

Controller 层

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
package ml.guest997.controller;

import lombok.extern.slf4j.Slf4j;
import ml.guest997.service.PaymentService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
public class OrderController {
@Resource
private PaymentService paymentService;

@GetMapping("/consumer/payment/ok/{id}")
public String paymentOK(@PathVariable("id") Integer id) {
return paymentService.paymentOK(id);
}

@GetMapping("/consumer/payment/timeout/{id}")
public String paymentTimeOut(@PathVariable("id") Integer id) {
return paymentService.paymentTimeOut(id);
}
}

测试

分别启动 Eureka-Server、Payment5 和 Order3 模块后,浏览器访问:127.0.0.1/consumer/payment/ok/997,能够正常返回数据而且响应很快。

JMeter 高并发压测

使用上面同样的方面设置20000个并发请求,然后再在浏览器测试,会发现响应也是会被拖慢的。