最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot整合sentinel的方法教程

 更新時間:2024年10月11日 11:57:04   作者:DeyouKong  
這篇文章主要介紹了springboot整合sentinel的方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

用到的依賴及配置

<!-- Sentinel核心服務(wù) -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-core</artifactId>
    <version>1.8.6</version>
</dependency>
<!-- Sentinel核心服務(wù) -->
<!-- Sentinel本地應(yīng)用接入控制臺 -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-transport-simple-http</artifactId>
    <version>1.8.6</version>
</dependency>
<!-- Sentinel本地應(yīng)用接入控制臺 -->
<!-- Sentinel提供注解無侵入定義資源 -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-annotation-aspectj</artifactId>
    <version>1.8.6</version>
</dependency>
<!-- Sentinel提供注解無侵入定義資源 -->

配置

spring:
  application:
    name: sentinel

1、搭建項目

1.1、 Maven 依賴

<!-- 版本與控制臺保持一致即可 -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-core</artifactId>
    <version>1.8.6</version>
</dependency>
<!-- 版本與控制臺保持一致即可 -->

1.2、Controller 層

@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/add")
    public String create(){
        try {
            // 設(shè)置一個資源名稱為 Hello
            Entry ignored = SphU.entry("AddUser");
            System.out.println("新建一個用戶");
            return "新建一個用戶";
        } catch (BlockException e) {
            System.out.println("系統(tǒng)繁忙,請稍后");
            e.printStackTrace();
            return "系統(tǒng)繁忙,請稍后";
        }
    }

    /**
     * 使用代碼編寫流控規(guī)則,項目中不推薦使用,這是硬編碼方式
     *
     * 注解 @PostConstruct 的含義是:本類構(gòu)造方法執(zhí)行結(jié)束后執(zhí)行
     */
    @PostConstruct
    public void initFlowRule() {
        /* 1.創(chuàng)建存放限流規(guī)則的集合 */
        List<FlowRule> rules = new ArrayList<>();
        /* 2.創(chuàng)建限流規(guī)則 */
        FlowRule rule = new FlowRule();
        /* 定義資源,表示 Sentinel 會對哪個資源生效 */
        rule.setResource("AddUser");
        /* 定義限流的類型(此處使用 QPS 作為限流類型) */
        rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
        /* 定義 QPS 每秒通過的請求數(shù) */
        rule.setCount(2);
        /* 3.將限流規(guī)則存放到集合中 */
        rules.add(rule);
        /* 4.加載限流規(guī)則 */
        FlowRuleManager.loadRules(rules);
    }
    @GetMapping("/edit")
    public String edit(){
        return "編輯一個用戶";
    }
}

2、搭建 Sentinel 控制臺

下載 Sentinel 控制臺 jar 包:https://github.com/alibaba/Sentinel/releases

啟動 Sentinel 控制臺,如下圖所示

java -Dserver.port=9000 -jar sentinel-dashboard-1.8.6.jar

訪問 Sentinel 控制臺:http://127.0.0.1:9000/

賬號/密碼:sentinel/sentinel

3、SpringBoot 整合 Sentinel

3.1、Maven 依賴

<!-- Sentinel本地應(yīng)用接入控制臺,版本與控制臺保持一致即可 -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-transport-simple-http</artifactId>
    <version>1.8.6</version>
</dependency>
<!-- Sentinel本地應(yīng)用接入控制臺,版本與控制臺保持一致即可 -->

3.2、在 idea 中設(shè)置本地應(yīng)用的 JVM 啟動參數(shù)

-Dcsp.sentinel.dashboard.server=127.0.0.1:9000   Sentinel控制臺的地址和端口號
-Dproject.name=sentinel	  本地應(yīng)用在控制臺中的名稱

3.3. 運行測試

第一次查看控制臺,需要先訪問一次被限流控制的接口,否則控制臺中沒有東西

快速在頁面刷新,就會出現(xiàn)限流后的返回提示語

