日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Springboot/Cloud集成Sentinel进阶实战

發布時間:2024/9/27 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Springboot/Cloud集成Sentinel进阶实战 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

          • 一、自定義限流處理
            • 1. 自定義處理類
            • 2. 請求一次測試
            • 3. 重新配置流控規則
            • 4. 重新測試
            • 5. controller
          • 二、方法限流處理
            • 2.1. 創建接口
            • 2.2. 創建接口實現類
            • 2.3. 接口調用
            • 2.4. 請求
            • 2.5. 設置流控規則

一、自定義限流處理

自定義限流文檔

1. 自定義處理類
package com.gblfy.distributedlimiter.handle;import com.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler; import com.alibaba.csp.sentinel.slots.block.BlockException; import com.gblfy.distributedlimiter.enums.ServiceErrCode; import com.gblfy.distributedlimiter.exception.BaseServiceException; import org.springframework.stereotype.Component;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;/*** Springboot自定義全局異常類返回json* https://www.cnblogs.com/maolinjava/archive/2018/12/28/10193280.html*/ @Component public class LimiterBlockHandler implements BlockExceptionHandler {@Overridepublic void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, BlockException e) throws Exception {//如果超過流控管理的就拋出異常throw new BaseServiceException(ServiceErrCode.REQ_PARAM_ERR.getMsg(), ServiceErrCode.REQ_PARAM_ERR);} } //這里采用了返回json
2. 請求一次測試

由于sentinel流控規則存在內存中,springboot項目重啟,流控規則就沒了,需要重新設置,下面會重點解決此問題

http://localhost:8082/sentinel
3. 重新配置流控規則

4. 重新測試
http://localhost:8082/sentinel

請求數量>1

5. controller
package com.gblfy.distributedlimiter.controller;import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class SentinelLimiterController {@GetMapping("/sentinel")public String sentinel() {return "sentinel";} }

Springboot全局異常統一處理返回json
https://gblfy.blog.csdn.net/article/details/113824175

二、方法限流處理
2.1. 創建接口
package com.gblfy.distributedlimiter.service;public interface LimiterService {public String process(); }
2.2. 創建接口實現類
package com.gblfy.distributedlimiter.service.impl;import com.alibaba.csp.sentinel.annotation.SentinelResource; import com.gblfy.distributedlimiter.service.LimiterService; import org.springframework.stereotype.Service;@Service public class LimiterServiceImpl implements LimiterService {@Override@SentinelResource("LimiterService.process")//自定義埋點public String process() {return "process";} }
2.3. 接口調用
package com.gblfy.distributedlimiter.controller;import com.gblfy.distributedlimiter.service.LimiterService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class SentinelLimiterController {@Autowiredprivate LimiterService limiterService;@GetMapping("/sentinel")public String sentinel() {return limiterService.process();} }
2.4. 請求

http://localhost:8082/sentinel

2.5. 設置流控規則


具體信息相見文檔,后續補充
https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md

總結

以上是生活随笔為你收集整理的Springboot/Cloud集成Sentinel进阶实战的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。