1. 环境搭建
Jeecgboot 3.0
2. 坐标引入
在jeecg-cloud-gateway的pom文件中引入依赖
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-datasource-nacos</artifactId>
</dependency>
3.配置文件编写
在jeecg-cloud-gateway中编辑配置文件,编辑后的配置文件为
spring:
application:
name: jeecg-gateway
cloud:
#Sentinel配置
sentinel:
web-context-unify: false
transport:
dashboard: localhost:8087
# 懒加载Sentinel Dashboard菜单
eager: false
datasource:
flow-rules:
nacos:
server-addr: 127.0.0.1:8848
dataId: test111
username: naocs
password: nacos
groupId: DEFAULT_GROUP
data-type: json
rule-type: flow
4.配置类编写
在jeecg-cloud-gateway中增加配置类
package org.jeecg.config;
import com.alibaba.cloud.sentinel.SentinelProperties;
import com.alibaba.cloud.sentinel.datasource.config.NacosDataSourceProperties;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import java.util.Set;
/**
* @Description: 限流规则持久化
* @author: cmwl
* @Date: 2021/12/26 12:01
*/
@Configuration
@Order(2)
public class SentinelPersistenceConfig {
@Autowired
private SentinelProperties sentinelProperties;
@Bean
public SentinelPersistenceConfig init() throws Exception {
loadGWFlowRule();
return new SentinelPersistenceConfig();
}
private void loadGWFlowRule() {
sentinelProperties.getDatasource().entrySet().stream().filter(map -> {
return map.getValue().getNacos() != null;
}).forEach(map -> {
NacosDataSourceProperties nacos = map.getValue().getNacos();
ReadableDataSource<String, Set<GatewayFlowRule>> gwFlowRuleDataSource = new NacosDataSource<>(
nacos.getServerAddr(), nacos.getGroupId(), nacos.getDataId(),
source -> JSON.parseObject(source, new TypeReference<Set<GatewayFlowRule>>() {
}));
GatewayRuleManager.register2Property(gwFlowRuleDataSource.getProperty());
});
}
}
5. nacos增加配置文件
上图中的配置内容如下:
[
{
"resource": "jeecg-system",
"limitApp": "default",
"grade": 1,
"count": 2,
"strategy": 0,
"controlBehavior": 0,
"clusterMode": false
}
]
6. 测试接口编写
在jeecg-boot-module-system微服务中,增加controller接口
package org.jeecg.modules.sentinel;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Slf4j
@RestController
@RequestMapping("/sys")
public class TestController {
@GetMapping(value = "/testSentinel")
public Result<?> testSentinel(HttpServletRequest request, HttpServletResponse response) {
Result<?> result = new Result<>();
return Result.OK("成功获取sentinel接口today");
}
}
记得在shiroconfig中增加该路径的过滤
filterChainDefinitionMap.put("/sys/testSentinel", "anon"); //sentinel测试
7.测试结果
依次启动JeecgNacosApplication, JeecgGateWayApplication, JeecgSentinelDashboardApplication和JeecgSystemCloudApplication, 然后在浏览器输入地址http://localhost:9999/sys/testSentinel,在sentinel面板中可以看到流控规则
快速点击浏览器的刷新按钮,连续访问该controller接口,可以看到如下提示