Skip to content

Commit

Permalink
dashboard: add support for managing gateway flow rules (alibaba#699)
Browse files Browse the repository at this point in the history
  • Loading branch information
cdfive authored and sczyh30 committed Jul 10, 2019
1 parent eda7fdc commit 856ff31
Show file tree
Hide file tree
Showing 43 changed files with 4,514 additions and 130 deletions.
5 changes: 5 additions & 0 deletions sentinel-dashboard/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@
<artifactId>sentinel-parameter-flow-control</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-api-gateway-adapter-common</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@
import java.util.concurrent.ExecutionException;
import java.util.stream.Collectors;

import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
import com.alibaba.csp.sentinel.command.CommandConstants;
import com.alibaba.csp.sentinel.config.SentinelConfig;
import com.alibaba.csp.sentinel.command.vo.NodeVo;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.ApiDefinitionEntity;
import com.alibaba.csp.sentinel.dashboard.datasource.entity.gateway.GatewayFlowRuleEntity;
import com.alibaba.csp.sentinel.dashboard.util.AsyncUtils;
import com.alibaba.csp.sentinel.slots.block.Rule;
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
Expand Down Expand Up @@ -109,6 +112,12 @@ public class SentinelApiClient {
private static final String MODIFY_CLUSTER_SERVER_FLOW_CONFIG_PATH = "cluster/server/modifyFlowConfig";
private static final String MODIFY_CLUSTER_SERVER_NAMESPACE_SET_PATH = "cluster/server/modifyNamespaceSet";

private static final String FETCH_GATEWAY_API_PATH = "gateway/getApiDefinitions";
private static final String MODIFY_GATEWAY_API_PATH = "gateway/updateApiDefinitions";

private static final String FETCH_GATEWAY_FLOW_RULE_PATH = "gateway/getRules";
private static final String MODIFY_GATEWAY_FLOW_RULE_PATH = "gateway/updateRules";

private static final String FLOW_RULE_TYPE = "flow";
private static final String DEGRADE_RULE_TYPE = "degrade";
private static final String SYSTEM_RULE_TYPE = "system";
Expand Down Expand Up @@ -693,4 +702,90 @@ public CompletableFuture<ClusterServerStateVO> fetchClusterServerBasicInfo(Strin
return AsyncUtils.newFailedFuture(ex);
}
}

public CompletableFuture<List<ApiDefinitionEntity>> fetchApis(String app, String ip, int port) {
if (StringUtil.isBlank(ip) || port <= 0) {
return AsyncUtils.newFailedFuture(new IllegalArgumentException("Invalid parameter"));
}

try {
return executeCommand(ip, port, FETCH_GATEWAY_API_PATH, false)
.thenApply(r -> {
List<ApiDefinitionEntity> entities = JSON.parseArray(r, ApiDefinitionEntity.class);
if (entities != null) {
for (ApiDefinitionEntity entity : entities) {
entity.setApp(app);
entity.setIp(ip);
entity.setPort(port);
}
}
return entities;
});
} catch (Exception ex) {
logger.warn("Error when fetching gateway apis", ex);
return AsyncUtils.newFailedFuture(ex);
}
}

public boolean modifyApis(String app, String ip, int port, List<ApiDefinitionEntity> apis) {
if (apis == null) {
return true;
}

try {
AssertUtil.notEmpty(app, "Bad app name");
AssertUtil.notEmpty(ip, "Bad machine IP");
AssertUtil.isTrue(port > 0, "Bad machine port");
String data = JSON.toJSONString(
apis.stream().map(r -> r.toApiDefinition()).collect(Collectors.toList()));
Map<String, String> params = new HashMap<>(2);
params.put("data", data);
String result = executeCommand(app, ip, port, MODIFY_GATEWAY_API_PATH, params, true).get();
logger.info("Modify gateway apis: {}", result);
return true;
} catch (Exception e) {
logger.warn("Error when modifying gateway apis", e);
return false;
}
}

public CompletableFuture<List<GatewayFlowRuleEntity>> fetchGatewayFlowRules(String app, String ip, int port) {
if (StringUtil.isBlank(ip) || port <= 0) {
return AsyncUtils.newFailedFuture(new IllegalArgumentException("Invalid parameter"));
}

try {
return executeCommand(ip, port, FETCH_GATEWAY_FLOW_RULE_PATH, false)
.thenApply(r -> {
List<GatewayFlowRule> gatewayFlowRules = JSON.parseArray(r, GatewayFlowRule.class);
List<GatewayFlowRuleEntity> entities = gatewayFlowRules.stream().map(rule -> GatewayFlowRuleEntity.fromGatewayFlowRule(app, ip, port, rule)).collect(Collectors.toList());
return entities;
});
} catch (Exception ex) {
logger.warn("Error when fetching gateway flow rules", ex);
return AsyncUtils.newFailedFuture(ex);
}
}

public boolean modifyGatewayFlowRules(String app, String ip, int port, List<GatewayFlowRuleEntity> rules) {
if (rules == null) {
return true;
}

try {
AssertUtil.notEmpty(app, "Bad app name");
AssertUtil.notEmpty(ip, "Bad machine IP");
AssertUtil.isTrue(port > 0, "Bad machine port");
String data = JSON.toJSONString(
rules.stream().map(r -> r.toGatewayFlowRule()).collect(Collectors.toList()));
Map<String, String> params = new HashMap<>(2);
params.put("data", data);
String result = executeCommand(app, ip, port, MODIFY_GATEWAY_FLOW_RULE_PATH, params, true).get();
logger.info("Modify gateway flow rules: {}", result);
return true;
} catch (Exception e) {
logger.warn("Error when modifying gateway apis", e);
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,7 @@ public Result<List<MachineInfoVo>> getMachinesByApp(@PathVariable("app") String
return Result.ofSuccess(null);
}
List<MachineInfo> list = new ArrayList<>(appInfo.getMachines());
Collections.sort(list, (o1, o2) -> {
int t = o1.getApp().compareTo(o2.getApp());
if (t != 0) {
return t;
}
t = o1.getIp().compareTo(o2.getIp());
if (t != 0) {
return t;
}
return o1.getPort().compareTo(o2.getPort());
});
Collections.sort(list, Comparator.comparing(MachineInfo::getApp).thenComparing(MachineInfo::getIp).thenComparingInt(MachineInfo::getPort));
return Result.ofSuccess(MachineInfoVo.fromMachineInfoList(list));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
Expand All @@ -40,7 +41,7 @@ public class MachineRegistryController {

@ResponseBody
@RequestMapping("/machine")
public Result<?> receiveHeartBeat(String app, Long version, String v, String hostname, String ip, Integer port) {
public Result<?> receiveHeartBeat(String app, @RequestParam(value = "app_type", required = false, defaultValue = "0") Integer appType, Long version, String v, String hostname, String ip, Integer port) {
if (app == null) {
app = MachineDiscovery.UNKNOWN_APP_NAME;
}
Expand All @@ -59,6 +60,7 @@ public Result<?> receiveHeartBeat(String app, Long version, String v, String hos
try {
MachineInfo machineInfo = new MachineInfo();
machineInfo.setApp(app);
machineInfo.setAppType(appType);
machineInfo.setHostname(hostname);
machineInfo.setIp(ip);
machineInfo.setPort(port);
Expand Down
Loading

0 comments on commit 856ff31

Please sign in to comment.