springcloud 第1课 :Rest学习环境搭建—服务提供者
springcloud 第2课 :Rest学习环境搭建—服务消费者
springcloud 第3课 :Eureka是什么
springcloud 第4课 :Eureka服务注册、信息配置、自我保护机制
springcloud 第5课 :Eureka集群环境配置
springcloud 第6课 :Ribbon实现负载均衡
springcloud 第7课 :Feign负载均衡
springcloud 第8课 :Hystrix服务熔断
springcloud 第9课 :Hystrix服务降级
1、新建springcloud-consumer-hystrix-dashboard类
1.1、导入消费者80的pom依赖
<dependencies>
<!--实体类-->
<dependency>
<groupId>org.example</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--web启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--热部署-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--Ribbon-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<!--eureka-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
<!--hystrix监控-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
</dependencies>
1.2、application.yaml 配置端口号
server:
port: 9001
1.3、主启动类 DeptConsumerDashboard_9001
@ EnableHystrixDashboard开启仪表盘监控注解
package com.lian.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard;
@EnableHystrixDashboard //开启监控页面
@SpringBootApplication
public class DeptConsumerDashboard_9001 {
public static void main(String[] args) {
SpringApplication.run(DeptConsumerDashboard_9001.class,args);
}
}
注意:微服务提供者pom添加监控jar包,才能被dashboard监控生产
例如:微服务提供者 springcloud-provider-dept-8001/8002/8003
<!-- actuator监控信息完善 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
1.4、测试
(1)启动3个 eureka 服务;
(2)启动新建的监控服务:springcloud-consumer-hystrix-dashboard
(3)浏览器中访问:http://localhost:9001/hystrix,若显示如下图所示,则表示微服务监控服务正常启动;
2、生产者导入监控依赖
以生产者 springcloud-provider-dept-8001 为例
2.1、监控页面需要生产者导入 actuator 和 hystrix 依赖
<dependencies>
<!--我们需要拿到实体类,所以需要配置 api 模块-->
<dependency>
<groupId>org.example</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!--junit-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--druid-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
</dependency>
<!--logback日志门面-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<!--mybatis的springboot启动器-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!--springboot的测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--jetty,当做应用服务器,和tomcat类似-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
<!--热部署工具-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--加入eureka依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- actuator 完善监控信息-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--hystrix-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.7.RELEASE</version>
</dependency>
</dependencies>
2.2、主启动类
package com.lian.springcloud;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
/**
* 主启动类
* 因为是maven项目,不是spring initialize项目,需要自己写主启动类
*
*/
@EnableDiscoveryClient //服务发现
@EnableEurekaClient //eureka是cs架构,配置此注解,在服务启动后,会自动注册到eureka服务端
@SpringBootApplication
public class DeptProvider_8001 {
public static void main(String[] args) {
SpringApplication.run(DeptProvider_8001.class,args);
}
//监控页面固定写法,增加一个servlet,需要导入hystrix依赖
@Bean
public ServletRegistrationBean hystrixMetricsStreamServlet(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
registrationBean.addUrlMappings("/actuator/hystrix.stream");
return registrationBean;
}
}
2.3、测试
eureka:springcloud-eureka-7001
生产者:springcloud-provider-dept-8001
消费者:springcloud-consumer-hystrix-dashboard
浏览器传值
http://localhost:8001/actuator/hystrix.stream
(a)Deplay 该参数用来控制服务器上轮询监控信息的延迟时间,默认是2000毫秒,可以通过配置该属性来降低客户端的网络和cpu消耗。
(b)Title该参数对应了头部标题Hystrix Stream之后的内容,默认会使用哦具体监控实例的URL,可疑通过配置该信息来展示更合适的标题。
在浏览器中输入:http://localhost:8001/dept/get/1,并多次刷新,显示如下:
实心圆:共有两种含义。它通过颜色的变化代表了实例的健康程度,它的健康程度从 绿色 > 黄色 > 橙色 > 红色 递减;
该实心圆除了颜色的变化之外,它的大小也会根据实例的请求流量发生变化,流量越大实心圆就越大,所以通过该实心圆的展示,就可以在大量实例中快速的发现故障实例和高压力实例。