前言
记录一次学习SpringCloud的过程,不断积累经验,手撸方能熟能生巧。
新手上路难免有误,人非圣贤,欢迎各位指出不足之处,虚心听取各位的建议与意见。
项目源码:https://github.com/Ahua0918/hwa
一、简介
在微服务架构中为保证程序的可用性,防止程序出错导致网络堵塞,出现了断路器模型。断路器的状况反映了一个程序的可用性和健壮性,它是一个重要指标。Hystrix Dashboard是作为断路器状态的一个组件,提供了数据监控和友好的图形化界面。
二、准备工作
2.1 基于Maven父项目创建子项目,右击项目--Module--选择Maven--选择对应JDK版本--点击Next--输入模块名hwa-hystrix-dashboard即可
2.2 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>
<parent>
<artifactId>hwa</artifactId>
<groupId>com.lee</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>hwa-hystrix-dashboard</artifactId>
<packaging>jar</packaging>
<description>SuperHystrix 断路器监控</description>
<dependencies>
<!-- 用于访问路径:/actuator/bus-refresh -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Hystrix断路器依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<!-- Hystrix断路器监控依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
</dependencies>
</project>
2.3 编写断路器监控启动类
package com.lee.hystrix;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@SpringBootApplication
@EnableHystrixDashboard
public class HwaHystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HwaHystrixDashboardApplication.class,args);
}
}
2.4 编写配置文件 application.yml
spring:
application:
name: hwa-hystrix-dashboard
server:
port: 8882
2.5 修改视图微服务项目
修改HwaFeignApplication,使得可以把信息共享给监控中心,添加@EnableCircuitBreaker
package com.lee.feign;
import cn.hutool.core.net.NetUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients //通过注解@EnableFeignClients配置Feign客户端功能
@RestController
@RefreshScope //配置文件自动刷新
@EnableHystrix //通过注解@EnableHystrix开启Hystrix
@EnableCircuitBreaker
public class HwaFeignApplication {
public static void main(String[] args) {
//判断 RabbitMQ 是否启动
int rabbitMQPort=5672;
if(NetUtil.isUsableLocalPort(rabbitMQPort)){
System.err.printf("未在端口%d 发现 rabbitMQ服务,请检查rabbitMQ 是否启动", rabbitMQPort);
System.exit(1);
}
SpringApplication.run(HwaFeignApplication.class, args);
}
@Value("${hwa}")
String hwa;
@RequestMapping(value = "/hiConfigClient")
public String hi(){
return hwa;
}
}
2.6 工具类
准备一个不停访问服务的类AccessViewService,这样可以不停的访问服务,才便于在监控那里观察现象
package com.lee.feign.util;
import cn.hutool.core.thread.ThreadUtil;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
public class AccessViewService {
public static void main(String[] args) {
while (true){
ThreadUtil.sleep(1000);
try {
String s=AccessViewService.sendGet("http://127.0.0.1:8884/hi","name=hwa");
System.out.println(s);
}catch (Exception e){
System.err.println(e.getMessage());
}
}
}
public static String sendGet(String url, String param) {
String result = "";
BufferedReader in = null;
try {
String urlNameString = url + "?" + param;
URL realUrl = new URL(urlNameString);
// 打开和URL之间的连接
URLConnection connection = realUrl.openConnection();
// 设置通用的请求属性
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("connection", "Keep-Alive");
connection.setRequestProperty("user-agent",
"Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
// 建立实际的连接
connection.connect();
// 获取所有响应头字段
Map<String, List<String>> map = connection.getHeaderFields();
// 遍历所有的响应头字段
for (String key : map.keySet()) {
System.out.println(key + "--->" + map.get(key));
}
// 定义 BufferedReader输入流来读取URL的响应
in = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
} catch (Exception e) {
System.out.println("发送GET请求出现异常!" + e);
e.printStackTrace();
}
// 使用finally块来关闭输入流
finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
}
return result;
}
}
2.7 启动并访问
依次启动HwaEurekaApplication,HwaConfigServerApplication,HwaClientApplication,HwaFeignApplication,HwaHystrixDashboardApplication(启动报错,暂时不理不影响使用),运行工具类的AccessViewService来周期性的访问http://127.0.0.1:8884/hi,只有访问了,监控里才能看到数据
开打监控地址http://localhost:8882/hystrix,最上面输入地址http://localhost:8884/actuator/hystrix.stream
2.8 监控图解