未验证 提交 67a9ff83 编写于 作者: M min 提交者: GitHub

Develop (#104)

* add codemirror

* remove unused import

* add yaml parser& routing rule

* add yaml parser& routing rule
上级 28036397
......@@ -70,6 +70,12 @@
<version>1.2.46</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.22</version>
</dependency>
</dependencies>
<build>
......
package org.apache.dubbo.admin.controller;
import org.apache.dubbo.admin.governance.service.ProviderService;
import org.apache.dubbo.admin.governance.service.RouteService;
import org.apache.dubbo.admin.registry.common.domain.Route;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.yaml.snakeyaml.Yaml;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping("/api/routes")
public class RoutesController {
@Autowired
private RouteService routeService;
@Autowired
private ProviderService providerService;
@RequestMapping("/create")
public void createRule(@RequestParam String serviceName, @RequestParam String rule) {
public void createRule(@RequestParam(required = false) String serviceName,
@RequestParam(required = false) String app,
@RequestParam String rule) {
if (serviceName == null && app == null) {
}
Yaml yaml = new Yaml();
Map<String, Object> result = yaml.load(rule);
if (serviceName != null) {
result.put("scope", serviceName);
result.put("group/service:version", result.get("group") + "/" + serviceName);
//2.6
String version = null;
String service = serviceName;
if (serviceName.contains(":") && !serviceName.endsWith(":")) {
version = serviceName.split(":")[1];
service = serviceName.split(":")[0];
}
List<String> conditions = (List)result.get("conditions");
for (String condition : conditions) {
Route route = new Route();
route.setService(service);
route.setVersion(version);
route.setEnabled((boolean)getParameter(result, "enabled", true));
route.setForce((boolean)getParameter(result, "force", false));
route.setGroup((String)getParameter(result, "group", null));
route.setDynamic((boolean)getParameter(result, "dynamic", false));
route.setRuntime((boolean)getParameter(result, "runtime", false));
route.setPriority((int)getParameter(result, "priority", 0));
route.setRule(condition);
routeService.createRoute(route);
}
} else {
//new feature in 2.7
result.put("scope", "application");
result.put("appname", app);
}
}
private Object getParameter(Map<String, Object> result, String key, Object defaultValue) {
if (result.get(key) != null) {
return result.get(key);
}
return defaultValue;
}
public static void main(String[] args) {
String yaml =
"enable: true\n" +
"priority: 0\n" +
"runtime: true\n" +
"category: routers\n" +
"dynamic: true\n" +
"conditions:\n" +
" - '=> host != 172.22.3.91'\n" +
" - 'host != 10.20.153.10,10.20.153.11 =>'\n" +
" - 'host = 10.20.153.10,10.20.153.11 =>'\n" +
" - 'application != kylin => host != 172.22.3.95,172.22.3.96'\n" +
" - 'method = find*,list*,get*,is* => host = 172.22.3.94,172.22.3.95,172.22.3.96'";
}
}
\ No newline at end of file
......@@ -65,6 +65,14 @@ public class Route extends Entity {
private boolean force;
private String version;
private String group;
private boolean dynamic;
private boolean runtime;
private List<Route> children;
public Route() {
......@@ -122,6 +130,38 @@ public class Route extends Entity {
this.enabled = enabled;
}
public boolean isDynamic() {
return dynamic;
}
public void setDynamic(boolean dynamic) {
this.dynamic = dynamic;
}
public boolean isRuntime() {
return runtime;
}
public void setRuntime(boolean runtime) {
this.runtime = runtime;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public boolean isForce() {
return force;
}
......@@ -144,12 +184,12 @@ public class Route extends Entity {
public void setRule(String rule) {
this.rule = rule;
String[] rules = rule.split(" => ");
if (rules.length != 2) {
throw new IllegalArgumentException("Illegal Route Condition Rule");
}
this.matchRule = rules[0];
this.filterRule = rules[1];
// String[] rules = rule.split(" => ");
// if (rules.length != 2) {
// throw new IllegalArgumentException("Illegal Route Condition Rule");
// }
// this.matchRule = rules[0];
// this.filterRule = rules[1];
}
public String getMatchRule() {
......@@ -177,25 +217,12 @@ public class Route extends Entity {
}
public URL toUrl() {
String group = null;
String version = null;
String path = service;
int i = path.indexOf("/");
if (i > 0) {
group = path.substring(0, i);
path = path.substring(i + 1);
}
i = path.lastIndexOf(":");
if (i > 0) {
version = path.substring(i + 1);
path = path.substring(0, i);
}
return URL.valueOf(Constants.ROUTE_PROTOCOL + "://" + Constants.ANYHOST_VALUE + "/" + path
return URL.valueOf(Constants.ROUTE_PROTOCOL + "://" + Constants.ANYHOST_VALUE + "/" + getService()
+ "?" + Constants.CATEGORY_KEY + "=" + Constants.ROUTERS_CATEGORY
+ "&router=condition&runtime=false&enabled=" + isEnabled() + "&priority=" + getPriority() + "&force=" + isForce() + "&dynamic=false"
+ "&name=" + getName() + "&" + Constants.RULE_KEY + "=" + URL.encode(getMatchRule() + " => " + getFilterRule())
+ (group == null ? "" : "&" + Constants.GROUP_KEY + "=" + group)
+ (version == null ? "" : "&" + Constants.VERSION_KEY + "=" + version));
+ "&router=condition&runtime=false&enabled=" + isEnabled() + "&priority=" + getPriority() + "&force=" + isForce() + "&dynamic=" + isDynamic()
+ "&name=" + getName() + "&" + Constants.RULE_KEY + "=" + URL.encode(getRule())
+ (getGroup() == null ? "" : "&" + Constants.GROUP_KEY + "=" + getGroup())
+ (getVersion() == null ? "" : "&" + Constants.VERSION_KEY + "=" + getVersion()));
}
}
......@@ -88,23 +88,15 @@
</v-card-title>
<v-card-text >
<v-text-field
placeholder="service:version or application, version is optional"
placeholder="service:version, version is optional"
required
ref="scope"
:rules="[() => !!scope || 'This field is required']"
v-model="scope"
v-model="service"
></v-text-field>
<v-text-field
placeholder="group, only effective on service"
v-model="group"
placeholder="application name"
required
v-model="application"
></v-text-field>
<!--<v-textarea-->
<!--id="rule-content"-->
<!--name="input-7-1"-->
<!--box-->
<!--:height="height"-->
<!--:placeholder="placeholder"-->
<!--&gt;</v-textarea>-->
<codemirror :placeholder='placeholder' :options="cmOption"></codemirror>
</v-card-text>
<v-card-actions>
......@@ -131,8 +123,8 @@
pattern: 'Service',
filter: '',
dialog: false,
group: '',
scope: '',
application: '',
service: '',
height: 0,
routingRules: [
{
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册