# 从浅入深 学习 SpringCloud 微服务架构(七)Hystrix(3)

news/2024/9/23 6:12:40/

从浅入深 学习 SpringCloud 微服务架构(七)Hystrix(3)

hystrix_Actuator__hystrix__3">一、hystrix:通过 Actuator 获取 hystrix 的监控数据

1、Hystrix 的监控平台介绍:

1)Hystrix 除了实现容错功能,Hystrix 还提供了近乎实时的监控,
HystrixCommand 和 HystrixObservableCommand 在执行时,会生成执行结果和运行指标。
比如每秒的请求数量,成功数量等。

2)这些状态会暴露在 Actuator 提供的 /health 端点中。
我们只需为项目添加 spring-boot-actuator 依赖,重启项目,

访问 http://localhost:9001/actuator/hystrix.stream, 即可看到实时的监控数据。

hystrix_Actuator__hystrix___17">2、hystrix:通过 Actuator 获取 hystrix 的监控数据 步骤。

1)在项目 pom.xml 配置文件中,导入 Actuator 相关依赖坐标。

<!-- 1)引入 Hystrix 依赖坐标 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- 引入 hystrix 的监控信息 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

2)在项目启动类 激活 Hystrix 组件。

@SpringBootApplication
@EntityScan("djh.it.order.domain")
@EnableEurekaClient  //激活 EurekaClient,同 @EnableDiscoveryClient 注解相同。
@EnableFeignClients  //激活 Feign
@EnableCircuitBreaker  // 2)激活 hystrix
public class OrderApplication {...}

3)在项目 application.yml 配置文件上,配置 Actuator 获取 hystrix 的监控数据信息。

management:   # 配置 Actuator 获取 hystrix 的监控数据 暴露端点。endpoints:web:exposure:include: '*'   # 暴露所有端点。

3、在服务消费者 order_service 子工程(子模块)pom.xml 配置文件中,引入 Actuator 相关依赖坐标。

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><parent><artifactId>spring_cloud_consul_demo</artifactId><groupId>djh.it</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>order_service</artifactId><dependencies><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><!--            <version>5.1.32</version>--><version>8.0.26</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><!-- 导入 eureka 注册中心 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!-- springcloud 提供的对基于 consul 的服务发现 -->
<!--        <dependency>-->
<!--            <groupId>org.springframework.cloud</groupId>-->
<!--            <artifactId>spring-cloud-starter-consul-discovery</artifactId>-->
<!--        </dependency>-->
<!--        &lt;!&ndash; actuator 健康检查 &ndash;&gt;-->
<!--        <dependency>-->
<!--            <groupId>org.springframework.boot</groupId>-->
<!--            <artifactId>spring-boot-starter-actuator</artifactId>-->
<!--        </dependency>--><!-- springcloud 整合 openFeign --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-openfeign</artifactId></dependency><!-- Hystrix 组件 对 RestTemplate 的支持4步:1)引入 Hystrix 依赖坐标 --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId></dependency><!-- 引入 hystrix 的监控信息 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId></dependency></dependencies>
</project>
<!--  spring_cloud_consul_demo\order_service\pom.xml -->

hystrix__127">4、在服务消费者 order_service 子工程(子模块)启动类 OrderApplication.java 上激活 hystrix 组件。

/***    spring_cloud_demo\order_service\src\main\java\djh\it\order\OrderApplication.java**   2024-4-27  启动类 OrderApplication.java*/
package djh.it.order;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;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EntityScan("djh.it.order.domain")
@EnableEurekaClient  //激活 EurekaClient,同 @EnableDiscoveryClient 注解相同。
@EnableFeignClients  //激活 Feign
@EnableCircuitBreaker  // 2)激活 hystrix
public class OrderApplication {//@EnableFeignClients  //激活 Feign 组件后,不需要以下配置。
//    @LoadBalanced
//    @Bean
//    public RestTemplate restTemplate(){
//        return new RestTemplate();
//    }public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);}
}

hystrix__166">5、在服务消费者 order_service 子工程(子模块)application.yml 配置文件中,配置 Actuator 获取 hystrix 的监控数据信息。

