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

相关推荐
廋到被风吹走1 分钟前
【Java】【Jdk】Jdk17->Jdk21
java·开发语言
叁散1 小时前
实验一:船舶位置感知与MMSI数据分析
java
研☆香1 小时前
什么是对象 什么是数组 区别是什么??
java·前端·javascript
利刃大大1 小时前
【SpringBoot】搭建Java部署环境 && 部署项目到Linux服务器
java·服务器·spring boot
zhaokuner1 小时前
04-实体与标识-DDD领域驱动设计
java·开发语言·设计模式·架构
Zaralike1 小时前
程序错误处理
java·开发语言
有一个好名字1 小时前
力扣:除自身以外数组的乘积
java·算法·leetcode
cike_y1 小时前
Spring的配置&各种依赖注入
java·开发语言·后端·spring
椰果子1 小时前
Nacos 2.x.x版本不适用JDK17的处理方式
java·spring boot·后端
Wang15302 小时前
Java网络IO模型
java