浏览代码

增加网关限流规则,aip规则持久化nacos

dengsixing 3 年之前
父节点
当前提交
0a7022cb18

+ 1 - 0
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/DegradeController.java

@@ -93,6 +93,7 @@ public class DegradeController {
             return Result.ofFail(-1, "port can't be null");
         }
         try {
+            Thread.sleep(100);
             List<DegradeRuleEntity> rules = ruleProvider.getRules(app);
             rules = repository.saveAll(rules);
             return Result.ofSuccess(rules);

+ 33 - 11
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/gateway/GatewayApiController.java

@@ -20,16 +20,20 @@ import com.alibaba.csp.sentinel.dashboard.auth.AuthService;
 import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient;
 import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity;
 import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiPredicateItemEntity;
+import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
 import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo;
 import com.alibaba.csp.sentinel.dashboard.domain.Result;
 import com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.AddApiReqVo;
 import com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.ApiPredicateItemVo;
 import com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.UpdateApiReqVo;
 import com.alibaba.csp.sentinel.dashboard.repository.gateway.InMemApiDefinitionStore;
+import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
+import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
 import com.alibaba.csp.sentinel.util.StringUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.util.CollectionUtils;
 import org.springframework.web.bind.annotation.*;
 
@@ -56,6 +60,13 @@ public class GatewayApiController {
     @Autowired
     private SentinelApiClient sentinelApiClient;
 
+    @Autowired
+    @Qualifier("apiGroupRuleNacosProvider")
+    private DynamicRuleProvider<List<ApiDefinitionEntity>> ruleProvider;
+    @Autowired
+    @Qualifier("apiGroupRuleNacosPublisher")
+    private DynamicRulePublisher<List<ApiDefinitionEntity>> rulePublisher;
+
     @GetMapping("/list.json")
     @AuthAction(AuthService.PrivilegeType.READ_RULE)
     public Result<List<ApiDefinitionEntity>> queryApis(String app, String ip, Integer port) {
@@ -71,8 +82,14 @@ public class GatewayApiController {
         }
 
         try {
-            List<ApiDefinitionEntity> apis = sentinelApiClient.fetchApis(app, ip, port).get();
+//            List<ApiDefinitionEntity> apis = sentinelApiClient.fetchApis(app, ip, port).get();
+//            if(CollectionUtils.isEmpty(apis)){
+//                apis = ruleProvider.getRules(app);
+//            }
+            Thread.sleep(100);
+            List<ApiDefinitionEntity> apis = ruleProvider.getRules(app);
             repository.saveAll(apis);
+            publishApis(app);
             return Result.ofSuccess(apis);
         } catch (Throwable throwable) {
             logger.error("queryApis error:", throwable);
@@ -82,7 +99,8 @@ public class GatewayApiController {
 
     @PostMapping("/new.json")
     @AuthAction(AuthService.PrivilegeType.WRITE_RULE)
-    public Result<ApiDefinitionEntity> addApi(HttpServletRequest request, @RequestBody AddApiReqVo reqVo) {
+    public Result<ApiDefinitionEntity> addApi(HttpServletRequest request, @RequestBody AddApiReqVo reqVo)
+        throws Exception {
 
         String app = reqVo.getApp();
         if (StringUtil.isBlank(app)) {
@@ -140,7 +158,8 @@ public class GatewayApiController {
         entity.setPredicateItems(new LinkedHashSet<>(predicateItemEntities));
 
         // 检查API名称不能重复
-        List<ApiDefinitionEntity> allApis = repository.findAllByMachine(MachineInfo.of(app.trim(), ip.trim(), port));
+//        List<ApiDefinitionEntity> allApis = repository.findAllByMachine(MachineInfo.of(app.trim(), ip.trim(), port));
+        List<ApiDefinitionEntity> allApis = ruleProvider.getRules(app);
         if (allApis.stream().map(o -> o.getApiName()).anyMatch(o -> o.equals(apiName.trim()))) {
             return Result.ofFail(-1, "apiName exists: " + apiName);
         }
@@ -156,7 +175,7 @@ public class GatewayApiController {
             return Result.ofThrowable(-1, throwable);
         }
 
-        if (!publishApis(app, ip, port)) {
+        if (!publishApis(app)) {
             logger.warn("publish gateway apis fail after add");
         }
 
@@ -165,7 +184,8 @@ public class GatewayApiController {
 
     @PostMapping("/save.json")
     @AuthAction(AuthService.PrivilegeType.WRITE_RULE)
-    public Result<ApiDefinitionEntity> updateApi(@RequestBody UpdateApiReqVo reqVo) {
+    public Result<ApiDefinitionEntity> updateApi(@RequestBody UpdateApiReqVo reqVo)
+        throws Exception {
         String app = reqVo.getApp();
         if (StringUtil.isBlank(app)) {
             return Result.ofFail(-1, "app can't be null or empty");
@@ -219,7 +239,7 @@ public class GatewayApiController {
             return Result.ofThrowable(-1, throwable);
         }
 
-        if (!publishApis(app, entity.getIp(), entity.getPort())) {
+        if (!publishApis(app)) {
             logger.warn("publish gateway apis fail after update");
         }
 
@@ -229,7 +249,7 @@ public class GatewayApiController {
     @PostMapping("/delete.json")
     @AuthAction(AuthService.PrivilegeType.DELETE_RULE)
 
-    public Result<Long> deleteApi(Long id) {
+    public Result<Long> deleteApi(Long id) throws Exception {
         if (id == null) {
             return Result.ofFail(-1, "id can't be null");
         }
@@ -246,15 +266,17 @@ public class GatewayApiController {
             return Result.ofThrowable(-1, throwable);
         }
 
-        if (!publishApis(oldEntity.getApp(), oldEntity.getIp(), oldEntity.getPort())) {
+        if (!publishApis(oldEntity.getApp())) {
             logger.warn("publish gateway apis fail after delete");
         }
 
         return Result.ofSuccess(id);
     }
 
-    private boolean publishApis(String app, String ip, Integer port) {
-        List<ApiDefinitionEntity> apis = repository.findAllByMachine(MachineInfo.of(app, ip, port));
-        return sentinelApiClient.modifyApis(app, ip, port, apis);
+    private boolean publishApis(String app) throws Exception {
+        List<ApiDefinitionEntity> apis = repository.findAllByApp(app);
+        rulePublisher.publish(app, apis);
+        return true;
+//        return sentinelApiClient.modifyApis(app, ip, port, apis);
     }
 }

+ 269 - 0
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/gateway/GatewayApiController_bak.java

@@ -0,0 +1,269 @@
+///*
+// * Copyright 1999-2018 Alibaba Group Holding Ltd.
+// *
+// * Licensed under the Apache License, Version 2.0 (the "License");
+// * you may not use this file except in compliance with the License.
+// * You may obtain a copy of the License at
+// *
+// *      http://www.apache.org/licenses/LICENSE-2.0
+// *
+// * Unless required by applicable law or agreed to in writing, software
+// * distributed under the License is distributed on an "AS IS" BASIS,
+// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// * See the License for the specific language governing permissions and
+// * limitations under the License.
+// */
+//package com.alibaba.csp.sentinel.dashboard.controller.gateway;
+//
+//import static com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants.URL_MATCH_STRATEGY_EXACT;
+//import static com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants.URL_MATCH_STRATEGY_PREFIX;
+//import static com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants.URL_MATCH_STRATEGY_REGEX;
+//
+//import com.alibaba.csp.sentinel.dashboard.auth.AuthAction;
+//import com.alibaba.csp.sentinel.dashboard.auth.AuthService;
+//import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient;
+//import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity;
+//import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiPredicateItemEntity;
+//import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo;
+//import com.alibaba.csp.sentinel.dashboard.domain.Result;
+//import com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.AddApiReqVo;
+//import com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.ApiPredicateItemVo;
+//import com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.api.UpdateApiReqVo;
+//import com.alibaba.csp.sentinel.dashboard.repository.gateway.InMemApiDefinitionStore;
+//import com.alibaba.csp.sentinel.util.StringUtil;
+//import java.util.ArrayList;
+//import java.util.Arrays;
+//import java.util.Date;
+//import java.util.LinkedHashSet;
+//import java.util.List;
+//import javax.servlet.http.HttpServletRequest;
+//import org.slf4j.Logger;
+//import org.slf4j.LoggerFactory;
+//import org.springframework.beans.factory.annotation.Autowired;
+//import org.springframework.util.CollectionUtils;
+//import org.springframework.web.bind.annotation.GetMapping;
+//import org.springframework.web.bind.annotation.PostMapping;
+//import org.springframework.web.bind.annotation.RequestBody;
+//import org.springframework.web.bind.annotation.RequestMapping;
+//import org.springframework.web.bind.annotation.RestController;
+//
+///**
+// * Gateway api Controller for manage gateway api definitions.
+// *
+// * @author cdfive
+// * @since 1.7.0
+// */
+//@RestController
+//@RequestMapping(value = "/gateway/api")
+//public class GatewayApiController_bak {
+//
+//    private final Logger logger = LoggerFactory.getLogger(GatewayApiController_bak.class);
+//
+//    @Autowired
+//    private InMemApiDefinitionStore repository;
+//
+//    @Autowired
+//    private SentinelApiClient sentinelApiClient;
+//
+//    @GetMapping("/list.json")
+//    @AuthAction(AuthService.PrivilegeType.READ_RULE)
+//    public Result<List<ApiDefinitionEntity>> queryApis(String app, String ip, Integer port) {
+//
+//        if (StringUtil.isEmpty(app)) {
+//            return Result.ofFail(-1, "app can't be null or empty");
+//        }
+//        if (StringUtil.isEmpty(ip)) {
+//            return Result.ofFail(-1, "ip can't be null or empty");
+//        }
+//        if (port == null) {
+//            return Result.ofFail(-1, "port can't be null");
+//        }
+//
+//        try {
+//            List<ApiDefinitionEntity> apis = sentinelApiClient.fetchApis(app, ip, port).get();
+//            repository.saveAll(apis);
+//            return Result.ofSuccess(apis);
+//        } catch (Throwable throwable) {
+//            logger.error("queryApis error:", throwable);
+//            return Result.ofThrowable(-1, throwable);
+//        }
+//    }
+//
+//    @PostMapping("/new.json")
+//    @AuthAction(AuthService.PrivilegeType.WRITE_RULE)
+//    public Result<ApiDefinitionEntity> addApi(HttpServletRequest request, @RequestBody AddApiReqVo reqVo) {
+//
+//        String app = reqVo.getApp();
+//        if (StringUtil.isBlank(app)) {
+//            return Result.ofFail(-1, "app can't be null or empty");
+//        }
+//
+//        ApiDefinitionEntity entity = new ApiDefinitionEntity();
+//        entity.setApp(app.trim());
+//
+//        String ip = reqVo.getIp();
+//        if (StringUtil.isBlank(ip)) {
+//            return Result.ofFail(-1, "ip can't be null or empty");
+//        }
+//        entity.setIp(ip.trim());
+//
+//        Integer port = reqVo.getPort();
+//        if (port == null) {
+//            return Result.ofFail(-1, "port can't be null");
+//        }
+//        entity.setPort(port);
+//
+//        // API名称
+//        String apiName = reqVo.getApiName();
+//        if (StringUtil.isBlank(apiName)) {
+//            return Result.ofFail(-1, "apiName can't be null or empty");
+//        }
+//        entity.setApiName(apiName.trim());
+//
+//        // 匹配规则列表
+//        List<ApiPredicateItemVo> predicateItems = reqVo.getPredicateItems();
+//        if (CollectionUtils.isEmpty(predicateItems)) {
+//            return Result.ofFail(-1, "predicateItems can't empty");
+//        }
+//
+//        List<ApiPredicateItemEntity> predicateItemEntities = new ArrayList<>();
+//        for (ApiPredicateItemVo predicateItem : predicateItems) {
+//            ApiPredicateItemEntity predicateItemEntity = new ApiPredicateItemEntity();
+//
+//            // 匹配模式
+//            Integer matchStrategy = predicateItem.getMatchStrategy();
+//            if (!Arrays.asList(URL_MATCH_STRATEGY_EXACT, URL_MATCH_STRATEGY_PREFIX, URL_MATCH_STRATEGY_REGEX).contains(matchStrategy)) {
+//                return Result.ofFail(-1, "invalid matchStrategy: " + matchStrategy);
+//            }
+//            predicateItemEntity.setMatchStrategy(matchStrategy);
+//
+//            // 匹配串
+//            String pattern = predicateItem.getPattern();
+//            if (StringUtil.isBlank(pattern)) {
+//                return Result.ofFail(-1, "pattern can't be null or empty");
+//            }
+//            predicateItemEntity.setPattern(pattern);
+//
+//            predicateItemEntities.add(predicateItemEntity);
+//        }
+//        entity.setPredicateItems(new LinkedHashSet<>(predicateItemEntities));
+//
+//        // 检查API名称不能重复
+//        List<ApiDefinitionEntity> allApis = repository.findAllByMachine(MachineInfo.of(app.trim(), ip.trim(), port));
+//        if (allApis.stream().map(o -> o.getApiName()).anyMatch(o -> o.equals(apiName.trim()))) {
+//            return Result.ofFail(-1, "apiName exists: " + apiName);
+//        }
+//
+//        Date date = new Date();
+//        entity.setGmtCreate(date);
+//        entity.setGmtModified(date);
+//
+//        try {
+//            entity = repository.save(entity);
+//        } catch (Throwable throwable) {
+//            logger.error("add gateway api error:", throwable);
+//            return Result.ofThrowable(-1, throwable);
+//        }
+//
+//        if (!publishApis(app, ip, port)) {
+//            logger.warn("publish gateway apis fail after add");
+//        }
+//
+//        return Result.ofSuccess(entity);
+//    }
+//
+//    @PostMapping("/save.json")
+//    @AuthAction(AuthService.PrivilegeType.WRITE_RULE)
+//    public Result<ApiDefinitionEntity> updateApi(@RequestBody UpdateApiReqVo reqVo) {
+//        String app = reqVo.getApp();
+//        if (StringUtil.isBlank(app)) {
+//            return Result.ofFail(-1, "app can't be null or empty");
+//        }
+//
+//        Long id = reqVo.getId();
+//        if (id == null) {
+//            return Result.ofFail(-1, "id can't be null");
+//        }
+//
+//        ApiDefinitionEntity entity = repository.findById(id);
+//        if (entity == null) {
+//            return Result.ofFail(-1, "api does not exist, id=" + id);
+//        }
+//
+//        // 匹配规则列表
+//        List<ApiPredicateItemVo> predicateItems = reqVo.getPredicateItems();
+//        if (CollectionUtils.isEmpty(predicateItems)) {
+//            return Result.ofFail(-1, "predicateItems can't empty");
+//        }
+//
+//        List<ApiPredicateItemEntity> predicateItemEntities = new ArrayList<>();
+//        for (ApiPredicateItemVo predicateItem : predicateItems) {
+//            ApiPredicateItemEntity predicateItemEntity = new ApiPredicateItemEntity();
+//
+//            // 匹配模式
+//            int matchStrategy = predicateItem.getMatchStrategy();
+//            if (!Arrays.asList(URL_MATCH_STRATEGY_EXACT, URL_MATCH_STRATEGY_PREFIX, URL_MATCH_STRATEGY_REGEX).contains(matchStrategy)) {
+//                return Result.ofFail(-1, "Invalid matchStrategy: " + matchStrategy);
+//            }
+//            predicateItemEntity.setMatchStrategy(matchStrategy);
+//
+//            // 匹配串
+//            String pattern = predicateItem.getPattern();
+//            if (StringUtil.isBlank(pattern)) {
+//                return Result.ofFail(-1, "pattern can't be null or empty");
+//            }
+//            predicateItemEntity.setPattern(pattern);
+//
+//            predicateItemEntities.add(predicateItemEntity);
+//        }
+//        entity.setPredicateItems(new LinkedHashSet<>(predicateItemEntities));
+//
+//        Date date = new Date();
+//        entity.setGmtModified(date);
+//
+//        try {
+//            entity = repository.save(entity);
+//        } catch (Throwable throwable) {
+//            logger.error("update gateway api error:", throwable);
+//            return Result.ofThrowable(-1, throwable);
+//        }
+//
+//        if (!publishApis(app, entity.getIp(), entity.getPort())) {
+//            logger.warn("publish gateway apis fail after update");
+//        }
+//
+//        return Result.ofSuccess(entity);
+//    }
+//
+//    @PostMapping("/delete.json")
+//    @AuthAction(AuthService.PrivilegeType.DELETE_RULE)
+//
+//    public Result<Long> deleteApi(Long id) {
+//        if (id == null) {
+//            return Result.ofFail(-1, "id can't be null");
+//        }
+//
+//        ApiDefinitionEntity oldEntity = repository.findById(id);
+//        if (oldEntity == null) {
+//            return Result.ofSuccess(null);
+//        }
+//
+//        try {
+//            repository.delete(id);
+//        } catch (Throwable throwable) {
+//            logger.error("delete gateway api error:", throwable);
+//            return Result.ofThrowable(-1, throwable);
+//        }
+//
+//        if (!publishApis(oldEntity.getApp(), oldEntity.getIp(), oldEntity.getPort())) {
+//            logger.warn("publish gateway apis fail after delete");
+//        }
+//
+//        return Result.ofSuccess(id);
+//    }
+//
+//    private boolean publishApis(String app, String ip, Integer port) {
+//        List<ApiDefinitionEntity> apis = repository.findAllByMachine(MachineInfo.of(app, ip, port));
+//        return sentinelApiClient.modifyApis(app, ip, port, apis);
+//    }
+//}

+ 449 - 0
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/gateway/GatewayFlowRuleController_bak.java

@@ -0,0 +1,449 @@
+///*
+// * Copyright 1999-2018 Alibaba Group Holding Ltd.
+// *
+// * Licensed under the Apache License, Version 2.0 (the "License");
+// * you may not use this file except in compliance with the License.
+// * You may obtain a copy of the License at
+// *
+// *      http://www.apache.org/licenses/LICENSE-2.0
+// *
+// * Unless required by applicable law or agreed to in writing, software
+// * distributed under the License is distributed on an "AS IS" BASIS,
+// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// * See the License for the specific language governing permissions and
+// * limitations under the License.
+// */
+//package com.alibaba.csp.sentinel.dashboard.controller.gateway;
+//
+//
+//import static com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants.PARAM_MATCH_STRATEGY_CONTAINS;
+//import static com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants.PARAM_MATCH_STRATEGY_EXACT;
+//import static com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants.PARAM_MATCH_STRATEGY_REGEX;
+//import static com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants.PARAM_PARSE_STRATEGY_CLIENT_IP;
+//import static com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants.PARAM_PARSE_STRATEGY_COOKIE;
+//import static com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HEADER;
+//import static com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants.PARAM_PARSE_STRATEGY_HOST;
+//import static com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants.PARAM_PARSE_STRATEGY_URL_PARAM;
+//import static com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants.RESOURCE_MODE_CUSTOM_API_NAME;
+//import static com.alibaba.csp.sentinel.adapter.gateway.common.SentinelGatewayConstants.RESOURCE_MODE_ROUTE_ID;
+//import static com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity.INTERVAL_UNIT_DAY;
+//import static com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity.INTERVAL_UNIT_HOUR;
+//import static com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity.INTERVAL_UNIT_MINUTE;
+//import static com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity.INTERVAL_UNIT_SECOND;
+//import static com.alibaba.csp.sentinel.slots.block.RuleConstant.CONTROL_BEHAVIOR_DEFAULT;
+//import static com.alibaba.csp.sentinel.slots.block.RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER;
+//import static com.alibaba.csp.sentinel.slots.block.RuleConstant.FLOW_GRADE_QPS;
+//import static com.alibaba.csp.sentinel.slots.block.RuleConstant.FLOW_GRADE_THREAD;
+//
+//import com.alibaba.csp.sentinel.dashboard.auth.AuthAction;
+//import com.alibaba.csp.sentinel.dashboard.auth.AuthService;
+//import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient;
+//import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity;
+//import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayParamFlowItemEntity;
+//import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo;
+//import com.alibaba.csp.sentinel.dashboard.domain.Result;
+//import com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.AddFlowRuleReqVo;
+//import com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.GatewayParamFlowItemVo;
+//import com.alibaba.csp.sentinel.dashboard.domain.vo.gateway.rule.UpdateFlowRuleReqVo;
+//import com.alibaba.csp.sentinel.dashboard.repository.gateway.InMemGatewayFlowRuleStore;
+//import com.alibaba.csp.sentinel.util.StringUtil;
+//import java.util.Arrays;
+//import java.util.Date;
+//import java.util.List;
+//import org.slf4j.Logger;
+//import org.slf4j.LoggerFactory;
+//import org.springframework.beans.factory.annotation.Autowired;
+//import org.springframework.web.bind.annotation.GetMapping;
+//import org.springframework.web.bind.annotation.PostMapping;
+//import org.springframework.web.bind.annotation.RequestBody;
+//import org.springframework.web.bind.annotation.RequestMapping;
+//import org.springframework.web.bind.annotation.RestController;
+//
+///**
+// * Gateway flow rule Controller for manage gateway flow rules.
+// *
+// * @author cdfive
+// * @since 1.7.0
+// */
+//@RestController
+//@RequestMapping(value = "/gateway/flow")
+//public class GatewayFlowRuleController_bak {
+//
+//    private final Logger logger = LoggerFactory.getLogger(GatewayFlowRuleController_bak.class);
+//
+//    @Autowired
+//    private InMemGatewayFlowRuleStore repository;
+//
+//    @Autowired
+//    private SentinelApiClient sentinelApiClient;
+//
+//    @GetMapping("/list.json")
+//    @AuthAction(AuthService.PrivilegeType.READ_RULE)
+//    public Result<List<GatewayFlowRuleEntity>> queryFlowRules(String app, String ip, Integer port) {
+//
+//        if (StringUtil.isEmpty(app)) {
+//            return Result.ofFail(-1, "app can't be null or empty");
+//        }
+//        if (StringUtil.isEmpty(ip)) {
+//            return Result.ofFail(-1, "ip can't be null or empty");
+//        }
+//        if (port == null) {
+//            return Result.ofFail(-1, "port can't be null");
+//        }
+//
+//        try {
+//            List<GatewayFlowRuleEntity> rules = sentinelApiClient.fetchGatewayFlowRules(app, ip, port).get();
+//            repository.saveAll(rules);
+//            return Result.ofSuccess(rules);
+//        } catch (Throwable throwable) {
+//            logger.error("query gateway flow rules error:", throwable);
+//            return Result.ofThrowable(-1, throwable);
+//        }
+//    }
+//
+//    @PostMapping("/new.json")
+//    @AuthAction(AuthService.PrivilegeType.WRITE_RULE)
+//    public Result<GatewayFlowRuleEntity> addFlowRule(@RequestBody AddFlowRuleReqVo reqVo) {
+//
+//        String app = reqVo.getApp();
+//        if (StringUtil.isBlank(app)) {
+//            return Result.ofFail(-1, "app can't be null or empty");
+//        }
+//
+//        GatewayFlowRuleEntity entity = new GatewayFlowRuleEntity();
+//        entity.setApp(app.trim());
+//
+//        String ip = reqVo.getIp();
+//        if (StringUtil.isBlank(ip)) {
+//            return Result.ofFail(-1, "ip can't be null or empty");
+//        }
+//        entity.setIp(ip.trim());
+//
+//        Integer port = reqVo.getPort();
+//        if (port == null) {
+//            return Result.ofFail(-1, "port can't be null");
+//        }
+//        entity.setPort(port);
+//
+//        // API类型, Route ID或API分组
+//        Integer resourceMode = reqVo.getResourceMode();
+//        if (resourceMode == null) {
+//            return Result.ofFail(-1, "resourceMode can't be null");
+//        }
+//        if (!Arrays.asList(RESOURCE_MODE_ROUTE_ID, RESOURCE_MODE_CUSTOM_API_NAME).contains(resourceMode)) {
+//            return Result.ofFail(-1, "invalid resourceMode: " + resourceMode);
+//        }
+//        entity.setResourceMode(resourceMode);
+//
+//        // API名称
+//        String resource = reqVo.getResource();
+//        if (StringUtil.isBlank(resource)) {
+//            return Result.ofFail(-1, "resource can't be null or empty");
+//        }
+//        entity.setResource(resource.trim());
+//
+//        // 针对请求属性
+//        GatewayParamFlowItemVo paramItem = reqVo.getParamItem();
+//        if (paramItem != null) {
+//            GatewayParamFlowItemEntity itemEntity = new GatewayParamFlowItemEntity();
+//            entity.setParamItem(itemEntity);
+//
+//            // 参数属性 0-ClientIP 1-Remote Host 2-Header 3-URL参数 4-Cookie
+//            Integer parseStrategy = paramItem.getParseStrategy();
+//            if (!Arrays.asList(PARAM_PARSE_STRATEGY_CLIENT_IP, PARAM_PARSE_STRATEGY_HOST, PARAM_PARSE_STRATEGY_HEADER
+//                    , PARAM_PARSE_STRATEGY_URL_PARAM, PARAM_PARSE_STRATEGY_COOKIE).contains(parseStrategy)) {
+//                return Result.ofFail(-1, "invalid parseStrategy: " + parseStrategy);
+//            }
+//            itemEntity.setParseStrategy(paramItem.getParseStrategy());
+//
+//            // 当参数属性为2-Header 3-URL参数 4-Cookie时,参数名称必填
+//            if (Arrays.asList(PARAM_PARSE_STRATEGY_HEADER, PARAM_PARSE_STRATEGY_URL_PARAM, PARAM_PARSE_STRATEGY_COOKIE).contains(parseStrategy)) {
+//                // 参数名称
+//                String fieldName = paramItem.getFieldName();
+//                if (StringUtil.isBlank(fieldName)) {
+//                    return Result.ofFail(-1, "fieldName can't be null or empty");
+//                }
+//                itemEntity.setFieldName(paramItem.getFieldName());
+//            }
+//
+//            String pattern = paramItem.getPattern();
+//            // 如果匹配串不为空,验证匹配模式
+//            if (StringUtil.isNotEmpty(pattern)) {
+//                itemEntity.setPattern(pattern);
+//                Integer matchStrategy = paramItem.getMatchStrategy();
+//                if (!Arrays.asList(PARAM_MATCH_STRATEGY_EXACT, PARAM_MATCH_STRATEGY_CONTAINS, PARAM_MATCH_STRATEGY_REGEX).contains(matchStrategy)) {
+//                    return Result.ofFail(-1, "invalid matchStrategy: " + matchStrategy);
+//                }
+//                itemEntity.setMatchStrategy(matchStrategy);
+//            }
+//        }
+//
+//        // 阈值类型 0-线程数 1-QPS
+//        Integer grade = reqVo.getGrade();
+//        if (grade == null) {
+//            return Result.ofFail(-1, "grade can't be null");
+//        }
+//        if (!Arrays.asList(FLOW_GRADE_THREAD, FLOW_GRADE_QPS).contains(grade)) {
+//            return Result.ofFail(-1, "invalid grade: " + grade);
+//        }
+//        entity.setGrade(grade);
+//
+//        // QPS阈值
+//        Double count = reqVo.getCount();
+//        if (count == null) {
+//            return Result.ofFail(-1, "count can't be null");
+//        }
+//        if (count < 0) {
+//            return Result.ofFail(-1, "count should be at lease zero");
+//        }
+//        entity.setCount(count);
+//
+//        // 间隔
+//        Long interval = reqVo.getInterval();
+//        if (interval == null) {
+//            return Result.ofFail(-1, "interval can't be null");
+//        }
+//        if (interval <= 0) {
+//            return Result.ofFail(-1, "interval should be greater than zero");
+//        }
+//        entity.setInterval(interval);
+//
+//        // 间隔单位
+//        Integer intervalUnit = reqVo.getIntervalUnit();
+//        if (intervalUnit == null) {
+//            return Result.ofFail(-1, "intervalUnit can't be null");
+//        }
+//        if (!Arrays.asList(INTERVAL_UNIT_SECOND, INTERVAL_UNIT_MINUTE, INTERVAL_UNIT_HOUR, INTERVAL_UNIT_DAY).contains(intervalUnit)) {
+//            return Result.ofFail(-1, "Invalid intervalUnit: " + intervalUnit);
+//        }
+//        entity.setIntervalUnit(intervalUnit);
+//
+//        // 流控方式 0-快速失败 2-匀速排队
+//        Integer controlBehavior = reqVo.getControlBehavior();
+//        if (controlBehavior == null) {
+//            return Result.ofFail(-1, "controlBehavior can't be null");
+//        }
+//        if (!Arrays.asList(CONTROL_BEHAVIOR_DEFAULT, CONTROL_BEHAVIOR_RATE_LIMITER).contains(controlBehavior)) {
+//            return Result.ofFail(-1, "invalid controlBehavior: " + controlBehavior);
+//        }
+//        entity.setControlBehavior(controlBehavior);
+//
+//        if (CONTROL_BEHAVIOR_DEFAULT == controlBehavior) {
+//            // 0-快速失败, 则Burst size必填
+//            Integer burst = reqVo.getBurst();
+//            if (burst == null) {
+//                return Result.ofFail(-1, "burst can't be null");
+//            }
+//            if (burst < 0) {
+//                return Result.ofFail(-1, "invalid burst: " + burst);
+//            }
+//            entity.setBurst(burst);
+//        } else if (CONTROL_BEHAVIOR_RATE_LIMITER == controlBehavior) {
+//            // 1-匀速排队, 则超时时间必填
+//            Integer maxQueueingTimeoutMs = reqVo.getMaxQueueingTimeoutMs();
+//            if (maxQueueingTimeoutMs == null) {
+//                return Result.ofFail(-1, "maxQueueingTimeoutMs can't be null");
+//            }
+//            if (maxQueueingTimeoutMs < 0) {
+//                return Result.ofFail(-1, "invalid maxQueueingTimeoutMs: " + maxQueueingTimeoutMs);
+//            }
+//            entity.setMaxQueueingTimeoutMs(maxQueueingTimeoutMs);
+//        }
+//
+//        Date date = new Date();
+//        entity.setGmtCreate(date);
+//        entity.setGmtModified(date);
+//
+//        try {
+//            entity = repository.save(entity);
+//        } catch (Throwable throwable) {
+//            logger.error("add gateway flow rule error:", throwable);
+//            return Result.ofThrowable(-1, throwable);
+//        }
+//
+//        if (!publishRules(app, ip, port)) {
+//            logger.warn("publish gateway flow rules fail after add");
+//        }
+//
+//        return Result.ofSuccess(entity);
+//    }
+//
+//    @PostMapping("/save.json")
+//    @AuthAction(AuthService.PrivilegeType.WRITE_RULE)
+//    public Result<GatewayFlowRuleEntity> updateFlowRule(@RequestBody UpdateFlowRuleReqVo reqVo) {
+//
+//        String app = reqVo.getApp();
+//        if (StringUtil.isBlank(app)) {
+//            return Result.ofFail(-1, "app can't be null or empty");
+//        }
+//
+//        Long id = reqVo.getId();
+//        if (id == null) {
+//            return Result.ofFail(-1, "id can't be null");
+//        }
+//
+//        GatewayFlowRuleEntity entity = repository.findById(id);
+//        if (entity == null) {
+//            return Result.ofFail(-1, "gateway flow rule does not exist, id=" + id);
+//        }
+//
+//        // 针对请求属性
+//        GatewayParamFlowItemVo paramItem = reqVo.getParamItem();
+//        if (paramItem != null) {
+//            GatewayParamFlowItemEntity itemEntity = new GatewayParamFlowItemEntity();
+//            entity.setParamItem(itemEntity);
+//
+//            // 参数属性 0-ClientIP 1-Remote Host 2-Header 3-URL参数 4-Cookie
+//            Integer parseStrategy = paramItem.getParseStrategy();
+//            if (!Arrays.asList(PARAM_PARSE_STRATEGY_CLIENT_IP, PARAM_PARSE_STRATEGY_HOST, PARAM_PARSE_STRATEGY_HEADER
+//                    , PARAM_PARSE_STRATEGY_URL_PARAM, PARAM_PARSE_STRATEGY_COOKIE).contains(parseStrategy)) {
+//                return Result.ofFail(-1, "invalid parseStrategy: " + parseStrategy);
+//            }
+//            itemEntity.setParseStrategy(paramItem.getParseStrategy());
+//
+//            // 当参数属性为2-Header 3-URL参数 4-Cookie时,参数名称必填
+//            if (Arrays.asList(PARAM_PARSE_STRATEGY_HEADER, PARAM_PARSE_STRATEGY_URL_PARAM, PARAM_PARSE_STRATEGY_COOKIE).contains(parseStrategy)) {
+//                // 参数名称
+//                String fieldName = paramItem.getFieldName();
+//                if (StringUtil.isBlank(fieldName)) {
+//                    return Result.ofFail(-1, "fieldName can't be null or empty");
+//                }
+//                itemEntity.setFieldName(paramItem.getFieldName());
+//            }
+//
+//            String pattern = paramItem.getPattern();
+//            // 如果匹配串不为空,验证匹配模式
+//            if (StringUtil.isNotEmpty(pattern)) {
+//                itemEntity.setPattern(pattern);
+//                Integer matchStrategy = paramItem.getMatchStrategy();
+//                if (!Arrays.asList(PARAM_MATCH_STRATEGY_EXACT, PARAM_MATCH_STRATEGY_CONTAINS, PARAM_MATCH_STRATEGY_REGEX).contains(matchStrategy)) {
+//                    return Result.ofFail(-1, "invalid matchStrategy: " + matchStrategy);
+//                }
+//                itemEntity.setMatchStrategy(matchStrategy);
+//            }
+//        } else {
+//            entity.setParamItem(null);
+//        }
+//
+//        // 阈值类型 0-线程数 1-QPS
+//        Integer grade = reqVo.getGrade();
+//        if (grade == null) {
+//            return Result.ofFail(-1, "grade can't be null");
+//        }
+//        if (!Arrays.asList(FLOW_GRADE_THREAD, FLOW_GRADE_QPS).contains(grade)) {
+//            return Result.ofFail(-1, "invalid grade: " + grade);
+//        }
+//        entity.setGrade(grade);
+//
+//        // QPS阈值
+//        Double count = reqVo.getCount();
+//        if (count == null) {
+//            return Result.ofFail(-1, "count can't be null");
+//        }
+//        if (count < 0) {
+//            return Result.ofFail(-1, "count should be at lease zero");
+//        }
+//        entity.setCount(count);
+//
+//        // 间隔
+//        Long interval = reqVo.getInterval();
+//        if (interval == null) {
+//            return Result.ofFail(-1, "interval can't be null");
+//        }
+//        if (interval <= 0) {
+//            return Result.ofFail(-1, "interval should be greater than zero");
+//        }
+//        entity.setInterval(interval);
+//
+//        // 间隔单位
+//        Integer intervalUnit = reqVo.getIntervalUnit();
+//        if (intervalUnit == null) {
+//            return Result.ofFail(-1, "intervalUnit can't be null");
+//        }
+//        if (!Arrays.asList(INTERVAL_UNIT_SECOND, INTERVAL_UNIT_MINUTE, INTERVAL_UNIT_HOUR, INTERVAL_UNIT_DAY).contains(intervalUnit)) {
+//            return Result.ofFail(-1, "Invalid intervalUnit: " + intervalUnit);
+//        }
+//        entity.setIntervalUnit(intervalUnit);
+//
+//        // 流控方式 0-快速失败 2-匀速排队
+//        Integer controlBehavior = reqVo.getControlBehavior();
+//        if (controlBehavior == null) {
+//            return Result.ofFail(-1, "controlBehavior can't be null");
+//        }
+//        if (!Arrays.asList(CONTROL_BEHAVIOR_DEFAULT, CONTROL_BEHAVIOR_RATE_LIMITER).contains(controlBehavior)) {
+//            return Result.ofFail(-1, "invalid controlBehavior: " + controlBehavior);
+//        }
+//        entity.setControlBehavior(controlBehavior);
+//
+//        if (CONTROL_BEHAVIOR_DEFAULT == controlBehavior) {
+//            // 0-快速失败, 则Burst size必填
+//            Integer burst = reqVo.getBurst();
+//            if (burst == null) {
+//                return Result.ofFail(-1, "burst can't be null");
+//            }
+//            if (burst < 0) {
+//                return Result.ofFail(-1, "invalid burst: " + burst);
+//            }
+//            entity.setBurst(burst);
+//        } else if (CONTROL_BEHAVIOR_RATE_LIMITER == controlBehavior) {
+//            // 2-匀速排队, 则超时时间必填
+//            Integer maxQueueingTimeoutMs = reqVo.getMaxQueueingTimeoutMs();
+//            if (maxQueueingTimeoutMs == null) {
+//                return Result.ofFail(-1, "maxQueueingTimeoutMs can't be null");
+//            }
+//            if (maxQueueingTimeoutMs < 0) {
+//                return Result.ofFail(-1, "invalid maxQueueingTimeoutMs: " + maxQueueingTimeoutMs);
+//            }
+//            entity.setMaxQueueingTimeoutMs(maxQueueingTimeoutMs);
+//        }
+//
+//        Date date = new Date();
+//        entity.setGmtModified(date);
+//
+//        try {
+//            entity = repository.save(entity);
+//        } catch (Throwable throwable) {
+//            logger.error("update gateway flow rule error:", throwable);
+//            return Result.ofThrowable(-1, throwable);
+//        }
+//
+//        if (!publishRules(app, entity.getIp(), entity.getPort())) {
+//            logger.warn("publish gateway flow rules fail after update");
+//        }
+//
+//        return Result.ofSuccess(entity);
+//    }
+//
+//
+//    @PostMapping("/delete.json")
+//    @AuthAction(AuthService.PrivilegeType.DELETE_RULE)
+//    public Result<Long> deleteFlowRule(Long id) {
+//
+//        if (id == null) {
+//            return Result.ofFail(-1, "id can't be null");
+//        }
+//
+//        GatewayFlowRuleEntity oldEntity = repository.findById(id);
+//        if (oldEntity == null) {
+//            return Result.ofSuccess(null);
+//        }
+//
+//        try {
+//            repository.delete(id);
+//        } catch (Throwable throwable) {
+//            logger.error("delete gateway flow rule error:", throwable);
+//            return Result.ofThrowable(-1, throwable);
+//        }
+//
+//        if (!publishRules(oldEntity.getApp(), oldEntity.getIp(), oldEntity.getPort())) {
+//            logger.warn("publish gateway flow rules fail after delete");
+//        }
+//
+//        return Result.ofSuccess(id);
+//    }
+//
+//    private boolean publishRules(String app, String ip, Integer port) {
+//        List<GatewayFlowRuleEntity> rules = repository.findAllByMachine(MachineInfo.of(app, ip, port));
+//        return sentinelApiClient.modifyGatewayFlowRules(app, ip, port, rules);
+//    }
+//}

+ 2 - 0
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/controller/v2/FlowControllerV2.java

@@ -33,6 +33,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.util.CollectionUtils;
 import org.springframework.web.bind.annotation.DeleteMapping;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.PathVariable;
@@ -80,6 +81,7 @@ public class FlowControllerV2 {
             return Result.ofFail(-1, "app can't be null or empty");
         }
         try {
+            Thread.sleep(100);
             List<FlowRuleEntity> rules = ruleProvider.getRules(app);
             if (rules != null && !rules.isEmpty()) {
                 for (FlowRuleEntity entity : rules) {

+ 9 - 1
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/datasource/entity/gateway/ApiDefinitionEntity.java

@@ -169,7 +169,15 @@ public class ApiDefinitionEntity implements RuleEntity {
 
     @Override
     public Rule toRule() {
-        return null;
+        ApiGroupRule apiGroupRule = new ApiGroupRule();
+        apiGroupRule.setApp(this.getApp());
+        apiGroupRule.setIp(this.getIp());
+        apiGroupRule.setPort(this.getPort());
+        apiGroupRule.setGmtCreate(this.getGmtCreate());
+        apiGroupRule.setGmtModified(this.getGmtModified());
+        apiGroupRule.setApiName(this.getApiName());
+        apiGroupRule.setPredicateItems(this.getPredicateItems());
+        return apiGroupRule;
     }
 
     @Override

+ 140 - 0
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/datasource/entity/gateway/ApiGroupRule.java

@@ -0,0 +1,140 @@
+/*
+ * Copyright 1999-2018 Alibaba Group Holding Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway;
+
+import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
+import com.alibaba.csp.sentinel.slots.block.AbstractRule;
+import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
+import java.util.Date;
+import java.util.Objects;
+import java.util.Set;
+
+/**
+ * Entity for {@link ApiGroupRule}.
+ *
+ * @author cdfive
+ * @since 1.7.0
+ */
+public class ApiGroupRule extends AbstractRule {
+
+    private Long id;
+    private String app;
+    private String ip;
+    private Integer port;
+
+    private Date gmtCreate;
+    private Date gmtModified;
+
+    private String apiName;
+    private Set<ApiPredicateItemEntity> predicateItems;
+
+    public ApiGroupRule() {
+
+    }
+
+//    public static ApiDefinitionEntity fromApiGroupRule(String app, ApiGroupRule rule) {
+//        ApiDefinitionEntity entity = new ApiDefinitionEntity();
+//        entity.setApp(app);
+//        return entity;
+//    }
+
+    public ApiGroupRule(String apiName, Set<ApiPredicateItemEntity> predicateItems) {
+        this.apiName = apiName;
+        this.predicateItems = predicateItems;
+    }
+
+    public Long getId(){
+        return id;
+    }
+
+    public void setId(Long id){
+        this.id = id;
+    }
+
+    public String getApiName() {
+        return apiName;
+    }
+
+    public void setApiName(String apiName) {
+        this.apiName = apiName;
+    }
+
+    public Set<ApiPredicateItemEntity> getPredicateItems() {
+        return predicateItems;
+    }
+
+    public void setPredicateItems(Set<ApiPredicateItemEntity> predicateItems) {
+        this.predicateItems = predicateItems;
+    }
+
+    public void setApp(String app) {
+        this.app = app;
+    }
+
+    public void setIp(String ip) {
+        this.ip = ip;
+    }
+
+    public void setPort(Integer port) {
+        this.port = port;
+    }
+
+    public void setGmtCreate(Date gmtCreate) {
+        this.gmtCreate = gmtCreate;
+    }
+
+    public Date getGmtModified() {
+        return gmtModified;
+    }
+
+    public void setGmtModified(Date gmtModified) {
+        this.gmtModified = gmtModified;
+    }
+
+    @Override
+    public boolean equals(Object o) {
+        if (this == o) { return true; }
+        if (o == null || getClass() != o.getClass()) { return false; }
+        ApiGroupRule entity = (ApiGroupRule) o;
+        return Objects.equals(id, entity.id) &&
+                Objects.equals(app, entity.app) &&
+                Objects.equals(ip, entity.ip) &&
+                Objects.equals(port, entity.port) &&
+                Objects.equals(gmtCreate, entity.gmtCreate) &&
+                Objects.equals(gmtModified, entity.gmtModified) &&
+                Objects.equals(apiName, entity.apiName) &&
+                Objects.equals(predicateItems, entity.predicateItems);
+    }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(id, app, ip, port, gmtCreate, gmtModified, apiName, predicateItems);
+    }
+
+    @Override
+    public String toString() {
+        return "ApiDefinition{" +
+                "id=" + id +
+                ", app='" + app + '\'' +
+                ", ip='" + ip + '\'' +
+                ", port=" + port +
+                ", gmtCreate=" + gmtCreate +
+                ", gmtModified=" + gmtModified +
+                ", apiName='" + apiName + '\'' +
+                ", predicateItems=" + predicateItems +
+                '}';
+    }
+}

+ 33 - 0
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/ApiGroupRuleNacosProvider.java

@@ -0,0 +1,33 @@
+package com.alibaba.csp.sentinel.dashboard.rule.nacos;
+
+import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity;
+import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity;
+import com.alibaba.csp.sentinel.dashboard.rule.DynamicRuleProvider;
+import com.alibaba.nacos.api.config.ConfigService;
+import java.util.List;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+/**
+ * <p>
+ * TODO
+ * </p>
+ *
+ * @author dengsixing
+ * @since 2022/2/15
+ **/
+@Component("apiGroupRuleNacosProvider")
+public class ApiGroupRuleNacosProvider implements DynamicRuleProvider<List<ApiDefinitionEntity>> {
+    @Autowired
+    private ConfigService configService;
+
+    @Override
+    public List<ApiDefinitionEntity> getRules(String appName) throws Exception {
+        return NacosConfigUtil.getRuleEntitiesFromNacos(
+            this.configService,
+            appName,
+            NacosConfigUtil.API_GROUP_DATA_ID_POSTFIX,
+            ApiDefinitionEntity.class
+        );
+    }
+}

+ 33 - 0
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/ApiGroupRuleNacosPublisher.java

@@ -0,0 +1,33 @@
+package com.alibaba.csp.sentinel.dashboard.rule.nacos;
+
+import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity;
+import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity;
+import com.alibaba.csp.sentinel.dashboard.rule.DynamicRulePublisher;
+import com.alibaba.nacos.api.config.ConfigService;
+import java.util.List;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+/**
+ * <p>
+ * TODO
+ * </p>
+ *
+ * @author dengsixing
+ * @since 2022/2/15
+ **/
+@Component("apiGroupRuleNacosPublisher")
+public class ApiGroupRuleNacosPublisher implements DynamicRulePublisher<List<ApiDefinitionEntity>> {
+    @Autowired
+    private ConfigService configService;
+
+    @Override
+    public void publish(String app, List<ApiDefinitionEntity> rules) throws Exception {
+        NacosConfigUtil.setRuleStringToNacos(
+            this.configService,
+            app,
+            NacosConfigUtil.API_GROUP_DATA_ID_POSTFIX,
+            rules
+        );
+    }
+}

+ 1 - 0
sentinel-dashboard/src/main/java/com/alibaba/csp/sentinel/dashboard/rule/nacos/NacosConfigUtil.java

@@ -36,6 +36,7 @@ public final class NacosConfigUtil {
     
     public static final String FLOW_DATA_ID_POSTFIX = "-flow-rules";
     public static final String DEGRADE_DATA_ID_POSTFIX = "-degrade-rules";
+    public static final String API_GROUP_DATA_ID_POSTFIX = "-api-group-rules";
     public static final String PARAM_FLOW_DATA_ID_POSTFIX = "-param-rules";
     public static final String CLUSTER_MAP_DATA_ID_POSTFIX = "-cluster-map";
 

+ 2 - 2
sentinel-dashboard/src/main/resources/application.properties

@@ -24,8 +24,8 @@ auth.password=sentinel
 sentinel.dashboard.version=@project.version@
 
 
-nacos.address=192.168.0.47:8848
-nacos.namespace=4dkankan-dev
+nacos.address=120.24.144.164:8848
+nacos.namespace=4dkankan-test
 nacos.username=nacos
 nacos.password=nacos
 

+ 4 - 4
sentinel-dashboard/src/main/webapp/resources/app/scripts/directives/sidebar/sidebar.html

@@ -53,10 +53,10 @@
 <!--              <i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则</a>-->
 <!--          </li>-->
 
-<!--          <li ui-sref-active="active" ng-if="!entry.isGateway">-->
-<!--            <a ui-sref="dashboard.flowV1({app: entry.app})">-->
-<!--              <i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则</a>-->
-<!--          </li>-->
+          <li ui-sref-active="active" ng-if="entry.isGateway">
+            <a ui-sref="dashboard.flow({app: entry.app})">
+              <i class="glyphicon glyphicon-filter"></i>&nbsp;&nbsp;流控规则</a>
+          </li>
 
           <li ui-sref-active="active">
             <a ui-sref="dashboard.degrade({app: entry.app})">