springcloud-知识点总结(三):Hystrix & Dashboard & turbine & Zuul & SpringCloud Config

喜欢而已 提交于 2020-03-31 13:21:23

1.Hystrix断路器简介

Hystrix断路器简介

  hystrix对应的中文名字是“豪猪”,豪猪周身长满了刺,能保护自己不受天敌的伤害,代表了一种防御机制,这与hystrix本身的功能不谋而合,因此Netflix团队将该框架命名为Hystrix,并使用了对应的卡通形象做作为logo。

  在一个分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,如何能够保证在一个依赖出问题的情况下,不会导致整体服务失败,这个就是Hystrix需要做的事情。Hystrix提供了熔断、隔离、Fallback、cache、监控等功能,能够在一个、或多个依赖同时出现问题时保证系统依然可用。

2.服务雪崩效应

服务雪崩效应

 

当一个请求依赖多个服务的时候:

正常情况下的访问 

 

但是,当请求的服务中出现无法访问、异常、超时等问题时(图中的I),那么用户的请求将会被阻塞。

 

 

如果多个用户的请求中,都存在无法访问的服务,那么他们都将陷入阻塞的状态中。

 

Hystrix的引入,可以通过服务熔断和服务降级来解决这个问题。

3.Hystrix服务熔断服务降级@HystrixCommand fallbackMethod

Hystrix服务熔断服务降级@HystrixCommand fallbackMethod

 

熔断机制是应对雪崩效应的一种微服务链路保护机制。

当某个服务不可用或者响应时间超时,会进行服务降级,进而熔断该节点的服务调用,快速返回自定义的错误影响页面信息。

 

我们写个项目来测试下;

 

我们写一个新的带服务熔断的服务提供者项目 microservice-student-provider-hystrix-1004

把 配置和 代码 都复制一份到这个项目里;

 

然后修改;

 

1,pom.xml加下 hystrix支持

<dependency>

<groupId>org.springframework.cloud</groupId>

<artifactId>spring-cloud-starter-hystrix</artifactId>

</dependency>

 

2,application.yml修改下端口和实例名称

port: 1004

 instance-id: microservice-student-hystrix:1004 #客户端实例名称

 

3,启动类名称改成StudentProviderHystrixApplication_1004

以及加下注解支持 @EnableCircuitBreaker

 

4,我们在( microservice-student-provider-hystrix-1004)新增方法getInfo

 1  /**
 2  * 获取信息
 3  * @return
 4  * @throws InterruptedException 
 5  */
 6 @ResponseBody
 7 @GetMapping(value="/getInfo")
 8 @HystrixCommand(fallbackMethod="getInfoFallback")
 9 public Map<String,Object> getInfo() throws InterruptedException{
10     Thread.sleep(2000);
11     Map<String,Object> map=new HashMap<String,Object>();
12     map.put("code", 200);
13     map.put("info", "业务数据xxxxx");
14     return map;
15 }
16  
17 public Map<String,Object> getInfoFallback() throws InterruptedException{
18     Map<String,Object> map=new HashMap<String,Object>();
19     map.put("code", 500);
20     map.put("info", "系统出错,稍后重试");
21     return map;
22 }
View Code

 

这里我正常访问 返回的是 200  业务数据xxxxx 

但是我们这里Thread.sleep(2000) 模拟超时;

这里的话 我们加上@HystrixCommand注解 以及 fallbackMethod

表明这个方法我们再 没有异常以及没有超时(hystrix默认1秒算超时)的情况,才返回正常的业务数据;

否则,进入我们fallback指定的本地方法,我们搞的是500  系统出错,稍后重试,有效的解决雪崩效应,以及返回给用户界面

很好的报错提示信息;

 

 

============================

 

microservice-student-consumer-80项目也要对应的加个方法

 1 /**
 2  * 熔断器测试方法
 3  * @return
 4  */
 5 @SuppressWarnings("unchecked")
 6 @GetMapping(value="/getInfo")
 7 @ResponseBody
 8 public Map<String,Object> getInfo(){
 9     return restTemplate.getForObject(PRE_HOST+"/student/getInfo/", Map.class);
10 }
View Code

然后我们来测试下;

先启动三个eureka,再启动带hystrix的provider,最后启动普通的consumer;

因为 Hystrix默认1算超时,所有 sleep了2秒 所以进入自定义fallback方法,防止服务雪崩;

 

