sentinel网关限流配置及使用
创始人
2025-01-10 23:05:59
0

sentinel控制台源码:https://download.csdn.net/download/yixin605691235/89543923

sentinel控制台jar包:https://download.csdn.net/download/yixin605691235/89543931

不同环境直接修改jar包中的application.yml文件中的nacos地址就可以了。

一、网关限流配置及使用

1、nacos配置

新增文件gdebs-gateway-sentinel.yml、gdebs-gateway-sentinel-dynamic.properties

spring:   cloud:     sentinel:       eager: true       #配置网关       scg:         fallback:           content-type: application/json           # 模式 response、redirect           mode: response           # 响应状态码           response-status: ${sentinel.renturn.code}           # 响应信息           response-body: ${sentinel.renturn.msg}       transport:         dashboard: ${sentinel.dashboard.host}:${sentinel.dashboard.port}         filter:           enabled: false         ip: gdebs-gateway-service       # 控制台数据持久化配置       datasource:         ds1:           nacos:             server-addr: ${sentinel.nacos.host}:${sentinel.nacos.port}             username: ${sentinel.nacos.name}             password: ${sentinel.nacos.pwd}             namespace: ${sentinel.nacos.namespace}             group-id: ${sentinel.nacos.group}             data-id: ${spring.application.name}-sentinel-flow-rules.json             data-type: json             rule-type: gw-flow         ds2:           nacos:             server-addr: ${sentinel.nacos.host}:${sentinel.nacos.port}             username: ${sentinel.nacos.name}             password: ${sentinel.nacos.pwd}             namespace: ${sentinel.nacos.namespace}             group-id: ${sentinel.nacos.group}             data-id: ${spring.application.name}-sentinel-api-rules.json             data-type: json             rule-type: gw-api-group     gateway:       # spring cloud gateway 路由配置方式       discovery:         locator:           #表明gateway开启服务注册和发现的功能           enabled: true           #将请求路径上的服务名配置为小写           lower-case-service-id: true 
# sentinel控制台信息 sentinel.dashboard.host=xxxx sentinel.dashboard.port=xxx  # sentinel返回信息 sentinel.renturn.msg= 对不起,已经被限流了!!! sentinel.renturn.code= xxx  # sentinel数据持久化配置 sentinel.nacos.host=xxx.xx.xx.xx sentinel.nacos.port=xxxx sentinel.nacos.name=xx sentinel.nacos.pwd=nacxxos sentinel.nacos.namespace=xxx sentinel.nacos.group=xxx spring.application.name=xxx 

2、网关应用增加依赖:

             com.alibaba.csp             sentinel-datasource-nacos                               com.alibaba.cloud             spring-cloud-starter-alibaba-sentinel                               com.alibaba.cloud             spring-cloud-alibaba-sentinel-gateway                               com.alibaba.csp             sentinel-spring-cloud-gateway-adapter          

3、控制台配置限流规则

http://localhost:8081/

帐号/密码:sentinel/sentinel

此处注意:sentinel控制台采用懒加载,因此需要现有请求才能看到链路

API管理:可以根据请求路径来设置API组

设置网关可以按照API分组设置,也可以设置app应用的总体限流

流控方式有快速失败和匀速排队两种:

快速失败可以设置Burst size,这是一个突刺个数,如果QPS设置为1,突刺数设置为10 ,当高并发请求时,首先能消耗的请求数是11个,超过11则拒绝,后续还是可通过一个请求。

匀速排队可以设置等待时长毫秒,当高并发请求超过QPS时,进入等待状态,如果超过等待时长则拒绝。

二、资源和规则配置说明

官网详细说明:basic-api-resource-rule | Sentinel

1、资源定义

主流框架默认适配:系统当前使用的springcloud本身支持请求链路

另外一种方式通过注解引用的方式:@SentinelResource

注解需要增加一个依赖

     com.alibaba.csp     sentinel-core     1.8.6  
@RestController @RequestMapping(value = "/sentinel") @Api(value = "sentinel", tags = {"testController"}) public class TestController {     @SentinelResource(value = "tesSentinel" , blockHandler = "myBlockHander")     @PostMapping("/tesSentinel")     public String tesSentinel() {return "1";}      // 限流或者熔断之后执行的方法     public String myBlockHander(BlockException blockException){         if(blockException instanceof FlowException){             // 限流异常             return "您被限流了";         }else if(blockException instanceof DegradeException){             // 熔断异常             return "您被熔断了";         }         return "被限制了";     } } 

2、规则定义

可以通过代码设置规则,也可以通过控制台设置规则

private void initFlowQpsRule() {          List rules=new ArrayList<>();         System.out.println("限流初始化规则");         // 定义一个限流规则         FlowRule flowRule=new FlowRule();         flowRule.setResource("tesSentinel"); // 资源名|必须参数         flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS); // 限流指标:QPS/线程数 |必须参数         flowRule.setCount(1);  // 限流数量(上一步 QPS 或线程数的值) |必须参数         flowRule.setStrategy(RuleConstant.STRATEGY_DIRECT); //调用关系限流策略【非必须设置】         flowRule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER); // 流控效果【非必须设置】 排队等待         flowRule.setMaxQueueingTimeMs(1000); // 等待超时时间         flowRule.setClusterMode(false); // 是否集群限流【非必须设置,默认非集群】         rules.add(flowRule);         FlowRuleManager.loadRules(rules);       } 

提供的关联流控和链路流控可以应用到一些特殊的业务场景的流控

上一篇:threejs

下一篇:LeetCode热题100(JavaScript)

相关内容

热门资讯

透视讲解!wepoker辅助软... 透视讲解!wepoker辅助软件价格(透视)wepoker能不能透视(切实真的有挂)wepoker辅...
透视计算!德州透视脚本(透视)... 透视计算!德州透视脚本(透视)黑科技教程(2020已更新)(哔哩哔哩)1、用户打开应用后不用登录就可...
透视苹果版!wepoker透视... 透视苹果版!wepoker透视脚本(透视)辅助是真的假的(原来是有挂)1、点击下载安装,wepoke...
透视插件!wepokerplu... 透视插件!wepokerplus脚本(透视)wepoker是不是有人用挂(都是有挂)1、wepoke...
透视挂!pokermaster... 透视挂!pokermaster脚本(透视)必胜教程(2022已更新)(哔哩哔哩);1、pokerma...
透视ai代打!wepoker透... 透视ai代打!wepoker透视脚本免费使用视频(透视)辅助透视软件(一直真的是有挂)1、首先打开w...
透视脚本!WePoKer辅助器... 透视脚本!WePoKer辅助器(透视)wepoker脚本下载(竟然是有挂)1、下载好wepoker脚...
透视好友!wepoker有没有... 透视好友!wepoker有没有机器人(透视)有机器人(都是有挂)1、让任何用户在无需wepoker有...
透视存在!哈糖大菠萝有挂吗(透... 透视存在!哈糖大菠萝有挂吗(透视)可靠技巧(2023已更新)(哔哩哔哩);哈糖大菠萝有挂吗辅助器中分...
透视ai!wepokerplu... 透视ai!wepokerplus到底是挂了吗(透视)wepoker私人局透视教程(总是存在有挂);1...