消费方调用提供方接口,提供方如果出异常了,需要向消费方返回一个降级方法
1.提供方降级
导入Hystrix对应的坐标,才能向消费方提供降级的方法
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
编写服务接口对应的降级方案,要用@HystrixCommand注解声明这是一个降级方法
package com.gwx.controller;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/hello")
public class ProviderController {
@RequestMapping("/hello")
// 指定这个接口出现异常调用的降级方法
@HystrixCommand(fallbackMethod = "hello_fallback",commandProperties = {
// 表示这个接口超时多少秒出现异常才会去调用降级的方法
@HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
})
public String hello(){
return "hello nacos";
}
// 定义降级的方法
public String hello_fallback(){
return "降级了";
}
}
在启动类开启Hystrix的服务降级的功能
@SpringBootApplication
@EnableCircuitBreaker
public class ProviderApp {
public static void main(String[] args) {
SpringApplication.run(ProviderApp.class,args);
}
}
2.消费方降级
在配置文件中开启Feign对Hystrix的支持
#开启消费方feign对hystrix的支持
feign:
hystrix:
enabled: true
编写Feign接口对应的实现类,实现类中的方法就是对应提供方的接口方法的降级方案
package com.gwx.feign.impl;
import com.gwx.feign.MyFeign;
import org.springframework.stereotype.Component;
@Component
public class MyFeignImpl implements MyFeign {
@Override
public String hello() {
return "又被降级了";
}
}
在Feign接口的注解@FeignClient增加一个属性,声明服务降级的实现类,这个属性的值就是Feign接口的实现类对应的降级方法
package com.gwx.feign;
import com.gwx.config.FeignLogLevelConfig;
import com.gwx.feign.impl.MyFeignImpl;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
/*
简化ribbon的两行代码
String url = "http://NACOS-PROVIDER/hello/hello";
String s = restTemplate.getForObject(url, String.class);
*/
//接口上加上注解,表示这个接口是Feign客户端,value属性表示要对哪个服务节点进行远程调用,替换上方的url
//fallback表示这个远程调用相对应的降级方案
@FeignClient(value = "NACOS-PROVIDER",configuration = FeignLogLevelConfig.class,fallback = MyFeignImpl.class)
public interface MyFeign {
// 接口内方法与controller层远程调用的方法一摸一样,地址则要改成提供方的地址,feign内部自动使用RestTemplate调用
@RequestMapping("/hello/hello")
public String hello();
}
当一个消费方调用一个服务方,如果在短时间内一直请求失败,达到了一个阈值,那么服务方的熔断器就会打开,拒绝所有消费方所有的请求,过了5秒后,熔断器进入半开状态,会放入一些请求过来,如果正常响应,熔断器则关闭,如果还是请求失败,继续持续打开状态,如果熔断器在半开的状态时放进来的请求还是请求失败,熔断器的状态会在打开和半开一直循环。
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ReEUBeVy-1598771056732)(C:\Users\gwx\AppData\Roaming\Typora\typora-user-images\image-20200829220808817.png)]
更改熔断器的默认配置
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-3blbm7rV-1598771056735)(C:\Users\gwx\AppData\Roaming\Typora\typora-user-images\image-20200829221839454.png)]
Hystrix提供了Hystrix-dashboard功能,用于实现对微服务接口运行状态的监控
Circuit只能提供一个微服务的监控,而Turbine底层基于circuit实现了聚合监控
1.搭建监控模块
创建hystrix-monitor模块,使用Turbine聚合监控多个Hystrix dashboard功能
2.监控模块中引入Turbine聚合监控起步依赖
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-turbineartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboardartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
3.修改监控模块的配置文件application.yml文件
# 指定模块名称
spring:
application.name: hystrix-monitor
server:
#对应端口
port: 8769
turbine:
#默认配置
combine-host-port: true
# 配置需要监控的服务名称列表,多个用逗号隔开
app-config: hystrix-provider,hystrix-consumer
#默认配置
cluster-name-expression: "'default'"
aggregator:
#默认配置
cluster-config: default
#instanceUrlSuffix: /actuator/hystrix.stream
eureka:
client:
serviceUrl:
# eureka注册中心,这个监控模块需要连接eureka注册中心,才能拿到所有的实例对象
defaultZone: http://localhost:8761/eureka/
4.创建spring-boot项目的启动类
@SpringBootApplication
@EnableEurekaClient
@EnableTurbine //开启Turbine 很聚合监控功能
@EnableHystrixDashboard //开启Hystrix仪表盘监控功能
public class HystrixMonitorApp {
public static void main(String[] args) {
SpringApplication.run(HystrixMonitorApp.class, args);
}
}
5.分别修改消费方模块和提供方模块
导入依赖
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrixartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-netflix-hystrix-dashboardartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-actuatorartifactId>
dependency>
6.配置聚合监控需要的bean
@Bean
public ServletRegistrationBean getServlet() {
HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
ServletRegistrationBean registrationBean = new ServletRegistrationBean(streamServlet);
registrationBean.setLoadOnStartup(1);
registrationBean.addUrlMappings("/actuator/hystrix.stream");
registrationBean.setName("HystrixMetricsStreamServlet");
return registrationBean;
}
7.启动类上添加注解@EnableHystrixDashboard,开启Hystrix仪表盘监控功能
8.启动对应的注册中心,监控,消费方,提供方服务,在浏览器访问http://localhost:8769/hystrix/ 进入Hystrix Dashboard界面
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-tX81jhdN-1598771056736)(F:\javaee24\typora笔记\1. turbine 搭建\1585421193757.png)]
界面中输入监控的Url地址 http://localhost:8769/turbine.stream,监控时间间隔2000毫秒和title
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-P5hVDV4t-1598771056738)(F:\javaee24\typora笔记\1. turbine 搭建\1585421278837.png)]
中输入监控的Url地址 http://localhost:8769/turbine.stream,监控时间间隔2000毫秒和title
[外链图片转存中…(img-P5hVDV4t-1598771056738)]
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-sZYSX9wH-1598771056740)(F:\javaee24\typora笔记\1. turbine 搭建\1167856120180.png)]