我们这里改sleep修改成100毫秒;

 

4.Hystrix默认超时时间设置

Hystrix默认超时时间设置

 

Hystrix默认超时时间是1秒,我们可以通过hystrix源码看到,

找到 hystrix-core.jar com.netflix.hystrix包下的HystrixCommandProperties类

default_executionTimeoutInMilliseconds属性局势默认的超时时间

 

默认1000毫秒 1秒

 

我们系统里假如要自定义设置hystrix的默认时间的话;

 

application.yml配置文件加上 

 

hystrix:

  command:

    default:

      execution:

        isolation:

          thread:

            timeoutInMilliseconds: 3000

 

修改成3秒  然后 我们代码里sleep修改成2秒测试;

 

5.Feign Hystrix整合&服务熔断服务降级彻底解耦

Feign Hystrix整合&服务熔断服务降级彻底解耦

 

前面的代码,用@HystrixCommand fallbackMethod是很不好的,因为和业务代码耦合度太高,不利于维护,所以需要解耦,这我们讲下Feign Hystrix整合。

 

第一,microservice-student-provider-hystrix-1004项目修改

我们不用原先那套。按照正常的逻辑来写;

StudentService加新的接口方法:

/**

 * 获取信息
 * @return
 */
public Map<String,Object> getInfo();
 
StudentServiceImpl写具体实现:
@Override
public Map<String, Object> getInfo() {
    Map<String,Object> map=new HashMap<String,Object>();
    map.put("code"200);
    map.put("info""业务数据xxxxx");
    return map;
}
StudentProviderController正常调用service方法:
/**
 * 获取信息
 * @return
 * @throws InterruptedException 
 */
@ResponseBody
@GetMapping(value="/getInfo")
public Map<String,Object> getInfo() throws InterruptedException{
    Thread.sleep(900);
    return studentService.getInfo();
}
 

第二步:microservice-common项目新建FallbackFactory类,解耦服务熔断服务降级

StudentClientService接口,新增getInfo方法;

/**

 * 获取信息
 * @return
 */
@GetMapping(value="/student/getInfo")
public Map<String,Object> getInfo();
 
新建 StudentClientFallbackFactory 类,实现FallbackFactory<StudentClientService>接口;
 1 @Component
 2 public class StudentClientFallbackFactory implements FallbackFactory<StudentClientService>{
 3  
 4     @Override
 5     public StudentClientService create(Throwable cause) {
 6         // TODO Auto-generated method stub
 7         return new StudentClientService() {
 8              
 9             @Override
10             public boolean save(Student student) {
11                 // TODO Auto-generated method stub
12                 return false;
13             }
14              
15             @Override
16             public List<Student> list() {
17                 // TODO Auto-generated method stub
18                 return null;
19             }
20              
21             @Override
22             public Map<String, Object> getInfo() {
23                 Map<String,Object> map=new HashMap<String,Object>();
24                 map.put("code", 500);
25                 map.put("info", "系统出错,稍后重试");
26                 return map;
27             }
28              
29             @Override
30             public Student get(Integer id) {
31                 // TODO Auto-generated method stub
32                 return null;
33             }
34              
35             @Override
36             public boolean delete(Integer id) {
37                 // TODO Auto-generated method stub
38                 return false;
39             }
40         };
41     }
42  
43 }
View Code

 

StudentClientService接口的@FeignClient注解加下 fallbackFactory属性 

@FeignClient(value="MICROSERVICE-STUDENT",fallbackFactory=StudentClientFallbackFactory.class)

 

这类我们实现了 降级处理方法实现;

最后测试即可。

6.Hystrix服务监控Dashboard

Hystrix服务监控Dashboard仪表盘

 

Hystrix提供了 准实时的服务调用监控项目Dashboard,能够实时记录通过Hystrix发起的请求执行情况,

可以通过图表的形式展现给用户看。

 

我们新建项目:microservice-student-consumer-hystrix-dashboard-90

加依赖:

 1 <dependency>
 2     <groupId>org.springframework.cloud</groupId>
 3     <artifactId>spring-cloud-starter-hystrix</artifactId>
 4 </dependency>
 5 <dependency>
 6     <groupId>org.springframework.cloud</groupId>
 7     <artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
 8 </dependency>
 9 <dependency>
