Sentinel Getting Started And Integration of Spring Cloud Alibaba Tutorials

原文链接:Sentinel Getting Started And Integration of Spring Cloud Alibaba Tutorials

Sentinel Getting Started And Integration of Spring Cloud Alibaba Tutorials

TIPS

This article based on:a

  • Spring Boot 2.1.5
  • Spring Cloud Greenwich.SR1
  • Spring Cloud Alibaba 0.9.0
  • Nacos 1.0.0

1. What is Sentinel ?

With the popularity of microservices, the stability between services and services is becoming more and more important. Sentinel uses traffic as an entry point to protect the stability of services from multiple dimensions such as flow control, blowdown, and system load-balance protection.

In a nutshell, Sentinel is a lightweight flow control, blowdown and degraded Java library.

Sentinel has the following characteristics:

  • Rich application scenarios:Sentinel undertakes the core scene of Alibaba‘s "Double Eleven" promotion traffic for nearly 10 years. For example, spikes (that is, burst flow control can be tolerated in the system capacity), message peaking and valley filling, cluster flow control, real-time fuse downstream applications that are not available.
  • Complete real-time monitoring:Sentinel also provides real-time monitoring. You can see the single machine second-level data of the access application in the console, or even the aggregate operation of clusters of less than 500 sizes.
  • Extensive open source ecology:Sentinel Provides out-of-the-box integration modules with other open source frameworks/libraries. For example, integration with Spring Cloud, Dubbo, gRPC. You only need to introduce the appropriate dependencies and perform a simple configuration to quickly access Sentinel.
  • Complete SPI extension point:Sentinel provides an easy-to-use, comprehensive SPI expansion interface。You can quickly customize the logic by implementing an extension interface. Such as custom rule management, adapting dynamic data sources, etc.

2. Guides

  • Add dependencies:

    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
      <version>0.2.1.RELEASE</version>
    </dependency>
  • Add configurations:
    server:
      port: 8010
    spring:
      application:
        # Specify the name of the service registered to the nacos server
        name: microservice-consumer-movie
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848
    management:
      endpoints:
        web:
          exposure:
            include: '*'
    
  • Add Controller:
    @RequestMapping("/movies")
    @RestController
    public class MovieController {
      @Autowired
      private RestTemplate restTemplate;
    
      @GetMapping("/users/{id}")
      public User findById(@PathVariable Long id) {
        // Use the placeholder of the RestTemplate
        User user = this.restTemplate.getForObject(
          "http://microservice-provider-user/users/{id}",
          User.class,
          id
        );
        // ...Movie microservices business...
        return user;
      }
    }

    It can be seen from the code that this one can‘t be normal controller! Because Sentinel starter will provide a current limit for all HTTP services by default, the Controller can be protected by Sentinel (but there are no rules for configuring protection yet, so it has not been protected yet)!

3. Test

  • Access http://localhost:8010/actuator/sentinel ,the following results can be obtained:

    {
      "DegradeRules": [],
      "datasources": {},
      "ParamFlowRule": [],
      "SystemRules": [],
      "FlowRules": [],
      "properties": {
          "eager": false,
          "enabled": true,
          "datasource": {},
          "transport": {
              "port": "8719",
              "dashboard": "localhost:8080",
              "heartbeatIntervalMs": null
          },
          "metric": {
              "fileSingleSize": null,
              "fileTotalCount": null,
              "charset": "UTF-8"
          },
          "servlet": {
              "blockPage": null
          },
          "filter": {
              "order": -2147483648,
              "urlPatterns": ["/*"]
          },
          "flow": {
              "coldFactor": "3"
          },
          "log": {
              "dir": null,
              "switchPid": false
          }
      }
    }

At the moment, we don‘t know what the meaning of the result exposed by /actuator/sentinel is. It doesn‘t matter, please read on.

4. Summary

Just add the spring-cloud-starter-alibaba-sentinel dependency to your app, and all HTTP interfaces get Sentinel protection! Of course, we currently have no rules for configuring protection for Sentinel.

5. Sample Code

GitHub

Gitee

原文链接:Sentinel Getting Started And Integration of Spring Cloud Alibaba Tutorials

转载,请保留原文地址,谢谢 ~

原文地址:https://www.cnblogs.com/Alandre/p/11751724.html

时间: 2024-10-10 12:34:47

Sentinel Getting Started And Integration of Spring Cloud Alibaba Tutorials的相关文章

Spring Cloud Alibaba基础教程:Sentinel使用Apollo存储规则

上一篇我们介绍了如何通过Nacos的配置功能来存储限流规则.Apollo是国内用户非常多的配置中心,所以,今天我们继续说说Spring Cloud Alibaba Sentinel中如何将流控规则存储在Apollo中. 使用Apollo存储限流规则 Sentinel自身就支持了多种不同的数据源来持久化规则配置,目前包括以下几种方式: 文件配置 Nacos配置 ZooKeeper配置 Apollo配置 本文我们就来一起动手尝试一下,如何使用Apollo来存储限流规则. 准备工作 下面我们将同时使用

