SpringCloudAlibaba:5.1Sentinel的基本使用

概述

简介

Sentinel是阿里开源的项目,提供了流量控制、熔断降级、系统负载保护等多个维度来保障服务之间的稳定性。

官网

复制代码
https://sentinelguard.io/zh-cn/

Sentinel的历史

2012 年,Sentinel 诞生,主要功能为入口流量控制。

2013-2017 年,Sentinel 在阿里巴巴集团内部迅速发展,成为基础技术模块,覆盖了所有的核心场景。

2018 年,Sentinel 开源,并持续演进。

2019 年,Sentinel 朝着多语言扩展的方向不断探索,推出 C++ 原生版本, 同时针对 Service Mesh 场景也推出了 Envoy 集群流量控制支持,以解决 Service Mesh 架构下多语言限流的问题。

2020 年,推出 Sentinel Go 版本,继续朝着云原生方向演进。

2021 年,Sentinel 正在朝着 2.0 云原生高可用决策中心组件进行演进; 同时推出了 Sentinel Rust 原生版本。同时我们也在 Rust 社区进行了 Envoy WASM extension 及 eBPF extension 等场景探索。

2022 年,Sentinel 品牌升级为流量治理,领域涵盖流量路由/调度、流量染色、流控降级、过载保护/实例摘除等; 同时社区将流量治理相关标准抽出到 OpenSergo 标准中,Sentinel 作为流量治理标准实现。

特性

1.丰富的应用场景:Sentinel 承接了阿里巴巴近 10 年的双十一大促流量的核心场景, 例如秒杀(即突发流量控制在系统容量可以承受的范围)、消息削峰填谷、集群流量控制、实时熔断下游不可用应用等。

2.完备的实时监控:Sentinel 同时提供实时的监控功能。您可以在控制台中看到接入应用的单台机器秒级数据,甚至 500 台以下规模的集群的汇总运行情况。

3.广泛的开源生态:Sentinel 提供开箱即用的与其它开源框架/库的整合模块,例如与 Spring Cloud、Dubbo、gRPC 的整合。

4.完善的SPI扩展点:Sentinel 提供简单易用、完善的 SPI 扩展接口。您可以通过实现扩展接口来快速地定制逻辑。例如定制规则管理、适配动态数据源等。

工作原理

1.对主流框架提供适配或者显示的 API,来定义需要保护的资源,并提供设施对资源进行实时统计和调用链路分析。

2.根据预设的规则,结合对资源的实时统计信息,对流量进行控制。同时,Sentinel 提供开放的接口,方便您定义及改变规则。

3.Sentinel 提供实时的监控系统,方便您快速了解目前系统的状态。

使用

1.控制台(Dashboard):Dashboard主要负责管理推送规则、监控、管理机器信息等。

2.核心库(Java客户端):不依赖任何框架/库,能够运行于Java 8及以上的版本的运行时环境,同时对Dubbo/Spring Cloud 等框架也有较好的支持。

服务端

docker安装

1.拉取镜像

复制代码
 docker pull docker.io/bladex/sentinel-dashboard

2.创建启动容器【自启动】

复制代码
docker run --name sentinel --restart=always -d  -p 8038:8858 docker.io/bladex/sentinel-dashboard

3.访问

复制代码
192.168.66.101:8038

账号密码【sentinel/sentinel】

客户端

依赖

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>java_sc_alibaba</artifactId>
        <groupId>jkw.life</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>test-sentinel-8008</artifactId>

    <dependencies>
        <!--sentinel-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
        </dependency>
        <!--actuator-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- SpringMVC-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>


</project>

application.yml

复制代码
server:
  port: 8008
spring:
  application:
    name: test-sentinel-8008
  cloud:
    sentinel:
      transport:
        # Sentinel 控制台地址
        dashboard: 192.168.66.101:8038

启动类

复制代码
package jkw;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@Slf4j
public class Main8008 {
    public static void main(String[] args) {
        SpringApplication.run(Main8008.class, args);
        log.info("************Main8008 启动成功 **********");
    }
}

测试控制器

复制代码
package jkw.controller;

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

/**
 * 测试控制器
 */
@RestController
@RequestMapping("/test")
public class TestCon {
    @GetMapping("/index")
    public String index() {
        return "helle sentinel";
    }
}

测试

发送一次请求,然后打开Sentinel控制台就可以看到

http://localhost:8008/test/index

相关推荐
yoyo_zzm13 小时前
SpringCloud Gateway 集成 Sentinel 详解 及实现动态监听Nacos规则配置实时更新流控规则
spring cloud·gateway·sentinel
短剑重铸之日1 天前
深入理解Sentinel: 01 一次服务雪崩问题排查经历
java·sentinel·降级熔断
列星随旋4 天前
sentinel
sentinel
zz0723204 天前
阿里开源流量防护组件——Sentinel
sentinel
weixin199701080164 天前
《淘宝双11同款:基于 Sentinel 的微服务流量防卫兵实战》
微服务·架构·sentinel
一叶飘零_sweeeet5 天前
高可用架构核心:限流熔断降级全解,Sentinel 与 Resilience4j 原理 + 落地实战
架构·sentinel
Fang fan8 天前
Redis基础数据结构
数据结构·数据库·redis·缓存·bootstrap·sentinel
回到原点的码农9 天前
SpringCloud Gateway 集成 Sentinel 详解 及实现动态监听Nacos规则配置实时更新流控规则
spring cloud·gateway·sentinel
没有bug.的程序员10 天前
黑客僵尸网络的降维打击:Spring Cloud Gateway 自定义限流剿杀 Sentinel 内存黑洞
java·网络·spring·gateway·sentinel