Sentinel 官方网站
sentinel-dashboard-1.8.7.jar包下载地址
在window通过命令行启动(java -Dserver.port=8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.7.jar),可以通过 -Dserver.port修改控制台的端口
使用的版本最好都一致
注意:如果项目不是springcloud,不可以在yml配置dashboard的地址,配置也都是无效的。如果需要和dashboard整合有以下几种方法
方法一:启动参数配置项官网
-Dcsp.sentinel.dashboard.server=192.168.11.47:8858 #dashboard的访问路径
-Dcsp.sentinel.api.port=8869 #客户端监控 API 的端口(默认是 8719)
-Dproject.name=sentinel-name #指定应用的名称
-Dcsp.sentinel.heartbeat.client.ip=192.168.11.50 #客户端指定心跳包中本机的 IP
jar包启动的方式
java -Dproject.name=sentinel-name -Dcsp.sentinel.heartbeat.client.ip=192.168.11.50 -Dcsp.sentinel.api.port=8869 -Dcsp.sentinel.dashboard.server=192.168.11.47:8858 -jar sentinel-demo.jar
在代码中设置的方式
System.setProperty("project.name", "sentinel-name");
System.setProperty("csp.sentinel.heartbeat.client.ip", "192.168.11.50");
System.setProperty("csp.sentinel.api.port", "8869");
System.setProperty("csp.sentinel.dashboard.server", "192.168.11.47:8858");
idea配置的方式
pom的配置有两种方式,spring-cloud-starter-alibaba-sentinel包含sentinel全部的core和annotation等之类的依赖
<?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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.11</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sentinel-demo</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2021.0.4.0</version>
</dependency>
</dependencies>
</project>
pom.xml:简约包
<?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>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.11</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>sentinel-demo</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--sentinel核心库-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.8.5</version>
</dependency>
<!--如果要使用注解@SentinelResource-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-annotation-aspectj</artifactId>
<version>1.8.5</version>
</dependency>
<!--客户端接入控制台-->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
<version>1.8.5</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
</dependencies>
</project>
使用编写代码的方式实现(流控降级)熔断,不使用dashboard,这是流控规则,保存在代码中
package com.test.order.controller;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
/**
* @Description:
* @Author: xu
* @Data: 2024-2024/3/29-16
* @Version: V1.0
*/
@RestController
@RequestMapping("/order")
public class OrderController {
private final static String resourceName = "flow";
private static Logger log = LoggerFactory.getLogger(OrderController.class);
@RequestMapping("/flow")
public String flow() {
System.out.println("aaaaaaaaaaaaaaaaa");
Entry entry = null;
//务必保证 finally 会被执行
try {
// 资源名可使用任意有业务语义的字符串开启资源的保护
entry = SphU.entry(resourceName);
//被保护的业务逻辑method
//do something...
} catch (BlockException ex) {
//资源访问阻止,被限流或被降级
//Sentinel定义异常 流控规则,降级规则,热点参数规则。。服务降级(降级规则)
//进行相应的处理操作
log.info("block!!!");
return "被流控了";
} catch (Exception ex) {
//若需要配置降级规则,需要通过这种方式记录业务异常RuntimeExceptionmock feign:fallback
Tracer.traceEntry(ex, entry);
} finally {
//务必保证 exit,务必保证每个 entry 与 exit 配对
if (entry != null) {
entry.exit();
}
}
return "flow";
}
@PostConstruct
private void initFlowQpsRule() {
List<FlowRule> rules = new ArrayList<>();
FlowRule rule = new FlowRule(resourceName);
// set limit qps to 20
rule.setCount(2);
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setLimitApp("default");
rules.add(rule);
FlowRuleManager.loadRules(rules);
}
}
可使用注解的方式简介编写@SentinelResource,使用@SentinelResource注意事项可以查看注解埋点官网
package com.test.order.controller;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.Tracer;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
/**
* @Description:
* @Author: xu
* @Data: 2024-2024/3/29-16
* @Version: V1.0
*/
@RestController
@RequestMapping("/order")
public class OrderController {
private final static String resourceName = "flow";
private static Logger log = LoggerFactory.getLogger(OrderController.class);
//blockHandler / blockHandlerClass: blockHandler 对应处理 BlockException 的函数名称,可选项。
// blockHandler 函数访问范围需要是 public,返回类型需要与原方法相匹配,参数类型需要和原方法相匹配并且最后加一个额外的参数,类型为 BlockException。
// blockHandler 函数默认需要和原方法在同一个类中。若希望使用其他类的函数,则可以指定 blockHandlerClass 为对应的类的 Class 对象,注意对应的函数必需为 static 函数,否则无法解析
//fallback / fallbackClass:fallback 函数名称,可选项,用于在抛出异常的时候提供 fallback 处理逻辑。
// fallback 函数可以针对所有类型的异常
//blockHandler的优先级比fallback高,官网中都有讲解 https://github.com/alibaba/Sentinel/wiki/%E6%B3%A8%E8%A7%A3%E6%94%AF%E6%8C%81
@RequestMapping("/flow")
@SentinelResource(value = resourceName, blockHandler = "exceptionHandler",fallback = "helloFallback")
public String flow() {
System.out.println("aaaaaaaaaaaaaaaaa");
//被保护的业务逻辑method
//do something...
return "flow";
}
public String exceptionHandler(BlockException ex) {
// Do some log here.
ex.printStackTrace();
return "被流控了";
}
// Fallback 函数,函数签名与原函数一致或加一个 Throwable 类型的参数.
public String helloFallback(long s) {
return String.format("Halooooo %d", s);
}
@PostConstruct
private void initFlowQpsRule() {
List<FlowRule> rules = new ArrayList<>();
FlowRule rule = new FlowRule(resourceName);
// set limit qps to 20
rule.setCount(2);
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule.setLimitApp("default");
rules.add(rule);
FlowRuleManager.loadRules(rules);
}
}
这是降级规则,保存在代码中
@PostConstruct
private void initDegradeRule() {
List<DegradeRule> rules = new ArrayList<>();
DegradeRule rule = new DegradeRule();
rule.setResource(resourceName);
// set threshold RT, 10 ms
rule.setCount(10);
rule.setGrade(RuleConstant.DEGRADE_GRADE_RT);
rule.setTimeWindow(10);
rules.add(rule);
DegradeRuleManager.loadRules(rules);
}
@RequestMapping("/degrade")
@SentinelResource(value = resourceName, entryType = EntryType.IN, blockHandler = "exceptionHandler2", fallback = "helloFallback2")
public String degrade() {
throw new RuntimeException("异常");
}
其他的规则都是类似的原理可以查看官网使用
整合后可以在界面查看
如果项目是springcloud项目,可以直接在yml配置
server:
port: 8079
spring:
application:
name: order-sentinel
cloud:
sentinel:
transport:
dashboard: 192.168.11.47:8858
#注意:yml配置client-ip 是本地ip才行
client-ip: 192.168.11.50
port: 8719
pom.xml依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>