前言:

最近刚入职,公司使用了SpringCloud,之前有了解过SpringCloud,但是长时间不去搭建不去使用很容易就忘了,因此空闲时间重新复习一下SpringCloud。但是之前开的SpringCloud的版本可能有点低,公司现在用的 " Greenwich.RELEASE "的版本,SpringBoot使用了“ 2.1.x ”的版本,算是比较新了,因此在使用 Hystrix-Dashboard 的时候会有点坑,因此想把踩到的坑记录下来,让更多的人避开这个坑,废话不多说,开始了!

一、创建 Hystrix-Dashboard 监控服务:springcloud-hystrix-dashboard

  1. 工程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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gxc.test</groupId>
<artifactId>springcloud-hystrix-dashboard</artifactId>
<version>1.0.0</version> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
  1. yml配置文件
server:
port: 10000
spring:
application:
name: springcloud-hystrix-dashboard
  1. 在启动类上添加注解 @EnableHystrixDashboard
@EnableHystrixDashboard
@SpringBootApplication
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}
  1. 启动!浏览器访问地址:http://localhost:10000/hystrix ,如下图:

二、创建Eureka注册中心:springcloud-eureka-server

有读者可能会问:不是说玩 Hystrix-Dashboard 吗?为啥还要用到Eureka?

答:请耐心往下看,这也是我为什么题目说的坑...

  1. 工程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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gxc.test</groupId>
<artifactId>springcloud-hystrix-dashboard</artifactId>
<version>1.0.0</version> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
  1. yml配置文件
server:
port: 9000 spring:
application:
name: springcloud-eureka-server eureka:
client:
register-with-eureka: false
fetch-registry: false
service-url:
defaultZone: http://127.0.0.1:${server.port}/eureka
  1. 启动类添加注解:@EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
  1. 启动!浏览器访问:http://localhost:9000/ ,如下图:

三、创建被监控服务:springcloud-user

  1. 工程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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.gxc.test</groupId>
<artifactId>springcloud-user</artifactId>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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-openfeign</artifactId>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
  1. yml配置文件
server:
port: 10001 spring:
application:
name: springcloud-user # 将服务注册到Eureka注册中心
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:9000/eureka
instance:
prefer-ip-address: true
instance-id: ${spring.application.name}:${server.port} feign:
hystrix:
enabled: true
  1. 启动类添加注解
//@EnableCircuitBreaker
//@EnableEurekaClient
//@EnableFeignClients
//@SpringBootApplication // 以上四个注解可以用如下两个注解取代
@EnableFeignClients
@SpringCloudApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
  1. 创建测试Controller类
@RestController
public class UserController {
@GetMapping("/user")
public Object getUser() {
Map<String, Object> user = Maps.newHashMap();
user.put("username","GongXincheng");
user.put("age", 23);
user.put("birthday", new Date());
return user;
}
}
  1. 启动!浏览器访问:http://localhost:10001/user,如下图:

四、测试 Hystrix-Dashboard 监控 user 服务

  1. 浏览器访问:http://localhost:10001/hystrix.stream ,出现了404错误

    说明:第一个坑,在SpringBoot 2.0之前,只要添加了Actuator依赖,不会出现404错误的

  2. 解决404问题:创建一个配置类,并注册一个Servlet
@Configuration
public class ServletConfigBean {
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet() {
ServletRegistrationBean regist = new ServletRegistrationBean();
regist.setServlet(new HystrixMetricsStreamServlet());
regist.setName("hystrixMetricsStreamServlet");
regist.setLoadOnStartup(1);
regist.addUrlMappings("/hystrix.stream");
return regist;
}
}
  1. 浏览器访问:http://localhost:10001/hystrix.stream ,如下图:

  2. 将第一步的链接:http://localhost:10001/hystrix.stream ,复制到 Hystrix-Dashboard页面中

  3. Hystrix-Dashboard会一直在 loading... 中,这就是第二个坑...

  4. 解决办法:

1:创建另一个服务(例如:springcloud-order)
2:将新的服务(order)注册到Eureka
3:在user服务写一个新的Api接口,通过 Feign 访问新服务(order)的Api接口
4:注意:经过本人测试通过RestTemplate的方式调用,仍然没有效果。
5:当通过Feign调用一次新服务(order)后,hystrix.stream 正常,效果如下:



  1. Controller层代码
@RestController
public class UserController { private static final String ORDER_SERVER_URL = "http://springcloud-order"; @Resource private OrderFeign orderFeign;
@Resource private RestTemplate restTemplate; @GetMapping("/user")
public Object getUser() {
Map<String, Object> user = Maps.newHashMap();
user.put("username","GongXincheng");
user.put("age", 23);
user.put("birthday", new Date());
return user;
} @GetMapping("/feign/order")
public Object feignGetOrder() {
return orderFeign.getOrder();
} @GetMapping("/rest/order")
public Object restGetOrder() {
return restTemplate.getForObject(ORDER_SERVER_URL + "/order", Object.class);
}
}

8:user服务和oder服务目录结构 和 相关代码

五:代码链接

https://gitee.com/gxc6/gxc-springcloud-study

如果有什么错误的地方,欢迎大家帮忙指正!感谢!

我透... 写完这个已经凌晨3点了... 溜了溜了...

