日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
使用Sentinel實現(xiàn)接口限流

本文轉(zhuǎn)載自微信公眾號「運(yùn)維開發(fā)故事」,作者老鄭。轉(zhuǎn)載本文請聯(lián)系運(yùn)維開發(fā)故事公眾號。

集賢ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!

在前面一篇文章我已經(jīng)對 Sentinel 做了一個簡單的介紹,相信大家對 Sentinel 有一個簡單的了解,本次主要是講 Sentinel 的使用。在 sentinel-dashboard 配置流控規(guī)則,以及使用 Sentinel 整合 RestTemplate、OpenFeign 進(jìn)行流控使用(建議網(wǎng)頁版閱讀)。

安裝 sentinel dashboard

我使用的 sentinel 版本是: sentinel-dashboard-1.8.0

啟動控制臺命令:

 
 
 
 
  1. java -jar sentinel-dashboard-1.8.0.jar

默認(rèn)啟動的是 8080 端口, 登錄賬號和密碼默認(rèn)都是: sentinel。 如果需要修改啟動端口可以在啟動命令前面加 -Dserver.port=9999 進(jìn)行修改。

使用介紹

通常我們在項目中對于 Sentinel 最常用的場景,就是默認(rèn)的流控對接口的訪問添加流控規(guī)則。Sentinel 也提供了對于 RestTemplate 、OpenFegin 的支持。

簡單案例

1. 導(dǎo)入依賴

如果我們需要使用 Sentinel ,首先我們需要在業(yè)務(wù)服務(wù)中,導(dǎo)入 Sentinel 客戶端的依賴。下面是 Maven 的 pom 依賴。 我們可以直接使用 spring-coud-starter-alibaba-sentinel 進(jìn)行快速整合。

 
 
 
 
  1.   com.alibaba.cloud
  2.   spring-cloud-starter-alibaba-sentinel

對于 spring-cloud-alibaba 相關(guān)的版本依賴信息如下:

 
 
 
 
  1.   2.3.10.RELEASE
  2.   Hoxton.SR8
  3.   2.2.5.RELEASE
  4.   
  5.     
  6.       org.springframework.boot
  7.       spring-boot-dependencies
  8.       ${spring-boot.version}
  9.       pom
  10.       import
  11.     
  12.     
  13.       org.springframework.cloud
  14.       spring-cloud-dependencies
  15.       ${spring-cloud.version}
  16.       pom
  17.       import
  18.     
  19.     
  20.     
  21.       com.alibaba.cloud
  22.       spring-cloud-alibaba-dependencies
  23.       ${spring-cloud-alibaba.version}
  24.       pom
  25.       import
  26.     
  27.   

2. YML 配置

我們在業(yè)務(wù)服務(wù)中導(dǎo)入了依賴過后,我們需要修改 application.yml 文件讓服務(wù)啟動過后自動注冊到 sentinel-dashboard 服務(wù)上。

 
 
 
 
  1. spring:
  2.   cloud:
  3.     sentinel:
  4.       transport:
  5.         port: 8719
  6.         dashboard: localhost:8080

3. 測試接口定義

首先我們需要定義對外開放的接口。

 
 
 
 
  1. @RestController
  2. public class HelloController {
  3.     
  4.     @GetMapping("/hello")
  5.     public String hello () {
  6.         return "OK";
  7.     }
  8. }

4. 通過控制臺配置流控規(guī)則

注意:如果已經(jīng)啟動 snetinel-dashboard 后并且啟動業(yè)務(wù)服務(wù),在 sentinel-dashboard 后臺還是沒有服務(wù)的話,我們可以先訪問一下業(yè)務(wù)服務(wù)的接口,然后在刷新snetinel-dashboard 觀察是否正常。如果還是不正常請考慮 sentinel 的 client 版本和 dashboard 是否匹配。

首先選擇自己對應(yīng)服務(wù)展開,然后選擇【簇點(diǎn)鏈路】 菜單。選擇需要流控的接口 /hello 然后選擇 【流控】按鈕進(jìn)行流控配置

