一、Hystrix熔斷器
在微服務架構中,根據業務來拆分成一個個的服務,服務與服務之間可以互相調用(RPC)。如果單個服務出現問題,調用這個服務就會出現線程阻塞,此時若有大量的請求湧入,Servlet容器的線程資源會被消耗完畢,導緻服務癱瘓。服務與服務之間的依賴性,故障會傳播,會對整個微服務系統造成災難性的嚴重後果,這就是服務故障的“雪崩”效應。
斷路打開後,可用避免連鎖故障,熔斷方法可以直接傳回一個固定值。
二、在ribbon中使用斷路器
在第三小節的源碼上,修改ribbon項目。
2.1 導入ribbon熔斷器依賴
<!-- 熔斷器依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
2.2 使用注解開啟熔斷功能
在啟動類上增加@EnableHystrix注解,以開啟熔斷功能。
@SpringBootApplication
@EnableEurekaClient
@EnableDiscoveryClient
@EnableHystrix
public class ApplicationRibbon {
public static void main(String[] args) {
SpringApplication.run( ApplicationRibbon.class, args );
}
@Bean
@LoadBalanced
RestTemplate restTemplate() {
return new RestTemplate();
}
}
2.3 編寫熔斷處理方法
因為ribbon采用的是RestTemplate來調用其它微服務的接口,是以需要修改方法,在方法上增加@HystrixCommand注解。并指明如果請求不到指定的伺服器資料,則執行fallbackMethod指定的方法。
package com.safesoft.ribbon.service;
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;
/**
* @author jay.zhou
* @date 2019/1/24
* @time 16:08
*/
@Service
public class HelloService {
/**
* 将IOC容器中的負載均衡RestTemplate工具注入進來
*/
@Autowired
RestTemplate restTemplate;
@HystrixCommand(fallbackMethod = "obtainFailure")
public String obtainOtherServiceData(String name) {
//嘗試調用其它微服務接口,通路的是SERVICE-CLIENT這個伺服器的/hi 接口
return restTemplate.getForObject("http://SERVICE-CLIENT/hi?name=" + name, String.class);
}
public String obtainFailure(String name) {
return "sorry " + name + " , server internal error";
}
}
2.4 測試
依次開啟8761注冊中心server,8763服務提供者client,目前項目8764 ribbon。通路http://localhost:8764/hello?name=dayu,請求被ribbon轉發給8763服務提供者類。
僅關閉服務提供者client項目,再次通路http://localhost:8764/hello?name=dayu
三、在feign中使用斷路器
在第三小節的源碼上,修改feign項目。
2.1 配置檔案
feign內建了ribbon,是以自己就能夠熔斷,不需要導入其它依賴。需要在配置檔案中加上開啟熔斷功能的配置。
#指明注冊中心的位置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8761/eureka/
#目前項目部署的端口
server:
port: 8765
#目前項目的名稱
spring:
application:
name: service-feign
#開啟熔斷功能
feign:
hystrix:
enabled: true
2.2 配置熔斷處理類
在接口上的@FeignClient注解裡,增加fallback = ErrorHandler.class配置。
package com.safesoft.feign.service;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(value = "SERVICE-CLIENT",fallback = ErrorHandler.class)
public interface HelloService {
/**
* 從SERVICE-CLIENT伺服器的/hi接口擷取JSON資料
*
* @param name
* @return
*/
@RequestMapping("/hi")
String obtainOtherServerJsonData(@RequestParam(value = "name") String name);
}
2.3 編寫熔斷處理類
在接口上的@FeignClient注解裡,增加fallback = ErrorHandler.class配置。
需要實作@FeignClient注解的接口,并且使用@Component加入IOC容器。
package com.safesoft.feign.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class ErrorHandler implements HelloService {
@Value("${server.port}")
private String port;
@Override
public String obtainOtherServerJsonData(String name) {
return "sorry " + name + " , " + port + " server internal error";
}
}
2.3 測試
開啟8761注冊中心Server,開啟8763服務提供者client,再開啟目前8765端口的feign項目。
在浏覽器輸入http://localhost:8765/sayHello?name=dayu
僅關閉8763服務提供者client項目,再次在浏覽器輸入http://localhost:8765/sayHello?name=dayu。
四、源碼下載下傳
https://github.com/hairdryre/Study_SpringCloud
參考文章:史上最簡單的SpringCloud教程 | 第四篇:斷路器(Hystrix)(Finchley版本)
----------------------------------------------------分割線-------------------------------------------------------