SpringCloud之Hystrix-Dashboard监控,以及踩的坑...的更多相关文章

  1. Spring Cloud学习笔记【五】Hystrix Dashboard监控面板

    ystrix除了隔离依赖服务的调用以外,Hystrix 还提供了准实时的调用监控(Hystrix Dashboard),Hystrix 会持续地记录所有通过 Hystrix 发起的请求的执行信息,并以 ...

  2. SpringCloud (十) Hystrix Dashboard单体监控、集群监控、与消息代理结合

    一.前言 Dashboard又称为仪表盘,是用来监控项目的执行情况的,本文旨在Dashboard的使用 分别为单体监控.集群监控.与消息代理结合. 代码请戳我的github 二.快速入门 新建一个Sp ...

  3. Spring Cloud(五)断路器监控(Hystrix Dashboard)

    在上两篇文章中讲了,服务提供者 Eureka + 服务消费者 Feign,服务提供者 Eureka + 服务消费者(rest + Ribbon),本篇文章结合,上两篇文章中代码进行修改加入 断路器监控 ...

  4. springcloud Finchley 版本hystrix 和 hystrix Dashboard

    hystrix的断路功能 引用上个项目,创建新的model ,cloud-hystrix pom.xml <?xml version="1.0" encoding=" ...

  5. spring cloud Hystrix监控面板Hystrix Dashboard和Turbine

    我们提到断路器是根据一段时间窗内的请求情况来判断并操作断路器的打开和关闭状态的.而这些请求情况的指标信息都是HystrixCommand和HystrixObservableCommand实例在执行过程 ...

  6. Spring-Cloud之Hystrix熔断器-5

    一.在分布式系统中,服务与服务之间的依赖错综复杂,一种不可避免的情况就是某些服务会出现故障,导致依赖于它们的其他服务出现远程调度的线程阻塞 Hystrix是Netflix 公司开源的一个项目,它提供了 ...

  7. Hystrix + Hystrix Dashboard搭建(Spring Cloud 2.X)

    本机IP为  192.168.1.102 一.搭建Hystrix Dashboard 1.   新建 Maven 项目  hystrix-dashboard 2. pom.xml <projec ...

  8. Spring Cloud 学习 (四) Hystrix & Hystrix Dashboard & Turbine

    在复杂的分布式系统中,可能有几十个服务相互依赖,这些服务由于某些原因,例如机房的不可靠性.网络服务商的不可靠性等,导致某个服务不可用 . 如果系统不隔离该不可用的服务,可能会导致整个系统不可用.Hys ...

  9. 【SpringCloud】第十一篇: 断路器监控(Hystrix Dashboard)

    前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...

  10. 史上最简单的SpringCloud教程 | 第十二篇: 断路器监控(Hystrix Dashboard)(Finchley版本)

    转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f12-dash/ 本文出自方志朋的博客 在我的第四篇文章断路 ...

随机推荐

  1. nodeJs环境添加代理

    目的:实现前后端分离,前端减少路径请求的所需的路由文件: 第一步:安装http代理中间件 npm install http-proxy-middleware --save 第二步: express文件 ...

  2. Forest plot(森林图) | Cox生存分析可视化

    本文首发于“生信补给站”微信公众号,https://mp.weixin.qq.com/s/2W1W-8JKTM4S4nml3VF51w 更多关于R语言,ggplot2绘图,生信分析的内容,敬请关注小号 ...

  3. Ionic2优于Ionic1的6个理由

    经历了一个从0到有的app的开发,我已经很熟悉Ionic1了,在此期间我曾发现过Ionic1的一些bug,和一些不合理的地方(根基版本 不同,后续我会陆续发表这些文章),我甚至在此期间对Ionic1进 ...

  4. kubernetes垃圾回收器GarbageCollector Controller源码分析(二)

    kubernetes版本:1.13.2 接上一节:kubernetes垃圾回收器GarbageCollector Controller源码分析(一) 主要步骤 GarbageCollector Con ...

  5. JavaWeb http协议的自我描述

    1.http协议的组成 http:规范那种协议 localhost.127.0.0.1:访问的ip地址(默认,根据自己的需求改变) 端口号:8080(默认,根据自己的需求改变) 工程:XXX 资源:可 ...

  6. 1.Eclipse下载、常用配置、快捷键

    Eclipse官网下载:https://www.eclipse.org/downloads/packages/ 自动补全 位置:Eclipse——Window——Perferences——Java—— ...

  7. bugku细心地大象

    解压得到图片,查看属性,发现一段编码. 用winhex打开图片,发现头文件是错的,正常jpg文件头文件为FF D8 FF E0 说明不是图片,是zip的文件头,更换格式. 丢到kali用binwalk ...

  8. Arouter核心思路和源码详解

    前言 阅读本文之前,建议读者: 对Arouter的使用有一定的了解. 对Apt技术有所了解. Arouter是一款Alibaba出品的优秀的路由框架,本文不对其进行全面的分析,只对其最重要的功能进行源 ...

  9. python 报错TypeError: 'range' object does not support item assignment,解决方法

    贴问题 nums = range(5)#range is a built-in function that creates a list of integers print(nums)#prints ...

  10. 程序员IT狗有什么副业可以做呢?

    1. 开篇 副业有很多,全网有做什么公众号.闲鱼.手机卡,各种各样的都有,大部分是骗子,小部分是通过自己的努力,获得了成功. 从年初就开始实践如何做一个自由职业者,近大半年有一些感受正好一起分享交流一 ...