##  spring_cloud_consul_demo\order_service\src\main\resources\application.ymlserver:port: 9002  # 启动端口 命令行注入。
#  port: ${port:9002}  # 启动端口设置为动态传参,如果未传参数,默认端口为 9002
#  tomcat:
#    max-threads: 10  # 设置 tomcat 最大连接数量,用以模拟高并发环境问题。spring:application:name: service-order  #spring应用名, # 注意 FeignClient 不支持名字带下划线
#  main:
#    allow-bean-definition-overriding: true # SpringBoot2.1 需要设定。datasource:driver-class-name: com.mysql.jdbc.Driver  # mysql 驱动
#    url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8url: jdbc:mysql://localhost:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai# MySQL8.0 可以写成  root, 012311 或  'root', '012311'   # MySQL5.7 只能写成 'root', '012311'  # 注意用户名和密码后一定不能有空格。username: 'root'password: '12311'jpa:database: MySQLshow-sql: trueopen-in-view: trueeureka:  # 配置 Eurekaclient:service-url:defaultZone: http://localhost:9000/eureka/  # 多个 eurekaserver 用 , 隔开。instance:prefer-ip-address: true  # 使用 ip 地址注册instance-id: ${spring.cloud.client.ip-address}:${server.port}# 配置 feign 日志的输出。
# 日志配置:NONE:GI 不输出日志,BASIC:适用于生产环境追踪问题,HEADERS:在BASIC基础上,记录请求和响应头信息,FULL:记录所有。
logging:level:djh.it.order.feign.ProductFeignClient: debugfeign:client:config:default:connectTimeout: 5000   #服务之间建立连接所用的时间  #不设置 connectTimeout 会导致 readTimeout 设置不生效readTimeout: 5000   #建立连接后从服务端读取到数据用的时间service-product:  # 需要调用的服务名称loggerLevel: FULLhystrix:  # 开启对 hystrix 的支持。enabled: truehystrix:  # 配置 hystrix 熔断(Hystrix:基于 RestTemplate 的统一降级配置)command:default:execution:isolation:thread:timeoutInMilliseconds: 5000  # 默认的熔断超时时间为1秒,若1秒没有返回数据,会自动触发降级逻辑。management:   # 配置 Actuator 获取 hystrix 的监控数据 暴躁端点。endpoints:web:exposure:include: '*'   # 暴露所有端点。#  # 配置 consul 的服务注册
#  cloud:
#    consul:
#      host: 127.0.0.1  # consul 服务器的主机地址
#      port: 8500  # consul 端口
#      discovery:
#        register: false  # 是否需要注册,默认是 false
#        instance-id: ${spring.application.name}-1  # 注册的实例 ID(唯一标志)
#        service-name: ${spring.application.name}  # 服务的名称
#        port: ${server.port}  # 服务的请求端口
#        prefer-ip-address: true  # 指定开启 IP 地址注册
#        ip-address: ${spring.cloud.client.ip-address}  # 当前服务的请求 IP

eureka_service_order_service_product_service__248">6、重新运行 eureka_service, order_service, product_service 三个模块启动类,进行测试。

1)浏览器地址栏输入:http://localhost:9002/actuator/hystrix.stream 查看访问监控数据。

在这里插入图片描述

2)浏览器地址栏输入:http://localhost:9002/actuator 可以查看当前 actuator 输出的端点信息。

在这里插入图片描述

3)浏览器地址栏输入:http://localhost:9002/order/buy/1
刷新请求,重新查看访问监控数据

在这里插入图片描述

hystrix_hystrix__dashboard__hystrix__265">二、hystrix:通过 hystrix 的 dashboard 监控 hystrix 数据流

1、Hvstrix 官方还提供了基于图形化的 DashBoard(仪表板)监控平台。Hystrix 仪表板可以显示每个断路器(被@HystrixCommand注解的方法)的状态。

hystrix_hystrix__dashboard__hystrix__Hystrix_DashBoard__270">2、hystrix:通过 hystrix 的 dashboard 监控 hystrix 数据流,搭建 Hystrix DashBoard 监控平台步骤。

1)在项目 pom.xml 配置文件中,导入 Actuator 相关依赖坐标。

<!-- 1)引入 Hystrix 依赖坐标 -->
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- 引入 hystrix 的监控信息 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>

2)在项目 启动类 使用 @EnableHystrixDashboard 注解,激活仪表盘项目。

@EnableHystrixDashboard  // 激活 Hystrix 基于图形化的 DashBoard(仪表板)监控平台
public class OrderApplication {...}

3)运行启动类,访问测试。

3、在服务消费者 order_service 子工程(子模块)pom.xml 配置文件中,引入 Actuator 相关依赖坐标。

略,前面已经导入。

4、在服务消费者 order_service 子工程(子模块)启动类 OrderApplication.java 使用 @EnableHystrixDashboard 注解,激活仪表盘项目。

