SpringCloud之Hystrix-Dashboard监控,以及踩的坑...
前言:
最近刚入职,公司使用了SpringCloud,之前有了解过SpringCloud,但是长时间不去搭建不去使用很容易就忘了,因此空闲时间重新复习一下SpringCloud。但是之前开的SpringCloud的版本可能有点低,公司现在用的 " Greenwich.RELEASE "的版本,SpringBoot使用了“ 2.1.x ”的版本,算是比较新了,因此在使用 Hystrix-Dashboard 的时候会有点坑,因此想把踩到的坑记录下来,让更多的人避开这个坑,废话不多说,开始了!
一、创建 Hystrix-Dashboard 监控服务:springcloud-hystrix-dashboard
- 工程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>
- yml配置文件
server:
port: 10000
spring:
application:
name: springcloud-hystrix-dashboard
- 在启动类上添加注解 @EnableHystrixDashboard
@EnableHystrixDashboard
@SpringBootApplication
public class HystrixDashboardApplication {
public static void main(String[] args) {
SpringApplication.run(HystrixDashboardApplication.class, args);
}
}
- 启动!浏览器访问地址:http://localhost:10000/hystrix ,如下图:
二、创建Eureka注册中心:springcloud-eureka-server
有读者可能会问:不是说玩 Hystrix-Dashboard 吗?为啥还要用到Eureka?
答:请耐心往下看,这也是我为什么题目说的坑...
- 工程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>
- 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
- 启动类添加注解:@EnableEurekaServer
@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaServerApplication.class, args);
}
}
- 启动!浏览器访问:http://localhost:9000/ ,如下图:
三、创建被监控服务:springcloud-user
- 工程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>
- 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
- 启动类添加注解
//@EnableCircuitBreaker
//@EnableEurekaClient
//@EnableFeignClients
//@SpringBootApplication
// 以上四个注解可以用如下两个注解取代
@EnableFeignClients
@SpringCloudApplication
public class UserApplication {
public static void main(String[] args) {
SpringApplication.run(UserApplication.class, args);
}
}
- 创建测试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;
}
}
- 启动!浏览器访问:http://localhost:10001/user,如下图:
四、测试 Hystrix-Dashboard 监控 user 服务
- 浏览器访问:http://localhost:10001/hystrix.stream ,出现了404错误
说明:第一个坑,在SpringBoot 2.0之前,只要添加了Actuator依赖,不会出现404错误的
- 解决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;
}
}
浏览器访问:http://localhost:10001/hystrix.stream ,如下图:
将第一步的链接:http://localhost:10001/hystrix.stream ,复制到 Hystrix-Dashboard页面中
Hystrix-Dashboard会一直在 loading... 中,这就是第二个坑...
解决办法:
1:创建另一个服务(例如:springcloud-order)
2:将新的服务(order)注册到Eureka
3:在user服务写一个新的Api接口,通过 Feign 访问新服务(order)的Api接口
4:注意:经过本人测试通过RestTemplate的方式调用,仍然没有效果。
5:当通过Feign调用一次新服务(order)后,hystrix.stream 正常,效果如下:
- 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监控,以及踩的坑...的更多相关文章
- Spring Cloud学习笔记【五】Hystrix Dashboard监控面板
ystrix除了隔离依赖服务的调用以外,Hystrix 还提供了准实时的调用监控(Hystrix Dashboard),Hystrix 会持续地记录所有通过 Hystrix 发起的请求的执行信息,并以 ...
- SpringCloud (十) Hystrix Dashboard单体监控、集群监控、与消息代理结合
一.前言 Dashboard又称为仪表盘,是用来监控项目的执行情况的,本文旨在Dashboard的使用 分别为单体监控.集群监控.与消息代理结合. 代码请戳我的github 二.快速入门 新建一个Sp ...
- Spring Cloud(五)断路器监控(Hystrix Dashboard)
在上两篇文章中讲了,服务提供者 Eureka + 服务消费者 Feign,服务提供者 Eureka + 服务消费者(rest + Ribbon),本篇文章结合,上两篇文章中代码进行修改加入 断路器监控 ...
- springcloud Finchley 版本hystrix 和 hystrix Dashboard
hystrix的断路功能 引用上个项目,创建新的model ,cloud-hystrix pom.xml <?xml version="1.0" encoding=" ...
- spring cloud Hystrix监控面板Hystrix Dashboard和Turbine
我们提到断路器是根据一段时间窗内的请求情况来判断并操作断路器的打开和关闭状态的.而这些请求情况的指标信息都是HystrixCommand和HystrixObservableCommand实例在执行过程 ...
- Spring-Cloud之Hystrix熔断器-5
一.在分布式系统中,服务与服务之间的依赖错综复杂,一种不可避免的情况就是某些服务会出现故障,导致依赖于它们的其他服务出现远程调度的线程阻塞 Hystrix是Netflix 公司开源的一个项目,它提供了 ...
- Hystrix + Hystrix Dashboard搭建(Spring Cloud 2.X)
本机IP为 192.168.1.102 一.搭建Hystrix Dashboard 1. 新建 Maven 项目 hystrix-dashboard 2. pom.xml <projec ...
- Spring Cloud 学习 (四) Hystrix & Hystrix Dashboard & Turbine
在复杂的分布式系统中,可能有几十个服务相互依赖,这些服务由于某些原因,例如机房的不可靠性.网络服务商的不可靠性等,导致某个服务不可用 . 如果系统不隔离该不可用的服务,可能会导致整个系统不可用.Hys ...
- 【SpringCloud】第十一篇: 断路器监控(Hystrix Dashboard)
前言: 必需学会SpringBoot基础知识 简介: spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选. ...
- 史上最简单的SpringCloud教程 | 第十二篇: 断路器监控(Hystrix Dashboard)(Finchley版本)
转载请标明出处: 原文首发于:https://www.fangzhipeng.com/springcloud/2018/08/30/sc-f12-dash/ 本文出自方志朋的博客 在我的第四篇文章断路 ...
随机推荐
- MongoDB的复制源oplog
之前有说过MongoDB的复制是异步复制的,其实也就是通过oplog来实现的,他存放在local数据库中,我们来查询一下主节点的日志大小. 除了主节点有oplog之外,其他节点也就有oplog ...
- Java基础学习笔记(一) - 基础语法
1.Java程序开发过程 编译: 是指将我们编写的Java源文件翻译成JVM认识的class文件,javac编译器会检查我们所写的程序是否有错误,有错误就会提示出来,如果没有错误就会编译成功. 运行: ...
- ping本地局域网
#!/bin/bash for i in `seq 1 254` do ping -c 1 192.168.192.$i > /dev/null if [ $? -eq 0 ];then ech ...
- 集合查询表--Map
查询表 Map接口java提供了一组可以以键值对(key-value)的形式存储数据的数据结构,这种数据结构成为Map.我们可以把Map看成一个多行两列的表格,其中第一列存放key,第二列存放valu ...
- RocketMQ 源码学习笔记 Producer 是怎么将消息发送至 Broker 的?
目录 RocketMQ 源码学习笔记 Producer 是怎么将消息发送至 Broker 的? 前言 项目结构 rocketmq-client 模块 DefaultMQProducerTest Roc ...
- Spring系列(六):Spring事务源码解析
一.事务概述 1.1 什么是事务 事务是一组原子性的SQL查询,或者说是一个独立的工作单元.要么全部执行,要么全部不执行. 1.2 事务的特性(ACID) ①原子性(atomicity) 一个事务必须 ...
- kali切换到西电源
准备研究kali的openvas,打开发现居然没有.apt-get更新一下结果各种报错,换成中科大源.阿里源还是始终报错,气到吐血.最后上西电开源社区换成了西电的kali源,更新速度2m多,一气呵成~ ...
- php微信支付v3版本签名生成
前几天需要对接微信支付卡包营销活动需要对接微信新版SDKv3版 签名生成规则,微信的官方文档里面说明的还算可以吧,不过个人觉得不太理想- -. 自己调试的时候调试了半天才找了错误原因. https: ...
- Asp.net内置对象用途说明
Asp.net 内置对象 1.Session当客户第一次请求网页,session创建.当客户最后一次请求页面,一段时间后,session销毁.默认30分钟. 一般存用户信息,即登陆成功后,在sessi ...
- MySQL的索引原理(图解)
数据库的索引原理 0.什么是索引 索引是一种特殊的文件(InnoDB数据表上的索引是表空间的一个组成部分),它们包含着对数据表里所有记录的引用指针.更通俗的说,数据库索引好比是一本书前面的目录,能 ...