我們可以配置, 我們選擇【閥值類型】選擇【QPS】,然后設(shè)置【單機(jī)閥值】 填入 1 。表示該接口每秒鐘只能接受一個 QPS ,如果超過閾值過后就會觸發(fā) 【流控】默認(rèn) Sentinel 返回 Blocked by Sentinel (flow limiting)

5. 流控規(guī)則觸發(fā)

如果我們需要觸發(fā)流控規(guī)則我們頻繁訪問 /hello 接口即可。

 
 
 
 
  1. ~ curl http://127.0.0.1:8066/hello
  2. OK%                                                                                                                                                   ~ curl http://127.0.0.1:8066/hello
  3. ~ curl http://127.0.0.1:8066/hello
  4. Blocked by Sentinel (flow limiting)%

通過上面的結(jié)果我們可以看到當(dāng)單位時間內(nèi)超過閾值過后, 就會觸發(fā) flow limit

整合 RestTemplate

1. YML 配置

Sentinel 整合 Resttemplate 除了需要導(dǎo)入 spring-cloud-starter-alibaba-sentinel 開需要開啟 Sentinel 對 Resttemplate 的支持。

 
 
 
 
  1. resttemplate:
  2.   sentinel:
  3.     enabled: true

2. 創(chuàng)建 RestTemplate

如果 RestTemplate 在使用的時候需要使用到 Sentinel 的流控規(guī)則,首先需要在創(chuàng)建 RestTemplate 的時候添加 @SentinelRestTemplate 注解。注意: SentinelExceptionHandler 中的方法都是 static 方法

 
 
 
 
  1. @Configuration
  2. public class RestTemplateConfig {
  3.     @Bean
  4.     @ConditionalOnMissingBean(RestTemplate.class)
  5.     @LoadBalanced
  6.     @SentinelRestTemplate(
  7.             blockHandler = "handlerException", blockHandlerClass = SentinelExceptionHandler.class,
  8.             fallback = "handleFallback", fallbackClass = SentinelExceptionHandler.class)
  9.     public RestTemplate restTemplate() {
  10.         return new RestTemplate();
  11.     }
  12. }
  13. // 異常處理類
  14. public class SentinelExceptionHandler {
  15.     
  16.     //限流熔斷業(yè)務(wù)邏輯
  17.     public static SentinelClientHttpResponse handlerException(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
  18.         String message = JSON.toJSONString(CommonResult.error(-100,"系統(tǒng)錯誤 (限流熔斷業(yè)務(wù)邏輯)"));
  19.         return new SentinelClientHttpResponse(message);
  20.     }
  21.     //異常降級業(yè)務(wù)邏輯
  22.     public static SentinelClientHttpResponse handleFallback(HttpRequest request, byte[] body, ClientHttpRequestExecution execution, BlockException ex) {
  23.         String message = JSON.toJSONString(CommonResult.error(-100,"系統(tǒng)錯誤 (異常降級業(yè)務(wù)邏輯)"));
  24.         return new SentinelClientHttpResponse(message);
  25.     }
  26. }

3. 接口定義

下面就是我們使用的代碼,可能寫得稍微有點(diǎn)復(fù)雜,我來解釋一下。首先我是通過 RestTemplate 訪問 stock-service 服務(wù)的 /getStockDetail 接口然后將接口的返回數(shù)據(jù)解析,通過CommonResult 實例對象進(jìn)行接收, 如果失敗就返回錯誤信息。

 
 
 
 
  1. @Autowired
  2. private RestTemplate restTemplate;
  3. @GetMapping("/hello2")
  4. public CommonResult hello2() {
  5.   ParameterizedTypeReference> typeRef =
  6.     new ParameterizedTypeReference>() {
  7.   };
  8.   ResponseEntity>
  9.     forEntity = restTemplate.exchange("http://stock-service/getStockDetail", HttpMethod.GET,
  10.                                       HttpEntity.EMPTY, typeRef);
  11.   OrderModel orderModel = new OrderModel();
  12.   orderModel.setId(100);
  13.   orderModel.setCode("100-100");
  14.   if (Objects.equals(forEntity.getStatusCode(), HttpStatus.OK) && Objects.nonNull(forEntity.getBody())) {
  15.     CommonResult result = forEntity.getBody();
  16.     if (result.getCode() != 1) {
  17.       return CommonResult.error(null, result.getCode(), result.getMessage());
  18.     }
  19.     orderModel.setStockModel(result.getData());
  20.   }
  21.   return CommonResult.success(orderModel);
  22. }

