建立项目cloud-sentineljavascript
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>
复制代码
server.port=18084
spring.application.name=service-sentinel
#Sentinel 控制台地址
spring.cloud.sentinel.transport.dashboard=localhost:8080
#取消Sentinel控制台懒加载
spring.cloud.sentinel.eager=true
复制代码
Sentinel 默认为全部的 HTTP 服务提供了限流埋点。引入依赖后自动完成全部埋点。只须要在控制配置限流规则便可java
@SentinelResource("resource")
@RequestMapping("/sentinel/hello")
public Map<String,Object> hello(){
Map<String,Object> map=new HashMap<>(2);
map.put("appName",appName);
map.put("method","hello");
return map;
}
复制代码
Sentinel下载git
执行 Java 命令 java -jar sentinel-dashboard.jar
默认的监听端口为 8080
github
打开http://localhost:8080 便可看到控制台界面 spring
若是控制台没有找到本身的应用,能够先调用一下进行了 Sentinel 埋点的 URL 或方法或着禁用Sentinel 的赖加载spring.cloud.sentinel.eager=true
数据库
控制器随便添加一个普通的http方法json
/** * 经过控制台配置URL 限流 * @return */
@RequestMapping("/sentinel/test")
public Map<String,Object> test(){
Map<String,Object> map=new HashMap<>(2);
map.put("appName",appName);
map.put("method","test");
return map;
}
复制代码
点击新增流控规则。为了方便测试阀值设为 1 浏览器
整个URL限流就完成了。可是返回的提示不够友好。bash
自定义限流规则就不是添加方法的访问路径。 配置的是@SentinelResource注解中value的值。好比@SentinelResource("resource")
就是配置路径为resource 并发
@SentinelResource
注解埋点配置的限流规则若是没有自定义处理限流逻辑,当请求到达限流的阀值时就返回404页面
@SentinelResource 注解包含如下属性:
//经过注解限流并自定义限流逻辑
@SentinelResource(value = "resource2", blockHandler = "handleException", blockHandlerClass = {ExceptionUtil.class})
@RequestMapping("/sentinel/test2")
public Map<String,Object> test2() {
Map<String,Object> map=new HashMap<>();
map.put("method","test2");
map.put("msg","自定义限流逻辑处理");
return map;
}
public class ExceptionUtil {
public static Map<String,Object> handleException(BlockException ex) {
Map<String,Object> map=new HashMap<>();
System.out.println("Oops: " + ex.getClass().getCanonicalName());
map.put("Oops",ex.getClass().getCanonicalName());
map.put("msg","经过@SentinelResource注解配置限流埋点并自定义处理限流后的逻辑");
return map;
}
}
复制代码
控制台新增resource2的限流规则并设置阀值为1。访问http://localhost:18084/sentinel/test2 请求到达阀值时机会返回自定义的异常消息
基本的限流处理就完成了。 可是每次服务重启后 以前配置的限流规则就会被清空由于是内存态的规则对象.因此下面就要用到Sentinel一个特性ReadableDataSource 获取文件、数据库或者配置中心是限流规则
一条限流规则主要由下面几个因素组成:
#经过文件读取限流规则
spring.cloud.sentinel.datasource.ds1.file.file=classpath: flowrule.json
spring.cloud.sentinel.datasource.ds1.file.data-type=json
spring.cloud.sentinel.datasource.ds1.file.rule-type=flow
复制代码
在resources新建一个文件 好比flowrule.json 添加限流规则
[
{
"resource": "resource",
"controlBehavior": 0,
"count": 1,
"grade": 1,
"limitApp": "default",
"strategy": 0
},
{
"resource": "resource3",
"controlBehavior": 0,
"count": 1,
"grade": 1,
"limitApp": "default",
"strategy": 0
}
]
复制代码
从新启动项目。出现以下日志说明文件读取成功
[Sentinel Starter] DataSource ds1-sentinel-file-datasource start to loadConfig
[Sentinel Starter] DataSource ds1-sentinel-file-datasource load 2 FlowRule
复制代码
刷新Sentinel 控制台 限流规则就会自动添加进去
spring.cloud.sentinel.enabled #Sentinel自动化配置是否生效
spring.cloud.sentinel.eager #取消Sentinel控制台懒加载
spring.cloud.sentinel.transport.dashboard #Sentinel 控制台地址
spring.cloud.sentinel.transport.heartbeatIntervalMs #应用与Sentinel控制台的心跳间隔时间
spring.cloud.sentinel.log.dir #Sentinel 日志文件所在的目录
复制代码