欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot整合sentinel的方法教程

 更新時間:2024年10月11日 11:57:04   作者:DeyouKong  
這篇文章主要介紹了springboot整合sentinel的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(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、搭建項(xiàng)目

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ī)則,項(xiàng)目中不推薦使用,這是硬編碼方式
     *
     * 注解 @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. 運(yùn)行測試

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

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

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

3.4.1. 修改上述 UserController 類

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

@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. 啟動上述項(xiàng)目,如下操作

3.4.3、測試

  • 正常請求

  • 快速刷新頁面

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

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

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 實(shí)現(xiàn)限流熔斷的其他方式

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

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

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

使用這種方式當(dāng)資源發(fā)生限流后會拋出 BlockException 異常。這個時候可以捕獲異常,進(jìn)行限流之后的邏輯處理,關(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 {
        // 限流后進(jìn)行的操作
        System.out.println("系統(tǒng)繁忙,請稍后再試");
        return false;
    }
}

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

在啟動類中添加 @EnableAsync,表示 SpringBoot 項(xiàng)目開啟異步調(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.進(jìn)行限流控制
    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)文章

  • 深入理解Java設(shè)計(jì)模式之原型模式

    深入理解Java設(shè)計(jì)模式之原型模式

    這篇文章主要介紹了JAVA設(shè)計(jì)模式之原型模式的的相關(guān)資料,文中示例代碼非常詳細(xì),供大家參考和學(xué)習(xí),感興趣的朋友可以了解下
    2021-11-11
  • Java實(shí)現(xiàn)象棋算法的示例代碼

    Java實(shí)現(xiàn)象棋算法的示例代碼

    象棋算法包括搜索算法、評估函數(shù)和剪枝算法,本文主要介紹了Java實(shí)現(xiàn)象棋算法的示例代碼,具有一定的參考價值,感興趣的可以了解一下
    2023-12-12
  • Java判斷是否為簡體中文字符的實(shí)現(xiàn)方法

    Java判斷是否為簡體中文字符的實(shí)現(xiàn)方法

    在應(yīng)用開發(fā)中,判斷簡體中文字符是一個重要但常被忽視的任務(wù),簡體中文和繁體中文在字符上有顯著的區(qū)別,因此在某些場景下我們需要判斷輸入的文本是否為簡體中文,本文將介紹如何使用Java進(jìn)行此判斷,并提供相應(yīng)的代碼示例,幫助開發(fā)者更好地理解這一過程
    2024-09-09
  • 詳解基于java的Socket聊天程序——服務(wù)端(附demo)

    詳解基于java的Socket聊天程序——服務(wù)端(附demo)

    這篇文章主要介紹了詳解基于java的Socket聊天程序——服務(wù)端(附demo),具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2016-12-12
  • Java Apache Shiro安全框架快速開發(fā)詳解流程

    Java Apache Shiro安全框架快速開發(fā)詳解流程

    Apache Shiro是一個強(qiáng)大且易用的Java安全框架,執(zhí)行身份驗(yàn)證、授權(quán)、密碼和會話管理。使用Shiro的易于理解的API,您可以快速、輕松地獲得任何應(yīng)用程序,從最小的移動應(yīng)用程序到最大的網(wǎng)絡(luò)和企業(yè)應(yīng)用程序
    2021-10-10
  • javax.management.InvalidApplicationException的問題解決

    javax.management.InvalidApplicationException的問題解決

    javax.management.InvalidApplicationException是與Java Management Extensions (JMX) API相關(guān)的一個常見異常,本文主要介紹了javax.management.InvalidApplicationException的問題解決,感興趣的可以了解一下
    2024-08-08
  • SpringBoot數(shù)據(jù)訪問的實(shí)現(xiàn)

    SpringBoot數(shù)據(jù)訪問的實(shí)現(xiàn)

    本文主要介紹了SpringBoot數(shù)據(jù)訪問的實(shí)現(xiàn),引入各種xxxTemplate,xxxRepository來簡化我們對數(shù)據(jù)訪問層的操作,感興趣的可以了解一下
    2023-11-11
  • 解決myBatis中openSession()自動提交的問題

    解決myBatis中openSession()自動提交的問題

    在學(xué)習(xí)MySQL過程中,發(fā)現(xiàn)插入操作自動提交,問題原因可能是myBatis中的openSession()方法設(shè)置了自動提交,或者是MySQL的默認(rèn)引擎設(shè)置為不支持事務(wù)的MyISAM,解決辦法包括更改myBatis的提交設(shè)置或?qū)ySQL表的引擎改為InnoDB
    2024-09-09
  • IDEA部署Tomcat的超詳細(xì)圖文教程

    IDEA部署Tomcat的超詳細(xì)圖文教程

    最近迫于無奈從我哪破舊的Eclipse轉(zhuǎn)換到了IDEA,然后就是超多的不習(xí)慣,下面這篇文章主要給大家介紹了關(guān)于IDEA部署Tomcat的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2023-02-02
  • java計(jì)算兩個日期中間的時間

    java計(jì)算兩個日期中間的時間

    這篇文章主要介紹了java計(jì)算兩個日期中間的時間的相關(guān)資料,需要的朋友可以參考下
    2017-04-04

最新評論