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

springboot整合sentinel的方法教程

 更新時(shí)間:2024年10月11日 11:57:04   作者:DeyouKong  
這篇文章主要介紹了springboot整合sentinel的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(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)用接入控制臺(tái) -->
<dependency>
    <groupId>com.alibaba.csp</groupId>
    <artifactId>sentinel-transport-simple-http</artifactId>
    <version>1.8.6</version>
</dependency>
<!-- Sentinel本地應(yīng)用接入控制臺(tái) -->
<!-- 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 依賴

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

1.2、Controller 層

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

    @GetMapping("/add")
    public String create(){
        try {
            // 設(shè)置一個(gè)資源名稱為 Hello
            Entry ignored = SphU.entry("AddUser");
            System.out.println("新建一個(gè)用戶");
            return "新建一個(gè)用戶";
        } 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 會(huì)對哪個(gè)資源生效 */
        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 "編輯一個(gè)用戶";
    }
}

2、搭建 Sentinel 控制臺(tái)

下載 Sentinel 控制臺(tái) jar 包:https://github.com/alibaba/Sentinel/releases

啟動(dòng) Sentinel 控制臺(tái),如下圖所示

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

訪問 Sentinel 控制臺(tái):http://127.0.0.1:9000/

賬號/密碼:sentinel/sentinel

3、SpringBoot 整合 Sentinel

3.1、Maven 依賴

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

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

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

3.3. 運(yùn)行測試

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

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

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

3.4.1. 修改上述 UserController 類

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

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

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

3.4.2. 啟動(dòng)上述項(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 "新增一個(gè)用戶";
    }

    // 限流處理類
    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ā)生限流后會(huì)拋出 BlockException 異常。這個(gè)時(shí)候可以捕獲異常,進(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) 否則會(huì)報(bào)錯(cuò)

@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)用支持示例

在啟動(dòng)類中添加 @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)文章

  • SpringBoot項(xiàng)目修改訪問端口和訪問路徑的方法

    SpringBoot項(xiàng)目修改訪問端口和訪問路徑的方法

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

    如何通過Java打印Word文檔

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

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

    本文主要介紹了java中TCP實(shí)現(xiàn)回顯服務(wù)器及客戶端,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(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代碼生成器的制作流程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • Spring整合Springmvc的相關(guān)介紹

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

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

    Java中ArrayList的使用詳細(xì)介紹

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

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

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

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

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

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

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

最新評論

大关县| 通道| 伊川县| 奈曼旗| 黄石市| 通渭县| 乳源| 南充市| 昭通市| 九江县| 无棣县| 冷水江市| 保靖县| 荥经县| 永春县| 临安市| 拉萨市| 南通市| 卓尼县| 新龙县| 梅河口市| 葫芦岛市| 平南县| 林芝县| 怀仁县| 汉寿县| 额敏县| 延寿县| 桃江县| 庄浪县| 新干县| 三河市| 昆明市| 西贡区| 沾化县| 饶平县| 张掖市| 班玛县| 东至县| 凤冈县| 滦平县|