• SpringCloud Alibaba Sentinel实现熔断与限流


    SpringCloud Alibaba Sentinel实现熔断与限流

    1、Sentinel的介绍

    GitHub中文版地址:https://github.com/alibaba/Sentinel/wiki/主页

    官网地址:https://sentinelguard.io/zh-cn/

    随着微服务的流行,服务和服务之间的稳定性变得越来越重要。Sentinel 是面向分布式、多语言异构化服务架构的流量治理组件,主要以流量为切入点,从流量路由、流量控制、流量整形、熔断降级、系统自适应过载保护、热点流量防护等多个维度来帮助开发者保障微服务的稳定性。

    2、下载Sentinel和运行

    下载地址:https://github.com/alibaba/Sentinel/releases

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4QlRUplx-1668643065832)(image/143、下载Sentinel.png)]

    在存放下载的Sentitneljar包位置通过如下命令进行启迪

    java -jar sentinel-dashboard-1.8.5.jar
    
    • 1

    页面访问本机地址的8080端口

    需要保证本机的8080端口不被占用

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6lZol6pN-1668643065833)(image/144、访问sentinel.png)]

    3、初始化演示工程

    3.1、启动Nacos8848(win版)

    win单节点启动命令

    startup.cmd -m standalone
    
    • 1

    浏览器访问:http://192.168.26.1:8848/nacos/index.html

    3.2、创建cloudalibaba-sentinel-service8401模块

    3.2.1、添加pom.xml依赖

    以后使用阿里巴巴cloud的都使用前三个依赖

    <dependencies>
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
        dependency>
        
        <dependency>
            <groupId>com.alibaba.cspgroupId>
            <artifactId>sentinel-datasource-nacosartifactId>
        dependency>
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-starter-openfeignartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-actuatorartifactId>
        dependency>
        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-devtoolsartifactId>
            <scope>runtimescope>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>cn.hutoolgroupId>
            <artifactId>hutool-allartifactId>
            <version>4.6.3version>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    
    dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54

    3.2.2、配置YAML文件

    server:
      port: 8401
    
    spring:
      application:
        name: cloudalibaba-sentinel-service
      cloud:
        nacos:
          discovery:
            #Nacos服务注册中心地址
            server-addr: localhost:8848
        sentinel:
          transport:
            #配置Sentinel dashboard地址,8080监控8401
            dashboard: localhost:8080
            #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
            port: 8719
    
    # 图像化展示,暴露所有端口
    management:
      endpoints:
        web:
          exposure:
            include: '*'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    3.2.3、添加主启动类

    package com.zcl.springcloud;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    
    /**
     * 描述:哨兵启动类
     *
     * @author zhong
     * @date 2022-09-27 22:06
     */
    @SpringBootApplication
    @EnableDiscoveryClient
    public class MainApp8401 {
        public static void main(String[] args) {
            SpringApplication.run(MainApp8401.class, args);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    3.2.4、添加业务控制器接口

    package com.zcl.springcloud.controller;
    
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * 描述:流控哨兵控制器接口
     *
     * @author zhong
     * @date 2022-09-27 22:07
     */
    @RestController
    public class FlowLimitController {
        @GetMapping("/testA")
        public String testA()
        {
            return "------testA";
        }
    
        @GetMapping("/testB")
        public String testB()
        {
            return "------testB";
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    3.2.5、启动项目

    3.3、启动Sentinel哨兵

    在下载的位置通过如下命令启动访问本机8080端口,进入登录

    java -jar sentinel-dashboard-1.8.5.jar
    
    • 1

    Sentinel是懒加载,上面的8401项目已经是跑起来了的,但是直接访问Sentinl是看不到具体的监控服务信息的,需要给服务发送一些请求

    http://localhost:8401/testA
    
    http://localhost:8401/testB
    
    • 1
    • 2
    • 3

    访问如上接口后再次查看Sentinel的监控页面

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1pbY221N-1668643065833)(image/145、Sentinel监控服务.png)]

  • 相关阅读:
    【DDR3 控制器设计】(5)DDR3 的仲裁读写操作设计
    PaddleOCR以及CUDA、cuDNN安装踩坑记录
    详解supervisor配置与案例
    推荐一款好用的日期控件jeDate
    activiti-api
    网页保存为pdf神器(可自定义编辑)—Print Edit WE
    以命令行形式执行Postman脚本(使用Newman)
    sourcetree这是一个无效的路径/url, mac版本
    基于若依ruoyi-nbcio支持flowable流程增加自定义业务表单(二)
    SpringBoot 根据不同环境切换不同文件路径
  • 原文地址:https://blog.csdn.net/baidu_39378193/article/details/127896771