赞
踩
参考:https://blog.csdn.net/ThinkWon/article/details/103770879
https://github.com/alibaba/Sentinel/wiki/%E6%96%B0%E6%89%8B%E6%8C%87%E5%8D%97
官网下载:https://github.com/alibaba/Sentinel/tags
下载完成后在命令行输入如下命令运行Sentinel控制台:
java -jar sentinel-dashboard-1.7.0.jar
Sentinel控制台默认运行在8080端口上,登录账号密码均为sentinel,通过如下地址可以进行访问:http://localhost:8080
加pom:
<!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-sentinel -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>2.2.1.RELEASE</version>
</dependency>
在application.yml中添加相关配置,主要是配置了Nacos和Sentinel控制台的地址:
server: port: 9093 spring: application: name: business-9093 # datasource: # # url: jdbc:mysql://rm-2vc7l62g4u9rt2l82vo.mysql.cn-chengdu.rds.aliyuncs.com:3306/materialbasecloud?characterEncoding=utf8&useSSL=false&autoReconnect=true&rewriteBatchedStatements=true # # username: root # # password: jjqhc123456 # url: jdbc:mysql://49.235.125.47:3306/live?characterEncoding=utf8&useSSL=false&autoReconnect=true&rewriteBatchedStatements=true # username: root # password: 123456 # driver-class-name: com.mysql.cj.jdbc.Driver # hikari: # max-lifetime: 180000 # minimum-idle: 20 # #设置最大线程数量: # maximum-pool-size: 30 # idle-timeout: 120000 # # 加了之后容易超时 # # connection-test-query: SELECT 1 #redis redis: # redis数据库索引(默认为0),我们使用索引为3的数据库,避免和其他数据库冲突,通过rdm查看时,在db3的下面 database: 1 # redis服务器地址(默认为loaclhost) host: 49.235.125.47 # redis端口(默认为6379) port: 6379 # redis访问密码(默认为空) password: 123456 # redis连接超时时间(单位毫秒) timeout: 10000 # redis连接池配置 jedis: pool: # 最大可用连接数(默认为8,负数表示无限) max-active: 8 # 最大空闲连接数(默认为8,负数表示无限) max-idle: 8 # 最小空闲连接数(默认为0,该值只有为正数才有用) min-idle: 0 # 从连接池中获取连接最大等待时间(默认为-1,单位为毫秒,负数表示无限) max-wait: -1 cloud: nacos: discovery: server-addr: 127.0.0.1:8848 # 配置sentinel客户端,注册进控制台里 sentinel: transport: dashboard: 127.0.0.1:8080 # 为应用开启额外的端口,上报监控信息 # 默认为8719端口,加入被占用将会自动从8719开始+1扫描,直至找到未被占用的端口地址 port: 8719 datasource: #配置流控规则持久化 ds1: nacos: server-addr: localhost:8848 dataId: cloudalibaba-sentinel-service groupId: DEFAULT_GROUP data-type: json rule-type: flow ## 监控相关 anagement: endpoints: web: exposure: include: '*' #开启 sentinel 对 feign 的支持 feign: sentinel: enabled: true
要访问了controller之后,才会有值,懒加载!!
编写controller测试:
package com.example.businesssentinel9093.controller; import cn.hutool.extra.tokenizer.Result; import com.alibaba.csp.sentinel.annotation.SentinelResource; import com.alibaba.csp.sentinel.slots.block.BlockException; import com.example.businesssentinel9093.myhandler.CustomerBlockHandler; import com.example.dtestcommon.vo.Res; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/sentinel") public class BusinessSentinelController { @PostMapping("/test") public String test(){ return "9093"; } /** * 按资源名称限流,需要指定限流处理逻辑 * * @return */ @PostMapping("/byResource") @SentinelResource(value = "byResource", blockHandler = "handleException") public Res byResource() { return Res.success(200,"按资源名称限流"); } /** * 按url限流,有默认的限流处理逻辑 * * @return */ @PostMapping("byUrl") @SentinelResource(value = "byUrl", blockHandler = "handleException") public Res byUrl() { return Res.success(200,"按url限流"); } public Res handleException(BlockException exception) { return Res.success(200,exception.getClass().getCanonicalName()); } @PostMapping("/customBlockHandler") @SentinelResource(value = "customBlockHandler", blockHandler = "handlerException", blockHandlerClass = CustomerBlockHandler.class) public Res blockHandler() { return Res.success(200,"限流成功"); } }
package com.example.businesssentinel9093.myhandler; import com.alibaba.csp.sentinel.slots.block.BlockException; import com.example.dtestcommon.vo.Res; public class CustomerBlockHandler { public static Res handlerException(BlockException exception) { return Res.success(444,"客户自定义限流处理信息1,global handlerException-----1"); } public static Res handlerException2(BlockException exception) { return Res.success(444,"客户自定义限流处理信息2,global handlerException-----2"); } }
测试:
前:
超过阈值后:
配置可以自己选择,资源吗或者路径的!!!
当关联的资源达到限流条件时,该资源才会开启限流
配置请求 /customBlockHandler限流QPS,关联 /byResource
当关联的资源达到阈值时customBlockHandler,会对该资源byResource进行限流
使用jmeter进行对customBlockHandler资源的访问:
写controller测试: package com.example.businesssentinel9093.controller; import com.alibaba.csp.sentinel.annotation.SentinelResource; import com.example.businesssentinel9093.myhandler.CustomerfallbackHandler; import com.example.dtestcommon.vo.Res; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; @RestController @RequestMapping("/downGroud") public class DownGrandSentinelController { // 匹配返回值要相同 /** * 降级测试 * @param * @return */ @GetMapping("/time") // @SentinelResource(value = "time",fallback = "handleFallback2") public Res time(){ //当请求参数为 1 时,响应延迟600ms 也就是RT可以配置允许时间比这个数低的值 try { Thread.sleep(600); } catch (InterruptedException e) { e.printStackTrace(); } return Res.success("jiangji test 当前时间:"+new Date().getTime()); } @GetMapping("/time2") @SentinelResource(value = "time2",blockHandler = "handlerException",blockHandlerClass = CustomerfallbackHandler.class) public Res time2(){ //当请求参数为 1 时,响应延迟600ms 也就是RT可以配置允许时间比这个数低的值 try { Thread.sleep(600); } catch (InterruptedException e) { e.printStackTrace(); } return Res.success("jiangji test 当前时间:"+new Date().getTime()); } @GetMapping("/fallbackException/{id}") @SentinelResource(value = "fallbackException", fallback = "handleFallback2", exceptionsToIgnore = {NullPointerException.class}) public Res fallbackException(@PathVariable Long id) { if (id == 1) { throw new IndexOutOfBoundsException(); } else if (id == 2) { throw new NullPointerException(); } return Res.success(1,"异常降级!!!"); } public Res handleFallback(Long id) { return Res.success(200,"服务降级返回1"); } public Res handleFallback2(Long id, Throwable e) { return Res.success(200,"服务降级返回2"); } }
package com.example.businesssentinel9093.myhandler; import com.alibaba.csp.sentinel.slots.block.BlockException; import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException; import com.example.dtestcommon.vo.Res; public class CustomerfallbackHandler { public static Res handlerException(BlockException exception) { return Res.success(444,"客户自定义限流处理信息1,global handlerException-----1"); } public static Res handlerException2(BlockException exception) { return Res.success(444,"客户自定义限流处理信息2,global handlerException-----2"); } }
若时不用@SentinelResource注解,会使用默认的
package com.example.businesssentinel9093.controller; import com.alibaba.csp.sentinel.annotation.SentinelResource; import com.example.businesssentinel9093.myhandler.CustomerfallbackHandler; import com.example.dtestcommon.vo.Res; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; @RestController @RequestMapping("/downGroud") public class DownGrandSentinelController { @GetMapping("/fallbackException/{id}") @SentinelResource(value = "fallbackException", fallback = "handleFallback2", exceptionsToIgnore = {NullPointerException.class}) public Res fallbackException(@PathVariable Long id) { if (id == 1) { throw new IndexOutOfBoundsException(); } else if (id == 2) { throw new NullPointerException(); } return Res.success(1,"异常降级!!!"); } public Res handleFallback(Long id) { return Res.success(200,"服务降级返回1"); } public Res handleFallback2(Long id, Throwable e) { return Res.success(200,"服务降级返回2"); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。