Alibaba Sentinel流控詳解
點(diǎn)擊上方藍(lán)色字體,選擇“標(biāo)星公眾號(hào)”
優(yōu)質(zhì)文章,第一時(shí)間送達(dá)
概述
1.QPS測(cè)試
@GetMapping(value = "/hello")
public String hello() {
return "Hello Sentinel1";
}
Hello Sentinel1
Blocked by Sentinel (flow limiting)
2.線程數(shù)測(cè)試
@GetMapping(value = "/hello")
public String hello() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "Hello Sentinel1";
}
3.關(guān)聯(lián)測(cè)試
@GetMapping(value = "/hello")
public String hello() {
return "Hello Sentinel1";
}
@GetMapping(value = "/hello2")
public String hello2() {
return "Hello Sentinel2";
}
4.鏈路測(cè)試
@Autowired
private TestService testService;
@GetMapping(value = "/hello")
public String hello() {
testService.listOrder();
return "Hello Sentinel1";
}
@GetMapping(value = "/hello2")
public String hello2() {
testService.listOrder();
return "Hello Sentinel2";
}
@Service
public class TestService {
//資源信息
@SentinelResource("listOrder")
public void listOrder(){
System.out.println("listOrder");
}
}
5.快速失敗
@ControllerAdvice
@RestController
public class CommonException {
@ExceptionHandler(BlockException.class)
public String customException(Exception e) {
return "系統(tǒng)出小差,請(qǐng)稍后再試。";
}
@ExceptionHandler(UndeclaredThrowableException.class)
public String undeclaredThrowableException(Exception e) {
return "系統(tǒng)出小差,請(qǐng)稍后再試。";
}
@ExceptionHandler(Exception.class)
public String exception(Exception e) {
return "系統(tǒng)出小差,請(qǐng)稍后再試。";
}
}
@Configuration
public class Config {
static {
WebCallbackManager.setUrlBlockHandler(new UrlBlockHandler() {
@Override
public void blocked(HttpServletRequest request, HttpServletResponse response, BlockException ex) throws IOException {
// 簡(jiǎn)單演示,這里可以自行處理
PrintWriter out = response.getWriter();
out.print("try again later");
out.flush();
out.close();
}
});
}
}
@SentinelResource(value = "helloResource" ,blockHandler = "getOrderDowngradeRtTypeFallback",fallback = "helloFallback")
@GetMapping(value = "/hello")
public String hello(@RequestParam(value = "id") Long id) {
if(id==1l){
throw new RuntimeException("1231");
}
return "SUCCESS";
}
public String helloFallback(Long id,Throwable e) {
System.out.println(1111);
return id+"helloFallback";
}
public String getOrderDowngradeRtTypeFallback(Long id,BlockException ex) {
return "服務(wù)降級(jí)啦,當(dāng)前服務(wù)器請(qǐng)求次數(shù)過(guò)多,請(qǐng)稍后重試!";
}
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
6.Warm Up
7.排隊(duì)等待
本文鏈接:
https://blog.csdn.net/qq_30285985/article/details/107692608


評(píng)論
圖片
表情











