# 流控
# 简介
流控规则是用于完成服务流控的。服务流控即对访问流量的控制,也称为服务限流。
Sentinel实现流控的原理是监控应用流量的QS或并发线程数等指标,当达到指定的阈值时对再到来的请求进行进行控制,以避免被瞬时的流量高峰冲垮,从而保障应用的高可用性。
# 在流控规则上设置单机阈值为2,表示并并发出超过2的时候,则开启流控
当方法注解标注了 blockHandler ,如果没有在入参上BlockException,那么也不会进入方法
@RequestMapping("/sen")
@RestController
public class ProviderController {
@GetMapping
@SentinelResource(blockHandler = "blockErrorMSg")
public String list() {
return "sentinel list ";
}
public String blockErrorMSg(BlockException blockException) {
return "blockErrorMSg" + blockException.getMessage();
}
}
# API 方法
设置 setResource ,在那个方法上 标注@SentinelResource 中,要指定 value 不然无效
- 启动类
@SpringBootApplication
public class Sentinel10000Application {
public static void main(String[] args) {
SpringApplication.run(Sentinel10000Application.class, args);
initFlowRule();
}
public static void initFlowRule() {
List<FlowRule> rules = new ArrayList<>();
rules.add(getConfig());
FlowRuleManager.loadRules(rules);
}
public static FlowRule getConfig() {
FlowRule flowRule = new FlowRule();
//哪个方法
flowRule.setResource("list");
//指定规则类型
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
//指定 QPS
flowRule.setCount(3);
//来源
flowRule.setLimitApp("default");
return flowRule;
}
}
- 流控的方法
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.gao.sentinel.entity.Depart;
import com.gao.sentinel.feign.DepartFeignService;
import jakarta.annotation.security.DenyAll;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/sen")
@RestController
public class ProviderController {
@GetMapping
@SentinelResource(value = "list", blockHandler = "blockErrorMSg")
public String list() {
return "sentinel list ";
}
public String blockErrorMSg(BlockException blockException) {
return "blockErrorMSg" + blockException.getMessage();
}
}
# 使用资源实体方式
好处就是当自己使用了规则之外,还想使用别人的规则,就可以使用资源实体方式
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.gao.sentinel.entity.Depart;
import com.gao.sentinel.feign.DepartFeignService;
import jakarta.annotation.security.DenyAll;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RequestMapping("/sen")
@RestController
public class ProviderController {
@GetMapping
public String list() {
Entry list = null;
try {
list = SphU.entry("list");
return "sentinel list ";
} catch (BlockException e) {
return "entry error";
} finally {
if (list != null) {
list.exit();
}
}
}
}
← 自定义 分布式事务 Seata →