SpringCloud之熔断器Hystrix及服务监控Dashboard

2023-10-21 11:40

本文主要是介绍SpringCloud之熔断器Hystrix及服务监控Dashboard,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

服务雪崩效应

在这里插入图片描述
当请求的服务中出现无法访问、异常、超时等问题时,那么用户的请求将会被阻塞、其他功能也会受到影响,

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

服务熔断服务降级

Hystrix断路器简介

hystrix对应的中文名字是“豪猪”,豪猪周身长满了刺,能保护自己不受天敌的伤害,代表了一种防御机制,这与hystrix本身的功能不谋而合,因此Netflix团队将该框架命名为Hystrix,并使用了对应的卡通形象做作为logo。
在这里插入图片描述
在一个分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等,如何能够保证在一个依赖出问题的情况下,不会导致整体服务失败,这个就是Hystrix需要做的事情。Hystrix提供了熔断、隔离、Fallback、cache、监控等功能,能够在一个、或多个依赖同时出现问题时保证系统依然可用。

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

熔断机制是应对雪崩效应的一种微服务链路保护机制。
当某个服务不可用或者响应时间超时,会进行服务降级,进而熔断该节点的服务调用,快速返回自定义的错误影响页面信息。

快速失败返回
没有做服务熔断处理的话
1001, 1102, 1003
1002发生故障,会5s才会想浏览器发生数据返回
做服务熔断处理的话
1002发生故障,2s之后没有返回数据的话,就自动进行服务熔断降级,并快速返回自定义错误页面信息

我们写个项目来测试下;

我们写一个新的带服务熔断的服务提供者项目 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: 1004context-path: /
spring:datasource:type: com.alibaba.druid.pool.DruidDataSourcedriver-class-name: com.mysql.jdbc.Driverurl: jdbc:mysql://localhost:3306/mybatis_ssm?useUnicode=true&characterEncoding=utf8username: mybatis_ssmpassword: xiaolijpa:hibernate:ddl-auto: updateshow-sql: trueapplication:name: microservice-studentprofiles: provider-hystrix-1004eureka:instance:hostname: localhostappname: microservice-studentinstance-id: microservice-student:1004prefer-ip-address: trueclient:service-url:defaultZone: http://eureka2001.xhh.com:2001/eureka/,http://eureka2002.xhh.com:2002/eureka/,http://eureka2003.xhh.com:2003/eureka/info:groupId: com.xhh.testSpringcloudartifactId: microservice-student-provider-hystrix-1004version: 1.0-SNAPSHOTuserName: http://xhh.comphone: 123456

启动类StudentProviderHystrixApplication_1004加下注解支持 @EnableCircuitBreaker

package com.xhh.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.xhh.*.*")
@EnableEurekaClient
@SpringBootApplication
public class MicroserviceStudentProviderHystrix1004Application {public static void main(String[] args) {SpringApplication.run(MicroserviceStudentProviderHystrix1004Application.class, args);}}

服务提供者1004中controller新增
如果StudentProviderController 的 hystrix()执行超过一秒,它就会转到这个hystrixFallback()方法来,并返回错误信息

package com.xhh.microservicestudentproviderhystrix1004.controller;就会调用import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.xhh.microservicecommon.entity.Student;
import com.xhh.microservicestudentproviderhystrix1004.service.StudentService;
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 {@Autowiredprivate 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;}
}

服务消费者
microservice-student-consumer-80项目也要对应的加个方法

 @GetMapping(value="/hystrix")@ResponseBodypublic Map<String,Object> hystrix(){return restTemplate.getForObject(SERVER_IP_PORT+"/student/hystrix/", Map.class);}

然后我们来测试下
先启动三个eureka,再启动带hystrix的provider,最后启动普通的consumer;

浏览器:http://localhost/student/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

在这里插入图片描述
在这里插入图片描述

Hystrix服务监控Dashboard

Hystrix服务监控Dashboard仪表盘

Hystrix提供了 准实时的服务调用监控项目Dashboard,能够实时记录通过Hystrix发起的请求执行情况,
可以通过图表的形式展现给用户看。

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

