Skip to content

Commit

Permalink
sentinel 各种示例的提交
Browse files Browse the repository at this point in the history
  • Loading branch information
YunaiV committed Jan 30, 2020
1 parent 1cf3789 commit 80595e8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lab-46/lab-46-sentinel-demo/pom.xml
Expand Up @@ -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>
Expand Down
Expand Up @@ -23,6 +23,20 @@ private void addSentinelWebInterceptor(InterceptorRegistry registry) {
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("/**");
Expand Down
@@ -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;
Expand All @@ -25,10 +28,32 @@ public String sleep() throws InterruptedException {
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();
}
}
}

}

0 comments on commit 80595e8

Please sign in to comment.