sentinel在controller上限流

折腾了半天终于大功告成,现在把重要的地方记录下来,希望帮到有需要的开发者:

1.添加三个依赖

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>0.1.2.RELEASE</version>    
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

        <!-- alibaba-sentinel -->        
         <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-annotation-aspectj</artifactId>
        </dependency>        
         <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-transport-simple-http</artifactId>
        </dependency>

关于spring-cloud-alibaba-dependencies的版本一定要特别注意,springboot2.0以下建议用0.1.2.RELEASE等其他较低版本,否则启动会报错

2.增加一个configuration类

@Configuration
public class AspectConfiguration {
    
    @Bean
    public SentinelResourceAspect sentinelResourceAspect() {
       return new SentinelResourceAspect();
    }    
}

3.在application.proerties增加配置

#Sentinel 控制台地址
spring.cloud.sentinel.transport.dashboard=localhost:9010
#取消Sentinel控制台懒加载
spring.cloud.sentinel.eager=true
spring.cloud.sentinel.transport.heartbeat-interval-ms=2000    //该值设置影响到sentinel-dashboard的配置什么时候被更新到客户端
spring.cloud.sentinel.transport.client-ip=127.0.0.1

 

    public String blockHandler(HttpServletRequest req,BlockException ex) {
        //可以记录日志,返回任何你想返回的东西
        return "请放慢速度";
    } 

  @GetMapping("/device")
    @SentinelResource(value="restApi-device",blockHandler="blockHandler")
    public String getDevice(HttpServletRequest req)

 

4.运行sentinel-dashboard(https://github.com/alibaba/Sentinel/releases

java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar target/sentinel-dashboard.jar

运行效果图:

 

参考文章:

1.https://github.com/alibaba/Sentinel/tree/master/sentinel-dashboard

2.https://github.com/alibaba/Sentinel/wiki/%E6%8E%A7%E5%88%B6%E5%8F%B0#3-%E5%AE%A2%E6%88%B7%E7%AB%AF%E6%8E%A5%E5%85%A5%E6%8E%A7%E5%88%B6%E5%8F%B0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值