目录
1.服务熔断(部署在服务端,某个服务超时或者异常,引起熔断)
2.服务降级(部署在客户端,从整体的网站请求负载考虑,服务将不在被调用,但是还是会有预先信息FallBackFactory返回,服务水平下降了,好歹能用 )
1.服务熔断(部署在服务端,某个服务超时或者异常,引起熔断)
当我们调用服务时出现错误,可能是服务崩了,或者断网了,都会导致线程崩溃,服务崩溃,Hystrix提供了很多比较好的方法,就是服务熔断
①首先创建一个服务提供者(DeptproviedeHystrix)
②导入依赖
<!-- 导入hystrix依赖-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
③编写接口,下面有两种接口方式,
这是正常我们写的,如果找不到id就直接把线程中断了,服务也中断了;
第二种
在接口上方添加一个注解@HystrixCommand(“备用方法的名字",这里我的备用方法时hystrix_Get)
然后在启动类上方添加一个注解,注意我们以前一般开启服务都是@Enablexxx服务,这里特殊是用@EnableCircuitBreaker 翻译过来为熔断器
2.服务降级(部署在客户端,从整体的网站请求负载考虑,服务将不在被调用,但是还是会有预先信息FallBackFactory返回,服务水平下降了,好歹能用 )
关掉服务器,客户访问依旧会提供一条信信息,不会影响用户的操作
3.Hystrix--dashboard(监控流)
x效果:
当你访问localhost:8004/dept/get/1这个接口的时候,监控就会把信心信息展示出来
步骤:①创建一个模块
②导入依赖(除了下面两个包还要把springcloud的其他包也导进来)
<!-- Hystrix依赖-->
<!-- 导入hystrix依赖-->
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-hystrix -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
<version>2.1.3.RELEASE</version>
</dependency>
<!-- 监控-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
③写配置
在启动类上加上注解@ EnableHystrixDashboard
创建application.yaml 编写端口号为9001
④在服务端的启动类上注册一个bean
@Bean
public ServletRegistrationBean servletRegistrationBean(){
ServletRegistrationBean registrationBean = new ServletRegistrationBean(new HystrixMetricsStreamServlet());
registrationBean.addUrlMappings("/actuator/hystrix.stream");
return registrationBean;
}
⑤启动服务中心,服务,和流监控
我们首先访问locakhost:8001/dept/get/1
然后访问localhost:8001/actuator/hystrix.stream,他会ping出数据
最后访问localhost:9001/hystrix.stream
输入 http://localhost:8004/actuator/hystrix.stream
然后就出现页面了
一个非常值得注意的点就是HystrixDashboard只能扫描有HystrixCommand注解的接口
4.springcloud-config 配置到gitee上
cd springcloud-config/ git .add /git commit -m "" /git push origin maste git pull --rebase origin master