springcloud Alibaba中gateway和sentinel联合使用

这篇具有很好参考价值的文章主要介绍了springcloud Alibaba中gateway和sentinel联合使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

看到这个文章相信你有一定的sentinel和gateway基础了吧。
官网的gateway和sentinel联合使用有些过时了,于是有了这个哈哈,给你看看官网的:
springcloud Alibaba中gateway和sentinel联合使用,springcloud alibaba,spring cloud,gateway,sentinel
才sentinel1.6,现在都几了啊,所以有些过时。
下面开始讲解:
首先我们总的回顾一下,sentinel就是需要运行一个jar包,开启dashbord页面,来显示流控信息。
然后运行我们的程序,调用需要的接口,该接口就会在dashbord上面显示了对吧。
但是啊,gateway和sentinel进行配合之后,两个dashbord内容是不同的:
下面我们来对比一下:
这个是gateway和sentinel配合之后的控制台:
springcloud Alibaba中gateway和sentinel联合使用,springcloud alibaba,spring cloud,gateway,sentinel
这个是配合之前的控制台:
springcloud Alibaba中gateway和sentinel联合使用,springcloud alibaba,spring cloud,gateway,sentinel
可以看到有明显的差别。。。

回忆到这里,我们在浏览器输入接口名称,调用接口的时候,如果接口调用错误就直接显示404啊,而且呀控制台也会报错:
'org.springframework.web.reactive.function.server.ServerResponse$BodyBuilder org.springframework. web.reactive.function.server.ServerResponse.status(org.springframework.http.HttpStatus)'
那现在我不想让他在控制台报错,不然就影响的对bug的判断了。我想让他按照我的想法来显示指定的页面或者指定的字符串该怎么做?
下面有具体几种做法:
1.文件配置添加scg:

server:
  port: 7009

spring:
  application:
    name: gateway-sentinel-name
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
        password: nacos
        username: nacos
    gateway:
      routes:
        - id: gateway_sentinel
          uri: lb://consume-name1
          predicates:
            - Path=/consume/**   #**/
          filters:
            - StripPrefix=1
    sentinel:
      transport:
        port: 8719
        dashboard: localhost:8080
      eager: true
      filter:
        enabled: false
=====================================================================================
#此时如果你在sentinel进行限流每秒两次。如果超过两次就会进行报错:
    'org.springframework.web.reactive.function.server.ServerResponse$BodyBuilder org.springframework.
    web.reactive.function.server.ServerResponse.status(org.springframework.http.HttpStatus)'并且加载不出数据
#上面的scg的配置可以解决这个异常哦!!!
#      scg: 
#        fallback:
#          mode: redirect #这个redirect是直接跳转新的地址
#          redirect: https://www.baidu.com
#下面这个可以自定义异常处理方式::[下面这个处理就是自定义的了,你想给前端返回啥就在这里定义就好]
#      scg: #这个是解决超过流控限制的时候进行什么操作(添加这个后,就算超过流控了,也不会正在控制套打印啥错误了,而是直接跳转指定页面!!!非常的方便的!!)
#        fallback:
#          mode: response
#          response-status: 429
#          response-body: '{"status":429,"msg":"请求过于频繁"}'
#          content-type: "application/json"

2.API方式,就是写的代码,不是配置文件配置了。

##嘿嘿还有其他处理方式:::编码处理方式:(使用Gateway API)
#  //        如果使用配置的方式就卸载配置文件中,如果使用编码方式就写在启动类里面!!!
##使用编码处理方式的时候。就是在网关回调的时候进行出来被拦截的请求!!!
#那网关回调是啥?官网可以看到哦,就是请求经过网关与两个过程,一个是经历网关过滤器,经过过滤器后会进行服务调用。调用后在返回给网关,网关在传给前端信息。
  @SpringBootApplication
  public class GatewaySentinelApplication {

  public static void main(String[] args) {
  SpringApplication.run(GatewaySentinelApplication.class, args);

  GatewayCallbackManager.setBlockHandler(new RedirectBlockRequestHandler("https://www.baidu.com"));

}
}

#编码处理方式(自定义)这个就是自定义的了,是不是和application.xml配置的自定义的很像呢!
  @SpringBootApplication
  public class GatewaySentinelApplication {

  public static void main(String[] args) {
  SpringApplication.run(GatewaySentinelApplication.class, args);

  GatewayCallbackManager.setBlockHandler(new BlockRequestHandler() {
  @Override
  public Mono<ServerResponse> handleRequest(ServerWebExchange exchange, Throwable t) {
  Map<String,Object> map1=new HashMap();
  map1.put("status",HttpStatus.TOO_MANY_REQUESTS);
  map1.put("msg","请求过于频繁");
  return ServerResponse.status(HttpStatus.TOO_MANY_REQUESTS)
  .contentType(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(map1));
  }
});
}
}
网关流控规则

springcloud Alibaba中gateway和sentinel联合使用,springcloud alibaba,spring cloud,gateway,sentinel

