未验证 提交 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 @@ ...@@ -70,6 +70,12 @@
<version>1.2.46</version> <version>1.2.46</version>
</dependency> </dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>1.22</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package org.apache.dubbo.admin.controller; 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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import org.yaml.snakeyaml.Yaml;
import java.util.List;
import java.util.Map;
@RestController @RestController
@RequestMapping("/api/routes") @RequestMapping("/api/routes")
public class RoutesController { public class RoutesController {
@Autowired
private RouteService routeService;
@Autowired
private ProviderService providerService;
@RequestMapping("/create") @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 { ...@@ -65,6 +65,14 @@ public class Route extends Entity {
private boolean force; private boolean force;
private String version;
private String group;
private boolean dynamic;
private boolean runtime;
private List<Route> children; private List<Route> children;
public Route() { public Route() {
...@@ -122,6 +130,38 @@ public class Route extends Entity { ...@@ -122,6 +130,38 @@ public class Route extends Entity {
this.enabled = enabled; 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() { public boolean isForce() {
return force; return force;
} }
...@@ -144,12 +184,12 @@ public class Route extends Entity { ...@@ -144,12 +184,12 @@ public class Route extends Entity {
public void setRule(String rule) { public void setRule(String rule) {
this.rule = rule; this.rule = rule;
String[] rules = rule.split(" => "); // String[] rules = rule.split(" => ");
if (rules.length != 2) { // if (rules.length != 2) {
throw new IllegalArgumentException("Illegal Route Condition Rule"); // throw new IllegalArgumentException("Illegal Route Condition Rule");
} // }
this.matchRule = rules[0]; // this.matchRule = rules[0];
this.filterRule = rules[1]; // this.filterRule = rules[1];
} }
public String getMatchRule() { public String getMatchRule() {
...@@ -177,25 +217,12 @@ public class Route extends Entity { ...@@ -177,25 +217,12 @@ public class Route extends Entity {
} }
public URL toUrl() { public URL toUrl() {
String group = null; return URL.valueOf(Constants.ROUTE_PROTOCOL + "://" + Constants.ANYHOST_VALUE + "/" + getService()
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
+ "?" + Constants.CATEGORY_KEY + "=" + Constants.ROUTERS_CATEGORY + "?" + Constants.CATEGORY_KEY + "=" + Constants.ROUTERS_CATEGORY
+ "&router=condition&runtime=false&enabled=" + isEnabled() + "&priority=" + getPriority() + "&force=" + isForce() + "&dynamic=false" + "&router=condition&runtime=false&enabled=" + isEnabled() + "&priority=" + getPriority() + "&force=" + isForce() + "&dynamic=" + isDynamic()
+ "&name=" + getName() + "&" + Constants.RULE_KEY + "=" + URL.encode(getMatchRule() + " => " + getFilterRule()) + "&name=" + getName() + "&" + Constants.RULE_KEY + "=" + URL.encode(getRule())
+ (group == null ? "" : "&" + Constants.GROUP_KEY + "=" + group) + (getGroup() == null ? "" : "&" + Constants.GROUP_KEY + "=" + getGroup())
+ (version == null ? "" : "&" + Constants.VERSION_KEY + "=" + version)); + (getVersion() == null ? "" : "&" + Constants.VERSION_KEY + "=" + getVersion()));
} }
} }
...@@ -88,23 +88,15 @@ ...@@ -88,23 +88,15 @@
</v-card-title> </v-card-title>
<v-card-text > <v-card-text >
<v-text-field <v-text-field
placeholder="service:version or application, version is optional" placeholder="service:version, version is optional"
required required
ref="scope" v-model="service"
:rules="[() => !!scope || 'This field is required']"
v-model="scope"
></v-text-field> ></v-text-field>
<v-text-field <v-text-field
placeholder="group, only effective on service" placeholder="application name"
v-model="group" required
v-model="application"
></v-text-field> ></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> <codemirror :placeholder='placeholder' :options="cmOption"></codemirror>
</v-card-text> </v-card-text>
<v-card-actions> <v-card-actions>
...@@ -131,8 +123,8 @@ ...@@ -131,8 +123,8 @@
pattern: 'Service', pattern: 'Service',
filter: '', filter: '',
dialog: false, dialog: false,
group: '', application: '',
scope: '', service: '',
height: 0, height: 0,
routingRules: [ routingRules: [
{ {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册