当前位置: 首页 > news >正文

南阳专业做网站厦门网站设计开发网页公司

南阳专业做网站,厦门网站设计开发网页公司,又一个wordpress博客,建筑模板生产厂家Gateway整合Sentinel ​ 前面使用过Sentinel组件对服务提供者、服务消费者进行流控、限流等操作。除此之外#xff0c;Sentinel还支持对Gateway、Zuul等主流网关进行限流。 ​ 自sentinel1.6.0版开始#xff0c;Sentinel提供了Gateway的适配模块#xff0c;能针对路由(rou…Gateway整合Sentinel ​ 前面使用过Sentinel组件对服务提供者、服务消费者进行流控、限流等操作。除此之外Sentinel还支持对Gateway、Zuul等主流网关进行限流。 ​ 自sentinel1.6.0版开始Sentinel提供了Gateway的适配模块能针对路由(route)和自定义API分组两个维度进行限流。 路由维度 路由维度是指配置文件中的路由条目资源名是对应的routeId相比自定义API维度这是比较粗粒度的。看下如何实现 导入Sentinel组件为Gateway提供的适配依赖包在pom.xml中导入依赖 !--sentinel为gateway提供的适配包-- dependencygroupIdcom.alibaba.csp/groupIdartifactIdsentinel-spring-cloud-gateway-adapter/artifactId /dependency增加配置类SentinelRouteConfiguration实例化SentinelGatewayFilter和SentinelBlockExceptionHandler对象初始化限流规则 Configuration // 配置类 public class SentinelRouteConfiguration { // 路由限流规则配置类private final ListViewResolver viewResolvers;private final ServerCodecConfigurer serverCodecConfigurer;public SentinelRouteConfiguration(ObjectProviderListViewResolver viewResolversProvider, ServerCodecConfigurer serverCodecConfigurer) {this.viewResolvers viewResolversProvider.getIfAvailable(Collections::emptyList);this.serverCodecConfigurer serverCodecConfigurer;}PostConstructpublic void initGatewayRules() { // 初始化限流规则SetGatewayFlowRule rules new HashSet();GatewayFlowRule gatewayFlowRule new GatewayFlowRule(user_route); // 资源名(gateway中的路由id)gatewayFlowRule.setCount(1); // 限流阈值gatewayFlowRule.setIntervalSec(1); // 统计时间窗口默认1srules.add(gatewayFlowRule);GatewayRuleManager.loadRules(rules); // 载入规则}PostConstructpublic void initBlockHandlers() { // 限流后的响应BlockRequestHandler blockRequestHandler (serverWebExchange, throwable) - {MapString, Object result new HashMap();result.put(code, 0);result.put(message, 您已被限流);return ServerResponse.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON_UTF8).body(BodyInserters.fromObject(result));};GatewayCallbackManager.setBlockHandler(blockRequestHandler); // 设置限流响应}BeanOrder(Ordered.HIGHEST_PRECEDENCE)public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);}BeanOrder(Ordered.HIGHEST_PRECEDENCE)public GlobalFilter sentinelGatewayFilter() { // 初始化限流过滤器return new SentinelGatewayFilter();}}注意Gateway限流是通过Filter实现的主要是注入SentinelGatewayFilter实例和SentinelGatewayBlockExceptionHandler实例 在yml中设置两个routeuser_route和shop_route上面主要是对user_route限流了着重看下 server:port: 8083spring:application:name: gateway # 服务名cloud:nacos:discovery:server-addr: localhost:8848 # nacos地址gateway:routes: # 路由可配置多个- id: user_route # 路由id唯一即可默认UUIDuri: lb://user # 路由地址(匹配成功后的服务地址) user是用户服务的服务名称order: 1 # 路由优先级默认0越低优先级越高predicates:- Path/user/** # 断言匹配规则- id: shop_route # 路由id唯一即可默认UUIDuri: lb://shop # 路由地址(匹配成功后的服务地址) shop是用户服务的服务名称order: 1 # 路由优先级默认0越低优先级越高predicates:- Path/shop/** # 断言匹配规则启动服务开始调试 成功完成路由级别的限流那么后面看看API维度的限流 自定义API维度 ​ 上面那种限流方式可以看出灵活性不够高。自定义的API维度可以利用Sentinel提供的API自定义分组来进行限流。相比路由维度这是一种更加细粒度的限流方式。 实现 导入Gateway的适配包 !--sentinel为gateway提供的适配包-- dependencygroupIdcom.alibaba.csp/groupIdartifactIdsentinel-spring-cloud-gateway-adapter/artifactId /dependency依然是实例化SentinelGatewayFilter和SentinelBlockExceptionHandler对象初始化限流规则定义API分组 Configuration // 配置类 public class SentinelRouteConfiguration { // 路由限流规则配置类private final ListViewResolver viewResolvers;private final ServerCodecConfigurer serverCodecConfigurer;public SentinelRouteConfiguration(ObjectProviderListViewResolver viewResolversProvider, ServerCodecConfigurer serverCodecConfigurer) {this.viewResolvers viewResolversProvider.getIfAvailable(Collections::emptyList);this.serverCodecConfigurer serverCodecConfigurer;}PostConstructpublic void initGatewayRules() { // 初始化限流规则SetGatewayFlowRule rules new HashSet();GatewayFlowRule gatewayFlowRule new GatewayFlowRule(user_api); // 资源名,api分组的名称(自定义)gatewayFlowRule.setCount(1); // 限流阈值gatewayFlowRule.setIntervalSec(1); // 统计时间窗口默认1srules.add(gatewayFlowRule);GatewayRuleManager.loadRules(rules); // 载入规则}PostConstructpublic void initCustomizedApis() {SetApiDefinition apiDefinitions new HashSet();ApiDefinition apiDefinition new ApiDefinition(user_api) // 定义 api分组.setPredicateItems(new HashSetApiPredicateItem() {{add(new ApiPathPredicateItem().setPattern(/user/group/**) // 路径匹配规则.setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX)); // 匹配策略前缀匹配}});apiDefinitions.add(apiDefinition);GatewayApiDefinitionManager.loadApiDefinitions(apiDefinitions); // 载入API分组定义}PostConstructpublic void initBlockHandlers() { // 限流后的响应BlockRequestHandler blockRequestHandler (serverWebExchange, throwable) - {MapString, Object result new HashMap();result.put(code, 0);result.put(message, 您已被限流);return ServerResponse.status(HttpStatus.OK).contentType(MediaType.APPLICATION_JSON_UTF8).body(BodyInserters.fromObject(result));};GatewayCallbackManager.setBlockHandler(blockRequestHandler); // 设置限流响应}BeanOrder(Ordered.HIGHEST_PRECEDENCE)public SentinelGatewayBlockExceptionHandler sentinelGatewayBlockExceptionHandler() {return new SentinelGatewayBlockExceptionHandler(viewResolvers, serverCodecConfigurer);}BeanOrder(Ordered.HIGHEST_PRECEDENCE)public GlobalFilter sentinelGatewayFilter() { // 初始化限流过滤器return new SentinelGatewayFilter();}}唯一要注意的是路由匹配规则如果是单一的一个具体接口不是匹配符那么后面的匹配策略就没有必要再去配置了(setMatchStrategy()方法) 定义一个/user/group/findById接口 RequestMapping(/user/group/findById) public String findGById(RequestParam(id) Integer id) {return userInfo.getOrDefault(id, null); }启动调用测试 可以看到配置的没有问题满足/user/group/**规则的请求有被限流到 超时配置 Gateway默认没有超时的限制也就是数据什么时候返回就等待到什么时候。如果不想等待可以增加对超时的配置 gateway:httpclient:connect-timeout: 5000 # 建立连接时间限制单位毫秒response-timeout: 4s # 响应时间的时间限制尝试下接口睡眠5s再次调用 curl localhost:8083/user/group/findById?id1 {timestamp:2023-09-02T00:59:47.18400:00,path:/user/group/findById,status:504,error:Gateway Timeout,requestId:7f5ff558-1}被告知504超时了~ CORS配置 涉及到前后端分离的跨域请求时浏览器访问后端地址通常提示No Access-Control-Allow-Origin header is present on the requested resource 可以在gateway中增加跨域配置或者前端去配置都可以自行协商。 cloud:nacos:discovery:server-addr: localhost:8848 # nacos地址gateway:httpclient:connect-timeout: 5000 # 建立连接时间限制单位毫秒response-timeout: 4s # 响应时间的时间限制globalcors:cors-configurations: [/**]:allowedOrigins: * # 允许的来源allowedMethods: * # 允许的方法allowedHeaders: * # 允许的请求头 *表示所有通常情况下也可以不是按照全部允许来做按照你的项目实际开发需求搞就行了。
http://www.w-s-a.com/news/556197/