10     <groupId>org.springframework.boot</groupId>
11     <artifactId>spring-boot-starter-actuator</artifactId>
12 </dependency>
View Code

application.yml配置

 

server:

  port: 90

  context-path: /

 

新建启动类:StudentConsumerDashBoardApplication_90

加注解:@EnableHystrixDashboard

1 @SpringBootApplication(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
2 @EnableHystrixDashboard
3 public class StudentConsumerDashBoardApplication_90 {
4  
5     public static void main(String[] args) {
6         SpringApplication.run(StudentConsumerDashBoardApplication_90.class, args);
7     }
8 }
View Code

 

这样就完事了。

 

我们启动这个项目;

然后浏览器输入:http://localhost:90/hystrix

 

 

出现这个 就说明OK;

 

然后我们来测试下;

我们启动三个eureka,然后再启动microservice-student-provider-hystrix-1004

 

我们直接请求http://localhost:1004/student/getInfo

返回正常业务

 

我们监控的话,http://localhost:1004/hystrix.stream 这个路径即可;

 

 

一直是ping,然后data返回数据;

 

用图形化的话 

 

输入 ,然后点击按钮即可;

 

 

指标含义:

 

 

各种情况:

 

 

7.Hystrix集群监控turbine

前面Dashboard演示的仅仅是单机服务监控,实际项目基本都是集群,所以这里集群监控用的是turbine。

 

turbine是基于Dashboard的。

 

先搞个集群;

再microservice-student-provider-hystrix-1004项目的基础上再搞一个microservice-student-provider-hystrix-1005

 

代码和配置都复制一份,然后修改几个地方;

第一 yml配置 

server:

  port: 1005

instance-id: microservice-student-hystrix:1005 #客户端实例名称 

 

第二 启动类改成StudentProviderHystrixApplication_1005

 

这样的话 就有了 hystrix集群服务;

 

我们新建项目microservice-student-consumer-hystrix-turbine-91

pom.xml加下依赖

1 <dependency>
2     <groupId>org.springframework.boot</groupId>
3     <artifactId>spring-boot-starter-actuator</artifactId>
4 </dependency>
5 <dependency>
6     <groupId>org.springframework.cloud</groupId>
7     <artifactId>spring-cloud-starter-turbine</artifactId>
8 </dependency>
View Code

 

8.Zuul API路由网关服务简介

Zuul API路由网关服务简介

 

 

请看上图,这里的API 路由网关服务 由Zuul实现,主要就是对外提供服务接口的时候,起到了请求的路由和过滤作用,也因此能够隐藏内部服务的接口细节,从来有利于保护系统的安全性;

9.SpringCloud Config简介

SpringCloud Config简介

 

Spring Cloud Config 是 Spring Cloud 团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分。其中服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置仓库并为客户端提供获取配置信息、加密 / 解密信息等访问接口;而客户端则是微服务架构中的各个微服务应用或基础设施,它们通过指定的配置中心来管理应用资源与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。Spring Cloud Config 实现了对服务端和客户端中环境变量和属性配置的抽象映射,所以它除了适用于 Spring 构建的应用程序之外,也可以在任何其他语言运行的应用程序中使用。由于 Spring Cloud Config 实现的配置中心默认采用 Git 来存储配置信息,所以使用 Spring Cloud Config 构建的配置服务器,天然就支持对微服务应用配置信息的版本管理,并且可以通过 Git 客户端工具来方便的管理和访问配置内容。当然它也提供了对其他存储方式的支持,比如:GIT仓库、SVN 仓库、本地化文件系统。

 

Config Server端主要和Git/SVN服务器

 

通俗点,就是统一管理配置,包括方便切换环境配置,以及修改配置无需动代码,省心省力;

 

如果用上SpringCloud Bus,能实现无需重启,自动感知配置变化以及应用新配置;

 

 

10.Config Server基本使用

Config Server基本使用

 

根据前面SpringCloud架构图,首先第一步,要搞个 configServer来联通远程GIT仓库,来读取远程配置;

 

这里GIT仓库,我们一般选用GitHub https://github.com/,或者码云  https://gitee.com/  

我们课程用GitHub演示,首先大伙去GitHub注册个账号,

 

建个仓库 microservice-config  然后 Git下载本地;

 

上传一个配置文件上到git仓库,application.yml 记住要utf-8编码,否则乱码,解析各种问题;

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!