3.4. 采用控制臺設(shè)置流控規(guī)則

3.4.1. 修改上述 UserController 類

刪除使用代碼編寫的流控規(guī)則,項目中不推薦使用,這是硬編碼方式

@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/add")
    public String create(){
        try {
            // 設(shè)置一個資源名稱為 Hello
            Entry ignored = SphU.entry("AddUser");
            System.out.println("新建一個用戶");
            return "新建一個用戶";
        } catch (BlockException e) {
            System.out.println("系統(tǒng)繁忙,請稍后");
            e.printStackTrace();
            return "系統(tǒng)繁忙,請稍后";
        }
    }
    
    @GetMapping("/edit")
    public String edit(){
        return "編輯一個用戶";
    }
}

3.4.2. 啟動上述項目,如下操作

3.4.3、測試

  • 正常請求

  • 快速刷新頁面

4、注解方式無侵入定義資源(推薦使用)

Sentinel 支持通過 @SentinelResource 注解來定義資源,并配置 blockHandler 函數(shù)來進行限流之后的處理

4.1、Maven 依賴

<dependency>
	<groupId>com.alibaba.csp</groupId>
	<artifactId>sentinel-annotation-aspectj</artifactId>
	<version>1.8.0</version>
</dependency>

4.2、AspectJ 的配置類

@Configuration
public class SentinelAspectConfiguration {

    @Bean
    public SentinelResourceAspect sentinelResourceAspect(){
        return new SentinelResourceAspect();
    }
}

4.3、Controller 層

@RestController
@RequestMapping("/user")
public class UserController {

    // value:資源名稱 blockHandler:設(shè)置限流或降級處理的類
    @SentinelResource(value = "AddUser", blockHandler = "exceptionHandler")
    @GetMapping("/add")
    public String create(){
        return "新增一個用戶";
    }

    // 限流處理類
    public String exceptionHandler(@NotNull BlockException e) {
        e.printStackTrace();
        return "系統(tǒng)繁忙,請稍后再試";
    }
}

5、SpringBoot 整合 Sentinel 實現(xiàn)限流熔斷的其他方式

實現(xiàn)方式有以下幾種

  • 拋出異常的方式
  • 返回布爾值的方式
  • 異步調(diào)用支持
  • 注解方式(推薦,見目錄4)

5.1、拋出異常的方式定義資源示例

使用這種方式當(dāng)資源發(fā)生限流后會拋出 BlockException 異常。這個時候可以捕獲異常,進行限流之后的邏輯處理,關(guān)鍵代碼如下

@RequestMapping(path = {"/hello"}, method = RequestMethod.GET)
@ResponseBody
public String hello() {

    try {
        Entry ignored = SphU.entry("Hello");
        System.out.println("Hello Sentinel");
        return "Hello Sentinel";
    } catch (BlockException e) {
        System.out.println("系統(tǒng)繁忙,請稍后");
        e.printStackTrace();
        return "系統(tǒng)繁忙,請稍后";
    }
}

5.2、返回布爾值的方式定義資源示例

使用的 API 為 SphO,限流后返回的值為 boolean 類型。注意:SphO.entry 必須和 SphO.exit 成對出現(xiàn) 否則會報錯

@GetMapping("/boolean")
public boolean returnBoolean() {

    // 使用限流規(guī)則
    if (SphO.entry("Sentinel-boolean")){
         try {
             System.out.println("Hello Sentinel");
             return true;
          }finally {
          	  // 限流的出口
              SphO.exit();
          }
    } else {
        // 限流后進行的操作
        System.out.println("系統(tǒng)繁忙,請稍后再試");
        return false;
    }
}

5.3、異步調(diào)用支持示例

在啟動類中添加 @EnableAsync,表示 SpringBoot 項目開啟異步調(diào)用支持

@SpringBootApplication
@EnableAsync
public class SentinelQuickStartApplication {

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

5.3.1、創(chuàng)建 AsyncService 編寫異步調(diào)用的方法

@Service
public class AsyncService {

