国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Sentinel 降級、限流、熔斷

這篇具有很好參考價值的文章主要介紹了Sentinel 降級、限流、熔斷。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

前言

在現(xiàn)代分布式系統(tǒng)中,如何有效地保護系統(tǒng)免受突發(fā)流量和故障的影響,是每個開發(fā)人員和架構師都需要思考的重要問題。在這樣的背景下,Sentinel作為一個強大的系統(tǒng)保護和控制組件,為我們提供了降級、限流、熔斷等多種策略,幫助我們更好地保障系統(tǒng)的穩(wěn)定性和可用性。

https://sentinelguard.io/zh-cn/docs/quick-start.html
  • 在微服務的體系架構中,如果遇到服務提供方不能提供服務時,怎么辦?
    • 將采用spring cloud alibaba sentinel進行解決。
    • 在學習sentinel之前,先了解相關的觀念。

正文

一. 微服務常見概念

1 服務雪崩

  • 服務雪崩:在整條鏈路的服務中,一個服務失敗,導致整條鏈路的服務都失敗的情形。
  1. 存在整條鏈路服務(Service A、Service B、Service C)
  2. Service A 流量突然性增加,導致Service B 和Service C 流量也增加。
  3. Service C 因為抗不住請求,變得不可用。導致Service B的請求變得阻塞。
  4. 當Service B的資源耗盡,Service B就會變得不可用。
  5. 最后 Service A 不可用。

Sentinel 降級、限流、熔斷,sentinel,java,數(shù)據(jù)庫

2 服務熔斷

  • 服務熔斷:當下游的服務因為某種原因突然變得不可用響應過慢,上游服務為了保證自己整體服務的可用性,不再繼續(xù)調用目標服務,直接返回,快速釋放資源。如果目標服務情況好轉則恢復調用。

Sentinel 降級、限流、熔斷,sentinel,java,數(shù)據(jù)庫

  1. 最開始處于closed狀態(tài),一旦檢測到錯誤到達一定閾值,便轉為open狀態(tài);

  2. 這時候會有個 reset timeout,到了這個時間了,會轉移到half open狀態(tài);

  3. 嘗試放行一部分請求到后端,一旦檢測成功便回歸到closed狀態(tài),即恢復服務;

3 服務降級

  • 什么是服務降級呢?
    • 當下游的服務因為某種原因響應過慢,下游服務主動停掉一些不太重要的業(yè)務,釋放出服務器資源,增加響應速度!
    • 當下游的服務因為某種原因不可用,上游主動調用本地的一些降級邏輯,避免卡頓,迅速返回給用戶!

4 熔斷和降級的區(qū)別

  • 服務熔斷和服務降級的區(qū)別?

    • 服務降級有很多種降級方式!如開關降級、限流降級、熔斷降級!
    • 服務熔斷屬于降級方式的一種!
    • 當發(fā)生下游服務不可用的情況,熔斷和降級必定是一起出現(xiàn)。
  • 服務降級大多是屬于一種業(yè)務級別的處理,熔斷屬于框架層級的實現(xiàn)

  • 開關降級

    在配置中心配置一個開關(變量),在配置中心更改開關,決定哪些服務進行降級

5 Sentinel 介紹

  • Sentinel :一個高可用的流量控制與防護組件,保障微服務的穩(wěn)定性。
  • Sentinel分為兩個部分,sentinel-core與sentinel-dashboard。
    • sentinel-core 部分能夠支持在本地引入sentinel-core進行限流規(guī)則的整合與配置。
    • sentinel-dashboard 則在core之上能夠支持在線的流控規(guī)則與熔斷規(guī)則的維護與調整等。

二. core:降級

Sentinel 降級、限流、熔斷,sentinel,java,數(shù)據(jù)庫

1 現(xiàn)象1

  • 提供者搭建集群(8170/8270),調用者調用,此時關閉提供者的一個服務(8270)

  • 存在現(xiàn)象:訪問8170成功訪問,不能訪問8270

    • RestTemplate:8170可訪問,當訪問8270時異常。稍等片刻只有8170.
  • 略有卡頓,稍等片刻后,不再卡頓。