上面的这个具体的网关流控规则配置页面了。
你可以在针对参数属性里面选中Client IP ,然后选中属性值配置,在匹配传中添加127.0.0.1,你会发现,这次配置的流控规则只会对ip为127.0.0.1的ip有用。localhost都不会受这个流控规则的管制。
其他的参数属性也是如此。

我们可以清楚的看到上面的API类型是Route ID类型的。那要是我们有多个接口都想设置相同的流控规则呢?我们可以把API类型设置为API分组。

springcloud Alibaba中gateway和sentinel联合使用,springcloud alibaba,spring cloud,gateway,sentinel

设置成API分组前首先在API分组管理先设置匹配模式(就是在分组,就是说把哪些接口分组在一个起)。

springcloud Alibaba中gateway和sentinel联合使用,springcloud alibaba,spring cloud,gateway,sentinel
然后访问接口的时候就要按照流控规则了。比如我设置的是每秒只能访问一次,超过这个次数就返回我设置好的字符串了。
springcloud Alibaba中gateway和sentinel联合使用,springcloud alibaba,spring cloud,gateway,sentinel

api方式书写流网关限流规则

上面我们是通过dashbord方式进行限流的。这样可以直观的操作页面,但是重启程序就没了。接下来我们通过api方式书写在程序里吧。。
里面需要的各种参数可以看官方文档:github上的网关限流api

下面直接上代码:


/**
 * @ClassName GatewayFlowRuleConfig
 * @Description TODO
 * GatewayFlowRule : 专为 API 网关设计的流规则,支持对不同路由或自定义 API 分组进行流控。
 * 它还支持通过请求属性(例如 HTTP 标头、客户端 IP 和 URL 参数)进行流量控制。
 * @Author zyhh
 * @version: 1.0
 */
@Configuration
public class GatewayFlowRuleConfig {

    @PostConstruct
    public void initRules(){
        Set<GatewayFlowRule> rules=new HashSet();
        GatewayFlowRule rule=new GatewayFlowRule();
        rule.setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_ROUTE_ID);
        rule.setResource("zyhh");
//        对应阈值类型
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
//        对应QPS阈值
        rule.setCount(1);
//        对应间隔时间
        rule.setIntervalSec(1);
//        对应流控方式(默认的:快速失败)
        rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
//        对应流控方式的排队
//        rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER);
//        对应设置突发流量
        rule.setBurst(2);

//        针对属性来源
        GatewayParamFlowItem paramFlowItem=new GatewayParamFlowItem();
        paramFlowItem.setFieldName("token");
//        对应参数属性
        paramFlowItem.setParseStrategy(SentinelGatewayConstants.PARAM_PARSE_STRATEGY_CLIENT_IP);
//        对应匹配模式
        paramFlowItem.setMatchStrategy(SentinelGatewayConstants.PARAM_MATCH_STRATEGY_EXACT);
//        设置匹配模式
        paramFlowItem.setPattern("12345");

        rule.setParamItem(paramFlowItem);
        rules.add(rule);
        GatewayRuleManager.loadRules(rules);
    }
}

然后重启程序,再打开sentinel的dashbord发现结果如下:就给自己加上了。
springcloud Alibaba中gateway和sentinel联合使用,springcloud alibaba,spring cloud,gateway,sentinel
那如果我想使用代码的方式配置api分组呢?接下来贴上代码:
这个代码写在了springboot启动类里面,我本来是写在Configuration类里面的,运行之后sentinel没显示,于是就配置在springboot启动类里面了,就成功呢了。如果有小伙伴知道原因,可不可以在评论区指点一二,感激不尽。。
下面贴上我在springboot启动类运行成功的代码:


@SpringBootApplication
public class GatewaySentinelApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewaySentinelApplication.class, args);

        GatewayCallbackManager.setBlockHandler(new BlockRequestHandler() {
          
        initCustomizedApis();
        initRules();
    }
    private static void initCustomizedApis() {
        System.out.println("=====================================进api分组");
        Set<ApiDefinition> definitions = new HashSet<>();
//        对应完成了api分组
        ApiDefinition api1 = new ApiDefinition("zyhh_api")
                .setPredicateItems(new HashSet<ApiPredicateItem>() {{
                    add(new ApiPathPredicateItem().setPattern("/consume/get/1")
                            .setMatchStrategy(SentinelGatewayConstants.PARAM_MATCH_STRATEGY_EXACT));
                    add(new ApiPathPredicateItem().setPattern("/product/.*")
                            .setMatchStrategy(SentinelGatewayConstants.URL_MATCH_STRATEGY_REGEX));
                }});
        definitions.add(api1);
        GatewayApiDefinitionManager.loadApiDefinitions(definitions);
    }
    public static void initRules(){
        Set<GatewayFlowRule> rules=new HashSet();
//        ====接下来我们设置分组流控规则=======================
        GatewayFlowRule rule2=new GatewayFlowRule();
        rule2.setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME);
        rule2.setResource("zyhh_api");
        rule2.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rule2.setCount(2);
        rule2.setIntervalSec(1);
        rule2.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_DEFAULT);
        rule2.setBurst(2);
        rules.add(rule2);
