在微服务架构中,根据业务来拆分红一个个的服务,服务与服务之间能够经过 RPC 相互调用,在 Spring Cloud 中能够用 RestTemplate + Ribbon 和 Feign 来调用。为了保证其高可用,单个服务一般会集群部署。因为网络缘由或者自身的缘由,服务并不能保证 100% 可用,若是单个服务出现问题,调用这个服务就会出现线程阻塞,此时如有大量的请求涌入,Servlet 容器的线程资源会被消耗完毕,致使服务瘫痪。服务与服务之间的依赖性,故障会传播,会对整个微服务系统形成灾难性的严重后果,这就是服务故障的 雪崩效应。java
服务熔断
在微服务架构中,一个请求须要调用多个服务是很是常见的,较底层的服务若是出现故障,会致使连锁故障。当对特定的服务的调用的不可用达到一个阀值(Hystrix 默认是 5 秒 20 次) 熔断器将会被打开,为了不连锁故障,会触发服务降级。spring
服务降级
Fallback 至关因而降级操做,对于查询操做, 咱们能够实现一个 fallback 方法,当请求后端服务出现异常的时候,可使用 fallback 方法返回的值,fallback 方法的返回值通常是设置的默认值或者来自缓存,告知后面的请求服务不可用了,不要再请求了。后端
在 pom.xml
中添加 spring-cloud-starter-netflix-hystrix
依赖浏览器
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
在 Application
入口类中添加 @EnableHystrix
注解开启 Hystrix
缓存
@SpringBootApplication @EnableDiscoveryClient @EnableHystrix public class WebAdminRibbonApplication { public static void main(String[] args) { SpringApplication.run(WebAdminRibbonApplication.class, args); } }
在业务层添加 @HystrixCommand
注解,并指定 fallbackMethod
熔断方法。当熔断器打开后,会触发相应的 fallbackMethod
,也就是上面提到的服务降级网络
@Service public class AdminService { @Autowired private RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "hiFallback") public String sayHi(String message) { return restTemplate.getForObject("http://hello-sring-cloud-service-admin/hi?message=" + message, String.class); } public String hiFallback(String message) { return "Hi,your message is :\"" + message + "\" but request error."; } }
此时关闭服务提供者,再次请求 http://localhost:8764/hi?mess... 浏览器会显示 :架构
Hi,your message is :"HelloRibbon" but request error.
在 Feign 中是自带熔断器的,但默认是关闭的,须要在配置文件中配置打开它app
在 application.yml
中开启熔断器ide
feign: hystrix: enabled: true
在 Application
入口类中添加 @EnableHystrix
注解开启 Hystrix
微服务
@SpringBootApplication @EnableDiscoveryClient @EnableHystrix public class WebAdminRibbonApplication { public static void main(String[] args) { SpringApplication.run(WebAdminRibbonApplication.class, args); } }
在业务层添加 @FeignClient
注解,并经过 fallback
属性指定 Fallback
类。当熔断器打开后,会触发该 Fallback
类中的方法,也就是服务降级。
@FeignClient(value = "hello-spring-cloud-service-admin", fallback = AdminServiceFallback.class) public interface AdminService { @RequestMapping(value = "hi", method = RequestMethod.GET) public String sayHi(@RequestParam(value = "message") String message); }
在 Fallback 回调类中实现对应接口,服务调用失败后,将错误提示内容返回给客户端
@Component public class AdminServiceFallback implements AdminService { @Override public String sayHi(String message) { return "Hi,your message is :\"" + message + "\" but request error."; } }
此时再次关闭服务提供者,再次请求 http://localhost:8765/hi?mess... 浏览器会显示:
Hi,your message is :"HelloRibbon" but request error.
在 Ribbon
和 Feign
项目增长 Hystrix
仪表盘监控功能,两个项目的改造方式相同,这里以 Feign
项目为例。
在 pom.xml
中添加 spring-cloud-starter-netflix-hystrix-dashboard
依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency>
在 Application
入口类中添加 @EnableHystrixDashboard
注解
@SpringBootApplication @EnableDiscoveryClient @EnableHystrix @EnableHystrixDashboard public class WebAdminFeignApplication { public static void main(String[] args) { SpringApplication.run(WebAdminFeignApplication.class, args); } }
建立 HystrixDashboardConfiguration
配置类
@Configuration public class HystrixDashboardConfiguration { @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:8765/hystrix.stream
超时时间(默认1000ms,单位:ms)
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds
:在调用方配置,被该调用方的全部方法的超时时间都是该值,优先级低于下边的指定配置hystrix.command.HystrixCommandKey.execution.isolation.thread.timeoutInMilliseconds
:在调用方配置,被该调用方的指定方法(HystrixCommandKey 方法名)的超时时间是该值线程池核心线程数
hystrix.threadpool.default.coreSize
:默认为 10Queue 队列
hystrix.threadpool.default.maxQueueSize
:最大排队长度。默认 -1,使用 SynchronousQueue
。其余值则使用 LinkedBlockingQueue
。若是要从 -1 换成其余值则需重启,即该值不能动态调整,若要动态调整,须要使用到下边这个配置hystrix.threadpool.default.queueSizeRejectionThreshold
:排队线程数量阈值,默认为 5,达到时拒绝,若是配置了该选项,队列的大小是该队列若是 maxQueueSize
等于 -1 的话,则该选项不起做用
circuitBreaker 断路器
hystrix.command.default.circuitBreaker.requestVolumeThreshold
:当在配置时间窗口内达到此数量的失败后,进行短路。默认 20 个(10s 内请求失败数量达到 20 个,断路器开)hystrix.command.default.circuitBreaker.sleepWindowInMilliseconds
:短路多久之后开始尝试是否恢复,默认 5shystrix.command.default.circuitBreaker.errorThresholdPercentage
:出错百分比阈值,当达到此阈值后,开始短路。默认 50%fallback
hystrix.command.default.fallback.isolation.semaphore.maxConcurrentRequests
:调用线程容许请求 HystrixCommand.GetFallback()
的最大数量,默认 10。超出时将会有异常抛出,注意:该项配置对于 THREAD 隔离模式也起做用什么状况下会触发 fallback 方法
名字 | 描述 | 触发fallback方法 |
---|---|---|
EMIT (emit) | 值传递 | NO |
SUCCESS (success) | 执行完成 , 没有错误 | NO |
FAILURE (failure) | 执行抛出异常 | YES |
TIMEOUT (timeout) | 执行开始 , 但没有在容许的时间内完成 | YES |
BAD_REQUEST (bad_request) | 执行抛出HystrixBadRequestException | NO |
SHORT_CIRCUITED (short_circuited) | 断路器打开 , 不尝试执行 | YES |
THREAD_POOL_REJECTED (thread_pool_rejected) | 线程池拒绝 , 不尝试执行 | YES |
SEMAPHORE_REJECTED (semaphore_rejected) | 信号量拒绝 , 不尝试执行 | YES |
什么状况下 fallback 方法抛出异常
名字 | 描述 | 抛异常 |
---|---|---|
FALLBACK_EMIT (fallback_emit) | Fallback值传递 | NO |
FALLBACK_SUCCESS (fallback_success) | Fallback执行完成 , 没有错误 | NO |
FALLBACK_FAILURE (fallback_failure) | Fallback执行抛出出错 | YES |
FALLBACK_REJECTED (fallback_rejected) | Fallback信号量拒绝 , 不尝试执行 | YES |
FALLBACK_MISSING (fallback_missing) | 没有Fallback实例 | YES |