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

相关推荐
Ahtacca17 分钟前
Redis 五大常用数据类型详解及 Java 客户端(RedisTemplate)操作实战
java·数据库·redis·学习·缓存
Coder_Boy_34 分钟前
基于SpringAI企业级智能教学考试平台视频辅助学习模块全业务闭环方案
人工智能·spring cloud
廋到被风吹走39 分钟前
【Spring】Spring Cache 深度解析
java·后端·spring
计算机毕设VX:Fegn08951 小时前
计算机毕业设计|基于springboot + vue个人博客系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·课程设计
2501_946675641 小时前
Flutter与OpenHarmony打卡轮播图组件
java·javascript·flutter
独自破碎E1 小时前
Spring Boot 3.x和2.x版本相比有哪些区别与改进?
java·spring boot·后端
坚持学习前端日记1 小时前
个人运营小网站的最佳策略
java·学习·程序人生·职场和发展·创业创新
幽络源小助理1 小时前
SpringBoot+Vue美食网站系统源码 | Java餐饮项目免费下载 – 幽络源
java·vue.js·spring boot
k***92161 小时前
C++:继承
java·数据库·c++
Coder_Boy_1 小时前
基于SpringAI企业级智能教学考试平台考试模块全业务闭环方案
java·人工智能·spring boot·aiops