相关文章:

  • 百度网站推广申请深圳公众号制作
  • 百度站长怎么做网站维护中国深圳航空公司官网
  • xampp安装网站模板海南一家天涯社区
  • 网站建设 管理系统开发仿租号网站源码网站开发
  • 怎么自己弄网站免费网站设计用什么软件
  • 网站分几种access做网站数据方法
  • 网站默认图片s001网站建设公司
  • 淘宝的电子商务网站的建设东莞哪里有网站制作公司
  • 西安网站制作怎么联系wordpress登陆界面打开慢
  • 高端工作网站网站推广seo代理
  • 一般找素材都是做哪几个网站呢推广引流工具
  • 必须做网站等级保护html网页设计题库
  • 移动端网站开发 float手机在线建网站
  • 教育网站模板下载做汽车网站开题报告的意义
  • 网站首页做后台链接昌平网站制作
  • 营销型门户网站建设浏览器下载免费大全
  • 快三网站开发推广普通话手抄报内容50字
  • 沈阳专业做网站开发公司asp网站搭建教程
  • 网站建设代码福州小程序开发平台
  • 了解做房产广告的网站手机版官方网站的建设
  • 如何与别的网站做友情链接做网站排名大概要多少钱
  • 东莞市锂电池网站建设HTML5怎么做自适应网站
  • 江苏城乡建设学校网站群晖建立wordpress
  • wordpress导入网站模板seo自学网官网
  • 购物网站服务器带宽北京网站开发周期
  • 同性做视频网站网站怎么添加栏目
  • 新余网站设计seo自学网站
  • 新乡个人网站建设价格wordpress数据插件
  • 你是网站设计有限公司的项目经理网站推广的重要性
  • 网站定制开发怎么写泸州设计公司有哪些