在前一篇文章首先啟動了sentinel控制台,在本文中将介紹springboot用戶端的相關配置,實作對springboot接口的流量、請求次數進行控制。首先先介紹一個非常重要的注解:
注解介紹
在具體配置之前介紹一下一個非常重要的注解:@SentinelResource
@SentinelResource
用于定義資源,并提供可選的異常處理和 fallback 配置項。
@SentinelResource
注解包含以下屬性:
-
: 資源名稱,必需項(不能為空)value
-
: 入口類型,可選項: EntryType.IN和EntryType.OUT(預設為entryType
)EntryType.OUT
-
/blockHandler
:blockHandlerClass
對應處理blockHandler
的函數名稱,可選項。若未配置,則将BlockException
直接抛出。blockHandler 函數通路範圍需要是BlockException
,傳回類型需要與原方法相比對,參數類型需要和原方法相比對并且最後加一個額外的參數,類型為public
。blockHandler 函數預設需要和原方法在同一個類中。若希望使用其他類的函數,則可以指定BlockException
為對應的類的blockHandlerClass
對象,注意對應的函數必需為 static 函數,否則無法解析。Class
-
: fallback 函數名稱,可選項,僅針對降級功能生效(fallback
)。fallback 函數的通路範圍需要是DegradeException
,參數類型和傳回類型都需要與原方法相比對,并且需要和原方法在同一個類中。業務異常不會進入 fallback 邏輯。public
說明:
-
value:定義資源名,該名稱将會顯示在控制台中,并且在定義流控以及熔斷降級規則時,指定資源名稱:
-
blockHandler是異常處理的方法,預設需要與原方法在同一個類中,如果需要在另外的類中定義,則需要設定blockHandlerClass,并且噶異常處理的方法應為靜态方法。
用戶端配置步驟
引入依賴:
如果要在您的項目中引入 Sentinel,使用 group ID 為
org.springframework.cloud
和 artifact ID 為
spring-cloud-starter-alibaba-sentinel
的 starter。
<!-- Sentinel -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>0.2.1.RELEASE</version>
</dependency>
修改配置檔案
#這裡的 spring.cloud.sentinel.transport.port 端口配置會在應用對應的機器上啟動一個 Http Server,該 Server 會與 Sentinel 控制台做互動。比如 Sentinel 控制台添加了1個限流規則,會把規則資料 push 給這個 Http Server 接收,Http Server 再将規則注冊到 Sentinel 中。
spring.cloud.sentinel.transport.port=8731
#設定控制台位址
spring.cloud.sentinel.transport.dashboard=localhost:8080
# 設定用戶端端口
server.port=8090
# 設定用戶端名稱,該名稱将作為該用戶端在sentinel控制台的名稱
spring.application.name=sentinel-client
建立Sentinel異常處理類
建立Java類:
package com.xjl.sentinel.handler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.csp.sentinel.slots.block.BlockException;
public class SentinelExceptionHandler {
final static Logger logger = LoggerFactory.getLogger(SentinelExceptionHandler.class);
public static String blockExceptionHandle(String name, BlockException exception) {
exception.printStackTrace();
logger.info("sentinel 熔斷處理 {}", "SentinelExceptionHandler");
return "Sentinel 熔斷處理函數";
}
}
對需要限流的資源添加@SentinelResource注解
package com.xjl.sentinel.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.csp.sentinel.EntryType;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.xjl.sentinel.handler.SentinelExceptionHandler;
@RestController
public class SentinelTestController {
@SentinelResource(value = "hello", entryType = EntryType.OUT, blockHandlerClass = SentinelExceptionHandler.class, blockHandler = "blockExceptionHandle")
@GetMapping("/hello/{name}")
public String hello(@PathVariable("name") String name) {
System.out.println(name);
return "hello " + name;
}
}
啟動用戶端
啟動用戶端後,請求幾次用戶端的接口,否則控制台并不會顯示出用戶端。通路控制台首頁,可以看到有兩個應用,其中一個是控制台自己,另一個使我們的用戶端。
該用戶端的http server端口号就是配置檔案中配置的 spring.cloud.sentinel.transport.port=8731
如果控制台以及用戶端都是在本地電腦上啟動的,應注意控制台自身也會啟動一個http server,并且預設端口号是8719,如果用戶端也采用預設的8719,則用戶端啟動後會出現沖突,用戶端會自動修改該端口号,可能改成8720或者其他未占用的端口。
添加限流規則
測試熔斷效果
通路http://localhost:8090/hello/xjl,
快速重新整理,可以看到界面中輪流出現如下傳回:
同時控制台列印如下:
xjl
com.alibaba.csp.sentinel.slots.block.flow.FlowException
2019-03-30 14:54:57.682 INFO 33840 --- [nio-8090-exec-7] c.x.s.handler.SentinelExceptionHandler : sentinel 熔斷處理 SentinelExceptionHandler
com.alibaba.csp.sentinel.slots.block.flow.FlowException
2019-03-30 14:54:57.837 INFO 33840 --- [nio-8090-exec-8] c.x.s.handler.SentinelExceptionHandler : sentinel 熔斷處理 SentinelExceptionHandler
com.alibaba.csp.sentinel.slots.block.flow.FlowException
2019-03-30 14:54:57.995 INFO 33840 --- [nio-8090-exec-9] c.x.s.handler.SentinelExceptionHandler : sentinel 熔斷處理 SentinelExceptionHandler
com.alibaba.csp.sentinel.slots.block.flow.FlowException
2019-03-30 14:54:58.158 INFO 33840 --- [io-8090-exec-10] c.x.s.handler.SentinelExceptionHandler : sentinel 熔斷處理 SentinelExceptionHandler
2019-03-30 14:54:58.288 INFO 33840 --- [nio-8090-exec-1] c.x.s.handler.SentinelExceptionHandler : sentinel 熔斷處理 SentinelExceptionHandler
com.alibaba.csp.sentinel.slots.block.flow.FlowException
com.alibaba.csp.sentinel.slots.block.flow.FlowException
2019-03-30 14:54:58.476 INFO 33840 --- [nio-8090-exec-2] c.x.s.handler.SentinelExceptionHandler : sentinel 熔斷處理 SentinelExceptionHandler
xjl
com.alibaba.csp.sentinel.slots.block.flow.FlowException
2019-03-30 14:54:58.762 INFO 33840 --- [nio-8090-exec-4] c.x.s.handler.SentinelExceptionHandler : sentinel 熔斷處理 SentinelExceptionHandler
用戶端代碼:
https://github.com/xujingle1995/Learn-SpringCloudAlibaba/tree/master/sentinel-client