熔断器Hystrix及服务监控Dashboard
服务雪崩效应
当一个请求依赖多个服务的时候,当请求的服务中出现无法访问、异常、超时等问题时,那么用户的请求将会被阻塞、其他功能也会受到影响,如果多个用户的请求中,都存在无法访问的服务,那么他们都将陷入阻塞的状态中。服务提供者不可用,一个服务器影响多个服务器,导致消费者不可用,这就是雪崩现象。
正常情况下:
服务器出问题,请求被阻塞:
多个用户请求被阻塞:
我接下来会用服务熔断和服务降级方式解决雪崩问题
服务熔断服务降级
Hystrix断路器简介
hystrix对应的中文名字是“豪猪”,豪猪周身长满了刺,能保护自己不受天敌的伤害,代表了一种防御机制,这与hystrix本身的功能不谋而合,因此Netflix团队将该框架命名为Hystrix,并使用了对应的卡通形象做作为logo。
在一个分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,如何能够保证在一个依赖出问题的情况下,不会导致整体服务失败,这个就是Hystrix需要做的事情
Hystrix服务熔断服务原理
Hystrix提供了熔断、隔离、Fallback、cache、监控等功能,能够在一个、或多个依赖同时出现问题时保证系统依然可用。
熔断机制是应对雪崩效应的一种微服务链路保护机制。
当某个服务不可用或者响应时间超时,会进行服务降级,进而熔断该节点的服务调用,快速返回自定义的错误影响页面信息。
实现Hystrix服务熔断服务降级
Hystrix服务熔断服务降级用这个注解来实现:@HystrixCommand fallbackMethod
案例:
建立工程:microservice-student-provider-hystrix-1004
pom依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.hsl</groupId>
<artifactId>t237microservice</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>microservice-student-provider-hystrix-1004</artifactId>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
</dependency>
<!-- 修改后立即生效,热部署 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>com.hsl</groupId>
<artifactId>microservice-common</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--添加注册中心Eureka相关配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!--Hystrix相关依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
yml文件配置
server:
port: 1004
context-path: /
eureka:
instance:
hostname: localhost #eureka客户端主机实例名称
appname: microservice-student #客户端服务名
instance-id: microservice-student:1004 #客户端实例名称
prefer-ip-address: true #显示IP
client:
service-url:
defaultZone: http://eureka2001.hsl.com:2001/eureka/,http://eureka2002.hsl.com:2002/eureka/,http://eureka2003.hsl.com:2003/eureka/ #把服务注册到eureka注册中心
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8
username: root
password: 1234
jpa:
hibernate:
ddl-auto: update
show-sql: true
application:
name: microservice-student
profiles: provider-hystrix-1004
info:
groupId: com.hsl.testSpringcloud
artifactId: microservice-student-provider-hystrix-1004
version: 1.0-SNAPSHOT
userName: http://hsl.com
phone: 123456
启动类:
MicroserviceStudentProviderHystrix1004Application
package com.hsl.microservicestudentproviderhystrix1004;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableCircuitBreaker
@EntityScan("com.hsl.*.*")
@EnableEurekaClient
@SpringBootApplication
public class MicroserviceStudentProviderHystrix1004Application {
public static void main(String[] args) {
SpringApplication.run(MicroserviceStudentProviderHystrix1004Application.class, args);
}
}
把microservice-student-provider-1001工程controller,repository,service层代码cope过来
参考博文:https://blog.csdn.net/water_java/article/details/103871554
使用注解: @HystrixCommand(fallbackMethod="xxx")
在StudentProviderController.java类添加属性以及方法
@Value("${server.port}")
private String port;
/**
* 测试Hystrix服务降级
* @return
* @throws InterruptedException
*/
@GetMapping(value="/hystrix")
@HystrixCommand(fallbackMethod="hystrixFallback")
public Map<String,Object> hystrix() throws InterruptedException{
// Thread.sleep(2000);//模拟超时
Map<String,Object> map=new HashMap<String,Object>();
map.put("code", 200);
map.put("info","工号【"+port+"】正在为您服务");
return map;
}
public Map<String,Object> hystrixFallback() throws InterruptedException{
Map<String,Object> map=new HashMap<String,Object>();
map.put("code", 500);
map.put("info", "系统【"+port+"】繁忙,稍后重试");
return map;
}
microservice-student-consumer-80项目也要对应的加个方法
StudentConsumerController
/**
* 测试Hystrix服务降级
* @return
*/
@GetMapping(value="/hystrix")
@ResponseBody
public Map<String,Object> hystrix(){
return restTemplate.getForObject(SERVER_IP_PORT+"/student/hystrix/", Map.class);
}
注册中心
测试:http://localhost/student/hystrix
先启动注册中心,再启动生产者,在启动消费者
这里我正常访问
Thread.sleep(1100) 模拟超时;
报错提示信息:
Hystrix默认1算超时,所有 sleep了2秒 所以进入自定义fallback方法,防止服务雪崩;
Hystrix默认超时时间设置
Hystrix默认超时时间是1秒,我们可以通过hystrix源码看到,
找到 hystrix-core.jar com.netflix.hystrix包下的HystrixCommandProperties类
default_executionTimeoutInMilliseconds属性局势默认的超时时间
我们系统里假如要自定义设置hystrix的默认时间的话,改变yml配置
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 3000
改成3秒 然后 我们代码里sleep修改成2秒测试;
sleep修改成4秒测试:
Hystrix服务监控Dashboard
Hystrix服务监控Dashboard
Hystrix服务监控Dashboard仪表盘
Hystrix提供了 准实时的服务调用监控项目Dashboard,能够实时记录通过Hystrix发起的请求执行情况
我们新建项目:microservice-student-consumer-hystrix-dashboard-90
pom依赖
<!--Hystrix服务监控Dashboard依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
启动类:MicroserviceStudentConsumerHystrixDashboard90Application
package com.hsl.microservicestudentconsumerhystrixdashboard90;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@EnableHystrixDashboard
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class MicroserviceStudentConsumerHystrixDashboard90Application {
public static void main(String[] args) {
SpringApplication.run(MicroserviceStudentConsumerHystrixDashboard90Application.class, args);
}
}
我们启动这个项目
然后浏览器输入:http://localhost:90/hystrix
测试:启动注册中心,启动生产者1004,启动这个项目,启动消费者
http://localhost:1004/hystrix.stream