服务熔断与熔断监控
2018-09-26 本文已影响36人
AaronSimon
在微服务架构中,根据业务来拆分成一个个的服务,服务与服务之间可以相互调用(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