Alibaba Sentinel 一 :Sentinel初識

一: Sentinel簡介

Sentinel是Alibaba Spring Cloud中的重要一員,它是面向分佈式服務架構的流量控制組件,主要以流量爲切入點,從流量控制、熔斷降級、系統自適應保護等多個維度來幫助用戶保障微服務的穩定性。

Sentinel有以下幾大優勢:

  • 豐富的應用場景,出身於阿里巴巴,已經歷過千錘百煉。

  • 易於使用,快速接入。您只需要引入相應的依賴並進行簡單的配置即可快速地接入 Sentinel。

  • 多樣化的流量控制手段。

  • 可視化的監控和規則管理,完備的實時監控,Sentinel 同時提供實時的監控功能。您可以在控制檯中看到接入應用的單臺機器秒級數據,甚至 500 臺以下規模的集羣的彙總運行情況。

額外提一下在我所經歷的項目中,遇到的限流方式還有其它兩種,大家可以瞭解一下。一種是使用Guava中的Ratelimiter類進行限流,算法是令牌桶。還有一種是使用lua編程,在nginx中進行限流,這種方式需要很大的開發量。

 

二: Sentinel下載使用

Sentinel 的使用可以分爲兩個部分:

  1. 核心庫(Java 客戶端):不依賴任何框架/庫,能夠運行於 Java 7 及以上的版本的運行時環境,同時對 Dubbo / Spring Cloud 等框架也有較好的支持。
  2. 控制檯(Dashboard):Dashboard 主要負責管理推送規則、監控、管理機器信息等。

下載Sentinel Dashboard  https://github.com/alibaba/Sentinel/releases

下載夏下來的是一個jar,有java環境的可以直接運行:java -jar sentinel-dashboard.jar

啓動完成後,訪問http://localhost:8080,需要注意,sentinel的默認端口是8080。用戶名和密碼默認是sentinel。

到此,sentinel的控制檯啓動完畢。

三:Sentinel初始化監控

新建一個項目先體驗一下sentinel的功能。

在本專欄中的文章,由於是alibaba spring cloud系列,所以涉及到的項目,一般都需要註冊到nacos中。同樣,sentinel也不例外,雖然本示例不需要註冊,但是爲了系列文章的連貫性,我還是註冊到nacos中了。需要先啓動一個單機版的nacos。

1. 創建一個module

2. 修改pom

<dependencies>
    <!-- SpringCloud ailibaba nacos-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!-- SpringCloud ailibaba sentinel-->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--監控-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!--熱部署-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

3. 在resources目錄下新建application.yml文件

server:
  port: 8400

spring:
  application:
    name: sentinal-service
  cloud:
    nacos:
      discovery:
        #Nacos服務註冊中心地址
        server-addr: localhost:8848
    sentinel:
      transport:
        #配置Sentin dashboard地址
        dashboard: localhost:8080
        # 默認8719端口,假如被佔用了會自動從8719端口+1進行掃描,直到找到未被佔用的 端口
        port: 8719

management:
  endpoints:
    web:
      exposure:
        include: '*'

4. 創建啓動類

package com.xhc.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient
@SpringBootApplication
public class SentinelMain {

    public static void main(String[] args) {
        SpringApplication.run(SentinelMain.class,args);
    }
}

5. 創建一個測試業務類

package com.xhc.cloud.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class FlowLimitController {

    @GetMapping("/testLimit")
    public String testLimit() {
        return "----testLimit";
    }
}

6. 啓動項目,在nacos的服務列表中會發現sentinel已經註冊進去了。打開sentinel,發現什麼都沒有,這是正常的。sentinel需要一個觸發。


訪問:http://localhost:8400/testLimit

然後再一次刷新sentinel,這時就會發現已經有數據顯示了。

可以嘗試着在瀏覽器中多刷新幾次,sentinel控制檯就會更加明顯的顯示訪問的信息。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章