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

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月木@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值