2.Sentinel
spring cloud alibaba 整合 sentinel
1.引入alibaba-sentinel的starter依赖
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-sentinel</artifactId></dependency>
2.配置application.yml文件
server: port: 8010 spring: application: name: order-sentinel-dev cloud: sentinel: transport: #注意 这里是我的vmware centos服务器的IP #需要把这里的ip和端口换成你自己的sentinel dashboard的jar包进程端口 dashboard: 192.168.86.88:8333
上面提及的sentinel的dashboard需要提供一个jar包进程
并不是内嵌在上面的maven依赖里面的是一个单独的服务
如何 下载jar 并启动该dashboard
找到sentinel的github 下载对应的jar https://github.com/alibaba/Sentinel/releases
#8333是我自定义的 可以替换修改
java -Dserver.port=8333 -Dcsp.sentinel.dashboard.server=localhost:8333 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.x.x.jar
启动好之后 运行项目 先请求几次接口 随后访问dashboard
访问的url是我们
yml里面配置的 192.168.86.88:8333
Sentinel
QPS 线程数
- 流量控制
- 直接流控
- 关联流控
- 链路流控
自定义流量控制
"/hello") (value="hello",blockHandler="helloBlockHandler") (publicStringgetHello() { return"hello! hello!"; } "/helloThread") (value="helloThread",blockHandler="helloBlockHandler") (publicStringgetHelloThread() throwsInterruptedException { TimeUnit.SECONDS.sleep(5); return"hello! hello!"; } //BlockException publicStringhelloBlockHandler(BlockExceptione){ return"服务被流控了!"; } 自定义异常统一处理packagecom.example.ordersentinel.domain; /*** @ClassName Result* @Description* @Author Jackson* @Date 2023/4/13* @Version 1.0**/publicclassResult<T> { privateIntegercode; privateStringmsg; privateTdata; publicResult() { } publicResult(Integercode, Stringmsg) { this.code=code; this.msg=msg; } publicResult(Integercode, Stringmsg, Tdata) { this.code=code; this.msg=msg; this.data=data; } publicIntegergetCode() { returncode; } publicvoidsetCode(Integercode) { this.code=code; } publicStringgetMsg() { returnmsg; } publicvoidsetMsg(Stringmsg) { this.msg=msg; } publicTgetData() { returndata; } publicvoidsetData(Tdata) { this.data=data; } publicstaticResulterror(Integercode, Stringmsg) { returnnewResult(code, msg); } } packagecom.example.ordersentinel.exception; importcom.alibaba.csp.sentinel.adapter.spring.webmvc.callback.BlockExceptionHandler; importcom.alibaba.csp.sentinel.slots.block.BlockException; importcom.alibaba.csp.sentinel.slots.block.authority.AuthorityException; importcom.alibaba.csp.sentinel.slots.block.degrade.DegradeException; importcom.alibaba.csp.sentinel.slots.block.flow.FlowException; importcom.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowException; importcom.alibaba.csp.sentinel.slots.system.SystemBlockException; importcom.example.ordersentinel.domain.Result; importcom.fasterxml.jackson.databind.ObjectMapper; importorg.slf4j.Logger; importorg.slf4j.LoggerFactory; importorg.springframework.http.MediaType; importorg.springframework.stereotype.Component; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; /*** @ClassName MyBlockExceptionHandler* @Description* @Author Jackson* @Date 2023/4/13* @Version 1.0**/publicclassMyBlockExceptionHandlerimplementsBlockExceptionHandler { privatestaticfinalIntegerFLOWEXCECODE=100; privatestaticfinalIntegerDEGRADEEXCECODE=101; privatestaticfinalIntegerPARAMFLOWEXCECODE=102; privatestaticfinalIntegerSYSTEMBLOCKEXCECODE=103; privatestaticfinalIntegerAUTHORITYEXCECODE=104; Loggerlogger=LoggerFactory.getLogger(this.getClass()); publicvoidhandle(HttpServletRequesthttpServletRequest, HttpServletResponsehttpServletResponse, BlockExceptione) throwsException { logger.info("BlockExceptionHandler-----------------{}", e.getRule()); Resultr=null; if (einstanceofFlowException) { r=Result.error(FLOWEXCECODE, "接口限流"); } elseif (einstanceofDegradeException) { r=Result.error(DEGRADEEXCECODE, "服务降级"); } elseif (einstanceofParamFlowException) { r=Result.error(PARAMFLOWEXCECODE, "热点参数限流"); } elseif (einstanceofSystemBlockException) { r=Result.error(SYSTEMBLOCKEXCECODE, "触发系统保护规则"); } elseif (einstanceofAuthorityException) { r=Result.error(AUTHORITYEXCECODE, "授权不通过"); } httpServletResponse.setStatus(500); httpServletResponse.setCharacterEncoding("utf-8"); httpServletResponse.setContentType(MediaType.APPLICATION_JSON_VALUE); newObjectMapper().writeValue(httpServletResponse.getWriter(), r); } }
流控效果
- 快速失败
- Warm up
- 排队等待