springcloud-springcloudnetflix之hystrix;turbine(代码片段)

MinggeQingchun MinggeQingchun     2023-03-13     375

关键词:

阅读本文前可先参考

SpringCloud - Spring Cloud根/父项目,开发准备(二)
https://blog.csdn.net/MinggeQingchun/article/details/125308948

https://blog.csdn.net/MinggeQingchun/article/details/125312594 

https://blog.csdn.net/MinggeQingchun/article/details/125315839

Spring Cloud Hystrix Turbine

Hystrix Dashboard 的主要功能是可以对某一项微服务进行监控,但真实情况下,不可能只对一个微服务进行监控,我们有很多微服务,所以我们需要对很多微服务进行监控,这个时候就需要使用到turbine [ˈtɜːbaɪn] 来完成

Hystrix Dashboard 前面已经知道了,它的主要功能是可以对某一项微服务进行监控,但真实情况下,不可能只对一个微服务进行监控,我们有很多微服务,所以我们需要对很多微服务进行监控,这个时候就需要使用到turbine [ˈtɜːbaɪn] 来完成

单个hystrix服务的监控

多个hystrix服务的监控

1、新建一个springboot Module(springcloud-6-service-eureka-hystrix-turbine),设置父项目

2、添加 spring-cloud-starter-netflix-turbine等 依赖

<dependencies>
        <!-- spring-cloud-starter-netflix-turbine -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>
    </dependencies>
<!--继承统一的父项目-->
    <parent>
        <groupId>com.company</groupId>
        <artifactId>springcloud-demo</artifactId>
        <version>1.0.0</version>
        <!--        <relativePath/> &lt;!&ndash; lookup parent from repository &ndash;&gt;-->
    </parent>

    <groupId>com.company</groupId>
    <artifactId>springcloud-6-service-eureka-hystrix-turbine</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <name>springcloud-6-service-eureka-hystrix-turbine</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <!-- spring-cloud-starter-netflix-turbine -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

3、application.prperties配置文件中配置访问端口3722

server.port=3722

#不向注册中心注册自己
eureka.client.register-with-eureka=false

#注册中心的链接地址  http://localhost:8761/eureka
eureka.client.service-url.defaultZone=http://eureka8761:8761/eureka,http://eureka8762:8762/eureka,http://eureka8763:8763/eureka

#配置turbine
turbine.app-config=springcloud-6-service-eureka-hystrix-consumer8081,springcloud-6-service-eureka-hystrix-consumer8082
#需要有这个,没有的话聚合不了多个项目
turbine.cluster-name-expression="default"

4、在 Spring Boot 的启动类中,添加@EnableTurbine 注解,//开启turbine对hystrix聚合汇总支持

@EnableTurbine //开启turbine对hystrix聚合汇总支持
@SpringBootApplication
public class EurekaHystrix6TurbineApplication 

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

5、创建两个服务消费者 springcloud-6-service-eureka-hystrix-consumer8081,springcloud-6-service-eureka-hystrix-consumer8082(复制自springcloud-6-service-eureka-hystrix-consumer)

6、启动springboot启动类,访问应用,再去查看 DashBoard 控制中心控制台服务

http://localhost:8080/springcloud/eureka/hystrix/goodHystrixList

http://localhost:8081/springcloud/eureka/hystrix/goodHystrixList

http://localhost:8080/actuator/hystrix.stream

http://localhost:8081/actuator/hystrix.stream

http://localhost:3722/turbine.stream