SpringCloudAlibaba:5.1Sentinel的基本使用

简介: SpringCloudAlibaba:5.1Sentinel的基本使用

 概述

简介

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

官网

https://sentinelguard.io/zh-cn/

image.gif

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 等框架也有较好的支持。

服务端

image.gif 编辑

docker安装

1.拉取镜像

docker pull docker.io/bladex/sentinel-dashboard

image.gif

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

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

image.gif

3.访问

image.gif 编辑

image.gif 编辑

192.168.66.101:8038
image.gif

账号密码【sentinel/sentinel】

客户端

image.gif 编辑

依赖

<?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>

image.gif

application.yml

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

image.gif

启动类

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 启动成功 **********");
    }
}

image.gif

测试控制器

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";
    }
}

image.gif

测试

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

http://localhost:8008/test/index

image.gif 编辑

相关文章
|
5天前
|
NoSQL Java Redis
SpringBoot-引入Redis依赖
本文介绍如何在IDEA里将SpringBoot整合Redis。
122 0
|
5天前
|
NoSQL Java Redis
SpringBoot原理分析 | Redis集成
SpringBoot原理分析 | Redis集成
45 0
|
NoSQL Java 应用服务中间件
SpringBoot——SpringBoot集成Redis
SpringBoot——SpringBoot集成Redis
4982 0
SpringBoot——SpringBoot集成Redis
|
5天前
|
存储 NoSQL Java
springboot配置使用redis
springboot配置使用redis
55 0
|
5天前
|
监控 Java Sentinel
SpringCloud之Sentinel概述和安装及简单整合
SpringCloud之Sentinel概述和安装及简单整合
|
8月前
|
NoSQL Java Redis
springboot高级教程基于 redis 通过注解实现限流
springboot高级教程基于 redis 通过注解实现限流
102 0
|
9月前
|
SpringCloudAlibaba Java 微服务
|
10月前
|
消息中间件 SpringCloudAlibaba 监控
六.SpringCloudAlibaba极简入门-Sentinel限流
限流 , 限制流量,这里的流量我们可以理解成请求数量,其实就是限制服务器的请求并发数量,为什么要这么做?如果不做限流,那么在大量并发请求下我们的服务器会慢慢的变慢然后顶不住压力而挂掉(类似堵车)。并不是说并发越大越好,有的时候我们的项目规模和业务决定了我们不需要那么大的并发性,当大量的并发请求访问到服务器时我们需要把部分请求拒绝在外,这个是流量限制 - 限流。 熔断机制在在《Spring Cloud 极简入门》中有详细的解释,熔断机制是对服务调用链路的保护机制,如果链路上的某个服务不可访问,调用超时,发生异常等,服务会进行发熔断,触发降级返回托底数据。简单理解就是当服务器不可访问,可以返回一
|
10月前
|
消息中间件 监控 算法
springcloud整合Sentinel配置实战
springcloud整合Sentinel配置实战
|
10月前
|
SpringCloudAlibaba 双11 Sentinel
七.SpringCloudAlibaba极简入门-Sentinel熔断
在上一章节我们探讨了Sentinel的流控(限流)功能,Sentinel除了流控还提供了服务熔断和降级机制,服务之间的调用关系错综复杂,微服务的调用链上的某些服务资源不稳定(宕机,异常,超时)可能会导致可能请求的失败和请求的堆积,调用链产生连锁反应可能会导致整个微服务架构瘫痪。服务熔断降级机制是保障高可用的重要措施之一。