文章目录
-
- 一:什么是SpringCloud
- 二:Hystrix Dashboard是什么
- 三:Hystrix Dashboard的作用
- 四:实例
一:什么是SpringCloud
Spring Cloud provides tools for developers to quickly build some of the common patterns in distributed systems (e.g. configuration management, service discovery, circuit breakers, intelligent routing, micro-proxy, control bus, one-time tokens, global locks, leadership election, distributed sessions, cluster state). Coordination of distributed systems leads to boiler plate patterns, and using Spring Cloud developers can quickly stand up services and applications that implement those patterns. They will work well in any distributed environment, including the developer’s own laptop, bare metal data centres, and managed platforms such as Cloud Foundry.
翻译如下:
Spring Cloud为开发人员提供了快速构建分布式系统中的一些常见模式的工具(例如配置管理、服务发现、断路器、智能路由、微代理、控制总线、一次性令牌、全局锁、领导层选举、分布式会话、集群状态)。分布式系统的协调导致了锅炉板模式,使用Spring Cloud开发人员可以快速建立实现这些模式的服务和应用程序。它们在任何分布式环境中都能很好地工作,包括开发人员自己的笔记本电脑、裸机数据中心和云计算等托管平台。
SpringCloud主要框架:
- 服务发现——Netflix Eureka
- 服务调用——Netflix Feign
- 熔断器——Netflix Hystrix
- 服务网关——Netflix Zuul
- 分布式配置——Spring Cloud Config
- 消息总线 —— Spring Cloud Bus
二:Hystrix Dashboard是什么
是
Hystrix
中的一个组件,
Hystrix仪表盘
三:Hystrix Dashboard的作用
主要用来实时监控
Hystrix
的各项指标信息。通过
Hystrix DashBoard
反馈的实时信息,可以帮助我们快速防线系统中存在的问题,从而及时地采取对应措施。
四:实例
4.1,我们先创建一个springboot项目

4.2 创建完项目后依赖如下:
<?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.2.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.aa</groupId>
<artifactId>hystrix-dashboard</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hystrix-dashboard</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR1</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--hystrix仪表盘-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
4.3 接着在启动类
HystrixDashboardApplication
中加入注解
@EnableHystrixDashboard
,开启
hystrix仪表盘
.4.4 创建
application.yml
,加入配置
server:
port: 9010
spring:
application:
name: hystrix-dashboard
eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka/
4.5 启动后访问:
http://localhost:9010/hystrix
4.6 接下来我们要监控某一个服务了,那么就要有个服务提供
/hystrix.stream
接口,创建一个服务提供者
provider-service
,加入以下依赖,切记不能漏
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
4.7 在启动类上加上注解
@EnableCircuitBreaker
,开启熔断器
4.8 创建application.yml,加入配置
server:
port: 9020
spring:
application:
name: provider-service
eureka:
client:
service-url:
defaultZone: http://localhost:8888/eureka/
4.9 启动后访问
http://localhost:9020/hystrix.stream
4.9.1 这时候发现报错了,404,没有此接口
解决方案:只要在自己的项目里配置上这个servlet就ok了
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 配置
*/
@Configuration
public class ConfigBean {
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
}
重新启动,再次访问
http://localhost:9020/hystrix.stream
访问成功,会一直ping。。。 到了这一步,就可以把路径
http://localhost:9020/hystrix.stream
放入
Hystrix仪表盘
中进行监控了
走到这一步,就已经成功了,将会监控这个微服务
上一篇:熔断器Hystrix的使用
下一篇:服务网关Zuul