springcloud-alibaba-sentinel(3)sentinel的基本使用

前言

springcloud-alibaba生态环境

springcloud-alibaba-sentinel(1)sentinel流量卫兵介绍

springcloud-alibaba-sentinel(2)sentinel下载安装运行

在前边篇章中,已经讲解并下载运行了sentinel,接下啦,咱们就来进行实操使用!!!

我们既然是微服务组件,自然也就结合着springcloud微服务项目进行演练!

一、启动sentinel-dashboard

sentinel-dashboardsentinel服务端以及web管理平台

image-20210104212504279

二、微服务项目引入依赖

要使用sentinel的功能必须引入核心依赖

<!--引入sentinel依赖-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>

如果您是首次springcloudspringcloud-alibaba 组件混合使用,您可参考我下方的配置

1.要注意springboot版本号

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

2.指定cloud与cloud-alibaba版本号

    <properties>
        <java.version>1.8</java.version>
        <springcloud.alibaba.version>2.2.3.RELEASE</springcloud.alibaba.version>
        <spring-cloud.version>Hoxton.SR8</spring-cloud.version>
    </properties>

3.指定依赖管理

    <dependencyManagement>
        <dependencies>
            <!--指定spring-cloud-alibaba版本-->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${springcloud.alibaba.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--指定spring-cloud版本-->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

4.引入相关组件依赖

例如我这里同时使用了nacos与sentinel

        <!--引入Nacos配置中心客户端依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <!--open-feign依赖-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!--Nacos配置中心客户端依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
        <!-- sentinel客户端依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>

三、项目配置

我们需要在我们的项目配置文件中指明sentinel-dashboard的地址以及与dashboard的通信地址

spring:
  cloud:
    sentinel:
      transport:
        # sentinel-dashboard 地址
        dashboard: 192.168.125.11:8819
        # dashboard 通信端口 默认为8719
        port: 8719
        # 发送心跳间隔时间
        heartbeat-interval-ms: 5000

image-20210104215038648

四、启动项目查看setinel控制台

刚登陆sentinel控制台,sentinel界面为空白!
原因:默认情况下sentiel为延迟加载,不会在启动之后立即创建服务监控,需要对服务进行调用时才会初始化
image-20210117150909500

我们需要调用下我们的某一接口,sentinel控制台才会显示当前连接到的服务列表

    @GetMapping("/test/sentinel")
    public String get() {
        return "赵钱孙李"+new Random().nextInt(20);
    }

请求几次后,发现左侧应用列表已经我们启动的服务

image-20210117144352442

应用列表名,是读取项目中spring.application.name配置进行获取的

五、sentinel初体验

展开我们的服务app-login

image-20210117151031607

实时监控页面已有了信息

QPS:请求并发数,白话就是当前的请求频率(此刻一秒内有多少个请求)

此监控仍有一定的延时性,并非真正实时,延迟大约两秒左右!

image-20210117152608238

图表的数据都是sentinel控制台定时调用我们的sentinel服务端接口进行获取的!所有存在一定延迟性

image-20210117152814576

数据来源流程:

我们在服务的配置文件中配置好sentinel服务端地址以及通信地址(默认8719)后,当我们请求服务时会被sentinel收集,并将数据交给sentine-dashboard…页面访问sentinel-dashboard后端接口获取数据进行展示

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值