首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >sentinel在controller上限流

sentinel在controller上限流

作者头像
johnhuster的分享
发布于 2022-03-28 11:59:45
发布于 2022-03-28 11:59:45
82100
代码可运行
举报
文章被收录于专栏:johnhusterjohnhuster
运行总次数:0
代码可运行

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

1.添加三个依赖

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
    <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类

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
@Configuration
 public class AspectConfiguration {
 
     @Bean
     public SentinelResourceAspect sentinelResourceAspect() {
        return new SentinelResourceAspect();
     }    
 }

3.在application.proerties增加配置

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#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

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/06/05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档