502、Java分布式和集群15 -【SpringCloud视图微服务 - 断路器监控】 2021.06.30

0、需求

在上个知识点讲解了针对一个微服务的断路器监控,但是微服务通常会是多个实例组成的一个集群。 倘若集群里的实例比较多,难道要挨个挨个去监控这些实例吗? 何况有时候,根据集群的需要,会动态增加或者减少实例,监控起来就更麻烦了。
所以为了方便监控集群里的多个实例,springCloud 提供了一个 turbine 项目,它的作用是把一个集群里的多个实例汇聚在一个 turbine里,这个然后再在 断路器监控里查看这个 turbine, 这样就能够在集群层面进行监控啦。
在这里插入图片描述

1、先运行,看到效果,再学习

老规矩,先下载右上角的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。

  1. 首先挨个运行 EurekaServerApplication, ConfigServerApplication, ProductDataServiceApplication, ProductViewServiceFeignApplication:8012,, ProductViewServiceFeignApplication:8013,ProductServiceHystrixDashboardApplication, ProductServiceTurbineApplication.
  2. 运行视图微服务里的 AccessViewService 来周期性地访问 http://127.0.0.1:8012/products 和 http://127.0.0.1:8013/products。 因为只有访问了,监控里才能看到数据。
  3. 打开监控地址
    http://localhost:8020/hystrix
  4. 在最上面输入

http://localhost:8021/turbine.stream

这个地址就是汇聚了8012,8013 两个视图微服务 turbine聚合信息。
5. 然后点击 Monitor Stream 就可以看到监控信息了。
在这里插入图片描述

2、模仿和排错

在确保可运行项目能够正确无误地运行之后,再严格照着教程的步骤,对代码模仿一遍。
模仿过程难免代码有出入,导致无法得到期望的运行结果,此时此刻通过比较正确答案 ( 可运行项目 ) 和自己的代码,来定位问题所在。
采用这种方式,学习有效果,排错有效率,可以较为明显地提升学习速度,跨过学习路上的各个槛。

推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来
这里提供了绿色安装和使用教程:diffmerge 下载和使用教程

3、创建子项目

在这里插入图片描述

4、pom.xml

一堆jar

<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>
  <parent>
    <groupId>cn.how2j.springcloud</groupId>
    <artifactId>springcloud</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>productServiceTurbine</artifactId>
 
    <dependencies>
        <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.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-hystrix-dashboard</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-turbine</artifactId>
        </dependency>
 
    </dependencies>
 
</project>

5、ProductServiceTurbineApplication

启动类,主要是注解:@EnableTurbine

package cn.how2j.springcloud;
 
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.turbine.EnableTurbine;
 
import cn.hutool.core.util.NetUtil;
 
@SpringBootApplication
@EnableTurbine
public class ProductServiceTurbineApplication {
    public static void main(String[] args) {
        int port = 8021;
        if(!NetUtil.isUsableLocalPort(port)) {
            System.err.printf("端口%d被占用了,无法启动%n", port );
            System.exit(1);
        }
        new SpringApplicationBuilder(ProductServiceTurbineApplication.class).properties("server.port=" + port).run(args);
 
    }
 
}

6、application.yml

配置信息,主要是:

appConfig: product-view-service-feign

这就表示它会把所有微服务名称是product-view-service-feign 的实例信息都收集起来。

spring:
  application.name: turbine
turbine:
  aggregator:
    clusterConfig: default  
  appConfig: product-view-service-feign  ### 配置Eureka中的serviceId列表,表明监控哪些服务
  clusterNameExpression: new String("default")
eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

7、AccessViewService

修改一下AccessViewService, 使得其访问 8012和 8013端口

package cn.how2j.springcloud.util;
  
import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.http.HttpUtil;
  
public class AccessViewService {
  
    public static void main(String[] args) {
          
        while(true) {
            ThreadUtil.sleep(1000);
            access(8012);
            access(8013);
        }
          
    }
  
    public static void access(int port) {
        try {
            String html= HttpUtil.get(String.format("http://127.0.0.1:%d/products",port));
            System.out.printf("%d 地址的视图服务访问成功,返回大小是 %d%n" ,port, html.length());
        }
        catch(Exception e) {
            System.err.printf("%d 地址的视图服务无法访问%n",port);
        }
    }
}


8、参考链接

[01] How2j - SpringCloud视图微服务 - 断路器监控

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值