项目的配置:
pom.xml
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
<version>0.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-transport-simple-http</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-annotation-aspectj</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-web-servlet</artifactId>
<version>1.7.0</version>
</dependency>
application.yml
application:
name: xxxProjectName
cloud:
sentinel:
transport:
dashboard: localhost:8080 #sentinel DashBoard 的端口号和地址
eager: true
配置sentinel需要的bean 设置降级规则为快速失败等等
package com.lty.fsb.cabintech.config;
import com.alibaba.csp.sentinel.annotation.aspectj.SentinelResourceAspect;
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
@Configuration
public class SentinelAspectConfiguration {
@Bean
public SentinelResourceAspect sentinelResourceAspect() {
return new SentinelResourceAspect();
}
@PostConstruct
private void initRules() throws Exception {
FlowRule rule1 = new FlowRule();
rule1.setResource("getAllParkList");//@sentinelResource的value的名字
rule1.setGrade(RuleConstant.FLOW_GRADE_QPS); // 规则类型
rule1.setCount(1); // 每秒调用最大次数为 1 次
FlowRule rule2 = new FlowRule();
rule2.setResource("checkHealthInfo");
rule2.setGrade(RuleConstant.FLOW_GRADE_QPS);
rule2.setCount(1); // 每秒调用最大次数为 1 次
List<FlowRule> rules = new ArrayList<>();
rules.add(rule1);
rules.add(rule2);
// 将控制规则载入到 Sentinel
com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager.loadRules(rules);
}
}
public class AmountOptionalController {
@Autowired
AmountOptionalService amountOptionalService;
@RequestMapping("healthInfo")
public Result health() {
return amountOptionalService.checkHealthInfo();
}
}
@Override
@SentinelResource(value = "checkHealthInfo" ,fallback = "healthFallback")
public Result checkHealthInfo() {
return ResultUtil.requestSuccess("service health");
}
public Result healthFallback(){
return ResultUtil.requestFaild("进入限流");
}
@GetMapping("getAllParkList")
@SentinelResource(value = "getAllParkList",fallback = "getFailedParkList")
public Result getAllParkList() throws Exception {
return ResultUtil.requestSuccess("");
}
public Result getFailedParkList(){
return ResultUtil.requestFaild("请求太快,请求已拦截");
}
启动后请求接口 ,连续请求两次 结果:
配置dashboard:
文档
https://github.com/alibaba/Sentinel/wiki
dashboard release 下载地址:
https://github.com/alibaba/Sentinel/releases
启动Sentinel服务端后台管理
执行命令:java -Dserver.port=8080 -jar sentinel-dashboard-1.6.0.jar 默认8080端口
可用参数:
-Dcsp.sentinel.dashboard.server=ip:port | 启动端口号和Ip |
---|---|
-Dcsp.sentinel.api.port=8720 | |
-Dproject.name=report-test |
访问 localhost:8080
默认登录用户:sentinel
pwd:sentinel
在编辑规则处可以修改配置的参数
目前本人就学习到这里 还有个问题:
2020-04-02 15:04:53.985 ERROR 80796 --- [pool-2-thread-1] c.a.c.s.dashboard.metric.MetricFetcher : Failed to fetch metric from <http://192.168.18.162:8720/metric?startTime=1585811085000&endTime=1585811091000&refetch=false> (ConnectionException: Connection refused)
不知道如何处理 有大神的话 请私信 !!!