0
点赞
收藏
分享

微信扫一扫

微服务-sentinel

颜娘娘的碎碎念 2022-03-12 阅读 92

文章目录

主要特性

在这里插入图片描述

基本使用

下载

笔者这里使用的是1.7.0的版本
https://github.com/alibaba/Sentinel/releases

在这里插入图片描述

启动

java -jar sentinel-dashboard-1.7.0.jar

访问

http://127.0.0.1:8080

服务注册到sentinel中

添加依赖

 <dependencies>
        <!--SpringCloud ailibaba nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!--SpringCloud ailibaba sentinel-datasource-nacos 后续做持久化用到-->
        <dependency>
            <groupId>com.alibaba.csp</groupId>
            <artifactId>sentinel-datasource-nacos</artifactId>
        </dependency>
        <!--SpringCloud ailibaba sentinel -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!--openfeign-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- SpringBoot整合Web组件+actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--日常通用jar包配置-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.6.3</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>

编写yml

server:
  port: 8401

spring:
  application:
    name: cloudalibaba-sentinel-service
  cloud:
    nacos:
      discovery:
        #Nacos服务注册中心地址
        server-addr: localhost:8848
    sentinel:
      transport:
        #配置Sentinel dashboard地址
        dashboard: localhost:8080
        #默认8719端口,假如被占用会自动从8719开始依次+1扫描,直至找到未被占用的端口
        port: 8719

management:
  endpoints:
    web:
      exposure:
        include: '*'

编写controller

@RestController
public class FlowLimitController {

    @GetMapping("/getA")
    public String getA() {
        return "getA";
    }

    @GetMapping("/getB")
    public String getB() {
        return "this is b request";
    }
}


编写启动类

@EnableDiscoveryClient
@SpringBootApplication
public class MainApp8401 {
    public static void main(String[] args) {
        SpringApplication.run(MainApp8401.class, args);
    }
}

启动nacos和sentinel

由于sentinel是懒加载,所以上述controller需要两个映射地址都访问一遍,服务才会在sentinel控台显示

在这里插入图片描述

流控-快速失败

在这里插入图片描述
在这里插入图片描述
经过这样的配置之后,若我们在1s内疯狂访问http://localhost:8401/getA,那么就会出现下图所示的页面

在这里插入图片描述

流控-关联配置

场景说明

例如我们日常开发的电商网站在双十一会出现疯狂下单,导致服务器被打爆。我们的可以提出这样一种方案——当支付服务被打爆时,我们不妨就对下单进行流控。

编写支付和下单的请求

 /**
     * 下单
     * @return
     */
    @GetMapping("/order")
    public String order() {
        return "order...";
    }

    /**
     * 支付
     * @return
     */
    @GetMapping("/pay")
    public String pay() {
        return "pay...";
    }

配置

在这里插入图片描述

测试

  1. 创建一个集合编写一个pay请求

在这里插入图片描述在这里插入图片描述

  1. 配置压测计划

在这里插入图片描述在这里插入图片描述

  1. 这时候我们就可以看到我们order服务崩了

在这里插入图片描述

源码地址

https://gitee.com/fugongliudehua/mscloud

举报

相关推荐

0 条评论