1. Hystrix簡介
在微服務架構中,根據業務來拆分成一個個的服務,服務與服務之間可以相互調用(RPC),在Spring Cloud可以用RestTemplate+Ribbon和Feign來調用。為了保證其高可用,單個服務通常會集群部署。由於網絡原因或者自身的原因,服務並不能保證100%可用,如果單個服務出現問題,調用這個服務就會出現線程阻塞,此時若有大量的請求涌入,Servlet容器的線程資源會被消耗完畢,導致服務癱瘓。服務與服務之間的依賴性,故障會傳播,會對整個微服務系統造成災難性的嚴重后果,這就是服務故障的“雪崩”效應。
為了解決這個問題,業界提出了斷路器模型。
Netflix開源了Hystrix組件,實現了斷路器模式,SpringCloud對這一組件進行了整合。 在微服務架構中,一個請求需要調用多個服務是非常常見的,如下圖:
較底層的服務如果出現故障,會導致連鎖故障。當對特定的服務的調用的不可用達到一個閥值(Hystric 是5秒20次) 斷路器將會被打開。
斷路打開后,可用避免連鎖故障,fallback方法可以直接返回一個固定值。
這篇文章基於上一篇文章的工程,首先啟動上一篇文章的工程,啟動eureka-server 工程;啟動service-hi工程,它的端口為8762。
2. 在Ribbon中使用斷路器
改造service-ribbon 工程的代碼,首先在pox.xml文件中加入spring-cloud-starter-netflix-hystrix的起步依賴:
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
在程序的啟動類ServiceRibbonApplication 加@EnableHystrix注解開啟Hystrix:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient @EnableHystrix public class ServiceRibbonApplication { public static void main(String[] args) { SpringApplication.run( ServiceRibbonApplication.class, args ); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
改造HelloService類,在hiService方法上加上@HystrixCommand注解。該注解對該方法創建了熔斷器的功能,並指定了fallbackMethod熔斷方法,熔斷方法直接返回了一個字符串,字符串為”hi,”+name+”,sorry,error!”,代碼如下:
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; @Service public class HelloService { @Autowired RestTemplate restTemplate; @HystrixCommand(fallbackMethod = "hiError") public String hiService(String name) { return restTemplate.getForObject("http://SERVICE-HI/hi?name="+name,String.class); } public String hiError(String name) { return "hi,"+name+",sorry,error!"; } }
啟動:service-ribbon 工程,當我們訪問http://localhost:8764/hi?name=zang,瀏覽器顯示:
此時關閉 service-hi 工程,當我們再訪問http://localhost:8764/hi?name=forezp,瀏覽器會顯示:
這就說明當 service-hi 工程不可用的時候,service-ribbon調用 service-hi的API接口時,會執行快速失敗,直接返回一組字符串,而不是等待響應超時,這很好的控制了容器的線程阻塞。
3. Feign中使用斷路器
Feign是自帶斷路器的,在D版本的Spring Cloud之后,它沒有默認打開。需要在配置文件中配置打開它,在配置文件加以下代碼:
feign.hystrix.enabled: true
基於service-feign工程進行改造,只需要在FeignClient的SchedualServiceHi接口的注解中加上fallback的指定類就行了:
import com.zang.servicefeign.clients.fallback.SchedualServiceHiHystric; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; @FeignClient(value = "service-hi",fallback = SchedualServiceHiHystric.class) public interface SchedualServiceHi { @RequestMapping(value = "/hi",method = RequestMethod.GET) String sayHiFromClientOne(@RequestParam(value = "name") String name); }
SchedualServiceHiHystric需要實現SchedualServiceHi 接口,並注入到Ioc容器中,代碼如下:
import com.zang.servicefeign.clients.SchedualServiceHi; import org.springframework.stereotype.Component; @Component public class SchedualServiceHiHystric implements SchedualServiceHi { @Override public String sayHiFromClientOne(String name) { return "sorry, you are fail,"+name; } }
啟動servcie-feign工程,瀏覽器打開http://localhost:8765/hi?name=zang,注意此時service-hi工程沒有啟動,網頁顯示:
打開service-hi工程,再次訪問,瀏覽器顯示:
這證明斷路器起到作用了。
4. Hystrix Dashboard
Hystrix Dashboard是作為斷路器狀態的一個組件,提供了數據監控和友好的圖形化界面。
下面對【2. 在Ribbon中使用斷路器】中的代碼進行改造,添加熔斷的圖形化界面監控。
4.1 添加依賴
添加 spring-cloud-starter-netflix-hystrix-dashboard 等的依賴,如下
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
4.2 開啟注解
在程序的入口ServiceHiApplication類,加上@EnableHystrix注解開啟斷路器,這個已存在,並且需要在程序中聲明斷路點HystrixCommand;加上@EnableHystrixDashboard注解,開啟HystrixDashboard。
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.hystrix.EnableHystrix; import org.springframework.cloud.netflix.hystrix.dashboard.EnableHystrixDashboard; import org.springframework.context.annotation.Bean; import org.springframework.web.client.RestTemplate; @SpringBootApplication @EnableEurekaClient @EnableDiscoveryClient @EnableHystrix @EnableHystrixDashboard public class ServiceRibbonApplication { public static void main(String[] args) { SpringApplication.run( ServiceRibbonApplication.class, args ); } @Bean @LoadBalanced RestTemplate restTemplate() { return new RestTemplate(); } }
運行程序: 依次開啟eureka-server (port:8761),service-hi(port:8762)和 service-ribbon(port:8764)工程。
4.3 Hystrix Dashboard圖形展示
打開http://localhost:8764/actuator/hystrix.stream,可以看到一些具體的數據:
打開locahost:8764/hystrix 可以看見以下界面:
在界面依次輸入:http://localhost:8764/actuator/hystrix.stream 、2000 、hi;點確定。
在另一個窗口訪問: http://localhost:8764/hi?name=zang,重新刷新hystrix.stream網頁,你會看到良好的圖形化界面:
關閉service-hi(port:8762)服務,再次訪問 http://localhost:8764/hi?name=zang,使其進入斷路fallbackMethod,可以在監控平台上觀察到此次斷路。