完整目錄清單頁面(必看)
Sentinel 支援異步調用鍊路的統計。在異步調用中,需要通過
SphU.asyncEntry(xxx)
方法定義資源,并通常需要在異步的回調函數中調用
exit
方法。
第一步:啟動類加注解
@EnableAsync
,讓項目支援異步調用支援
@SpringBootApplication
@EnableAsync
public class SentinelHelloWorldApplication {
public static void main(String[] args) {
SpringApplication.run(SentinelHelloWorldApplication.class,args);
}
}
第二步:建立AsyncService異步調用類以及方法
package com.java1234.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
/**
* @author java1234_小鋒
* @site www.java1234.com
* @company Java知識分享網
* @create 2021-05-27 13:18
*/
@Service
public class AsyncService {
@Async
public void doSomethingAsync(){
System.out.println("async start...");
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("async end...");
}
}
第三步:建立Controller方法
@RequestMapping("helloWorld4")
public void helloWorld4(){
AsyncEntry asyncEntry =null;
try {
asyncEntry = SphU.asyncEntry("helloWorld4");
asyncService.doSomethingAsync();
} catch (BlockException e) {
System.out.println("系統繁忙,請稍後!");
}finally {
if(asyncEntry!=null){
asyncEntry.exit();
}
}
}
第四步:Sentinel控制台新增流控規則

第五步:測試
浏覽器請求:http://localhost/helloWorld4
正常通路控制台輸出:
頻繁通路控制台輸出:
微信搜一搜【java1234】關注這個放蕩不羁的程式員,關注後回複【資料】有我準備的一線大廠筆試面試資料以及履歷模闆。