sentinel基本使用
1.1 编程式
下面是启动main方法,然后会创建限流规则initFlowRules()
然后进入while循环,代码块Thread.sleep(10)
和System.out.println("hello world")
就是业务方法。然后entry = SphU.entry("HelloWorld")
是使用了一个HelloWorld
的资源。这个资源的限流模式再initFlowRules()
里面已经定义了。
public class MySentinelTest {
public static void main(String[] args) {
initFlowRules();
while (true) {
Entry entry = null;
try {
entry = SphU.entry("HelloWorld");
/*您的业务逻辑 - 开始*/
Thread.sleep(10);
System.out.println("hello world");
/*您的业务逻辑 - 结束*/
} catch (BlockException | InterruptedException e) {
/*流控逻辑处理 - 开始*/
System.out.println("block!");
/*流控逻辑处理 - 结束*/
} finally {
if (entry != null) {
entry.exit();
}
}
}
}
private static void initFlowRules(){
List<FlowRule> rules = new ArrayList<>();
FlowRule rule = new FlowRule();
rule.setResource("HelloWorld");
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
// Set limit QPS to 20.
rule.setCount(20);
rules.add(rule);
FlowRuleManager.loadRules(rules);
}
}
上面代码执行可以新建一个maven项目,然后导入sentinel依赖。由于现在spring主要推荐3.x所以无法再spring init上面创建jdk为1.8的项目。大家可以去alibaba上面创建空的项目。地址如下:https://start.aliyun.com/
1.2 注解式
第一步:创建一个controller层和service层。其中service层使用注解控制资源
controller层代码
@RestController
public class MySentinelController {
@Autowired
private MySentinelService mySentinelService;
@GetMapping("/helloworld")
public String helloworld(){
String message = mySentinelService.getInfo();
return message;
}
}
service层代码
controller层会调用service的getInfo方法。再getInfo方法上面新增资源注解@SentinelResource(value = "helloworld",blockHandler = "blockMessage
其中value是资源的名字,blockHandler是如果触发限流,那么执行什么方法。这个例子里面如果触发限流会调用下面的blockMessage
方法。主要这里的入参需要时BlockException
@Service
public class MySentinelService {
@SentinelResource(value = "helloworld",blockHandler = "blockMessage")
public String getInfo() {
return "hello world sentinel";
}
public String blockMessage(BlockException e){
System.out.println(e);
return "阻塞了!";
}
}
第二步:创建限流规则hellworld
springboot启动类里面新增初始化限流规则的方法
@SpringBootApplication
public class LearningSentinelApplication {
public static void main(String[] args) {
SpringApplication.run(LearningSentinelApplication.class, args);
initFlowRules();
}
private static void initFlowRules(){
List<FlowRule> rules = new ArrayList<>();
FlowRule rule = new FlowRule();
rule.setResource("helloworld");
rule.setGrade(RuleConstant.FLOW_GRADE_QPS);
// Set limit QPS to 20.
rule.setCount(1);
rules.add(rule);
FlowRuleManager.loadRules(rules);
}
}
:::success
上面是一个简单的sentinel入门程序,能够让我们看到sentinel的作用,也了解一些再sentinel里面的概念,比如:资源。下面是根据上面代码整合sentinel提供的dashboard
:::
dashboard环境搭建
2.1 下载dashboard
可以去github下载
2.2 启动dashboard
java -Dserver.port=8080 -Dcsp.sentinel.dashboard.server=localhost:8080 -Dproject.name=sentinel-dashboard -jar sentinel-dashboard-1.8.7.jar
2.3 访问dashboard
地址:http://localhost:8080/#/login
默认账号密码:sentinel/sentinel
访问结果:
2.4 项目接入dashboard
修改项目配置文件
# Sentinel 控制台地址
spring.cloud.sentinel.transport.dashboard=localhost:8080
# 取消Sentinel控制台懒加载
# 默认情况下 Sentinel 会在客户端首次调用的时候进行初始化,开始向控制台发送心跳包
# 配置 sentinel.eager=true 时,取消Sentinel控制台懒加载功能
spring.cloud.sentinel.eager=true
# 如果有多套网络,又无法正确获取本机IP,则需要使用下面的参数设置当前机器可被外部访问的IP地址,供admin控制台使用
# spring.cloud.sentinel.transport.client-ip=
server.port=8081
spring.application.name=learning_sentinel
spring boot启动类去掉初始化限流规则
在spring启动类中去掉限流规则。可以在dashboard上面创建限流规则,然后再业务代码中使用。后面也可以整合nacos通过nacos下发限流规则
public static void main(String[] args) {
SpringApplication.run(LearningSentinelApplication.class, args);
// initFlowRules();
}
2.5 启动项目,在dashboard上面配置helloworld限流规则
整合nacos实现下发限流规则
3.1 下载nacos客户端
可以去github上面下载
3.2 启动nacos
启动nacos
.\startup.cmd -m standalone
访问地址
http://192.168.31.14:8848/nacos/index.html
3.3 修改项目配置
项目添加nacos依赖
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
修改项目配置文件
下面是配置项目里面的application.properties
新增nacos地址,其中dataId一定要和配置的nacos保持一致,groupId也是一样
spring.cloud.sentinel.datasource.ds.nacos.server-addr=127.0.0.1:8848
spring.cloud.sentinel.datasource.ds.nacos.dataId=learning-sentinel
spring.cloud.sentinel.datasource.ds.nacos.groupId=DEFAULT_GROUP
spring.cloud.sentinel.datasource.ds.nacos.ruleType=flow
3.4 在nacos上面新增流控配置
其中json表示配置的资源和资源的流控规则
[
{
"resource": "helloworld",
"limitApp": "default",
"grade": 1,
"count": 2,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}
]