- 并发线程数
- QPS
- 资源
- 规则
- Entry->请求
- resoure
- count
- grade
- limitApp
- ControlBehavior
并发线程数
- Hystrix线程池隔离
- Sentinel 并发线程数
QPS(TPS)
策略(流量控制的策略)
-
直接拒绝
-
warm up(冷启动i)
-
匀速排队
通过ControlBehavior属性来控制策略
-
FlowRule
-
根据调用方来限流
limitApp -
根据调用链路入口来限流
-
针对具有关系的资源来限流
1、下载
github地址::https://github.com/alibaba/Sentinel/releases
2、启动命令
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.4.jar
配置:
3、配置文件挂载到控制台上
挂载成功
4、Jmeter测试
- 打开Jmeter新建一个Http请求
- 配置线程池
- 配置http请求
- 运行测试,观察控制台可以看见波动
- config center: zookeeper / apollo / etcd / redis / Consul / Eureka / Nacos
- 规则的感知(Pull,Push)
- pull
- push
Sentinel扩展(InitFunc实现)
原来代码是放在主方法里面的,例如:
package com.chaoxing.springboot_sentinel;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.ArrayList;
import java.util.List;
@SpringBootApplication
public class SpringBootSentinelApplication {
public static void main(String[] args) {
initFlowRule();
SpringApplication.run(SpringBootSentinelApplication.class, args);
}
private static void initFlowRule(){
List rules = new ArrayList<>();
FlowRule flowRule = new FlowRule();
//绑定规则名称,针对那个规则设置规则
flowRule.setResource("doTest");
//QPS或者并发数
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
//QPS=5
flowRule.setCount(5);
//添加规则
rules.add(flowRule);
FlowRuleManager.loadRules(rules);
}
}
现在:
1、创建FlowRuleInitFunc 引用InitFunc接口
2、设置Sentinel限流方式
package com.chaoxing.springboot_sentinel;
import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import java.util.ArrayList;
import java.util.List;
public class FlowRuleInitFunc implements InitFunc {
@Override
public void init() throws Exception {
List rules = new ArrayList<>();
FlowRule flowRule = new FlowRule();
//绑定规则名称,针对那个规则设置规则
flowRule.setResource("resource_Name");
//QPS或者并发数
flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
//QPS=5
flowRule.setCount(5);
//添加规则
rules.add(flowRule);
FlowRuleManager.loadRules(rules);
}
}
3、在resources下创建META-INF文件夹
4、在META-INF下创建 services文件夹
5、新建接口文件(SPI)进行扩展
文件名:com.alibaba.csp.sentinel.init.InitFunc
6、写入路径
1、导入依赖
com.alibaba.csp sentinel-datasource-nacos 1.8.0
2、编写方法
package com.chaoxing.springboot_sentinel;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import java.util.ArrayList;
import java.util.List;
public class FlowRuleInitFunc implements InitFunc {
private final String nacosAddress = "127.0.0.1:8848";
private final String groupId = "SENTINEL_GROUP";
private final String dataId ="SpringBoot-sentinel";
@Override
public void init() throws Exception {
// List rules = new ArrayList<>();
// FlowRule flowRule = new FlowRule();
// //绑定规则名称,针对那个规则设置规则
// flowRule.setResource("resource_Name");
// //QPS或者并发数
// flowRule.setGrade(RuleConstant.FLOW_GRADE_QPS);
// //QPS=5
// flowRule.setCount(5);
// //添加规则
// rules.add(flowRule);
// FlowRuleManager.loadRules(rules);
//从远程服务器加载规则(Nacos)
ReadableDataSource> flowRuleDs =
new NacosDataSource>(nacosAddress,groupId,dataId,
s -> JSON.parseObject(s,new TypeReference>(){}));
FlowRuleManager.register2Property(flowRuleDs.getProperty());
}
}
3、在nacos编写规则
1、在nacos设置规则
2、在配置文件中编辑
#使用nacos编辑规则方便Sentinel熔断降级机制的使用 spring.cloud.sentinel.datasource.dsl.nacos.server-addr=127.0.0.1:8848 spring.cloud.sentinel.datasource.dsl.nacos.data-id=SpringBoot-sentinel spring.cloud.sentinel.datasource.dsl.nacos.group-id=SENTINEL_GROUP spring.cloud.sentinel.datasource.dsl.nacos.rule-type=flow spring.cloud.sentinel.datasource.dsl.nacos.data-type=json spring.cloud.sentinel.datasource.dsl.nacos.username=nacos spring.cloud.sentinel.datasource.dsl.nacos.password=nacos
3、流控规则显示成功
1、集群示意图
2、建立project sentinel-token-sever
3、添加依赖
4.0.0 com.chaoxing.sentinel sentinel-token-sever 1.0-SNAPSHOT sentinel-token-sever http://www.example.com UTF-8 1.7 1.7 junit junit 4.11 test com.alibaba.csp sentinel-cluster-server-default 1.8.0 com.alibaba.csp sentinel-datasource-nacos 1.8.0 *** com.alibaba.csp sentinel-transport-simple-http 1.8.0 maven-clean-plugin 3.1.0 maven-resources-plugin 3.0.2 maven-compiler-plugin 3.8.0 maven-surefire-plugin 2.22.1 maven-jar-plugin 3.0.2 maven-install-plugin 2.5.2 maven-deploy-plugin 2.8.2 maven-site-plugin 3.7.1 org.apache.maven.plugins maven-compiler-plugin 8 8
4、建立服务类ClusterServer
package com.chaoxing.sentinel;
import com.alibaba.csp.sentinel.cluster.client.ClusterTokenClient;
import com.alibaba.csp.sentinel.cluster.server.ClusterTokenServer;
import com.alibaba.csp.sentinel.cluster.server.SentinelDefaultTokenServer;
import com.alibaba.csp.sentinel.cluster.server.config.ClusterServerConfigManager;
import com.alibaba.csp.sentinel.cluster.server.config.ServerTransportConfig;
import java.util.Collections;
public class ClusterServer {
public static void main(String[] args) throws Exception {
ClusterTokenServer tokenServer = new SentinelDefaultTokenServer();
//手动载入namespace和ClusterServerConfigManager
//集群限制流服务端通信相关配置
ClusterServerConfigManager.loadGlobalTransportConfig(new ServerTransportConfig().setIdleSeconds(600).setPort(9999));
//加载namespace集合列表
ClusterServerConfigManager.loadServerNamespaceSet(Collections.singleton("APP-Test"));
tokenServer.start();
//Token-client会上报自己的project.name到token-server会根据namespace来统一
}
}
5、建立获取配置类FlowRuleInitFunc
package com.chaoxing.sentinel;
import com.alibaba.csp.sentinel.cluster.flow.rule.ClusterFlowRuleManager;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.flow.ClusterFlowConfig;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import java.util.List;
public class FlowRuleInitFunc implements InitFunc {
private final String nacosAddress = "127.0.0.1:8848";
private final String groupId = "SENTINEL_GROUP";
private final String dataId ="-flow-rules";
@Override
public void init() throws Exception {
ClusterFlowRuleManager.setPropertySupplier(namespace->{
ReadableDataSource> flowRuleDs =
new NacosDataSource>(nacosAddress,groupId,namespace+dataId,
s -> JSON.parseObject(s,new TypeReference>(){}));
return flowRuleDs.getProperty();
});
}
}
6、添加从nacos获取配置的配置文件
7、添加VM
-Dcsp.sentinel.dashboard.server=127.0.0.1:8080 -Dproject.name=sentinel-token-sever
8、在nacos中配置
9、运行
10.客户端连接
修改FlowRuleInitFunc方法
package com.chaoxing.springboot_sentinel;
import com.alibaba.csp.sentinel.cluster.client.config.ClusterClientAssignConfig;
import com.alibaba.csp.sentinel.cluster.client.config.ClusterClientConfig;
import com.alibaba.csp.sentinel.cluster.client.config.ClusterClientConfigManager;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.csp.sentinel.init.InitFunc;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import java.util.ArrayList;
import java.util.List;
public class FlowRuleInitFunc implements InitFunc {
private final String nacosAddress = "127.0.0.1:8848";
private final String groupId = "SENTINEL_GROUP";
private final String dataId ="-flow-rules";
private final String clusterServerHost="localhost";
private final int clusterServerPort=9999;
private final int requestTimeOut=20000;
private final String appname = "App-Test";
@Override
public void init() throws Exception {
loadClusterConfig();
registerFlowRule();
}
private void loadClusterConfig(){
ClusterClientAssignConfig assignConfig = new ClusterClientAssignConfig();
assignConfig.setServerHost(clusterServerHost);//放到配置中心
assignConfig.setServerPort(clusterServerPort);
ClusterClientConfigManager.applyNewAssignConfig(assignConfig);
ClusterClientConfig clientConfig = new ClusterClientConfig();
clientConfig.setRequestTimeout(requestTimeOut);//放到配置中心
ClusterClientConfigManager.applyNewConfig(clientConfig);
}
private void registerFlowRule(){
ReadableDataSource> flowRuleDs =
new NacosDataSource>(nacosAddress,groupId,appname+dataId,
s -> JSON.parseObject(s,new TypeReference>(){}));
FlowRuleManager.register2Property(flowRuleDs.getProperty());
}
}
11、设置主方法为客户端
package com.chaoxing.springboot_sentinel;
import com.alibaba.csp.sentinel.cluster.ClusterStateManager;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.ArrayList;
import java.util.List;
@SpringBootApplication
public class SpringBootSentinelApplication {
public static void main(String[] args) {
ClusterStateManager.applyState(ClusterStateManager.CLUSTER_CLIENT);
SpringApplication.run(SpringBootSentinelApplication.class, args);
}
}
12 调试vm options
-Dsever.port=8888 -Dproject.name=App-Test -Dcsp.sentinel.dashboard.server=127.0.0.1:8080
13 确定配置文件可用
14 运行测试
连接成功
熔断降级



