mirror of
https://gitee.com/dromara/RuoYi-Cloud-Plus.git
synced 2026-03-22 10:47:17 +08:00
update sentinel 1.8.5 => 1.8.6
This commit is contained in:
@@ -15,7 +15,7 @@
|
||||
|
||||
<properties>
|
||||
<spring-cloud-alibaba.version>2021.0.4.0</spring-cloud-alibaba.version>
|
||||
<sentinel.version>1.8.5</sentinel.version>
|
||||
<sentinel.version>1.8.6</sentinel.version>
|
||||
<seata.version>1.5.2</seata.version>
|
||||
<nacos.client.version>2.1.1</nacos.client.version>
|
||||
<nacos.config.version>2.0.4</nacos.config.version>
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<properties>
|
||||
<sentinel.version>1.8.5</sentinel.version>
|
||||
<sentinel.version>1.8.6</sentinel.version>
|
||||
<curator.version>4.0.1</curator.version>
|
||||
</properties>
|
||||
|
||||
|
||||
@@ -15,28 +15,23 @@
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.dashboard.client;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
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.config.SentinelConfig;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.SentinelVersion;
|
||||
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.datasource.entity.rule.*;
|
||||
import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.cluster.ClusterClientInfoVO;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.cluster.config.ClusterClientConfig;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.cluster.config.ServerFlowConfig;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.cluster.config.ServerTransportConfig;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.cluster.state.ClusterServerStateVO;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.cluster.state.ClusterStateSimpleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.util.AsyncUtils;
|
||||
import com.alibaba.csp.sentinel.dashboard.util.VersionUtils;
|
||||
import com.alibaba.csp.sentinel.slots.block.Rule;
|
||||
import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule;
|
||||
import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule;
|
||||
@@ -46,22 +41,6 @@ import com.alibaba.csp.sentinel.slots.system.SystemRule;
|
||||
import com.alibaba.csp.sentinel.util.AssertUtil;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.SentinelVersion;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.ParamFlowRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.RuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.cluster.ClusterClientInfoVO;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.cluster.state.ClusterServerStateVO;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.cluster.state.ClusterStateSimpleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.cluster.config.ClusterClientConfig;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.cluster.config.ServerFlowConfig;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.cluster.config.ServerTransportConfig;
|
||||
import com.alibaba.csp.sentinel.dashboard.util.VersionUtils;
|
||||
|
||||
import org.apache.http.Consts;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.NameValuePair;
|
||||
@@ -71,6 +50,7 @@ import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.client.methods.HttpUriRequest;
|
||||
import org.apache.http.client.utils.URLEncodedUtils;
|
||||
import org.apache.http.concurrent.FutureCallback;
|
||||
import org.apache.http.conn.util.InetAddressUtils;
|
||||
import org.apache.http.entity.ContentType;
|
||||
import org.apache.http.impl.client.DefaultRedirectStrategy;
|
||||
import org.apache.http.impl.nio.client.CloseableHttpAsyncClient;
|
||||
@@ -84,6 +64,15 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.*;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Communicate with Sentinel client.
|
||||
*
|
||||
@@ -130,7 +119,7 @@ public class SentinelApiClient {
|
||||
|
||||
private static final SentinelVersion version160 = new SentinelVersion(1, 6, 0);
|
||||
private static final SentinelVersion version171 = new SentinelVersion(1, 7, 1);
|
||||
|
||||
|
||||
@Autowired
|
||||
private AppManagement appManagement;
|
||||
|
||||
@@ -149,11 +138,11 @@ public class SentinelApiClient {
|
||||
private boolean isSuccess(int statusCode) {
|
||||
return statusCode >= 200 && statusCode < 300;
|
||||
}
|
||||
|
||||
|
||||
private boolean isCommandNotFound(int statusCode, String body) {
|
||||
return statusCode == 400 && StringUtil.isNotEmpty(body) && body.contains(CommandConstants.MSG_UNKNOWN_COMMAND_PREFIX);
|
||||
}
|
||||
|
||||
|
||||
protected boolean isSupportPost(String app, String ip, int port) {
|
||||
return StringUtil.isNotEmpty(app) && Optional.ofNullable(appManagement.getDetailApp(app))
|
||||
.flatMap(e -> e.getMachine(ip, port))
|
||||
@@ -161,11 +150,11 @@ public class SentinelApiClient {
|
||||
.map(v -> v.greaterOrEqual(version160)))
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Check whether target instance (identified by tuple of app-ip:port)
|
||||
* supports the form of "xxxxx; xx=xx" in "Content-Type" header.
|
||||
*
|
||||
*
|
||||
* @param app target app name
|
||||
* @param ip target node's address
|
||||
* @param port target node's port
|
||||
@@ -177,7 +166,7 @@ public class SentinelApiClient {
|
||||
.map(v -> v.greaterOrEqual(version171)))
|
||||
.orElse(false);
|
||||
}
|
||||
|
||||
|
||||
private StringBuilder queryString(Map<String, String> params) {
|
||||
StringBuilder queryStringBuilder = new StringBuilder();
|
||||
for (Entry<String, String> entry : params.entrySet()) {
|
||||
@@ -195,10 +184,10 @@ public class SentinelApiClient {
|
||||
}
|
||||
return queryStringBuilder;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Build an `HttpUriRequest` in POST way.
|
||||
*
|
||||
*
|
||||
* @param url
|
||||
* @param params
|
||||
* @param supportEnhancedContentType see {@link #isSupportEnhancedContentType(String, String, int)}
|
||||
@@ -218,7 +207,7 @@ public class SentinelApiClient {
|
||||
}
|
||||
return httpPost;
|
||||
}
|
||||
|
||||
|
||||
private String urlEncode(String str) {
|
||||
try {
|
||||
return URLEncoder.encode(str, DEFAULT_CHARSET.name());
|
||||
@@ -227,7 +216,7 @@ public class SentinelApiClient {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private String getBody(HttpResponse response) throws Exception {
|
||||
Charset charset = null;
|
||||
try {
|
||||
@@ -240,10 +229,10 @@ public class SentinelApiClient {
|
||||
}
|
||||
return EntityUtils.toString(response.getEntity(), charset != null ? charset : DEFAULT_CHARSET);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* With no param
|
||||
*
|
||||
*
|
||||
* @param ip
|
||||
* @param port
|
||||
* @param api
|
||||
@@ -252,10 +241,10 @@ public class SentinelApiClient {
|
||||
private CompletableFuture<String> executeCommand(String ip, int port, String api, boolean useHttpPost) {
|
||||
return executeCommand(ip, port, api, null, useHttpPost);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* No app specified, force to GET
|
||||
*
|
||||
*
|
||||
* @param ip
|
||||
* @param port
|
||||
* @param api
|
||||
@@ -268,7 +257,7 @@ public class SentinelApiClient {
|
||||
|
||||
/**
|
||||
* Prefer to execute request using POST
|
||||
*
|
||||
*
|
||||
* @param app
|
||||
* @param ip
|
||||
* @param port
|
||||
@@ -282,6 +271,14 @@ public class SentinelApiClient {
|
||||
future.completeExceptionally(new IllegalArgumentException("Bad URL or command name"));
|
||||
return future;
|
||||
}
|
||||
if (!InetAddressUtils.isIPv4Address(ip) && !InetAddressUtils.isIPv6Address(ip)) {
|
||||
future.completeExceptionally(new IllegalArgumentException("Bad IP"));
|
||||
return future;
|
||||
}
|
||||
if (!StringUtil.isEmpty(app) && !appManagement.isValidMachineOfApp(app, ip)) {
|
||||
future.completeExceptionally(new IllegalArgumentException("Given ip does not belong to given app"));
|
||||
return future;
|
||||
}
|
||||
StringBuilder urlBuilder = new StringBuilder();
|
||||
urlBuilder.append("http://");
|
||||
urlBuilder.append(ip).append(':').append(port).append('/').append(api);
|
||||
@@ -305,7 +302,7 @@ public class SentinelApiClient {
|
||||
postRequest(urlBuilder.toString(), params, isSupportEnhancedContentType(app, ip, port)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private CompletableFuture<String> executeCommand(HttpUriRequest request) {
|
||||
CompletableFuture<String> future = new CompletableFuture<>();
|
||||
httpClient.execute(request, new FutureCallback<HttpResponse>() {
|
||||
@@ -343,11 +340,11 @@ public class SentinelApiClient {
|
||||
});
|
||||
return future;
|
||||
}
|
||||
|
||||
|
||||
public void close() throws Exception {
|
||||
httpClient.close();
|
||||
}
|
||||
|
||||
|
||||
@Nullable
|
||||
private <T> CompletableFuture<List<T>> fetchItemsAsync(String ip, int port, String api, String type, Class<T> ruleType) {
|
||||
AssertUtil.notEmpty(ip, "Bad machine IP");
|
||||
@@ -360,7 +357,7 @@ public class SentinelApiClient {
|
||||
return executeCommand(ip, port, api, params, false)
|
||||
.thenApply(json -> JSON.parseArray(json, ruleType));
|
||||
}
|
||||
|
||||
|
||||
@Nullable
|
||||
private <T> List<T> fetchItems(String ip, int port, String api, String type, Class<T> ruleType) {
|
||||
try {
|
||||
@@ -380,11 +377,11 @@ public class SentinelApiClient {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private <T extends Rule> List<T> fetchRules(String ip, int port, String type, Class<T> ruleType) {
|
||||
return fetchItems(ip, port, GET_RULES_PATH, type, ruleType);
|
||||
}
|
||||
|
||||
|
||||
private boolean setRules(String app, String ip, int port, String type, List<? extends RuleEntity> entities) {
|
||||
if (entities == null) {
|
||||
return true;
|
||||
|
||||
@@ -15,32 +15,23 @@
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.dashboard.controller;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthAction;
|
||||
import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient;
|
||||
import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo;
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType;
|
||||
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.AuthorityRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement;
|
||||
import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.Result;
|
||||
import com.alibaba.csp.sentinel.dashboard.repository.rule.RuleRepository;
|
||||
|
||||
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
||||
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.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Eric Zhao
|
||||
@@ -56,6 +47,8 @@ public class AuthorityRuleController {
|
||||
private SentinelApiClient sentinelApiClient;
|
||||
@Autowired
|
||||
private RuleRepository<AuthorityRuleEntity, Long> repository;
|
||||
@Autowired
|
||||
private AppManagement appManagement;
|
||||
|
||||
@GetMapping("/rules")
|
||||
@AuthAction(PrivilegeType.READ_RULE)
|
||||
@@ -71,6 +64,9 @@ public class AuthorityRuleController {
|
||||
if (port == null || port <= 0) {
|
||||
return Result.ofFail(-1, "Invalid parameter: port");
|
||||
}
|
||||
if (!appManagement.isValidMachineOfApp(app, ip)) {
|
||||
return Result.ofFail(-1, "given ip does not belong to given app");
|
||||
}
|
||||
try {
|
||||
List<AuthorityRuleEntity> rules = sentinelApiClient.fetchAuthorityRulesOfMachine(app, ip, port);
|
||||
rules = repository.saveAll(rules);
|
||||
|
||||
@@ -15,32 +15,24 @@
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.dashboard.controller;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthAction;
|
||||
import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient;
|
||||
import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo;
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType;
|
||||
import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement;
|
||||
import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.Result;
|
||||
import com.alibaba.csp.sentinel.dashboard.repository.rule.RuleRepository;
|
||||
import com.alibaba.csp.sentinel.slots.block.RuleConstant;
|
||||
import com.alibaba.csp.sentinel.slots.block.degrade.circuitbreaker.CircuitBreakerStrategy;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.DegradeRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.Result;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Controller regarding APIs of degrade rules. Refactored since 1.8.0.
|
||||
@@ -58,6 +50,8 @@ public class DegradeController {
|
||||
private RuleRepository<DegradeRuleEntity, Long> repository;
|
||||
@Autowired
|
||||
private SentinelApiClient sentinelApiClient;
|
||||
@Autowired
|
||||
private AppManagement appManagement;
|
||||
|
||||
@GetMapping("/rules.json")
|
||||
@AuthAction(PrivilegeType.READ_RULE)
|
||||
@@ -71,6 +65,9 @@ public class DegradeController {
|
||||
if (port == null) {
|
||||
return Result.ofFail(-1, "port can't be null");
|
||||
}
|
||||
if (!appManagement.isValidMachineOfApp(app, ip)) {
|
||||
return Result.ofFail(-1, "given ip does not belong to given app");
|
||||
}
|
||||
try {
|
||||
List<DegradeRuleEntity> rules = sentinelApiClient.fetchDegradeRuleOfMachine(app, ip, port);
|
||||
rules = repository.saveAll(rules);
|
||||
@@ -173,6 +170,9 @@ public class DegradeController {
|
||||
if (StringUtil.isBlank(entity.getIp())) {
|
||||
return Result.ofFail(-1, "ip can't be null or empty");
|
||||
}
|
||||
if (!appManagement.isValidMachineOfApp(entity.getApp(), entity.getIp())) {
|
||||
return Result.ofFail(-1, "given ip does not belong to given app");
|
||||
}
|
||||
if (entity.getPort() == null || entity.getPort() <= 0) {
|
||||
return Result.ofFail(-1, "invalid port: " + entity.getPort());
|
||||
}
|
||||
|
||||
@@ -15,34 +15,26 @@
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.dashboard.controller;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthAction;
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType;
|
||||
import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement;
|
||||
import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.Result;
|
||||
import com.alibaba.csp.sentinel.dashboard.repository.rule.InMemoryRuleRepositoryAdapter;
|
||||
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.web.bind.annotation.*;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthAction;
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient;
|
||||
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.repository.rule.InMemoryRuleRepositoryAdapter;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* Flow rule controller.
|
||||
*
|
||||
@@ -57,6 +49,8 @@ public class FlowControllerV1 {
|
||||
|
||||
@Autowired
|
||||
private InMemoryRuleRepositoryAdapter<FlowRuleEntity> repository;
|
||||
@Autowired
|
||||
private AppManagement appManagement;
|
||||
|
||||
@Autowired
|
||||
private SentinelApiClient sentinelApiClient;
|
||||
@@ -66,7 +60,6 @@ public class FlowControllerV1 {
|
||||
public Result<List<FlowRuleEntity>> apiQueryMachineRules(@RequestParam String app,
|
||||
@RequestParam String ip,
|
||||
@RequestParam Integer port) {
|
||||
|
||||
if (StringUtil.isEmpty(app)) {
|
||||
return Result.ofFail(-1, "app can't be null or empty");
|
||||
}
|
||||
@@ -76,6 +69,9 @@ public class FlowControllerV1 {
|
||||
if (port == null) {
|
||||
return Result.ofFail(-1, "port can't be null");
|
||||
}
|
||||
if (!appManagement.isValidMachineOfApp(app, ip)) {
|
||||
return Result.ofFail(-1, "given ip does not belong to given app");
|
||||
}
|
||||
try {
|
||||
List<FlowRuleEntity> rules = sentinelApiClient.fetchFlowRuleOfMachine(app, ip, port);
|
||||
rules = repository.saveAll(rules);
|
||||
@@ -96,6 +92,9 @@ public class FlowControllerV1 {
|
||||
if (entity.getPort() == null) {
|
||||
return Result.ofFail(-1, "port can't be null");
|
||||
}
|
||||
if (!appManagement.isValidMachineOfApp(entity.getApp(), entity.getIp())) {
|
||||
return Result.ofFail(-1, "given ip does not belong to given app");
|
||||
}
|
||||
if (StringUtil.isBlank(entity.getLimitApp())) {
|
||||
return Result.ofFail(-1, "limitApp can't be null or empty");
|
||||
}
|
||||
|
||||
@@ -20,6 +20,7 @@ import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.Result;
|
||||
|
||||
import org.apache.http.conn.util.InetAddressUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.controller;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthAction;
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService;
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType;
|
||||
import com.alibaba.csp.sentinel.dashboard.client.CommandNotFoundException;
|
||||
import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient;
|
||||
@@ -83,6 +84,9 @@ public class ParamFlowRuleController {
|
||||
if (port == null || port <= 0) {
|
||||
return Result.ofFail(-1, "Invalid parameter: port");
|
||||
}
|
||||
if (!appManagement.isValidMachineOfApp(app, ip)) {
|
||||
return Result.ofFail(-1, "given ip does not belong to given app");
|
||||
}
|
||||
if (!checkIfSupported(app, ip, port)) {
|
||||
return unsupportedVersion();
|
||||
}
|
||||
@@ -109,7 +113,7 @@ public class ParamFlowRuleController {
|
||||
}
|
||||
|
||||
@PostMapping("/rule")
|
||||
@AuthAction(PrivilegeType.WRITE_RULE)
|
||||
@AuthAction(AuthService.PrivilegeType.WRITE_RULE)
|
||||
public Result<ParamFlowRuleEntity> apiAddParamFlowRule(@RequestBody ParamFlowRuleEntity entity) {
|
||||
Result<ParamFlowRuleEntity> checkResult = checkEntityInternal(entity);
|
||||
if (checkResult != null) {
|
||||
@@ -178,7 +182,7 @@ public class ParamFlowRuleController {
|
||||
}
|
||||
|
||||
@PutMapping("/rule/{id}")
|
||||
@AuthAction(PrivilegeType.WRITE_RULE)
|
||||
@AuthAction(AuthService.PrivilegeType.WRITE_RULE)
|
||||
public Result<ParamFlowRuleEntity> apiUpdateParamFlowRule(@PathVariable("id") Long id,
|
||||
@RequestBody ParamFlowRuleEntity entity) {
|
||||
if (id == null || id <= 0) {
|
||||
|
||||
@@ -15,19 +15,15 @@
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.dashboard.controller;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthAction;
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType;
|
||||
import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.discovery.AppManagement;
|
||||
import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.Result;
|
||||
import com.alibaba.csp.sentinel.dashboard.repository.rule.RuleRepository;
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.SystemRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo;
|
||||
import com.alibaba.csp.sentinel.dashboard.client.SentinelApiClient;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.Result;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -35,6 +31,9 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author leyou(lihao)
|
||||
*/
|
||||
@@ -48,6 +47,8 @@ public class SystemController {
|
||||
private RuleRepository<SystemRuleEntity, Long> repository;
|
||||
@Autowired
|
||||
private SentinelApiClient sentinelApiClient;
|
||||
@Autowired
|
||||
private AppManagement appManagement;
|
||||
|
||||
private <R> Result<R> checkBasicParams(String app, String ip, Integer port) {
|
||||
if (StringUtil.isEmpty(app)) {
|
||||
@@ -59,6 +60,9 @@ public class SystemController {
|
||||
if (port == null) {
|
||||
return Result.ofFail(-1, "port can't be null");
|
||||
}
|
||||
if (!appManagement.isValidMachineOfApp(app, ip)) {
|
||||
return Result.ofFail(-1, "given ip does not belong to given app");
|
||||
}
|
||||
if (port <= 0 || port > 65535) {
|
||||
return Result.ofFail(-1, "port should be in (0, 65535)");
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
package com.alibaba.csp.sentinel.dashboard.controller.v2;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthAction;
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService;
|
||||
import com.alibaba.csp.sentinel.dashboard.auth.AuthService.PrivilegeType;
|
||||
import com.alibaba.csp.sentinel.dashboard.datasource.entity.rule.FlowRuleEntity;
|
||||
import com.alibaba.csp.sentinel.dashboard.domain.Result;
|
||||
@@ -124,7 +125,7 @@ public class FlowControllerV2 {
|
||||
}
|
||||
|
||||
@PostMapping("/rule")
|
||||
@AuthAction(value = PrivilegeType.WRITE_RULE)
|
||||
@AuthAction(value = AuthService.PrivilegeType.WRITE_RULE)
|
||||
public Result<FlowRuleEntity> apiAddFlowRule(@RequestBody FlowRuleEntity entity) {
|
||||
|
||||
Result<FlowRuleEntity> checkResult = checkEntityInternal(entity);
|
||||
@@ -148,7 +149,7 @@ public class FlowControllerV2 {
|
||||
}
|
||||
|
||||
@PutMapping("/rule/{id}")
|
||||
@AuthAction(PrivilegeType.WRITE_RULE)
|
||||
@AuthAction(AuthService.PrivilegeType.WRITE_RULE)
|
||||
|
||||
public Result<FlowRuleEntity> apiUpdateFlowRule(@PathVariable("id") Long id,
|
||||
@RequestBody FlowRuleEntity entity) {
|
||||
|
||||
@@ -15,15 +15,11 @@
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.dashboard.discovery;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.Optional;
|
||||
import java.util.Iterator;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import com.alibaba.csp.sentinel.dashboard.config.DashboardConfig;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class AppInfo {
|
||||
|
||||
private String app = "";
|
||||
@@ -96,6 +92,12 @@ public class AppInfo {
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public Optional<MachineInfo> getMachine(String ip) {
|
||||
return machines.stream()
|
||||
.filter(e -> e.getIp().equals(ip))
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
private boolean heartbeatJudge(final int threshold) {
|
||||
if (machines.size() == 0) {
|
||||
return false;
|
||||
|
||||
@@ -15,15 +15,16 @@
|
||||
*/
|
||||
package com.alibaba.csp.sentinel.dashboard.discovery;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import com.alibaba.csp.sentinel.util.StringUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
|
||||
@Component
|
||||
public class AppManagement implements MachineDiscovery {
|
||||
|
||||
@@ -46,7 +47,7 @@ public class AppManagement implements MachineDiscovery {
|
||||
public long addMachine(MachineInfo machineInfo) {
|
||||
return machineDiscovery.addMachine(machineInfo);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean removeMachine(String app, String ip, int port) {
|
||||
return machineDiscovery.removeMachine(app, ip, port);
|
||||
@@ -61,10 +62,19 @@ public class AppManagement implements MachineDiscovery {
|
||||
public AppInfo getDetailApp(String app) {
|
||||
return machineDiscovery.getDetailApp(app);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void removeApp(String app) {
|
||||
machineDiscovery.removeApp(app);
|
||||
}
|
||||
|
||||
public boolean isValidMachineOfApp(String app, String ip) {
|
||||
if (StringUtil.isEmpty(app)) {
|
||||
return false;
|
||||
}
|
||||
return Optional.ofNullable(getDetailApp(app))
|
||||
.flatMap(a -> a.getMachine(ip))
|
||||
.isPresent();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
## Env Requirement
|
||||
|
||||
- Node.js > 6.x
|
||||
- Node.js < 12.x
|
||||
|
||||
## Code Guide
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
## 环境要求
|
||||
|
||||
- Node.js > 6.x
|
||||
- Node.js < 12.x
|
||||
|
||||
## 编码规范
|
||||
|
||||
|
||||
Reference in New Issue
Block a user