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

相关推荐
黄焖鸡能干四碗4 分钟前
MES生产执行制造系统建设(Java+Mysql)
java·大数据·开发语言·信息可视化·需求分析
舒克日记12 分钟前
基于springboot的民谣网站的设计与实现
java·spring boot·后端
Ken_11151 小时前
SpringCloud系列(52)--SpringCloud Sleuth简介
spring cloud
凤山老林2 小时前
新一代Java应用日志可视化与监控系统开源啦
java·后端·开源
流星白龙2 小时前
【Qt】7.信号和槽_connect函数用法(2)
java·数据库·qt
黎宇幻生6 小时前
Java全栈学习笔记39
java·笔记·学习
Aurorar0rua7 小时前
C Primer Plus Notes 09
java·c语言·算法
nongcunqq8 小时前
abap 操作 excel
java·数据库·excel
史迪奇_xxx9 小时前
10、一个简易 vector:C++ 模板与 STL
java·开发语言·c++
2301_801252229 小时前
Java中的反射
java·开发语言