//===============分组流控api结束=============================
        GatewayRuleManager.loadRules(rules);
    }
}

运行成功后sentinel在dashbord就显示这样了,就说明成功了:
springcloud Alibaba中gateway和sentinel联合使用,springcloud alibaba,spring cloud,gateway,sentinel
==================== over=============================文章来源地址https://www.toymoban.com/news/detail-798772.html

到了这里,关于springcloud Alibaba中gateway和sentinel联合使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • SpringCloud.04.熔断器Hystrix( Spring Cloud Alibaba 熔断(Sentinel))

    目录 熔断器概述 使用Sentinel工具 什么是Sentinel 微服务集成Sentinel 配置provider文件,在里面加入有关控制台的配置 实现一个接口的限流 基本概念 重要功能 Sentinel规则 流控规则 简单配置 配置流控模式 配置流控效果 降级规则 @SentinelResource的使用 Feign整合Sentinel 由于Hystrix已经停

    2024年01月19日
    浏览(27)
  • SpringCloud学习6(Spring Cloud Alibaba)断路器Sentinel熔断降级

    SpringCloud、SpringCloudAlibaba、SpringBoot版本选择。为了避免各种千奇百怪的bug,我们还是采用官方推荐的毕业版本。 修改tomcat配置最大线程数 引入测试依赖 编写测试代码 这里同时我们在浏览器去请求该地址,响应会变得很慢 测试结论:此时会发现由于thread接口囤积大量请求,

    2023年04月08日
    浏览(39)
  • SpringCloud Alibaba微服务-- Sentinel的使用(保姆级)

    随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从流量控制、熔断降级、系统负载保护等多个维度保护服务的稳定性。 1、sentinel的特征 丰富的应用场景 : Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景,例如秒杀(即突

    2024年02月16日
    浏览(26)
  • Java之SpringCloud Alibaba【七】【Spring Cloud微服务网关Gateway组件】

    Java之SpringCloud Alibaba【一】【Nacos一篇文章精通系列】 跳转 Java之SpringCloud Alibaba【二】【微服务调用组件Feign】 跳转 Java之SpringCloud Alibaba【三】【微服务Nacos-config配置中心】 跳转 Java之SpringCloud Alibaba【四】【微服务 Sentinel服务熔断】 跳转 Java之SpringCloud Alibaba【五】【微服务

    2024年02月06日
    浏览(41)
  • 【SpringCloud Alibaba】(六)使用 Sentinel 实现服务限流与容错

    今天,我们就使用 Sentinel 实现接口的限流,并使用 Feign 整合 Sentinel 实现服务容错的功能,让我们体验下微服务使用了服务容错功能的效果。 因为内容仅仅围绕着 SpringCloud Alibaba技术栈展开,所以,这里我们使用的服务容错组件是阿里开源的 Sentinel。 当然,能够实现服务容错

    2024年02月14日
    浏览(31)
  • springcloud-alibaba (04)Gateway与Nacos结合使用

    🎉欢迎来到这里,今天我将为大家介绍如何将Spring Cloud Gateway和Nacos结合使用,实现一个高效稳定的服务网关!在微服务架构中,API网关是必不可少的一部分,它提供了路由请求、负载均衡、安全认证和限流等功能。Spring Cloud Gateway是基于Spring Framework、Spring Boot和Project Reacto

    2024年02月15日
    浏览(25)
  • SpringCloud Alibaba - Sentinel 微服务保护解决雪崩问题、Hystrix 区别、安装及使用

    目录 一、Sentinel 1.1、背景:雪崩问题 1.2、雪崩问题的解决办法 1.2.1、超时处理 缺陷:为什么这里只是 “缓解” 雪崩问题,而不是百分之百解决了雪问题呢? 1.2.2、舱壁模式 缺陷:资源浪费 1.2.3、熔断降级 1.2.4、流量控制 误区:那是不是只用流量控制就可以,前面三种方法

    2024年02月07日
    浏览(21)
  • SpringCloud Alibaba-Sentinel

    Sentinel 主页 随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 是面向分布式服务架构的流量控制组件,主要以流量为切入点,从 限流、流量整形、熔断降级、系统负载保护、热点防护 等多个维度来帮助开发者保障微服务的稳定性。 Sentinel核心组件 核心库

    2024年02月10日
    浏览(24)
  • springcloud alibaba sentinel熔断降级

    随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 以流量为切入点,从 流量控制、熔断降级、系统负载保护 等多个维度保护服务的稳定性。 sentinel相当于hystrix的升级版,加入了web界面,能够实时在线的改变流量策略。 Sentinel 分为两个部分: 核心库(J

    2024年01月23日
    浏览(36)
  • 熔断、限流、降级 —— SpringCloud Alibaba Sentinel

    Sentinel 是阿里中间件团队开源的,面向分布式服务架构的高可用流量防护组件,主要以流量为切入点,从限流、流量整形、熔断降级、系统负载保护、热点防护等多个维度来帮助开发者保障微服务的稳定性 Sentinel 提供了两个服务组件: Sentinel 用来实现微服务系统中服务熔断

    2024年02月08日
    浏览(47)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包