4. 流控觸發(fā)

如果我們頻繁的訪問我們的接口 /hello2 就會出現(xiàn)限流的邏輯

~ curl http://127.0.0.1:8066/hello2

{"code":1,"message":"this is a success message","data":{"id":100,"code":"100-100","stockModel":{"id":1,"code":"STOCK==>1000"}}}

~ curl http://127.0.0.1:8066/hello2

{"code":-100,"message":"系統(tǒng)錯誤 (限流熔斷業(yè)務(wù)邏輯)","data":null}

整合 OpenFegin

1. 導(dǎo)入 openfeign 依賴

Sentinel 整合 Openfeign 需要導(dǎo)入 spring-cloud-starter-openfeign

 
 
 
 
  1.   org.springframework.cloud
  2.   spring-cloud-starter-openfeign

2. YML 配置

Sentinel 整合 Openfeign 需要開啟對 feign 的支持,配置如下:

 
 
 
 
  1. feign:
  2.   sentinel:
  3.     enabled: true

注意:啟動類上要增加 @EnableFeignClients 來配置 Openfeign 的啟用

3. 調(diào)用代碼

Feign 接口調(diào)服務(wù) stock-service 的 /getStockDetail 接口,如果觸發(fā)流控規(guī)則就會執(zhí)行 FallbackFactory 中返回 StockFeign 的本地存根方法。

 
 
 
 
  1. @FeignClient(name = "stock-service", fallbackFactory = StockFeignFallbackFactory.class)
  2. public interface StockFeign {
  3.     @GetMapping("/getStockDetail")
  4.     CommonResult getStockDetail();
  5. }

StockFeignFallbackFactory 類是服務(wù)降級的處理。

 
 
 
 
  1. @Component
  2. public class StockFeignFallbackFactory implements FallbackFactory {
  3.     private Logger log = LoggerFactory.getLogger(StockFeignFallbackFactory.class);
  4.     @Override
  5.     public StockFeign create(Throwable throwable) {
  6.         return new StockFeign() {
  7.             @Override
  8.             public CommonResult getStockDetail() {
  9.                 log.error("調(diào)用查詢庫存詳情降級", throwable);
  10.                 return CommonResult.error(null, -100, "調(diào)用查詢庫存詳情降級");
  11.             }
  12.         };
  13.     }
  14. }

Controller 調(diào)用代碼

 
 
 
 
  1. @Autowired
  2. private StockFeign stockFeign;
  3. @GetMapping("/hello1")
  4. public CommonResult hello() {
  5.   CommonResult result = stockFeign.getStockDetail();
  6.   if (result.getCode() != 1) {
  7.     return CommonResult.error(null, result.getCode(), result.getMessage());
  8.   }
  9.   StockModel stockDetail = result.getData();
  10.   OrderModel orderModel = new OrderModel();
  11.   orderModel.setStockModel(stockDetail);
  12.   return CommonResult.success(orderModel);
  13. }

4. 業(yè)務(wù)執(zhí)行

如果我們多次訪問,Sentinel 就會觸發(fā)降級策略。然后執(zhí)行 StockFeignFallbackFactory 的本地存根方法返回

源碼地址

gitee: https://gitee.com/zhengsh/excavator

參考

https://spring-cloud-alibaba-group.github.io/github-pages/hoxton/en-us/index.html#_spring_cloud_alibaba_sentinel

https://segmentfault.com/a/1190000019070557


當(dāng)前題目:使用Sentinel實現(xiàn)接口限流
本文鏈接:http://www.5511xx.com/article/coccodh.html