how2j.cn

下载区
文件名 文件大小
springcloud.rar 68k
步骤 1 : 需求   
步骤 2 : 先运行,看到效果,再学习   
步骤 3 : 监控图解   
步骤 4 : 关闭数据服务   
步骤 5 : 模仿和排错   
步骤 6 : 创建子项目   
步骤 7 : pom.xml   
步骤 8 : ProductServiceHystrixDashboardApplication   
步骤 9 : application.yml   
步骤 10 : ProductViewServiceFeignApplication   
步骤 11 : AccessViewService   

前面讲解了断路器, 当数据服务不可用的时候, 断路器就会发挥作用。
那么数据服务什么时候可用,什么时候不可用,如何监控这个事情呢? 我们就要用到 断路器监控 来可视化掌控这个情况了。
步骤 2 :

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

edit
老规矩,先下载右上角的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。
1. 首先挨个运行 EurekaServerApplication, ConfigServerApplication, ProductDataServiceApplication, ProductViewServiceFeignApplication,ProductServiceHystrixDashboardApplication
2. 运行视图微服务里的 AccessViewService 来周期性地访问 http://127.0.0.1:8012/products。 因为只有访问了,监控里才能看到数据。
3. 打开监控地址
http://localhost:8020/hystrix
4. 如图所示,在最上面输入
http://localhost:8012/actuator/hystrix.stream

这个地址就是视图微服务的短路信息。
5. 然后点击 Monitor Stream 就可以看到监控信息了。
先运行,看到效果,再学习
监控信息就是如图所示这样,里面的各部分都标记了解释。
监控图解
步骤 4 :

关闭数据服务

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

推荐使用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较。
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,并且很明显地标记出来
这里提供了绿色安装和使用教程:diffmerge 下载和使用教程
创建 hystrix-dashboard 子项目
创建子项目
一堆 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>productServiceHystrixDashboard</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> </dependencies> </project>
步骤 8 :

ProductServiceHystrixDashboardApplication

edit
断路器监控启动类,主要就是@EnableHystrixDashboard 这个
package cn.how2j.springcloud; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import cn.hutool.core.util.NetUtil; @SpringBootApplication @EnableHystrixDashboard public class ProductServiceHystrixDashboardApplication { public static void main(String[] args) { int port = 8020; if(!NetUtil.isUsableLocalPort(port)) { System.err.printf("端口%d被占用了,无法启动%n", port ); System.exit(1); } new SpringApplicationBuilder(ProductServiceHystrixDashboardApplication.class).properties("server.port=" + port).run(args); } }
配置文件
spring: application: name: hystrix-dashboard
spring:
  application:
    name: hystrix-dashboard
步骤 10 :

ProductViewServiceFeignApplication

edit
接下来修改视图微服务项目,以使得它可以把信息共享给监控中心。
修改ProductViewServiceFeignApplication, 增加 @EnableCircuitBreaker
package cn.how2j.springcloud; import java.util.Scanner; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.context.annotation.Bean; import brave.sampler.Sampler; import cn.hutool.core.convert.Convert; import cn.hutool.core.thread.ThreadUtil; import cn.hutool.core.util.NetUtil; import cn.hutool.core.util.NumberUtil; @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient @EnableFeignClients @EnableCircuitBreaker public class ProductViewServiceFeignApplication { public static void main(String[] args) { //判断 rabiitMQ 是否启动 int rabbitMQPort = 5672; if(NetUtil.isUsableLocalPort(rabbitMQPort)) { System.err.printf("未在端口%d 发现 rabbitMQ服务,请检查rabbitMQ 是否启动", rabbitMQPort ); System.exit(1); } int port = 0; int defaultPort = 8012; Future<Integer> future = ThreadUtil.execAsync(() ->{ int p = 0; System.out.println("请于5秒钟内输入端口号, 推荐 8012 、 8013 或者 8014,超过5秒将默认使用"+defaultPort); Scanner scanner = new Scanner(System.in); while(true) { String strPort = scanner.nextLine(); if(!NumberUtil.isInteger(strPort)) { System.err.println("只能是数字"); continue; } else { p = Convert.toInt(strPort); scanner.close(); break; } } return p; }); try{ port=future.get(5,TimeUnit.SECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e){ port = defaultPort; } if(!NetUtil.isUsableLocalPort(port)) { System.err.printf("端口%d被占用了,无法启动%n", port ); System.exit(1); } new SpringApplicationBuilder(ProductViewServiceFeignApplication.class).properties("server.port=" + port).run(args); } @Bean public Sampler defaultSampler() { return Sampler.ALWAYS_SAMPLE; } }
步骤 11 :

AccessViewService

edit
准备一个不停访问服务的类: AccessViewService。 这样可以不断地访问服务,才便于在监控那里观察现象。
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); try { String html= HttpUtil.get("http://127.0.0.1:8012/products"); System.out.println("html length:" + html.length()); } catch(Exception e) { System.err.println(e.getMessage()); } } } }


HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。


问答区域    
2023-10-30 关于在eureka注册中心还是能监测到该服务的一个疑问
byteDance

为什么yml里面并没有添加eureka-client的配置,启动类也没有加eureka-client注解,在eureka注册中心还是能监测到该服务







回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到




2021-01-28 缺失ProductServiceHystrixDashboardApplication
JAVA界的盖伦

右上角没有ProductServiceHystrixDashboardApplication







回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
答案 或者 代码至少填写一项, 如果是自己有问题,请重新提问,否则站长有可能看不到




2020-11-23 关于链路追踪器
2020-06-30 一次性启动那么多服务,内存不够用啊,越到后面内存占用越高,真实的内存框架cloud
2020-06-27 按照站长的顺序重启的微服务


提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 3 条以前的提问,请 点击查看

提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
关于 分布式和集群-SpringCloud-断路器监控 的提问

尽量提供截图代码异常信息,有助于分析和解决问题。 也可进本站QQ群交流: 578362961
提问尽量提供完整的代码,环境描述,越是有利于问题的重现,您的问题越能更快得到解答。
对教程中代码有疑问,请提供是哪个步骤,哪一行有疑问,这样便于快速定位问题,提高问题得到解答的速度
在已经存在的几千个提问里,有相当大的比例,是因为使用了和站长不同版本的开发环境导致的,比如 jdk, eclpise, idea, mysql,tomcat 等等软件的版本不一致。
请使用和站长一样的版本,可以节约自己大量的学习时间。 站长把教学中用的软件版本整理了,都统一放在了这里, 方便大家下载: https://how2j.cn/k/helloworld/helloworld-version/1718.html

上传截图