
本最佳實踐将為您介紹如何使用Sentinel對微服務的限流降級。
1. 背景資訊
1.1 場景描述
Hystrix是通過代碼注入的形式實作應用的熔斷降級功能,而Sentinel隻需要導入一個SDK,無需業務代碼中添加限流降級注解或代碼塊,無侵入式的實作對微服務的限流降級。
1.2 Sentinel特征
-
豐富的應用場景:
Sentinel承接了阿裡巴巴近10年的雙十一大促流量的核心場景,例如秒殺(即突發流量控制在系統容量可以承受的範圍)、消息削峰填谷、實時熔斷下遊不可用應用等。
-
完備的實時監控:
Sentinel同時提供實時的監控功能。您可以在控制台中看到接入應用的單台機器秒級資料,甚至500台以下規模的叢集的彙總運作情況。
-
廣泛的開源生态:
Sentinel提供開箱即用的與其它開源架構/庫的整合子產品,例如與Spring Cloud、Dubbo、gRPC的整合。您隻需要引入相應的依賴并進行簡單的配置即可快速地接入Sentinel。
-
完善的SPI擴充點:
Sentinel提供簡單易用、完善的SPI擴充點。您可以通過實作擴充點,快速的定制邏輯。例如定制規則管理、适配資料源等。
1.3 最佳實踐價值
在微服務場景中,由于微服務調用的複雜性,希望盡量減少代碼的修改,并且希望對微服務的管理實作可是化的形式。是以使用Sentinel實作限流降級功能變得适合及友好,不僅開發人員開發變得簡單,對于運維人員管理微服務的限流降級也容易,同時對微服務應用的限流降級配置規則也可以在控制台修改,無需對代碼的改動,減小微服務部署重新開機等可能業務重大影響的操作。
2. 遷移指導
2.1 Spring Cloud請求熔斷遷移至Sentinel
1.在使用Hystrix工程中找到熔斷的依賴及代碼塊,替換maven工程中替換pom.xml檔案的導入依賴包。
Spring Cloud Hystrix的pom.xml檔案的配置:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
Spring Cloud Sentinel的pom.xml的檔案配置:
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
2.微服務新增熔斷機制。
Hystrix熔斷機制注解@HystrixCommand:
@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
//一旦調用服務方法失敗并抛出了錯誤資訊後,會自動調用@HystrixCommand标注好的fallbackMethod調用類中的指定方法
@HystrixCommand(fallbackMethod = "processHystrix_Get")
public Dept get(@PathVariable("id") Long id)
{
Dept dept = this.deptService.get(id);
if (null == dept) {
throw new RuntimeException("該ID:" + id + "沒有沒有對應的資訊");
}
return dept;
}
public Dept processHystrix_Get(@PathVariable("id") Long id)
{
Dept dept = new Dept();
dept.setDeptno(id);
dept.setDname("該ID:" + id + "沒有沒有對應的資訊,null--@HystrixCommand");
dept.setDb_source("no this database in MySQL");
return dept;
}
3.Spring Cloud Hystrix應用主程式類添加注解@EnableCircuitBreaker。
Hystrix啟動類注解:
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})
@MapperScan(basePackages="com.poc.springcloud.dao")
@EnableEurekaClient
//@EnableDiscoveryClient //服務發現
@EnableCircuitBreaker
//對Hystrix熔斷機制支援
public class DeptProvider_App {
public static void main(String[] args) {
SpringApplication.run(DeptProvider8001_App.class,args);
}
}
4.因Sentinel無代碼侵入,是以不需要修改業務代碼,登入Sentinel控制台。
5.在左側導航欄單擊簇點鍊路。
6.在簇點鍊路頁面,找到想要操作的資源,單擊操作欄中的流控。
2.2 Spring Cloud服務降級遷移至Sentinel
說明:降級服務:整體資源快不夠用時,将某些服務先關掉,當可用資源恢複後再開啟。所謂降級,就是從整體考慮,當某個服務熔斷之後,伺服器将不再被調用,此刻用戶端可以自己準備一個本地的fallback回調,傳回一個預設值,即使短期内使服務水準下降,但可以将伺服器一直維持在可用狀态。
1.在使用Hystrix工程中找到服務降級的依賴及代碼塊,替換pom.xml檔案的導入依賴包。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
2.微服務新增服務降級處理類。
Service接口實作一個FallbackFactory接口類DeptClientServiceFallbackFactory。
Spring Cloud Hystrix的DeptClientServiceFallbackFactory實作代碼:
@Component // 不要忘記添加
public class DeptClientServiceFallbackFactory implements FallbackFactory<DeptClientService>
{
@Override
public DeptClientService create(Throwable throwable)
{
return new DeptClientService() {
@Override
public Dept get(long id)
{
Dept dept = new Dept();
dept.setDeptno(id);
dept.setDname("該ID:" + id + "沒有沒有對應的資訊,Consumer用戶端提供的降級資訊,此刻服務Provider已經關閉");
dept.setDb_source("no this database in MySQL");
return dept;
}
@Override
public List<Dept> list() { return null; }
@Override
public boolean add(Dept dept) { return false; }
};
}
}
3.微服務的service限流降級處理機制。
Spring Cloud Hystrix對限流降級接口統一fallback處理:
@FeignClient(value = "MICROSERVICECLOUD-DEPT",fallbackFactory=DeptClientServiceFallbackFactory.class)
public interface DeptClientService
{
@RequestMapping(value = "/dept/get/{id}", method = RequestMethod.GET)
public Dept get(@PathVariable("id") long id);
}
6.在簇點鍊路頁面,找到想要操作的資源,單擊操作欄中的降級。
後續内容
專有雲Spring Cloud應用限流降級--Series2:應用部署 專有雲Spring Cloud應用限流降級--Series3:配置控制台規則我們是阿裡雲智能全球技術服務-SRE團隊,我們緻力成為一個以技術為基礎、面向服務、保障業務系統高可用的工程師團隊;提供專業、體系化的SRE服務,幫助廣大客戶更好地使用雲、基于雲建構更加穩定可靠的業務系統,提升業務穩定性。我們期望能夠分享更多幫助企業客戶上雲、用好雲,讓客戶雲上業務運作更加穩定可靠的技術,您可用釘釘掃描下方二維碼,加入阿裡雲SRE技術學院釘釘圈子,和更多雲上人交流關于雲平台的那些事。