在微服務架構中,根據業務來拆分紅一個個的服務,服務與服務之間能夠相互調用(RPC),在Spring Cloud
能夠用RestTemplate+Ribbon
或Feign
來調用。爲了保證其高可用,單個服務一般會集羣部署。因爲網絡緣由或者自身的緣由,服務並不能保證100%可用,若是單個服務出現問題,調用這個服務就會出現線程阻塞,此時如有大量的請求涌入,Servlet容器的線程資源會被消耗完畢,致使服務癱瘓。服務與服務之間的依賴性,故障會傳播,會對整個微服務系統形成災難性的嚴重後果,這就是服務故障的「雪崩」效應。java
爲了解決這個問題,業界提出了斷路器模型。git
Netflix has created a library called Hystrix that implements the circuit breaker pattern. In a microservice architecture it is common to have multiple layers of service calls.spring
Netflix建立了一個名爲Hystrix的庫,該庫實現了斷路器模式。在微服務架構中,一般有多個服務調用層。瀏覽器
較底層的服務若是出現故障,會致使連鎖故障。當對特定的服務的調用的不可用達到一個閥值(Hystric 是5秒20次) 斷路器將會被打開。網絡
斷路打開後,可用避免連鎖故障,fallback方法能夠直接返回一個固定值。架構
在以前工程的基礎上, 啓動eureka-server,端口爲9090;啓動eureka-client, 端口爲8040。app
改造rebbon-service
工程的代碼,首先在pox.xml文件中加入spring-cloud-starter-netflix-hystrix
的起步依賴:ide
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> </dependency>
在項目啓動類上註解@EnableHystrix, 開啓斷路器能力:微服務
@EnableEurekaClient @SpringBootApplication @EnableHystrix public class RibbonServiceApplication { public static void main(String[] args) { SpringApplication.run(RibbonServiceApplication.class, args); } @Bean @LoadBalanced public RestTemplate restTemplate() { return new RestTemplate(); } }
改造HelloController
類, 在hello
方法加上@HystrixCommand
註解。 該註解給方法低通了熔斷器的能力, 指定fallbackMethod
熔斷方法, 當遠程服務調用時候後執行熔斷方法:ui
@RestController public class HelloController { private final RestTemplate restTemplate; @Autowired public HelloController(RestTemplate restTemplate) { this.restTemplate = restTemplate; } @HystrixCommand(fallbackMethod = "helloError") @GetMapping("/hello") public String hello(@RequestParam("name") String name) { return restTemplate.getForObject("http://HELLO-ERUEKA-CLIENT/hello?name=" + name, String.class); } public String helloError(String name) { return String.format("Hello, %s! Access remote service fail!", name); } }
啓動ribbon-service項目,在瀏覽器訪問 http://localhost:8050/hello?name=Mars:
Hello, My name is Mars, I'm from port: 8040
這時候咱們關閉eureka-service項目, 再次訪問 http://localhost:8050/hello?name=Mars:
Hello, Mars! Access remote service fail!
這就說明eureka-service服務不可達時, ribbon-service調用接口會快速失敗, 直接調用熔斷方法, 而不是等待響應超時, 這很好的控制了容器的線程阻塞。
Feign已經集成了斷路器, 基於feign-service項目進行改造, 只須要在@FeignClient註解中加上fallback
的指定類就行:
@FeignClient(value = "hello-eureka-client", fallback = FeignServiceHystrix.class) public interface FeignService { @GetMapping(value = "/hello") String hello(@RequestParam(value = "name") String name); }
FeignServiceHystrix
須要實現FeignService
接口,並注入到Ioc容器中:
@Component public class FeignServiceHystrix implements FeignService { @Override public String hello(String name) { return String.format("Hello, %s! Access remote service fail!", name); } }
先啓動eureka-client和eureka-server項目, 而後再啓動feign-service項目,在瀏覽器訪問 http://localhost:8080/hello?name=Mars:
Hello, My name is Mars, I'm from port: 8040
這時候咱們關閉eureka-service項目, 再次訪問 http://localhost:8080/hello?name=Mars:
Hello, Mars! Access remote service fail!
這證實斷路器起到做用了。
關注公衆號:JAVA九點半課堂,回覆【資料】獲取1T最新技術資料!