springCloud - 第13篇 - 服务监控 集群模式 Hystrix-turbine

本文详细介绍了如何在SpringCloud环境中搭建Hystrix-Turbine集群监控服务,包括配置依赖、启动类、配置文件等关键步骤,以及如何实现对多个服务的实时监控。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家。点击跳转到教程。

1. 在springcloud 体系中,可以用 hystrix-dashboard  实时监控服务的运行状态。上一文记录了单实例的监控,现在实现集群监控。

2. 新建工程 hystrix-turbine 作为集群监控的实现服务。

2.1 file - new - module 

2.2  spring Initializr - module SDK 选择自己的 JDK ,其余的可以不用填写,next。

2.3 填写工程相关信息:包名、工程名等,next。

2.4 此步只是帮助自动生成依赖,可不选,直接 next。

2.5 工程名,代码存放位置等,finish 。

2.6 生成工程的结构如下:

2.7 pom.xml 重用 hystrix-dashboard 的依赖,另新增少量依赖:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com</groupId>
	<artifactId>hystrix-turbine</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>hystrix-turbine</name>
	<description>监控面板-集群 </description>

    <parent>
        <groupId>com</groupId>
        <artifactId>hystrix-dashboard</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

	<dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-turbine</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
	</dependencies>

</project>

2.8 启动类:

package com.hystrixturbine;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.turbine.EnableTurbine;


//监控面板-集群
@EnableTurbine

@SpringBootApplication
public class HystrixTurbineApplication {

	public static void main(String[] args) {
		SpringApplication.run(HystrixTurbineApplication.class, args);
	}

}

2.9 配置文件 application.properties:

# 注册中心 - 端口: 1234、工程名: eureka (见 eureka 工程中配置)。
eureka.client.serviceUrl.defaultZone= http://localhost:1234/eureka/

# 端口
server.port= 8889

# 工程名
spring.application.name= hystrix-turbine

# 被监控服务名称
turbine.app-config= ribbon,feign

# 集群名称为“default”。
# 应用服务很多时,可用多个 hystrix-turbine 来监控不同的聚合集群,集群名可区分这些不同的聚合集群,
# 此集群名亦可在 hystrix-dashboard 中用来定位不同的聚合集群:
# 即:“http://turbine-hostname:port/turbine.stream?cluster=[clusterName]” 中对应 “clusterName”便可。
turbine.cluster-name-expression= "default"

# 同一主机上的服务通过主机名与端口号的组合来进行区分,默认以 host 区分服务,
# 这样,本地调试时,本机上的不同服务则会聚合成一个服务来统计。
turbine.combine-host-port= true

3. 类同已有的 ribbon 工程,调整已有工程 feign 工程。

3.1 增加依赖 spring-cloud-starter-netflix-hystrix,此时 feign工程完整 pom 为:

<?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">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.feign</groupId>
    <artifactId>service-feign</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>service-feign</name>
    <description>服务消费 feign 方式</description>

    <parent>
        <groupId>com.base</groupId>
        <artifactId>base-config</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <dependencies>

        <!-- 开启 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</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>

</project>

3.2 配置文件中加上 hystrix 相关配置,完整配置为:

# 注册中心 - 端口: 1234、工程名: eureka (见 eureka 工程中配置)。
eureka.client.serviceUrl.defaultZone= http://localhost:1234/eureka/

# 端口
server.port= 8702

# 工程名
spring.application.name= feign

# 开启断熔器: ( Feign 自带断路器,但默认为不开启: false)
feign.hystrix.enabled=true

# 也可配置为'*' ,纳入 hystrix 服务监控
management.endpoints.web.exposure.include= hystrix.stream

3.3  启动类确认注解 :@EnableHystrix,同时加上 hystrixMetricsStreamServlet 方法,完整启动类为:

package com.feign.servicefeign;

import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Bean;

@SpringBootApplication

/**
 * 基于接口的注解,可插拔,可使用 Feign注解 和 JAX-RS注解
 * Feign 默认集成 Ribbon,并和 Eureka 结合,默认实现负载均衡。
 */
@EnableFeignClients

// 标明自已为服务
@EnableEurekaClient

// 开启断路器: Hystrix
@EnableHystrix
public class ServiceFeignApplication {

	public static void main(String[] args) {
		SpringApplication.run(ServiceFeignApplication.class, args);
	}

    @Bean
    public ServletRegistrationBean hystrixMetricsStreamServlet() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
        registration.addUrlMappings("/hystrix.stream");
        return registration;
    }
}

4. 依次启动工程: 注册中心 eureka ( 端口 1234 ) 、 服务应用 ribbon  ( 端口 8701 ) 、服务应用 feign ( 端口 8702)、

监控集群服务 hystrix-turbine ( 端口 8889)、监控仪表盘hystrix-dashboard  ( 端口 8888) 。

其中,服务应用 ribbon、feign 都调用第三方服务应用:seeParam  ( 端口 8801) 。

4.1 seeParam 工程未启动,由于有熔断机制,访问 feign 工程效果为:

4.2 同样,seeParam 工程未启动,由于有熔断机制,访问 ribbon 工程效果为:

4.3 访问 http://localhost:8888/hystrix.stream ,可看到和单实例监控界面入口一样的面板界面。

此时,在 hystrix-dashboard 中使用集群监控的 URL 查看监控信息,在页面第一个输入框中输入 hystrix-turbine 工程的访问路径:http://localhost:8889/turbine.stream

4.4  进入实时监控显示页面可看到 ,刚才已经多次刷新过请求的 feign、ribbon 工程的运行状况统计信息:

红色 100% 表示 请求 seeParam 工程失败。球体为红色也表明服务健康状态为最差。

4.5 启动  seeParam 工程后,请求其接口:

再多次刷新 feign、ribbon 的请求后,分别请求到了 seeParam 的接口:

此时,再查看 hysteix-dashboard 中的服务统计信息:

失败请求率为 0.0%,球体也变为绿色,表示服务运行正常。

至此,服务监控的集群模式也实现了。

-------------------------------------------------------------

下一篇:springCloud - 第14篇 - 

源码见:

https://gitee.com/FJ_WoMenDeShiJie/springcloud-feign

https://gitee.com/FJ_WoMenDeShiJie/springcloud-ribbon

https://gitee.com/FJ_WoMenDeShiJie/springcloud-seeParam

https://gitee.com/FJ_WoMenDeShiJie/springcloud-hystrix-turbine

https://gitee.com/FJ_WoMenDeShiJie/springcloud-hystrix-dashboard

-------------------------------------------------------------

PS:这个系列不定时更新,只是个人的学习分享,

内容全程参考书目:

《Spring Cloud 与 Docker 微服务架构空实战 》、

《Spring Cloud 微服务实战》及此书作者博客:http://blog.didispace.com/spring-cloud-learning/

《深入理解 Spring Cloud 与微服务构建》及此书作者博客:https://blog.youkuaiyun.com/forezp/article/details/70148833

----------------------------------------------------------------
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值