官網:https://sentinelguard.io/zh-cn/index.html
注解支持:https://github.com/alibaba/Sentinel/wiki/%E6%B3%A8%E8%A7%A3%E6%94%AF%E6%8C%81
sentinel控制台文檔:https://sentinelguard.io/zh-cn/docs/dashboard.html
參考:https://www.cnblogs.com/ralgo/p/14152390.html
1、下載sentinel-dashboard.jar,下載地址:https://github.com/alibaba/Sentinel/releases
並啟動,cmd執行命令,最后面是jar包路徑:
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar E:\download\sentinel-dashboard-1.8.3.jar
瀏覽器訪問 http://localhost:8080,登錄賬號密碼都是sentinel
2、項目的pom
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>cloud22</artifactId> <groupId>com.jay.springcloud</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>cloud-provider-payment8001</artifactId> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.4</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2021.0.1</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>2021.0.1.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> <version>2021.1</version> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bootstrap</artifactId> <version>3.1.1</version> </dependency> <!-- 不需要引用這些,僅僅引用 com.alibaba.cloud/spring-cloud-starter-alibaba-sentinel/2.1.0.RELEASE 就可以,版本號高了會導致jar包循環引用的問題--> <!-- <dependency>--> <!-- <groupId>com.alibaba.csp</groupId>--> <!-- <artifactId>sentinel-core</artifactId>--> <!-- <version>1.8.3</version>--> <!-- </dependency>--> <!-- <dependency>--> <!-- <groupId>com.alibaba.csp</groupId>--> <!-- <artifactId>sentinel-annotation-aspectj</artifactId>--> <!-- <version>1.8.3</version>--> <!-- </dependency>--> <!-- <dependency>--> <!-- <groupId>com.alibaba.csp</groupId>--> <!-- <artifactId>sentinel-transport-simple-http</artifactId>--> <!-- <version>1.8.3</version>--> <!-- </dependency>--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId> <version>2.1.0.RELEASE</version> </dependency> <!-- <dependency>--> <!-- <groupId>com.alibaba.csp</groupId>--> <!-- <artifactId>sentinel-datasource-nacos</artifactId>--> <!-- <version>1.8.3</version>--> <!-- </dependency>--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> </dependency> <!--mysql-connector-java--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--jdbc--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> </dependency> <dependency> <groupId>com.jay.springcloud</groupId> <artifactId>cloud-api-commons</artifactId> <version>1.0-SNAPSHOT</version> <scope>compile</scope> </dependency> </dependencies> </project>
yaml修改了,增加了sentinel那一段的配置
server: port: 8001 spring: cloud: nacos: discovery: server-addr: localhost:8848 #Nacos作為服務中心地址 config: server-addr: localhost:8848 file-extension: yaml group: SH_GROUP namespace: test sentinel: transport: dashboard: localhost:9090 #配置sentinel dashboard地址,監控項目端口8001 port: 8719 #默認8719端口,如果被占用,則從8719遞增檢查未被占用的端口 application: name: cloud-payment-service #cloud-payment-service-dev.yaml profiles: active: test #${prefix}-${spring.profiles.active}.${file-extension}
服務端代碼示例:當熱點規則配置 每秒1次,超過就會走dealGetPaymentById方法,deal方法的返回值和引用方法一樣,參數多了一個 BlockException
/** * SentinelResource 在 sentinel 網頁中配置熱點(HotKey)的時候使用 value,保持唯一, * blockHandler 是在 getPaymentById 方法異常后的處理方法 */ @SentinelResource(value = "PaymentController.getPaymentById", blockHandler = "dealGetPaymentById") @GetMapping(value = "/payment/get/{id}") public CommonResult<Payment> getPaymentById(@PathVariable(value = "id", required = true) Long id) { Payment payment = paymentService.getPaymentById(id); if (payment != null) { return new CommonResult(200, "查詢成功,serverPort: " + serverPort, payment); } else { return new CommonResult(444, "沒有對應記錄,查詢ID: " + id, null); } } /** * sentinel 限流處理 */ public CommonResult<Payment> dealGetPaymentById(Long id, BlockException ex) { return new CommonResult(555, "Sentinel HotKey限流: " + id, null); }
@SentinelResource中還可以配置fallback,服務如果出現異常,走fallback指定的方法處理異常Throwable。自定義異常處理。
如果同時配置了fallback和blockHandler,當超過sentinel的流控規則時,流控規則blockHandler優先,否則走fallback。
配置exceptionsToIgnore指定fallback不處理哪些異常。
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-feign</artifactId> <version>1.4.7.RELEASE</version> </dependency>
ymal:feign.sentinel.enabled = true
啟動類:@EnableFeignClients,消費端interface加@FeignClient(value = "服務id",fallback=xxx.class),例如:payment-service,
xxx實現interface,其中的實現就是運行出錯時的處理類。
參考:https://www.bilibili.com/video/BV18E411x7eT?p=136
保持接口和服務端簽名一致,加注解,例如@GetMapping(value = "服務的調用路徑 payment/get/{id}")。
sentinel持久化:https://www.bilibili.com/video/BV18E411x7eT?p=138
<dependency> <groupId>com.alibaba.csp</groupId> <artifactId>sentinel-datasource-nacos</artifactId> <version>1.8.0</version> </dependency>
ymal:
前綴:spring.cloud.sentinel dataSource ds1: nacos: server-addr: localhost:8848 dataId: cloudalibaba-sentinel-service groupId: DEFAULT_GROUP data-type: json rule-typle: flow