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

相关推荐
brave_zhao9 分钟前
关于javafx下打开postman无法正常关闭postman的问题
java·测试工具·postman
攻心的子乐13 分钟前
Spring IOC 源码
java·后端·spring
Sirens.13 分钟前
Java异常处理解析:从防御式编程到自定义异常类
java·开发语言·笔记·学习·github·javac
千寻技术帮24 分钟前
10351_基于Springboot的二手交易平台
java·spring boot·mysql·毕业设计·源码·代码·二手交易
alonewolf_9934 分钟前
Spring依赖注入源码深度解析:从@Autowired到@Resource的完整实现机制
java·后端·spring
雪碧聊技术40 分钟前
如何界定人工智能和java开发二者的关系?
java·人工智能·二者关系界定
Chase_______42 分钟前
【JAVA基础指南(四)】快速掌握类和对象
java·开发语言
muxin-始终如一1 小时前
Maven HTTP 仓库被阻止问题解决总结
java·http·maven
武斌1 小时前
需要独立的作业队列?看看Quartz增强框架Quartz Plus
java·spring boot·后端
廋到被风吹走1 小时前
【Java】【JVM】即时编译解析:C1/C2、分层编译、OSR与日志分析
java·开发语言·jvm