您当前的位置: 首页 >  童心同萌

Sentinel入门开发

童心同萌 发布时间:2022-07-25 19:32:53 ,浏览量:3

链接:https://pan.baidu.com/s/1ma9TX38TabEbGdOyDSobvQ?pwd=s9zu

  1. Sentinel控制台启动
    java -Dserver.port=8888 -Dcsp.sentinel.dashboard.server=localhost:8888 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.1.jar
  2.  Sentinel控制台访问http://localhost:8888 端口8888为启动命令设置的,默认用户名和密码均为sentinel563aa785e5c142179ef104e09aa8a4f4.png
  3.  添加父项目pom依赖
        
            UTF-8
            1.8
            1.8
            Hoxton.SR8
            2.3.2.RELEASE
            2.2.4.RELEASE
        
    
        
            
                
                    org.springframework.cloud
                    spring-cloud-dependencies
                    ${spring-cloud.version}
                    pom
                    import
                
                
                    org.springframework.boot
                    spring-boot-dependencies
                    ${springboot.version}
                    pom
                    import
                
                
                    com.alibaba.cloud
                    spring-cloud-alibaba-dependencies
                    ${springcloudalibaba.version}
                    pom
                    import
                
            
        
  4.  添加子项目pom依赖
        
            
                com.alibaba.cloud
                spring-cloud-starter-alibaba-sentinel
            
    
            
                org.springframework.boot
                spring-boot-starter-web
            
    
            
                com.alibaba.csp
                sentinel-core
            
    
            
                com.alibaba.csp
                sentinel-transport-simple-http
                1.8.0
            
    
            
                com.alibaba.csp
                sentinel-annotation-aspectj
                1.8.0
            
    
            
                com.alibaba.csp
                sentinel-parameter-flow-control
                1.4.0
            
    
            
                com.alibaba.csp
                sentinel-datasource-nacos
            
    
        
  5. VM options配置sentinel控制台 -Dcsp.sentinel.dashboard.server=localhost:8888 -Dproject.name=sentinel-helloWorld 端口号8888要和Sentinel控制台启动时设置的端口号一致,Dproject.name为该项目在sentinel控 制台显示的名称f8455ba77ff84ad58f4d67ead8fa11a0.png3dad06b676754e15b8a70f6b6986c1fe.png
  6.  注解方式定义资源
    /**
     * 添加配置类
     */
    @Configuration
    public class SentinelAspectConfiguration {
    
        @Bean
        public SentinelResourceAspect sentinelResourceAspect() {
            return new SentinelResourceAspect();
        }
    }
        /**
         * 注解方式定义资源
         * @SentinelResource  value 资源名称
         * @SentinelResource blockHandler 调用被限流/降级/系统保护的时候调用的方法
         * @return
         */
        @SentinelResource(value = "helloWorld",blockHandler = "blockHandlerForHelloWorld")
        @RequestMapping("helloWorld")
        public String helloWorld(){
            return "Sentinel 大爷你好!by 注解方式@SentinelResource"+System.currentTimeMillis();
        }
    
        /**
         * 原方法调用被限流/降级/系统保护的时候调用
         * @param ex
         * @return
         */
        public String blockHandlerForHelloWorld(BlockException ex) {
            ex.printStackTrace();
            return "系统繁忙,请稍后!";
        }
  7. 测试流控资源helloWorld 多次快速刷新:http://localhost:8090/helloWorld,由于helloWorld还未在Sentinel控制台定义,所以请求不会触发流控规则(注意:请求过流控资源后Sentinel控制台才会显示出项目名称)a1a8ff1d6bad4d33b760dce7b2581b4b.png47cef289e9fe4cad8f21a33614b101da.png
  8.  Sentinel控制台添加流控资源helloWorld后再次测试 多次快速刷新:http://localhost:8090/helloWorld,触发流控规则后进入blockHandlerForHelloWorld方法,返回报错提示19889a3bb7ef4e508da5f63abc87c753.pngcfe02ad9d6cb45088292c4d85b99a290.png18d81ff409454d89960dc4f38352f057.png
  9.  流控规则分析13505733eb174ca6a8482ec501822692.pngb54229acb06848c29c73510b52bca434.pngcf1a95baa76146b4a2197cdca9ccfeb3.pnge5181d5c3e1d4eecbeade2f34e529767.png7de6d4fd1f96457cb83a57e91e93e956.png18d70c23b4c04631a7b23741c4f0ef80.png
  10.  降级规则分析99f43a6dcff6472991dbb32c515fd1d9.png9aeba1a786914d81b17e7e6b28d17951.png101a070c44d34dcf8f8f1b9f407cc3bf.png
  11. Sentinel整合Nacos Sentinel定义的限流规则默认存储在内存中,Sentinel控制台重启后定义的限流规则将全部丢失,需要和Nacos进行整合实现规则数据的持久化(Nacos),注意:项目启动后,Nacos通过Json方式添加或者修改的流控规则,会被实时同步到Sentinel控制台,Sentinel原有的流控规则被覆盖,Sentinel控制台添加的流控规则不会被反向加载到Nacos,因此流控规则需要在Nacos添加,Sentinel控制台仅用来观测6c3ec637e300484ba64cb3feefdf2e0d.pngcbe9e01443804d3a84c6c445e433c6b6.png8ea645a7f13645018347ae6b91c7e7b6.pngb78dba66acb041a7a8fc985bff1cffcc.png

https://blog.csdn.net/caoli201314?type=blog

关注
打赏
1688896170
查看更多评论

童心同萌

暂无认证

  • 3浏览

    0关注

    87博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文
立即登录/注册

微信扫码登录

0.0486s