2 現(xiàn)象2:

  • 提供者搭建集群(8170/8270),調用者調用,此時關閉提供者的所有服務

  • 現(xiàn)象:無法訪問

    Sentinel 降級、限流、熔斷,sentinel,java,數(shù)據(jù)庫

3 降級操作

  • 添加坐標

    Sentinel 降級、限流、熔斷,sentinel,java,數(shù)據(jù)庫

    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>
    
    
  • 修改yml文件,開啟feign對sentinel的支持

    Sentinel 降級、限流、熔斷,sentinel,java,數(shù)據(jù)庫

    feign:
      sentinel:
        enabled: true
    
    
  • 修改啟動類,開啟feign

    Sentinel 降級、限流、熔斷,sentinel,java,數(shù)據(jù)庫

    package com.czxy;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
    import org.springframework.cloud.openfeign.EnableFeignClients;
    
    /**
     * @author 薛慕昭
     * @email 18716011269@163.com
     */
    @SpringBootApplication
    @EnableDiscoveryClient  //服務發(fā)現(xiàn)
    @EnableFeignClients     //遠程調用
    public class TestNacosConsumerApplication {
        public static void main(String[] args) {
            SpringApplication.run(TestNacosConsumerApplication.class, args );
        }
    }
    
    
    
  • 修改Feign實現(xiàn)

    Sentinel 降級、限流、熔斷,sentinel,java,數(shù)據(jù)庫

    package com.czxy.feign;
    
    import org.springframework.cloud.openfeign.FeignClient;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    
    /**
     * @author 薛慕昭
     * @email 18716011269@163.com
     */
    // @FeignClient(value = "服務名", path = "controller配置的路徑" )
    @FeignClient(value = "service-provider", fallback = EchoFeignFallback.class )
    public interface EchoFeign {
    
        // 與 nacos-provider-2.1>EchoController聲明的方法的完全一致
        @GetMapping("/echo/{string}")
        public String echo(@PathVariable String string);
    }
    
    
    
    package com.czxy.feign;
    
    import org.springframework.stereotype.Component;
    
    /**
     * @author 薛慕昭
     * @email 18716011269@163.com
     */
    @Component
    public class EchoFeignFallback implements EchoFeign {
        @Override
        public String echo(String string) {
            return "降級處理:" + string;
        }
    }
    
    
    
  • 關閉服務提供者,測試

    • 注意:需重啟服務

    Sentinel 降級、限流、熔斷,sentinel,java,數(shù)據(jù)庫

三. dashboard 控制面板

1 概述

  • Sentinel Dashboard 是一個可視化流控管理工具。

  • Sentinel Dashboard 是一個獨立的項目,sentinel-dashboard-1.8.4.jar,需要使用 java -jar 運行

    java -jar -Dserver.port=18080 sentinel-dashboard-1.8.4.jar
    
    

    Sentinel 降級、限流、熔斷,sentinel,java,數(shù)據(jù)庫

  • 下載地址

    https://github.com/alibaba/Sentinel/releases
    
    

2 配置dashboard

  • 添加坐標(已有)

    <!-- 降級 -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
    </dependency>
    
    
  • 配置yml

    #server.port=8071
    #spring.application.name=service-consumer
    #spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
    #spring.cloud.sentinel.transport.dashboard=192.168.152.153:8080
    #端口號
    server:
      port: 8071
    
    spring:
      application:
        name: service-consumer          #服務名
      cloud:
        nacos:
          discovery:
            server-addr: 127.0.0.1:8848   #nacos服務地址
        sentinel:
          transport:
            dashboard: 127.0.0.1:18080
    feign:
      sentinel:
        enabled: true
    
    
    
  • 測試

    • 先訪問資源 http://localhost:8071/feign/echo/123

    • dashboard 登錄

      Sentinel 降級、限流、熔斷,sentinel,java,數(shù)據(jù)庫

    • 查看控制面板 http://localhost:18080/

      Sentinel 降級、限流、熔斷,sentinel,java,數(shù)據(jù)庫

