當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
Springboot/Cloud集成Sentinel进阶实战
生活随笔
收集整理的這篇文章主要介紹了
Springboot/Cloud集成Sentinel进阶实战
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 一、自定義限流處理
- 1. 自定義處理類(lèi)
- 2. 請(qǐng)求一次測(cè)試
- 3. 重新配置流控規(guī)則
- 4. 重新測(cè)試
- 5. controller
- 二、方法限流處理
- 2.1. 創(chuàng)建接口
- 2.2. 創(chuàng)建接口實(shí)現(xiàn)類(lèi)
- 2.3. 接口調(diào)用
- 2.4. 請(qǐng)求
- 2.5. 設(shè)置流控規(guī)則
一、自定義限流處理
自定義限流文檔
1. 自定義處理類(lèi)
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自定義全局異常類(lèi)返回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 {//如果超過(guò)流控管理的就拋出異常throw new BaseServiceException(ServiceErrCode.REQ_PARAM_ERR.getMsg(), ServiceErrCode.REQ_PARAM_ERR);} } //這里采用了返回json2. 請(qǐng)求一次測(cè)試
由于sentinel流控規(guī)則存在內(nèi)存中,springboot項(xiàng)目重啟,流控規(guī)則就沒(méi)了,需要重新設(shè)置,下面會(huì)重點(diǎn)解決此問(wèn)題
http://localhost:8082/sentinel3. 重新配置流控規(guī)則
4. 重新測(cè)試
http://localhost:8082/sentinel請(qǐng)求數(shù)量>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全局異常統(tǒng)一處理返回json
https://gblfy.blog.csdn.net/article/details/113824175
二、方法限流處理
2.1. 創(chuàng)建接口
package com.gblfy.distributedlimiter.service;public interface LimiterService {public String process(); }2.2. 創(chuàng)建接口實(shí)現(xiàn)類(lèi)
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")//自定義埋點(diǎn)public String process() {return "process";} }2.3. 接口調(diào)用
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. 請(qǐng)求
http://localhost:8082/sentinel
2.5. 設(shè)置流控規(guī)則
具體信息相見(jiàn)文檔,后續(xù)補(bǔ)充
https://github.com/alibaba/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md
總結(jié)
以上是生活随笔為你收集整理的Springboot/Cloud集成Sentinel进阶实战的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Wrapper+map实现页面显示
- 下一篇: Linux Shell脚本_关闭防火墙