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

步骤 1 : 需求

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

步骤 2 : 先运行,看到效果,再学习

老规矩,先下载下载区(点击进入)的可运行项目,配置运行起来,确认可用之后,再学习做了哪些步骤以达到这样的效果。 
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 就可以看到监控信息了。

spring cloud监控 springcloud监控器_spring

步骤 3 : 监控图解

监控信息就是如图所示这样,里面的各部分都标记了解释。

spring cloud监控 springcloud监控器_System_02

步骤 4 : 关闭数据服务

此时关闭数据服务,再观察,不一会儿红色的数据就达到 100%啦

spring cloud监控 springcloud监控器_spring_03

步骤 5 : 模仿和排错

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

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

步骤 6 : 创建子项目

创建 hystrix-dashboard 子项目

spring cloud监控 springcloud监控器_spring cloud监控_04

步骤 7 : 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>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

断路器监控启动类,主要就是@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);
}
newSpringApplicationBuilder(ProductServiceHystrixDashboardApplication.class).properties("server.port="+ port).run(args);
 
}
 
}

步骤 9 : application.yml

配置文件

spring:
application:
name: hystrix-dashboard

步骤 10 : ProductViewServiceFeignApplication

接下来修改视图微服务项目,以使得它可以把信息共享给监控中心。
修改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);
}
newSpringApplicationBuilder(ProductViewServiceFeignApplication.class).properties("server.port="+ port).run(args);
 
}
@Bean
public Sampler defaultSampler() {
return Sampler.ALWAYS_SAMPLE;
}
 
}

步骤 11 : AccessViewService

准备一个不停访问服务的类: 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());
}
 
}
 
}
}