您的位置:首页 > 汽车 > 新车 > sentinel网关限流配置及使用

sentinel网关限流配置及使用

2024/9/20 14:56:12 来源:https://blog.csdn.net/yixin605691235/article/details/140442655  浏览:    关键词:sentinel网关限流配置及使用

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、redirectmode: response# 响应状态码response-status: ${sentinel.renturn.code}# 响应信息response-body: ${sentinel.renturn.msg}transport:dashboard: ${sentinel.dashboard.host}:${sentinel.dashboard.port}filter:enabled: falseip: 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.jsondata-type: jsonrule-type: gw-flowds2: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.jsondata-type: jsonrule-type: gw-api-groupgateway:# 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、网关应用增加依赖:

<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-datasource-nacos</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency><dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-sentinel-gateway</artifactId></dependency><dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-spring-cloud-gateway-adapter</artifactId></dependency>

3、控制台配置限流规则

http://localhost:8081/#/login

帐号/密码:sentinel/sentinel

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

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

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

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

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

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

二、资源和规则配置说明

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

1、资源定义

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

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

注解需要增加一个依赖

<dependency><groupId>com.alibaba.csp</groupId><artifactId>sentinel-core</artifactId><version>1.8.6</version>
</dependency>
@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<FlowRule> 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);}

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com