1. 服务熔断
1.1. 创建一个新模块

复制服务的提供者dept-8001
1.1.1 导入相同的依赖
<!-- 我们需要拿到实体类,从api项目的pojo拿-->
<dependency>
<groupId>com.common</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>
<!-- 日志核心依赖-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<!-- springboot启动依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
</dependency>
<!-- spirngboot测试依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<!-- springboot的web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- jetty依赖相当于tomcat(springboot自带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>
<!-- actutor完善监控信息(erekua的status指向网页的信息依赖)-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
1.1.2 相同的MVC三层架构
1.1.3. 修改主启动类的类名
1.2. 导入Hystrix依赖
<!-- Hystrix依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
1.3. application.yml修改

1.4. DeptController修改

1.5. 启动类上添加注解
启动对熔断的支持@EnableCircuitBreaker

1.6. 测试
抛异常的情况下,直接走熔断

a. 修改之前自定义的负载均衡算法
改为轮询

1.7.没配熔断机制的情况
使用dept-8001模块测试,修改Controller中部分代码

访问不存在的id会抛出异常
1.8. 去掉localhost

1.8.1 在application.yml中添加配置

1.8.2 变为IP地址

2. 服务降级
2.1 在API模块中编写降级操作

2.2 通过Feign指定

2.3 consumer-dept-feign的配置
在application.yml中开启降级

2.4 访问测试
正常可以访问
a. 这时候把dept-8001服务提供者关闭
b. 在配置了降级后不会报错

3. 总结比较
服务熔断:服务数某个服务超时或者异常,引起熔,保险
服务降级:客户浆从整体网站请求负载考虑,当某个服务熔断或者关闭之后,服务将不再被调那。
此时在客户端,我们可以准备一个FallbackFactory,返回一个默认的值(缺省值),整体的服务水平下降一但是,好歹能用~比直接挂掉强
4. Dashboard流监控
4.1 新建模块

4.2 把consumer-dept-80的依赖拿过来
<!-- 实体类依赖,从api中导-->
<dependency>
<groupId>com.common</groupId>
<artifactId>springcloud-api</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
<!-- spirngboot的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>
<!-- Eureka依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!-- Ribbon的依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-ribbon</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
4.3 导入自己需要的依赖
<!-- hystrix依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
<!-- hystrix监控页面依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix-dashboard</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
4.4 编写配置文件

4.5 编写启动类

4.6 确定服务端要有监控信息的依赖

4.7 访问成功

a. 在provider-dept-8001中导入Hystrix依赖

<!-- hystrix依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
<version>1.4.6.RELEASE</version>
</dependency>
b. 增加一个Servlet

//增加一个servlet,将8001服务注册到dashboard流监控
@Bean
public ServletRegistrationBean a() {
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
registrationBean.addUrlMappings("/actuator/hystrix.stream");
return registrationBean;
}
4.8 监控页面
监控http://localhost:8001/actuator/hystrix.stream

a. 每次请求,监控页面都会发生变化

b. 一圈
实心圆:公有两种含义,他通过颜色的变化代表了实例的健康程度
它的健康程度从绿色<黄色<橙色<红色 递减
该实心圆除了颜色的变化之外,它的大小也会根据实例的请求流量发生变化,流量越大,该实心圆就越大,所以通过该实心圆的展示,就可以在大量的实例中快速发现故障实例和高压力实例。

c. 一线
曲线:用来记录2分钟内流量的相对变化,可以通过它来观察到流量的上升和下降趋势!

d. 整图说明

网友评论