上次我們講過了《Sentinel是什么?Sentinel核心庫和控制臺》,這次我們將為大家介紹客戶端接入控制臺,
一、環境準備
sentinel-demo 聚合工程,SpringBoot 2.3.0.RELEASE、Spring Cloud Hoxton.SR4,
Nacos 注冊中心
product-service:商品服務,提供了 /product/{id} 介面
order-service-rest:訂單服務,基于 Ribbon 通過 RestTemplate 呼叫商品服務
order-server-feign:訂單服務,基于 Feign 通過宣告式服務呼叫商品服務
二、客戶端接入控制臺
控制臺啟動后,客戶端需要按照以下步驟接入到控制臺:
- 添加依賴
- 定義資源
- 定義規則
先把可能需要保護的資源定義好,之后再配置規則,也可以理解為,只要有了資源,我們就可以在任何時候靈活地定義各種流量控制規則,在編碼的時候,只需要考慮這個代碼是否需要保護,如果需要保護,就將之定義為一個資源,
由于我們的專案是 Spring Cloud 專案,所以可以借助官方檔案來進行學習,
Spring 官網檔案:https://spring-cloud-alibaba-group.github.io/github-pages/greenwich/spring-cloud-alibaba.html
Github 檔案:https://github.com/alibaba/spring-cloud-alibaba/wiki/Sentinel
1、添加依賴
父工程需要添加如下依賴:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
子工程需要添加如下依賴:
<!-- spring cloud alibaba sentinel 依賴 -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
組態檔
客戶端需要啟動 Transport 模塊來與 Sentinel 控制臺進行通信,
order-service-rest 的 application.yml
spring: cloud: # 配置 Sentinel sentinel: transport: port: 8719 dashboard: localhost:8080
這里的 spring.cloud.sentinel.transport.port 埠配置會在應用對應的機器上啟動一個 Http Server,該 Server 會與 Sentinel 控制臺做互動,比如 Sentinel 控制臺添加了一個限流規則,會把規則資料 push 給這個 Http Server 接收,Http Server 再將規則注冊到 Sentinel 中,
初始化客戶端
「確保客戶端有訪問量」,Sentinel 會在「客戶端首次呼叫的時候」進行初始化,開始向控制臺發送心跳包,
簡單的理解就是:訪問一次客戶端,Sentinel 即可完成客戶端初始化操作,并持續向控制臺發送心跳包,
訪問
多次訪問:http://localhost:9090/order/1 然后查看控制臺實時監控結果如下:
2、定義資源
「資源」 是 Sentinel 中的核心概念之一,我們說的資源,可以是任何東西,服務,服務里的方法,甚至是一段代碼,最常用的資源是我們代碼中的 Java 方法,Sentinel 提供了 @SentinelResource 注解用于定義資源,并提供了 AspectJ 的擴展用于自動定義資源、處理 BlockException 等,
只要通過 Sentinel API 定義的代碼,就是資源,能夠被 Sentinel 保護起來,大部分情況下,可以使用方法簽名,URL,甚至服務名稱作為資源名來標示資源,
官網檔案:https://github.com/alibaba/Sentinel/wiki/如何使用#定義資源
注解支持
官網檔案:https://github.com/alibaba/Sentinel/wiki/注解支持
OrderServiceImpl.java
package com.example.service.impl; import com.alibaba.csp.sentinel.annotation.SentinelResource; import com.alibaba.csp.sentinel.slots.block.BlockException; import com.example.pojo.Order; import com.example.service.OrderService; import com.example.service.ProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Arrays; @Service public class OrderServiceImpl implements OrderService { @Autowired private ProductService productService; /** * 根據主鍵和訂單編號查詢訂單 * * @param id * @param orderNo * @return */ @Override @SentinelResource(value = "selectOrderByIdAndOrderNo", blockHandler = "selectOrderByIdAndOrderNoBlockHandler", fallback = "selectOrderByIdAndOrderNoFallback") public Order selectOrderByIdAndOrderNo(Integer id, String orderNo) { return new Order(id, orderNo, "中國", 2666D, Arrays.asList(productService.selectProductById(1))); } // 服務流量控制處理,引數最后多一個 BlockException,其余與原函式一致, public Order selectOrderByIdAndOrderNoBlockHandler(Integer id, String orderNo, BlockException ex) { // Do some log here. ex.printStackTrace(); return new Order(id, "服務流量控制處理-托底資料", "中國", 2666D, Arrays.asList(productService.selectProductById(1))); } // 服務熔斷降級處理,函式簽名與原函式一致或加一個 Throwable 型別的引數 public Order selectOrderByIdAndOrderNoFallback(Integer id, String orderNo, Throwable throwable) { System.out.println("order-service 服務的 selectOrderById 方法出現例外,例外資訊如下:" + throwable); return new Order(id, "服務熔斷降級處理-托底資料", "中國", 2666D, Arrays.asList(productService.selectProductById(1))); } }
ProductServiceImpl.java
package com.example.service.impl; import com.alibaba.csp.sentinel.annotation.SentinelResource; import com.alibaba.csp.sentinel.slots.block.BlockException; import com.example.pojo.Product; import com.example.service.ProductService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; /** * 商品管理 */ @Service public class ProductServiceImpl implements ProductService { @Autowired private RestTemplate restTemplate; /** * 根據主鍵查詢商品 * * @param id * @return */ @SentinelResource(value = "selectProductById", blockHandler = "selectProductByIdBlockHandler", fallback = "selectProductByIdFallback") @Override public Product selectProductById(Integer id) { return restTemplate.getForObject("http://product-service/product/" + id, Product.class); } // 服務流量控制處理,引數最后多一個 BlockException,其余與原函式一致, public Product selectProductByIdBlockHandler(Integer id, BlockException ex) { // Do some log here. ex.printStackTrace(); return new Product(id, "服務流量控制處理-托底資料", 1, 2666D); } // 服務熔斷降級處理,函式簽名與原函式一致或加一個 Throwable 型別的引數 public Product selectProductByIdFallback(Integer id, Throwable throwable) { System.out.println("product-service 服務的 selectProductById 方法出現例外,例外資訊如下:" + throwable); return new Product(id, "服務熔斷降級處理-托底資料", 1, 2666D); } }
注意:注解方式埋點不支持 private 方法,
@SentinelResource 用于定義資源,并提供可選的例外處理和 fallback 配置項, @SentinelResource 注解包含以下屬性:
value:資源名稱,必需項(不能為空)
entryType:entry 型別,可選項(默認為 EntryType.OUT)
blockHandler / blockHandlerClass: blockHandler 對應處理 BlockException 的函式名稱,可選項,
blockHandler 函式訪問范圍需要是 public,回傳型別需要與原方法相匹配,引數型別需要和原方法相匹配并且最后加一個額外的引數,型別為 BlockException,blockHandler 函式默認需要和原方法在同一個類中,若希望使用其他類的函式,則可以指定 blockHandlerClass 為對應的類的 Class 物件,注意對應的函式必需為 static 函式,否則無法決議,
fallback:fallback 函式名稱,可選項,用于在拋出例外的時候提供 fallback 處理邏輯,fallback 函式可以針對所有型別的例外(除了 exceptionsToIgnore 里面排除掉的例外型別)進行處理,
fallback 函式簽名和位置要求:
- 回傳值型別必須與原函式回傳值型別一致;
- 方法引數串列需要和原函式一致,或者可以額外多一個 Throwable 型別的引數用于接收對應的例外,
- fallback 函式默認需要和原方法在同一個類中,若希望使用其他類的函式,則可以指定 fallbackClass 為對應的類的Class 物件,注意對應的函式必需為 static 函式,否則無法決議,
defaultFallback(since 1.6.0):默認的 fallback 函式名稱,可選項,通常用于通用的 fallback 邏輯(即可以用于很多服務或方法),默認 fallback 函式可以針對所有型別的例外(除了 exceptionsToIgnore 里面排除掉的例外型別)進行處理,若同時配置了 fallback 和 defaultFallback,則只有 fallback 會生效,
defaultFallback 函式簽名要求:
- 回傳值型別必須與原函式回傳值型別一致;
- 方法引數串列需要為空,或者可以額外多一個 Throwable 型別的引數用于接收對應的例外,
- defaultFallback 函式默認需要和原方法在同一個類中,若希望使用其他類的函式,則可以指定 fallbackClass為對應的類的 Class 物件,注意對應的函式必需為 static 函式,否則無法決議,
exceptionsToIgnore(since 1.6.0):用于指定哪些例外被排除掉,不會計入例外統計中,也不會進入 fallback 邏輯中,而是會原樣拋出,
注:1.6.0 之前的版本 fallback 函式只針對降級例外(DegradeException)進行處理,「不能針對業務例外進行處理」,
特別地,若 blockHandler 和 fallback 都進行了配置,則被限流降級而拋出 BlockException 時只會進入 blockHandler 處理邏輯,若未配置 blockHandler、fallback 和 defaultFallback,則被限流降級時會將 BlockException 「直接拋出」(若方法本身未定義 throws BlockException 則會被 JVM 包裝一層 UndeclaredThrowableException),
從 1.4.0 版本開始,注解方式定義資源支持自動統計業務例外,無需手動呼叫 Tracer.trace(ex) 來記錄業務例外,Sentinel 1.4.0 以前的版本需要自行呼叫 Tracer.trace(ex) 來記錄業務例外,
作者:哈嘍沃德先生,歡迎關注哈嘍沃德先生公眾號,點擊獲取更多springcloud alibaba視頻教程
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/103729.html
標籤:Java