Spring Cloud Alibaba基础教程:Sentinel Dashboard同步Apollo存储规则

在之前的两篇教程中我们分别介绍了如何将Sentinel的限流规则存储到Nacos和Apollo中.同时,在文末的思考中,我都指出了这两套整合方案都存在一个不足之处:不论采用什么配置中心,限流规则都只能通过Nacos界面或Apollo界面来完成修改才能得到持久化存储,而在Sentinel Dashboard中修改限流规则虽然可以生效,但是不会被持久化到配置中心.而在这两个配置中心里存储的数据是一个Json格式,当存储的规则越来越多,对该Json配置的可读性与可维护性会变的越来越差.所以,下面我们就

Spring Cloud Alibaba | Sentinel: 服务限流基础篇

目录 Spring Cloud Alibaba | Sentinel: 服务限流基础篇 1. 简介 2. 定义资源 2.1 主流框架的默认适配 2.2 抛出异常的方式定义资源 2.3 返回布尔值方式定义资源 2.4 注解方式定义资源 2.5 异步调用支持 3. 规则的种类 3.1 流量控制规则 (FlowRule) 3.2 熔断降级规则 (DegradeRule) 3.3 系统保护规则 (SystemRule) 3.4 访问控制规则 (AuthorityRule) Spring Cloud Al

Spring Cloud Alibaba | Sentinel: 服务限流高级篇

目录 Spring Cloud Alibaba | Sentinel: 服务限流高级篇 1. 熔断降级 1.1 降级策略 2. 热点参数限流 2.1 项目依赖 2.2 热点参数规则 3. 系统自适应限流 3.1 背景 3.2 系统规则 3.3 原理 3.4 示例 4. 黑白名单控制 4.1 规则配置 4.2 示例 Spring Cloud Alibaba | Sentinel: 服务限流高级篇 Springboot: 2.1.6.RELEASE SpringCloud: Greenwich.SR

Spring Cloud Alibaba之服务容错组件 - Sentinel [规则持久化篇]

规则持久化 - 拉模式 在Sentinel控制台对某个微服务的接口资源配置了流控.降级等规则后,若重启了该微服务,那么配置的相关规则就会丢失,因为Sentinel默认将规则存放在内存中.每次重启微服务都得重新配置规则显然是不合理的,所以我们需要将配置好的规则进行持久化存储,而Sentinel提供了两种规则持久化模式: 拉模式(pull) 推模式(push) 本小节先介绍一下拉模式(pull),该模式的架构图如下: Tips:规则在代码中实际上是一个对象,所以通常是转换成json字符串后再存储至本

Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵基础实战

Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵基础实战 Springboot: 2.1.8.RELEASE SpringCloud: Greenwich.SR2 1. Sentinel控制台概述 在介绍入门实战之前,先来介绍一下Sentinel.Sentinel控制台提供一个轻量级的开源控制台,它提供机器发现以及健康情况管理.监控(单机和集群),规则管理和推送的功能. Sentinel控制台主要功能: 查看机器列表以及健康情况:收集 Sentinel 客户

Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵进阶实战

Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵进阶实战 在阅读本文前,建议先阅读<Spring Cloud Alibaba | Sentinel:分布式系统的流量防卫兵基础实战>. 1. Sentinel整合Feign和RestTemplate Sentinel目前已经同时支持Feign和RestTemplate,需要我们引入对应的依赖,在使用Feign的时候需要在配置文件中打开Sentinel对Feign的支持:feign.sentinel.enabl

替代 Hystrix,Spring Cloud Alibaba Sentinel 快速入门

提起 Spring Cloud 的限流降级组件,一般首先想到的是 Netflix 的 Hystrix. 不过就在2018年底,Netflix 宣布不再积极开发 Hystrix,该项目将处于维护模式.官方表示 1.5.18 版本的 Hystrix 已经足够稳定,可以满足 Netflix 现有应用的需求,所以接下来其会把焦点转向对于自适应的实现,更多关注对应用程序的实时性能做出响应.对于新应用的熔断需求,将采用其它项目实现,Netflix 推荐了 Resilience4j. 作为 Spring Cl

Spring Cloud Alibaba 之 Sentinel 限流规则和控制台实例

这一节我们通过一个简单的实例,学习Sentinel的基本应用. 一.Sentinel 限流核心概念 在学习Sentinel的具体应用之前,我们先来了解一下Sentinel中两个核心的概念,资源和规则. 资源 资源 是 Sentinel 中的核心概念之一.既然是限流,或者系统保护,那么是针对什么做限流?保护的是什么?就是我们所说的资源. 其实 Sentinel 对资源的定义,和并发编程中 Synchronized的使用很类似,这里的资源,可以是服务里的方法,也可以是一段代码. 规则 定义了资源之后