熔断器Hystrix及服务监控Dashboard
服务雪崩效应
当一个请求依赖多个服务的时候:
正常情况下的访问 :

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

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

Hystrix的引入,可以通过服务熔断和服务降级来解决这个问题。
服务熔断服务降级
Hystrix断路器简介
hystrix对应的中文名字是“豪猪”,豪猪周身长满了刺,能保护自己不受天敌的伤害,代表了一种防御机制,这与hystrix本身的功能不谋而合,因此Netflix团队将该框架命名为Hystrix,并使用了对应的卡通形象做作为logo。

在一个分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,如何能够保证在一个依赖出问题的情况下,不会导致整体服务失败,这个就是Hystrix需要做的事情。Hystrix提供了熔断、隔离、Fallback、cache、监控等功能,能够在一个、或多个依赖同时出现问题时保证系统依然可用。
Hystrix服务熔断服务降级@HystrixCommand fallbackMethod
熔断机制是应对雪崩效应的一种微服务链路保护机制。
当某个服务不可用或者响应时间超时,会进行服务降级,进而熔断该节点的服务调用,快速返回自定义的错误影响页面信息。
我们写个项目来测试下;
我们写一个新的带服务熔断的服务提供者项目 microservice-student-provider-hystrix-1004
把 配置和 代码 都复制一份到这个项目里;
pom.xml加 hystrix支持
<!--Hystrix相关依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
application.yml
server:
port: 1004
context-path: /
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis_ssm?useUnicode=true&characterEncoding=utf8
username: root
password: 123
jpa:
hibernate:
ddl-auto: update
show-sql: true
application:
name: microservice-student
profiles: provider-hystrix-1004 eureka:
instance:
hostname: localhost
appname: microservice-student
instance-id: microservice-student:1004
prefer-ip-address: true
client:
service-url:
defaultZone: http://eureka2001.lingerqi.com:2001/eureka/,http://eureka2002.lingerqi.com:2002/eureka/,http://eureka2003.lingerqi.com:2003/eureka/ info:
groupId: com.lingerqi.testSpringcloud
artifactId: microservice-student-provider-hystrix-1004
version: 1.0-SNAPSHOT
userName: http://lingerqi.com
phone: 123456
StudentProviderHystrixApplication_1004
package com.lingerqi.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.lingerqi.*.*")
@EnableEurekaClient
@SpringBootApplication
public class MicroserviceStudentProviderHystrix1004Application { public static void main(String[] args) {
SpringApplication.run(MicroserviceStudentProviderHystrix1004Application.class, args);
} }
4,服务提供者1004中controller新增
package com.lingerqi.microservicestudentproviderhystrix1004.controller; import com.lingerqi.microservicecommon.entity.Student;
import com.lingerqi.microservicestudentproviderhystrix1004.service.StudentService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*; import java.util.HashMap;
import java.util.List;
import java.util.Map; @RestController
@RequestMapping("/student")
public class StudentProviderController { @Autowired
private StudentService studentService;
@Value("${server.port}")
private String port;
@PostMapping(value="/save")
public boolean save(Student student){
try{
studentService.save(student);
return true;
}catch(Exception e){
return false;
}
} @GetMapping(value="/list")
public List<Student> list(){
return studentService.list();
} @GetMapping(value="/get/{id}")
public Student get(@PathVariable("id") Integer id){
return studentService.findById(id);
} @GetMapping(value="/delete/{id}")
public boolean delete(@PathVariable("id") Integer id){
try{
studentService.delete(id);
return true;
}catch(Exception e){
return false;
}
} @RequestMapping("/ribbon")
public String ribbon(){
return "工号【"+port+"】正在为您服务";
}
/**
* 测试Hystrix服务降级
* @return
* @throws InterruptedException
*/
@ResponseBody
@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;
}
}
这里我正常访问 返回的是 200 业务数据xxxxx
但是我们这里Thread.sleep(2000) 模拟超时;
这里的话 我们加上@HystrixCommand注解 以及 fallbackMethod
表明这个方法我们再 没有异常以及没有超时(hystrix默认1秒算超时)的情况,才返回正常的业务数据;
否则,进入我们fallback指定的本地方法,我们搞的是500 系统出错,稍后重试,有效的解决雪崩效应,以及返回给用户界面
很好的报错提示信息;
microservice-student-consumer-80项目也要对应的加个方法:
/**
* 测试Hystrix服务降级
* @return
*/
@GetMapping(value="/hystrix")
@ResponseBody
public Map<String,Object> hystrix(){
return restTemplate.getForObject(SERVER_IP_PORT+"/student/hystrix/", Map.class);
}
测试下先启动三个eureka,再启动带hystrix的provider,最后启动普通的consumer;

因为 Hystrix默认1算超时,所有 sleep了2秒 所以进入自定义fallback方法,防止服务雪崩;
我们这里改sleep修改成100毫秒;

Hystrix默认超时时间设置
Hystrix默认超时时间是1秒,我们可以通过hystrix源码看到,
找到 hystrix-core.jar com.netflix.hystrix包下的HystrixCommandProperties类
default_executionTimeoutInMilliseconds属性局势默认的超时时间

我们系统里假如要自定义设置hystrix的默认时间的话;
application.yml配置文件加上
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 3000
改成3秒 然后 我们代码里sleep修改成2秒测试;

sleep修改成4秒;


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>
启动类加注解:
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@EnableHystrixDashboard

启动项目输入:http://localhost:90/hystrix,出现以上图表示OK
测试下;
我们启动三个eureka,然后再启动microservice-student-provider-hystrix-1004
我们直接请求 : http://localhost:1004/student/hystrix
返回正常业务