3 設置資源點(埋點)

  • 通過 @SentinelResource 注解,設置監(jiān)控點(定義控制資源、配置控制策略)

    package com.czxy.nacos.controller;
    
    import com.alibaba.csp.sentinel.annotation.SentinelResource;
    import com.czxy.nacos.feign.TestFeign;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    import javax.annotation.Resource;
    
    /**
     * @author 薛慕昭
     * @email 18716011269@163.com
     */
    @RestController
    @RequestMapping("/feign")
    public class TestFeignController {
        @Resource
        private TestFeign testMyFeign;
    
        @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET)
        @SentinelResource("/feign/echo")
        public String echo(@PathVariable String str) {
            return testMyFeign.echo(str);
        }
    }
    
    
    
  • 測試

    Sentinel 降級、限流、熔斷,sentinel,java,數(shù)據(jù)庫

四. 限流

1 編寫測試類

Sentinel 降級、限流、熔斷,sentinel,java,數(shù)據(jù)庫

package com.czxy.controller;

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

/**
 * @author 薛慕昭
 * @email 18716011269@163.com
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/login")
    public String login(String str) {
        return "登錄成功" + str;
    }


    @GetMapping("/register")
    public String register(String str) {
        return "注冊成功";
    }
}

2 限流方法

  • 通過@SentinelResource注解的blockHandler屬性制定限流的處理函數(shù)
package com.czxy.nacos.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 薛慕昭
 * @email 18716011269@163.com
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/login")
    // 限流設置
    @SentinelResource(value="login", blockHandler = "loginBlockHandler")
    public String login(String str) {
        return "登錄成功" + str;
    }

    public String loginBlockHandler(String str , BlockException e) {
        return str + ": 請稍后重試";
    }

    @GetMapping("/register")
    public String register(String str) {
        return "注冊成功";
    }
}


3 限流操作

  • 運行 sentinel-dashboard-1.8.4.jar

  • 訪問測試功能:http://localhost:8071/user/login?str=1234

  • 通過 dashboard 設置限流

    • QPS:一般指每秒查詢率

    Sentinel 降級、限流、熔斷,sentinel,java,數(shù)據(jù)庫

  • 連續(xù)快速2次訪問測試功能

    Sentinel 降級、限流、熔斷,sentinel,java,數(shù)據(jù)庫

五. 熔斷降級

1 降級方法

  • 使用@SentinelResource注解的fallback屬性來指定降級的方法名
package com.czxy.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 薛慕昭
 * @email 18716011269@163.com
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/login")
    // 限流設置
    @SentinelResource(value="login", blockHandler = "loginBlockHandler")
    public String login(String str) {
        return "登錄成功" + str;
    }

    public String loginBlockHandler(String str , BlockException e) {
        return str + ": 請稍后重試";
    }


    @GetMapping("/register")
    // 熔斷降級
    @SentinelResource(value="register", fallback = "registerFallback")
    public String register(String str) {
        int r = RandomUtils.nextInt(10);
        if(r < 5) {
            int i = 1 / 0;
        }
        return "注冊成功";
    }

    public String registerFallback(String str) {
        return str + ": 熔斷降級";
    }
}

2 測試

  • 成功

    Sentinel 降級、限流、熔斷,sentinel,java,數(shù)據(jù)庫

  • 熔斷降級

    Sentinel 降級、限流、熔斷,sentinel,java,數(shù)據(jù)庫

3 降級操作

  • 慢調用比例:
    • RT:平均響應時間
    • 比例閾值:
    • 熔斷時長:
    • 最小請求數(shù):
  • 異常比例:每秒異常總數(shù)占通過量的比值超過閾值(DegradeRule 中的 count)之后,資源進入降級狀態(tài)
  • 異常數(shù):當資源近 1 分鐘的異常數(shù)目超過閾值之后會進行熔斷

Sentinel 降級、限流、熔斷,sentinel,java,數(shù)據(jù)庫

六. 限流和降級的區(qū)別?

  • 限流是通過設置QPS(每秒查詢率)/線程數(shù),將超過閾值部分拒絕處理;
  • 服務降級是監(jiān)控請求響應時間、響應異常比例、異常數(shù)量;超過限定閾值,將進行服務降級熔斷,一定時間內不可用;

結尾

k(String str) {
return str + “: 熔斷降級”;
}
}文章來源地址http://www.zghlxwxcb.cn/news/detail-820420.html




### 2 測試

* 成功

  [外鏈圖片轉存中...(img-EtoFL8NX-1705283262451)]

* 熔斷降級 

  [外鏈圖片轉存中...(img-SNdK9B8Y-1705283262451)]





### 3 降級操作

* 慢調用比例:
  * RT:平均響應時間
  * 比例閾值:
  * 熔斷時長:
  * 最小請求數(shù):
* 異常比例:每秒異??倲?shù)占通過量的比值超過閾值(DegradeRule 中的 count)之后,資源進入降級狀態(tài)
* 異常數(shù):當資源近 1 分鐘的異常數(shù)目超過閾值之后會進行熔斷

[外鏈圖片轉存中...(img-7eNiRPkH-1705283262452)]







## 六. 限流和降級的區(qū)別?

* 限流是通過設置QPS(每秒查詢率)/線程數(shù),將超過閾值部分拒絕處理;
* 服務降級是監(jiān)控請求響應時間、響應異常比例、異常數(shù)量;超過限定閾值,將進行服務降級熔斷,一定時間內不可用;

# 結尾

   Sentinel的降級、限流、熔斷等功能為我們構建健壯的分布式系統(tǒng)提供了強有力的支持。通過合理地設置各項保護和控制策略,我們可以更好地抵御惡劣環(huán)境下的挑戰(zhàn),保持系統(tǒng)的穩(wěn)定和可靠。因此,在設計和實現(xiàn)分布式系統(tǒng)時,充分利用Sentinel的功能將是一個明智的選擇,它將為系統(tǒng)的高可用性和穩(wěn)定性保駕護航。

到了這里,關于Sentinel 降級、限流、熔斷的文章就介紹完了。如果您還想了解更多內容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。如若轉載,請注明出處: 如若內容造成侵權/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領支付寶紅包贊助服務器費用

相關文章

  • 聊一聊服務治理三板斧:限流、熔斷、降級和go-sentinel的實現(xiàn)

    聊一聊服務治理三板斧:限流、熔斷、降級和go-sentinel的實現(xiàn)

    我們知道,對于一個項目之初,我們不可能上來就按幾千的并發(fā)去配置,為什么?兩個方面,第一個是成本高。第二個是維護難度大。即便是天貓?zhí)詫氝@種,也是采用的動態(tài)擴容的方式來應對雙十一。那么一個項目如何應對突然的高并發(fā),我們有哪些常用的措施和處理呢?我

    2024年01月19日
    瀏覽(28)
  • 【Springcloud】Sentinel熔斷和降級

    【Springcloud】Sentinel熔斷和降級

    服務的穩(wěn)定是公司可持續(xù)發(fā)展的重要基石,隨著業(yè)務量的快速發(fā)展,一些平時正常運行的服務,會出現(xiàn)各種突發(fā)狀況,而且在分布式系統(tǒng)中,每個服務本身又存在很多不可控的因素,比如線程池處理緩慢,導致請求超時,資源不足,導致請求被拒絕,又甚至直接服務不可用、

    2024年02月09日
    瀏覽(18)
  • Sentinel流量控制與熔斷降級

    Sentinel流量控制與熔斷降級

    ?? 學技術、更要掌握學習的方法,一起學習,讓進步發(fā)生 ???? 作者:一只IT攻城獅 ,關注我,不迷路 。 ??學習建議:1、養(yǎng)成習慣,學習java的任何一個技術,都可以先去官網(wǎng)先看看,更準確、更專業(yè)。 ??學習建議:2、然后記住每個技術最關鍵的特性(通常一句話或者

    2024年02月10日
    瀏覽(22)
  • springcloud alibaba sentinel熔斷降級

    springcloud alibaba sentinel熔斷降級

    隨著微服務的流行,服務和服務之間的穩(wěn)定性變得越來越重要。Sentinel 以流量為切入點,從 流量控制、熔斷降級、系統(tǒng)負載保護 等多個維度保護服務的穩(wěn)定性。 sentinel相當于hystrix的升級版,加入了web界面,能夠實時在線的改變流量策略。 Sentinel 分為兩個部分: 核心庫(J

    2024年01月23日
    瀏覽(26)
  • Sentinel的線程隔離和熔斷降級

    Sentinel的線程隔離和熔斷降級

    上一節(jié)整理了Sentinel的限流,限流可以降低微服務的負載,避免因為高并發(fā)而故障,進而傳遞給其他相關服務而引發(fā)服務雪崩。以上僅為避免服務故障,而當某個服務真正故障時,如何處理才能防止服務雪崩? ? Sentinel支持隔離和降級兩種方案 采用線程隔離,即艙壁模式:

    2024年02月17日
    瀏覽(29)
  • sentinel熔斷與限流

    sentinel熔斷與限流

    sentinel官網(wǎng)地址 添加鏈接描述 sentinel - github地址 添加鏈接描述 隨著微服務的流行,服務和服務之間的穩(wěn)定性變得越來越重要。Sentinel 以流量為切入點,從流量控制、流量路由、熔斷降級、系統(tǒng)自適應過載保護、熱點流量防護等多個維度保護服務的穩(wěn)定性。 Sentinel 具有以下特

    2024年02月02日
    瀏覽(24)
  • Sentinel限流、熔斷

    Sentinel限流、熔斷

    ????????sentinel 提供了兩種不同的隔離機制:信號量隔離和線程池隔離,它們的主要區(qū)別如下: 信號量隔離(Semaphore Isolation) : 原理 :信號量隔離基于計數(shù)器(或稱令牌桶)的概念。對某個資源設置一個并發(fā)訪問的最大數(shù)量(信號量大?。?,當請求到達時,如果當前信

    2024年01月18日
    瀏覽(25)
  • Hystrix和Sentinel熔斷降級設計理念

    Hystrix和Sentinel熔斷降級設計理念

    Sentinel 和 Hystrix 的原則是一致的: 當檢測到調用鏈路中某個資源出現(xiàn)不穩(wěn)定的表現(xiàn),例如請求響應時間長或異常比例升高的時候,則對這個資源的調用進行限制,讓請求快速失敗,避免影響到其它的資源而導致級聯(lián)故障。 Sentinel 對這個問題采取了兩種手段: 通過并發(fā)線程數(shù)進

    2024年02月09日
    瀏覽(33)
  • 實戰(zhàn):Springboot集成Sentinel實現(xiàn)流量控制、熔斷降級、負載保護

    實戰(zhàn):Springboot集成Sentinel實現(xiàn)流量控制、熔斷降級、負載保護

    前面的文章我們學習了Hystrix并和springboot項目進行了集成,實現(xiàn)服務的熔斷降級、隔離措施。但是Hystrix對流量的控制不是很好,僅僅信號量也只能對指定的接口進行限流,至于保護機制Hystrix也只是達到指標進行熔斷。那么,有沒有一種中間件可以在兼容熔斷降級的同時精準實

    2024年02月16日
    瀏覽(22)
  • 【OpenFeign】OpenFeign結合Hystrix和Sentinel實現(xiàn)熔斷降級

    OpenFeign可以與Hystrix和Sentinel結合使用,實現(xiàn)降級和熔斷。 使用OpenFeign需要引入OpenFeign的依賴: spring-cloud-starter-openfeign 引入的依賴如下: 默認已經(jīng)自動引入了hystrix的依賴,不再需要單獨再引入hystrix了。 降級方法的類需要實現(xiàn)FeignClient的接口,同時這個類需要注入到Spring容器

    2024年02月11日
    瀏覽(30)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領取紅包,優(yōu)惠每天領

二維碼1

領取紅包

二維碼2

領紅包