<!--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>

application.yml

server:port: 90context-path: /

新建启动类:StudentConsumerDashBoardApplication_90
加注解:@EnableHystrixDashboard

然后我们启动这个项目;
然后浏览器输入:http://localhost:90/hystrix

在这里插入图片描述
如果出现这个 就说明OK了;

然后我们来测试下;

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

我们输入要监控的项目和方法路径
我们直接请求http://localhost:1004/student/hystrix
在这里插入图片描述

这篇关于SpringCloud之熔断器Hystrix及服务监控Dashboard的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:https://blog.csdn.net/linyaodonga/article/details/103939406
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/254147

相关文章

Spring Boot中的路径变量示例详解

《SpringBoot中的路径变量示例详解》SpringBoot中PathVariable通过@PathVariable注解实现URL参数与方法参数绑定,支持多参数接收、类型转换、可选参数、默认值及... 目录一. 基本用法与参数映射1.路径定义2.参数绑定&nhttp://www.chinasem.cnbs

JAVA中安装多个JDK的方法

《JAVA中安装多个JDK的方法》文章介绍了在Windows系统上安装多个JDK版本的方法,包括下载、安装路径修改、环境变量配置(JAVA_HOME和Path),并说明如何通过调整JAVA_HOME在... 首先去oracle官网下载好两个版本不同的jdk(需要登录Oracle账号,没有可以免费注册)下载完

Spring StateMachine实现状态机使用示例详解

《SpringStateMachine实现状态机使用示例详解》本文介绍SpringStateMachine实现状态机的步骤,包括依赖导入、枚举定义、状态转移规则配置、上下文管理及服务调用示例,重点解... 目录什么是状态机使用示例什么是状态机状态机是计算机科学中的​​核心建模工具​​,用于描述对象在其生命

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

Java中Integer128陷阱

《Java中Integer128陷阱》本文主要介绍了Java中Integer与int的区别及装箱拆箱机制,重点指出-128至127范围内的Integer值会复用缓存对象,导致==比较结果为true,下... 目录一、Integer和int的联系1.1 Integer和int的区别1.2 Integer和in

SpringSecurity整合redission序列化问题小结(最新整理)

《SpringSecurity整合redission序列化问题小结(最新整理)》文章详解SpringSecurity整合Redisson时的序列化问题,指出需排除官方Jackson依赖,通过自定义反序... 目录1. 前言2. Redission配置2.1 RedissonProperties2.2 Red

IntelliJ IDEA2025创建SpringBoot项目的实现步骤

《IntelliJIDEA2025创建SpringBoot项目的实现步骤》本文主要介绍了IntelliJIDEA2025创建SpringBoot项目的实现步骤,文中通过示例代码介绍的非常详细,对大家... 目录一、创建 Spring Boot 项目1. 新建项目2. 基础配置3. 选择依赖4. 生成项目5.

JSONArray在Java中的应用操作实例

《JSONArray在Java中的应用操作实例》JSONArray是org.json库用于处理JSON数组的类,可将Java对象(Map/List)转换为JSON格式,提供增删改查等操作,适用于前后端... 目录1. jsONArray定义与功能1.1 JSONArray概念阐释1.1.1 什么是JSONA

Java JDK1.8 安装和环境配置教程详解

《JavaJDK1.8安装和环境配置教程详解》文章简要介绍了JDK1.8的安装流程,包括官网下载对应系统版本、安装时选择非系统盘路径、配置JAVA_HOME、CLASSPATH和Path环境变量,... 目录1.下载JDK2.安装JDK3.配置环境变量4.检验JDK官网下载地址:Java Downloads

Spring boot整合dubbo+zookeeper的详细过程

《Springboot整合dubbo+zookeeper的详细过程》本文讲解SpringBoot整合Dubbo与Zookeeper实现API、Provider、Consumer模式,包含依赖配置、... 目录Spring boot整合dubbo+zookeeper1.创建父工程2.父工程引入依赖3.创建ap