熔断器Hystrix及服务监控Dashboard的更多相关文章
- SpringCloud之熔断器Hystrix及服务监控Dashboard
目的: 服务雪崩效应 服务熔断服务降级 Hystrix默认超时时间设置 Hystrix服务监控Dashboard 服务雪崩效应 雪崩效应就是一种不稳定的平衡状态也是加密算法的一种特征,它指明文 ...
- springCloud-Hystrix服务监控Dashboard
1.Hystrix服务监控Dashboard 介绍 Hystrix服务监控Dashboard仪表盘 在实际生产中,成千上万的服务,我们怎么知道提供服务的高可用情况,即服务的成功失败超时等相关情况; H ...
- spring cloud微服务快速教程之(四)熔断器(Hystrix)及其工具(Dashboard、Turbine)
0-为什么需要熔断器 在分布式系统中,各个服务相互调用相互依赖,如果某个服务挂了,很可能导致其他调用它的一连串服务也挂掉或者在不断等待中耗尽服务器资源,这种现象称之为雪崩效应: 未来防止系统雪崩,熔断 ...
- SpringCloud学习系列之三----- 断路器(Hystrix)和断路器监控(Dashboard)
前言 本篇主要介绍的是SpringCloud中的断路器(Hystrix)和断路器指标看板(Dashboard)的相关使用知识. SpringCloud Hystrix Hystrix 介绍 Netfl ...
- SpringCloud之Hystrix集群监控turbine仪表盘
1.引入 在前一节中我们演示了单机模式下Hystrix服务监控Dashboard仪表盘,但是在实际生产中微服务都是集群模式, 为了更接近世界生产,我们在这里也给大家讲一下如何监控集群模式 2.准备工作 ...
- Spring Cloud Hystrix Dashboard熔断器-Turbine集群监控(六)
序言 上一篇说啦hystrix的使用方法与配置还有工作流程及为何存在,我去,上一篇这么屌,去看看吧,没这么屌的话,我贴的有官方文档,好好仔细看看 hystrix除啦基本的熔断器功能之外,还可以对接口的 ...
- 微服务—熔断器Hystrix
前言在微服务架构中,我们将系统拆分成了一个个的服务单元,各单元应用间通过服务注册与发现的方式互相依赖. 由于每个单元都在不同的进程中运行,依赖通过远程调用的方式执行,这样就有可能因为网络原因或是依赖服 ...
- Spring Cloud 系列之 Netflix Hystrix 服务监控
Actuator Hystrix 除了可以实现服务容错之外,还提供了近乎实时的监控功能,将服务执行结果和运行指标,请求数量成功数量等等这些状态通过 Actuator 进行收集,然后访问 /actuat ...
- SpringCloud系列七:Hystrix 熔断机制(Hystrix基本配置、服务降级、HystrixDashboard服务监控、Turbine聚合监控)
1.概念:Hystrix 熔断机制 2.具体内容 所谓的熔断机制和日常生活中见到电路保险丝是非常相似的,当出现了问题之后,保险丝会自动烧断,以保护我们的电器, 那么如果换到了程序之中呢? 当现在服务的 ...
随机推荐
- 深入 .NET Core 基础 - 2:共享框架
深入 .NET Core 基础 - 2:共享框架 原文地址:https://natemcmaster.com/blog/2018/08/29/netcore-primitives-2/ 共享框架从 . ...
- 个人搭建后台管理模板 Bootstrap4 ,ASP.NET Core,EF Core,JWT
拥有一个美观好用的网站后台,是很多开发者的梦想,笔者在闲暇时间里搭建了一个不错的后台框架,这里分享诸位开发同仁. 项目地址:https://github.com/kongdf123/KentNoteB ...
- OS_Architecture_MemoryHierarchy
Hit: if the data CPU is looking for can not be found in a cache, it constitutes a hit. Miss: cache m ...
- 转:Spring Boot中使用AOP统一处理Web请求日志
在spring boot中,简单几步,使用spring AOP实现一个拦截器: 1.引入依赖: <dependency> <groupId>org.springframewor ...
- plsql判断和循环
if语句 语法1 如果条件成立,执行if和end if 之间的语句. if 条件表达式 then plsql语句; end if; 语法2 if 条件表达式 then 条件成立时执行的语句; else ...
- luogu P1412 经营与开发 |dp
题目描述 4X概念体系,是指在PC战略游戏中一种相当普及和成熟的系统概念,得名自4个同样以"EX"为开头的英语单词. eXplore(探索) eXpand(拓张与发展) eXplo ...
- WebGPU学习(五): 现代图形API技术要点和WebGPU支持情况调研
大家好,本文整理了现代图形API的技术要点,重点研究了并行和GPU Driven Render Pipeline相关的知识点,调查了WebGPU的相关支持情况. 另外,本文对实时光线追踪也进行了简要的 ...
- Postman 关联接口测试(带有token鉴权)
Postman 关联接口测试(带有token鉴权) 一.登陆接口 创建一个request请求 在Tests中添加JavaScript代码,用来获取鉴权 pm.test("V2", ...
- art-template与swiper发生冲突导致swiper的一些样式不起作用
我们在实际中的前后端分离开发中,在进行渲染后端返回来的数据时我们有时会用到模板来进行渲染数据,而在渲染数据中我们可能用到一些组件来进行一些样式显示.而在页面中数据显示了导致组件的一些样式没有显示,一些 ...
- 记录XunSearch(讯搜)的使用教程步骤(CentOS7下)
一.安装编译工具 yum install make gcc g++ gcc-c++ libtool autoconf automake imake mysql-devel libxml2-devel ...