    // Async表示方法為異步調(diào)用
    @Async
    public void hello(){
        System.out.println("異步調(diào)用開始======");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("異步調(diào)用結(jié)束=====");
    }
}

5.3.2、Controller層

@Autowired
private AsyncService asyncService;

@GetMapping("/async")
public void async() {

	// 1.進行限流控制
    AsyncEntry asyncEntry = null;
    try {
    	asyncEntry = SphU.asyncEntry("Sentinel_Async"); // 限流入口
        asyncService.hello(); // 異步調(diào)用方法
        System.out.println("異步測試");
    } catch (BlockException e) {
        e.printStackTrace();
        System.out.println("系統(tǒng)繁忙請稍后再試");
    } finally {
        if (asyncEntry != null) {
        	asyncEntry.exit(); // 限流出口
        }
	}
}

總結(jié) 

到此這篇關(guān)于springboot整合sentinel的文章就介紹到這了,更多相關(guān)springboot整合sentinel內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot項目修改訪問端口和訪問路徑的方法

    SpringBoot項目修改訪問端口和訪問路徑的方法

    這篇文章主要介紹了SpringBoot項目修改訪問端口和訪問路徑的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • 如何通過Java打印Word文檔

    如何通過Java打印Word文檔

    這篇文章主要介紹了如何通過Java打印Word文檔,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-02-02
  • java中TCP實現(xiàn)回顯服務(wù)器及客戶端

    java中TCP實現(xiàn)回顯服務(wù)器及客戶端

    本文主要介紹了java中TCP實現(xiàn)回顯服務(wù)器及客戶端,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-02-02
  • Java中的transient關(guān)鍵字介紹

    Java中的transient關(guān)鍵字介紹

    這篇文章主要介紹了Java中的transient關(guān)鍵字介紹,需要的朋友可以參考下
    2015-03-03
  • Java代碼生成器的制作流程詳解

    Java代碼生成器的制作流程詳解

    這篇文章主要介紹了Java代碼生成器的制作流程詳解,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Spring整合Springmvc的相關(guān)介紹

    Spring整合Springmvc的相關(guān)介紹

    今天小編就為大家分享一篇關(guān)于Spring整合Springmvc的相關(guān)介紹,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01
  • Java中ArrayList的使用詳細介紹

    Java中ArrayList的使用詳細介紹

    這篇文章主要介紹了Java中ArrayList的使用,本文給大家詳細講述該相關(guān)的知識點,并且會通過大量的案例加以說明,需要的朋友可以參考一下
    2022-04-04
  • Java實現(xiàn)Floyd算法的示例代碼

    Java實現(xiàn)Floyd算法的示例代碼

    Floyd算法又稱為插點法,是一種利用動態(tài)規(guī)劃的思想尋找給定的加權(quán)圖中多源點之間最短路徑的算法。本文將用Java語言實現(xiàn)Floyd算法,需要的可以參考一下
    2022-07-07
  • mybatis-plus批處理IService的實現(xiàn)示例

    mybatis-plus批處理IService的實現(xiàn)示例

    這篇文章主要介紹了mybatis-plus批處理IService的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-08-08
  • Java如何實現(xiàn)將類文件打包為jar包

    Java如何實現(xiàn)將類文件打包為jar包

    這篇文章主要介紹了Java如何實現(xiàn)將類文件打包為jar包,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06

最新評論

邹城市| 玛多县| 梨树县| 白山市| 赫章县| 高清| 健康| 丽水市| 绥中县| 伊川县| 治多县| 东方市| 莫力| 银川市| 洪湖市| 呈贡县| 大姚县| 黎平县| 沙河市| 黔东| 东乌| 黎川县| 卢湾区| 中宁县| 平邑县| 饶河县| 高平市| 安宁市| 偏关县| 明水县| 甘谷县| 高碑店市| 德令哈市| 新晃| 汤原县| 四川省| 云霄县| 永德县| 盘锦市| 永兴县| 永平县|