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

相关推荐
小光学长几秒前
基于ssm的宠物交易系统的设计与实现850mb48h(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
java·前端·数据库
编程大师哥5 分钟前
vxe-table 透视表分组汇总及排序基础配置
java
8***848218 分钟前
spring security 超详细使用教程(接入springboot、前后端分离)
java·spring boot·spring
9***J62821 分钟前
Spring Boot项目集成Redisson 原始依赖与 Spring Boot Starter 的流程
java·spring boot·后端
M***Z21032 分钟前
SQL 建表语句详解
java·数据库·sql
v***79432 分钟前
Spring Boot 热部署
java·spring boot·后端
执笔论英雄33 分钟前
【RL】python协程
java·网络·人工智能·python·设计模式
galaxyffang44 分钟前
认证、会话管理、授权的区别
java
未名编程1 小时前
Windows 下如何部署 Nacos 并导入配置文件
java·windows
boonya1 小时前
Java中Plugin设计模式的规范应用
java·spring·设计模式·插件模式