当前位置:   article > 正文

Springboot/Cloud集成Sentinel 和 入门实战_sentinel 是一个组件直接集成在springboot中么

sentinel 是一个组件直接集成在springboot中么

一、Springboot/Cloud集成Sentinel
1. spring-cloud-alibaba依赖

Sentinel首先属于spring-cloud-alibaba下面的组件,因此,第一步要引入spring-cloud-alibaba依赖

文档:
https://github.com/alibaba/spring-cloud-alibaba/blob/master/README-zh.md

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>2.2.5.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
2. 引入 Sentinel starter

官网文档:
https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md

       <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
  • 1
  • 2
  • 3
  • 4
3. 配置application.yml
#Springboot/Cloud 应用端口
server:
  port: 8082

#应用名称
spring:
  application:
    name: limiter
  cloud:
    sentinel:
      transport:
        dashboard: localhost:8080 #sentinel控制台服务端地址
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
3. 接入限流埋点

HTTP 埋点:
Sentinel starter 默认为所有的 HTTP 服务提供了限流埋点,如果只想对 HTTP 服务进行限流,那么只需要引入依赖,无需修改代码。

package com.gblfy.distributedlimiter;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SentinelLimiterController {

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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
4. 访问接口

第一次要先访问接口一次,sentinel控制台才会显示应用信息

http://localhost:8082/sentinel
  • 1
5. sentinel控制台

刷新sentinel控制台显示
在这里插入图片描述

二、入门实战
2.1. 流控管理

QPS每秒查询速率
在这里插入图片描述
在这里插入图片描述

2.2. 快速访问请求

由于咱们设置的每秒流控是1,每秒只能允许1个请求通过,测试1秒请求数量>1,反馈结果
在这里插入图片描述
得出结论当请求数量>1时,会返回Blocked by Sentinel (flow limiting)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/889144
推荐阅读
相关标签
  

闽ICP备14008679号