sentinel 各种示例的提交

This commit is contained in:
YunaiV
2020-01-30 17:47:04 +08:00
parent 1cf3789d97
commit 80595e87d3
3 changed files with 40 additions and 1 deletions

View File

@@ -43,7 +43,7 @@
<artifactId>sentinel-parameter-flow-control</artifactId>
<version>1.7.1</version>
</dependency>
<!-- Sentinel 对 Spring AOP 的支持 -->
<!-- Sentinel 对 Spring AOP 的拓展 -->
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-annotation-aspectj</artifactId>

View File

@@ -23,6 +23,20 @@ public class SpringMvcConfiguration implements WebMvcConfigurer {
SentinelWebMvcConfig config = new SentinelWebMvcConfig();
config.setHttpMethodSpecify(true); // 是否包含请求方法。即基于 URL 创建的资源,是否包含 Method。
// config.setBlockExceptionHandler(new DefaultBlockExceptionHandler()); // 设置 BlockException 处理器。
// config.setOriginParser(new RequestOriginParser() { // 设置请求来源解析器。用于黑白名单控制功能。
//
// @Override
// public String parseOrigin(HttpServletRequest request) {
// // 从 Header 中,获得请求来源
// String origin = request.getHeader("s-user");
// // 如果为空,给一个默认的
// if (StringUtils.isEmpty(origin)) {
// origin = "default";
// }
// return origin;
// }
//
// });
// 添加 SentinelWebInterceptor 拦截器
registry.addInterceptor(new SentinelWebInterceptor(config)).addPathPatterns("/**");

View File

@@ -1,6 +1,9 @@
package cn.iocoder.springboot.lab46.sentineldemo.controller;
import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -25,10 +28,32 @@ public class DemoController {
return "sleep";
}
// 测试热点参数限流
@GetMapping("/product_info")
@SentinelResource("demo_product_info_hot")
public String productInfo(Integer id) {
return "商品编号:" + id;
}
// 手动使用 Sentinel 客户端 API
@GetMapping("/entry_demo")
public String entryDemo() {
Entry entry = null;
try {
// 访问资源
entry = SphU.entry("entry_demo");
// ... 执行业务逻辑
return "执行成功";
} catch (BlockException ex) {
return "被拒绝";
} finally {
// 释放资源
if (entry != null) {
entry.exit();
}
}
}
}