Sentinel整合Gateway

  1. pom引入依赖

    text 复制代码
    <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>
  2. 添加配置

    properties 复制代码
    spring.cloud.sentinel.transport.dashboard=localhost:8858
    spring.cloud.sentinel.transport.port=8749
    spring.cloud.sentinel.filter.enabled=false
  3. 硬编码限流规则

    java 复制代码
    @Configuration
    public class SentinelConfig {
        @PostConstruct
        public void init(){
            this.initCustomizeRule();
        }
    
        private void initCustomizeRule(){
            Set<GatewayFlowRule> list = new HashSet<>() ;
            GatewayFlowRule rule = new GatewayFlowRule("hello-nacos-client") ;
            rule.setResourceMode(SentinelGatewayConstants.RESOURCE_MODE_ROUTE_ID) ;
            // qps: 1
            rule.setGrade(1) ;
            rule.setCount(1) ;
            rule.setIntervalSec(1) ;
            rule.setControlBehavior(1) ;
            list.add(rule) ;
            GatewayRuleManager.loadRules(list) ;
        }
    }
  4. 配置网关路由规则

    properties 复制代码
    spring.cloud.gateway.enabled=true
    spring.cloud.gateway.discovery.locator.lower-case-service-id=true
    spring.cloud.gateway.routes[0].id=hello-nacos-client
    spring.cloud.gateway.routes[0].uri=lb://hello-nacos-client
    spring.cloud.gateway.routes[0].predicates[0]=Path=/hello-nacos/**
     spring.cloud.gateway.routes[0].filters[0]=StripPrefix=1
  5. 浏览器访问:http://localhost:8080/hello-nacos/hello/index

相关推荐
yuren_xia2 分钟前
Spring MVC执行流程简介
java·spring·mvc
黄雪超26 分钟前
JVM——对象模型:JVM对象的内部机制和存在方式是怎样的?
java·开发语言·jvm
凌冰_26 分钟前
Tomcat 安装和配置
java·tomcat
一只叫煤球的猫31 分钟前
虚拟线程生产事故复盘:警惕高性能背后的陷阱
java·后端·性能优化
是烟花哈1 小时前
IDEA中的debug使用技巧
java·ide·intellij-idea
cui_hao_nan2 小时前
Prompt‏ 工程和优化技巧
java·prompt
还是鼠鼠2 小时前
HTTP 请求协议简单介绍
java·开发语言·网络·网络协议·http
浮游本尊2 小时前
Java学习第1天 - 完整版基础语法
java
on the way 1233 小时前
行为设计模式之Command (命令)
java·开发语言·设计模式
qqxhb3 小时前
零基础设计模式——行为型模式 - 责任链模式
java·设计模式·责任链模式