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的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/254147

相关文章

Spring Security简介、使用与最佳实践

《SpringSecurity简介、使用与最佳实践》SpringSecurity是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框架,本文给大家介绍SpringSec... 目录一、如何理解 Spring Security?—— 核心思想二、如何在 Java 项目中使用?——

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

springboot中使用okhttp3的小结

《springboot中使用okhttp3的小结》OkHttp3是一个JavaHTTP客户端,可以处理各种请求类型,比如GET、POST、PUT等,并且支持高效的HTTP连接池、请求和响应缓存、以及异... 在 Spring Boot 项目中使用 OkHttp3 进行 HTTP 请求是一个高效且流行的方式。

java.sql.SQLTransientConnectionException连接超时异常原因及解决方案

《java.sql.SQLTransientConnectionException连接超时异常原因及解决方案》:本文主要介绍java.sql.SQLTransientConnectionExcep... 目录一、引言二、异常信息分析三、可能的原因3.1 连接池配置不合理3.2 数据库负载过高3.3 连接泄漏

javacv依赖太大导致jar包也大的解决办法

《javacv依赖太大导致jar包也大的解决办法》随着项目的复杂度和依赖关系的增加,打包后的JAR包可能会变得很大,:本文主要介绍javacv依赖太大导致jar包也大的解决办法,文中通过代码介绍的... 目录前言1.检查依赖2.更改依赖3.检查副依赖总结 前言最近在写项目时,用到了Javacv里的获取视频

Java实现字节字符转bcd编码

《Java实现字节字符转bcd编码》BCD是一种将十进制数字编码为二进制的表示方式,常用于数字显示和存储,本文将介绍如何在Java中实现字节字符转BCD码的过程,需要的小伙伴可以了解下... 目录前言BCD码是什么Java实现字节转bcd编码方法补充总结前言BCD码(Binary-Coded Decima

SpringBoot全局域名替换的实现

《SpringBoot全局域名替换的实现》本文主要介绍了SpringBoot全局域名替换的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录 项目结构⚙️ 配置文件application.yml️ 配置类AppProperties.Ja

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

JavaScript中的高级调试方法全攻略指南

《JavaScript中的高级调试方法全攻略指南》什么是高级JavaScript调试技巧,它比console.log有何优势,如何使用断点调试定位问题,通过本文,我们将深入解答这些问题,带您从理论到实... 目录观点与案例结合观点1观点2观点3观点4观点5高级调试技巧详解实战案例断点调试:定位变量错误性能分

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H