Sentinel入门开发

链接: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为启动命令设置的,默认用户名和密码均为sentinel
    563aa785e5c142179ef104e09aa8a4f4.png
  3.  添加父项目pom依赖
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
            <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
            <springboot.version>2.3.2.RELEASE</springboot.version>
            <springcloudalibaba.version>2.2.4.RELEASE</springcloudalibaba.version>
        </properties>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.cloud</groupId>
                    <artifactId>spring-cloud-dependencies</artifactId>
                    <version>${spring-cloud.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>${springboot.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
                <dependency>
                    <groupId>com.alibaba.cloud</groupId>
                    <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                    <version>${springcloudalibaba.version}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
  4.  添加子项目pom依赖
        <dependencies>
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba.csp</groupId>
                <artifactId>sentinel-core</artifactId>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba.csp</groupId>
                <artifactId>sentinel-transport-simple-http</artifactId>
                <version>1.8.0</version>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba.csp</groupId>
                <artifactId>sentinel-annotation-aspectj</artifactId>
                <version>1.8.0</version>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba.csp</groupId>
                <artifactId>sentinel-parameter-flow-control</artifactId>
                <version>1.4.0</version>
            </dependency>
    
            <dependency>
                <groupId>com.alibaba.csp</groupId>
                <artifactId>sentinel-datasource-nacos</artifactId>
            </dependency>
    
        </dependencies>
  5. VM options配置sentinel控制台
    -Dcsp.sentinel.dashboard.server=localhost:8888 -Dproject.name=sentinel-helloWorld 端口号8888要和Sentinel控制台启动时设置的端口号一致,Dproject.name为该项目在sentinel控
    制台显示的名称
    f8455ba77ff84ad58f4d67ead8fa11a0.png
    3dad06b676754e15b8a70f6b6986c1fe.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.png
    47cef289e9fe4cad8f21a33614b101da.png
  8.  Sentinel控制台添加流控资源helloWorld后再次测试
    多次快速刷新:http://localhost:8090/helloWorld,触发流控规则后进入blockHandlerForHelloWorld方法,返回报错提示19889a3bb7ef4e508da5f63abc87c753.png
    cfe02ad9d6cb45088292c4d85b99a290.png
    18d81ff409454d89960dc4f38352f057.png
  9.  流控规则分析
    13505733eb174ca6a8482ec501822692.png
    b54229acb06848c29c73510b52bca434.png
    cf1a95baa76146b4a2197cdca9ccfeb3.png
    e5181d5c3e1d4eecbeade2f34e529767.png
    7de6d4fd1f96457cb83a57e91e93e956.png
    18d70c23b4c04631a7b23741c4f0ef80.png
  10.  降级规则分析
    99f43a6dcff6472991dbb32c515fd1d9.png
    9aeba1a786914d81b17e7e6b28d17951.png
    101a070c44d34dcf8f8f1b9f407cc3bf.png
  11. Sentinel整合Nacos
    Sentinel定义的限流规则默认存储在内存中,Sentinel控制台重启后定义的限流规则将全部丢失,需要和Nacos进行整合实现规则数据的持久化(Nacos),注意:项目启动后,Nacos通过Json方式添加或者修改的流控规则,会被实时同步到Sentinel控制台,Sentinel原有的流控规则被覆盖,Sentinel控制台添加的流控规则不会被反向加载到Nacos,因此流控规则需要在Nacos添加,Sentinel控制台仅用来观测
    6c3ec637e300484ba64cb3feefdf2e0d.png
    cbe9e01443804d3a84c6c445e433c6b6.png

    8ea645a7f13645018347ae6b91c7e7b6.png
    b78dba66acb041a7a8fc985bff1cffcc.png

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

童心同萌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值