/***    spring_cloud_demo\order_service\src\main\java\djh\it\order\OrderApplication.java**   2024-4-27  启动类 OrderApplication.java*/
package djh.it.order;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;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EntityScan("djh.it.order.domain")
@EnableEurekaClient  //激活 EurekaClient,同 @EnableDiscoveryClient 注解相同。
@EnableFeignClients  //激活 Feign
@EnableCircuitBreaker  // 2)激活 hystrix,  Hystrix 组件 对 RestTemplate 的支持4步:
@EnableHystrixDashboard  // 激活 Hystrix 基于图形化的 DashBoard(仪表板)监控平台
public class OrderApplication {//    //@EnableFeignClients  //激活 Feign 组件后,不需要以下配置。
//    @LoadBalanced
//    @Bean
//    public RestTemplate restTemplate(){
//        return new RestTemplate();
//    }public static void main(String[] args) {SpringApplication.run(OrderApplication.class, args);}
}

eureka_service_order_service_product_service__345">5、重新运行 eureka_service, order_service, product_service 三个模块启动类,进行测试。

1)浏览器地址栏输入:http://localhost:9002/hystrix

就进入 基于图形化的 DashBoard(仪表板)监控平台。

在这里插入图片描述
2)在输入框输入请求数据地址:http://localhost:9002/actuator/hystrix.stream 进入。

浏览器地址栏输入:http://localhost:9002/order/buy/1 多刷新几次,就会看到图表变化。
在这里插入图片描述

上一节链接请点击:
# 从浅入深 学习 SpringCloud 微服务架构(七)Hystrix(2)


http://www.ppmy.cn/news/1448084.html

相关文章

动态增删表格

期望目标&#xff1a;实现一个能通过按钮来动态增加表格栏&#xff0c;每次能添加一行&#xff0c;每行末尾有一个删减按钮。 <el-button type"text" class"primary"click"addMember()">添加</el-button> <el-table:data"m…

人事管理|基于SprinBoot+vue的企业人事管理系统(源码+数据库+文档)

人事管理目录 基于SprinBootvue的企业人事管理系统 一、前言 二、系统设计 三、系统功能设计 1管理员功能模块 2员工功能模块 四、数据库设计 五、核心代码 六、论文参考 七、最新计算机毕设选题推荐 八、源码获取&#xff1a; 博主介绍&#xff1a;✌️大厂码农|毕设…

2024年CMS市场的份额趋势和使用统计

目前市面上有超过一半的网站都是使用CMS来搭建的&#xff0c;据不完全统计&#xff0c;现在大概有900多种CDM可供选择&#xff0c;以下是最常见的CMS的市场份额和使用率信息&#xff1a; 除了WordPress以外&#xff0c;Shopify和Wix也是比较流行的内容管理系统&#xff0c;尤其…

webpack打包工具

目录 1. yarn包管理器 1.1 yarn 是什么, 有什么用? 1.2 yarn的使用 ​​​​​​2. webpack基本概述 2.1 webpack是什么&#xff1f; 2.2 什么是打包&#xff1f; 2.3 webpack能做什么&#xff1f; 3. webpack基本使用步骤 3.1 webpack基本使用步骤 3.2 package.jso…

移植 SquareLine 导出的 UI 源码到 HMI-Board

目录 准备工具创建 HMI 工程设计 UIUI 移植板级验证更多内容 HMI-Board 为 RT-Thread 联合瑞萨推出的高性价比图形评估套件&#xff0c;取代传统的 HMI 主控板 硬件&#xff0c;一套硬件即可实现 HMI IoT 控制 的全套能力。依托于瑞萨高性能芯片 RA6M3 及 RT-Thread 软件生态…

MATLAB | 全网唯一!使用MATLAB绘制各类螺旋图

全网唯一&#xff0c;MATLAB绘制各类螺旋图&#xff0c;还在为X轴过长而困扰吗&#xff1f;把X轴盘起来就可以优雅的绘图&#xff01;&#xff01;今天带来的是由本人开发的SSpiral MATLAB 工具包&#xff0c;&#xff0c;本期工具函数绘图效果&#xff1a; 工具函数放在最后&a…

GoLang Gin实际使用

所有代码同步到Admin/gitDemo - Gitee.comhttps://gitee.com/mec-deployment-team_0/git-demo/tree/dev/ 1.创建Gin框架 一般设计一个常规的web项目&#xff0c;都需要以下几个模块 runApp 主函数&#xff0c;运行整个项目routes 路由控制&#xff0c;管理跳转以及路由分组co…

windows编程中的位图操作,界面中插入位图方法

在界面中插入位图&#xff0c;主要用到BitBlt这个函数 BOOL CDC::BitBlt(int x, int y, int width, int height, CDC *src, int src_x, int src_y, DWORD dwRop)用法说明见官方说明 这里要注意的是&#xff0c;这个函数是把源图像从src这个设备上下文传递到目标设备上下文(这…