Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #760]Application discover support #807

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions dubbo-admin-server/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-x-discovery</artifactId>
</dependency>

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
Expand Down Expand Up @@ -167,11 +172,6 @@
<artifactId>netty-all</artifactId>
</dependency>

<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-serialization-kryo</artifactId>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public class Constants {
public static final Set<String> CONFIGS = new HashSet<>();
public static final String COLON = ":";
public static final String MESH_RULE_SUFFIX = ".MESHAPPRULE";
public static final String DEFAULT_MAPPING_GROUP = "mapping";
static {
CONFIGS.add(WEIGHT);
CONFIGS.add(BALANCING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@

import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.admin.model.domain.MethodMetadata;
import org.apache.dubbo.metadata.definition.model.FullServiceDefinition;
import org.apache.dubbo.metadata.definition.model.MethodDefinition;
import org.apache.dubbo.metadata.definition.model.ServiceDefinition;
import org.apache.dubbo.metadata.definition.model.TypeDefinition;
import org.apache.dubbo.admin.model.domain.FullServiceDefinition;
import org.apache.dubbo.admin.model.domain.MethodDefinition;
import org.apache.dubbo.admin.model.domain.ServiceDefinition;
import org.apache.dubbo.admin.model.domain.TypeDefinition;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -180,10 +180,7 @@ private static Object generateMapType(ServiceDefinition sd, TypeDefinition td) {
keyType = StringUtils.strip(keyType);

Map<Object, Object> map = new HashMap<>();
// 生成 key 默认值
Object key = generateType(sd, keyType);

// 生成 value 默认值
String valueType = StringUtils.substringAfter(td.getType(), ",");
valueType = StringUtils.substringBefore(valueType, ">");
valueType = StringUtils.strip(valueType);
Expand All @@ -197,7 +194,7 @@ private static Object generateCollectionType(ServiceDefinition sd, TypeDefinitio
String type = StringUtils.substringAfter(td.getType(), "<");
type = StringUtils.substringBefore(type, ">");
if (StringUtils.isEmpty(type)) {
// 如果 collection 类型未声明,则生成空 collection
// if type is null return empty collection
return new Object[] {};
}
return new Object[]{generateType(sd, type)};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,233 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.dubbo.admin.common.util;

import org.apache.dubbo.admin.model.domain.MethodMetadata;
import org.apache.dubbo.metadata.definition.model.FullServiceDefinition;
import org.apache.dubbo.metadata.definition.model.MethodDefinition;
import org.apache.dubbo.metadata.definition.model.ServiceDefinition;
import org.apache.dubbo.metadata.definition.model.TypeDefinition;

import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ServiceTestV3Util {
private static Pattern COLLECTION_PATTERN = Pattern.compile("^java\\.util\\..*(Set|List|Queue|Collection|Deque)(<.*>)*$");
private static Pattern MAP_PATTERN = Pattern.compile("^java\\.util\\..*Map.*(<.*>)*$");

public static boolean sameMethod(MethodDefinition m, String methodSig) {
String name = m.getName();
String[] parameters = m.getParameterTypes();
StringBuilder sb = new StringBuilder();
sb.append(name).append("~");
for (String parameter : parameters) {
sb.append(parameter).append(";");
}
String sig = StringUtils.removeEnd(sb.toString(), ";");
return sig.equals(methodSig);
}

public static MethodMetadata generateMethodMeta(FullServiceDefinition serviceDefinition, MethodDefinition methodDefinition) {
MethodMetadata methodMetadata = new MethodMetadata();
String[] parameterTypes = methodDefinition.getParameterTypes();
String returnType = methodDefinition.getReturnType();
String signature = methodDefinition.getName() + "~" + String.join(";", parameterTypes);
methodMetadata.setSignature(signature);
methodMetadata.setReturnType(returnType);
List<Object> parameters = generateParameterTypes(parameterTypes, serviceDefinition);
methodMetadata.setParameterTypes(parameters);
return methodMetadata;
}

private static boolean isPrimitiveType(TypeDefinition td) {
String type = td.getType();
return isPrimitiveType(type);
}

private static boolean isPrimitiveType(String type) {
return type.equals("byte") || type.equals("java.lang.Byte") ||
type.equals("short") || type.equals("java.lang.Short") ||
type.equals("int") || type.equals("java.lang.Integer") ||
type.equals("long") || type.equals("java.lang.Long") ||
type.equals("float") || type.equals("java.lang.Float") ||
type.equals("double") || type.equals("java.lang.Double") ||
type.equals("boolean") || type.equals("java.lang.Boolean") ||
type.equals("void") || type.equals("java.lang.Void") ||
type.equals("java.lang.String") ||
type.equals("java.util.Date") ||
type.equals("java.lang.Object");
}

private static List<Object> generateParameterTypes(String[] parameterTypes, ServiceDefinition serviceDefinition) {
List<Object> parameters = new ArrayList<>();
for (String type : parameterTypes) {
Object result = generateType(serviceDefinition, type);
parameters.add(result);
}
return parameters;
}

private static TypeDefinition findTypeDefinition(ServiceDefinition serviceDefinition, String type) {
return serviceDefinition.getTypes().stream()
.filter(t -> t.getType().equals(type))
.findFirst().orElse(new TypeDefinition(type));
}

private static void generateComplexType(ServiceDefinition sd, TypeDefinition td, Map<String, Object> holder) {
td.getProperties().forEach((name, type) -> {
if (isPrimitiveType(type)) {
holder.put(name, generatePrimitiveType(type));
} else {
generateEnclosedType(holder, name, sd, type);
}
});
}

private static Object generateComplexType(ServiceDefinition sd, TypeDefinition td) {
Map<String, Object> holder = new HashMap<>();
generateComplexType(sd, td, holder);
return holder;
}

private static boolean isMap(TypeDefinition td) {
String type = StringUtils.substringBefore(td.getType(), "<");
Matcher matcher = MAP_PATTERN.matcher(type);
return matcher.matches();
}

private static boolean isCollection(TypeDefinition td) {
String type = StringUtils.substringBefore(td.getType(), "<");
Matcher matcher = COLLECTION_PATTERN.matcher(type);
return matcher.matches();
}

private static boolean isArray(TypeDefinition td) {
return StringUtils.endsWith(td.getType(), "[]");
}

private static Object generatePrimitiveType(TypeDefinition td) {
return generatePrimitiveType(td.getType());
}

private static Object generatePrimitiveType(String type) {
switch (type) {
case "byte":
case "java.lang.Byte":
case "short":
case "java.lang.Short":
case "int":
case "java.lang.Integer":
case "long":
case "java.lang.Long":
return 0;
case "float":
case "java.lang.Float":
case "double":
case "java.lang.Double":
return 0.0;
case "boolean":
case "java.lang.Boolean":
return true;
case "void":
case "java.lang.Void":
return null;
case "java.lang.String":
return "";
case "java.lang.Object":
return Collections.emptyMap();
case "java.util.Date":
return System.currentTimeMillis();
default:
return Collections.emptyMap();
}
}

private static Object generateType(ServiceDefinition sd, String type) {
TypeDefinition td = findTypeDefinition(sd, type);
return generateType(sd, td);
}

private static Object generateType(ServiceDefinition sd, TypeDefinition td) {
if (isPrimitiveType(td)) {
return generatePrimitiveType(td);
} else if (isMap(td)) {
return generateMapType(sd, td);
} else if (isArray(td)) {
return generateArrayType(sd, td);
} else if (isCollection(td)) {
return generateCollectionType(sd, td);
} else {
return generateComplexType(sd, td);
}
}

private static Object generateMapType(ServiceDefinition sd, TypeDefinition td) {
String keyType = StringUtils.substringAfter(td.getType(), "<");
keyType = StringUtils.substringBefore(keyType, ",");
keyType = StringUtils.strip(keyType);

Map<Object, Object> map = new HashMap<>();
Object key = generateType(sd, keyType);
String valueType = StringUtils.substringAfter(td.getType(), ",");
valueType = StringUtils.substringBefore(valueType, ">");
valueType = StringUtils.strip(valueType);
valueType = StringUtils.isNotEmpty(valueType) ? valueType : "java.lang.Object";
Object value = generateType(sd, valueType);
map.put(key, value);
return map;
}

private static Object generateCollectionType(ServiceDefinition sd, TypeDefinition td) {
String type = StringUtils.substringAfter(td.getType(), "<");
type = StringUtils.substringBefore(type, ">");
if (StringUtils.isEmpty(type)) {
// if type is null return empty collection
return new Object[]{};
}
return new Object[]{generateType(sd, type)};

}

private static Object generateArrayType(ServiceDefinition sd, TypeDefinition td) {
String type = StringUtils.substringBeforeLast(td.getType(), "[]");
return new Object[]{generateType(sd, type)};
}

private static void generateEnclosedType(Map<String, Object> holder, String key, ServiceDefinition sd, String type) {
if (isPrimitiveType(type)) {
holder.put(key, generateType(sd, type));
} else {
TypeDefinition td = findTypeDefinition(sd, type);
if (td.getProperties() == null || td.getProperties().size() == 0) {
holder.put(key, generateType(sd, td));
} else {
Map<String, Object> enclosedMap = new HashMap<>();
holder.put(key, enclosedMap);
generateComplexType(sd, td, enclosedMap);
}

}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

import org.apache.dubbo.admin.model.domain.Consumer;
import org.apache.dubbo.admin.model.domain.Provider;
import org.apache.dubbo.admin.model.domain.RegistrySource;
import org.apache.dubbo.common.BaseServiceMetadata;
import org.apache.dubbo.common.URL;

import java.util.ArrayList;
Expand Down Expand Up @@ -48,7 +50,10 @@ public static Provider url2Provider(Pair<String, URL> pair) {

Provider p = new Provider();
p.setHash(id);
p.setService(url.getServiceKey());
String group = url.getUrlParam().getParameter(Constants.GROUP_KEY);
String version = url.getUrlParam().getParameter(Constants.VERSION_KEY);
String service = BaseServiceMetadata.buildServiceKey(url.getServiceInterface(), group, version);
p.setService(service);
p.setAddress(url.getAddress());
p.setApplication(url.getParameter(Constants.APPLICATION_KEY));
p.setUrl(url.toIdentityString());
Expand All @@ -58,6 +63,7 @@ public static Provider url2Provider(Pair<String, URL> pair) {
p.setEnabled(url.getParameter(Constants.ENABLED_KEY, true));
p.setWeight(url.getParameter(Constants.WEIGHT_KEY, Constants.DEFAULT_WEIGHT));
p.setUsername(url.getParameter("owner"));
p.setRegistrySource(RegistrySource.INTERFACE);

return p;
}
Expand All @@ -83,7 +89,10 @@ public static Consumer url2Consumer(Pair<String, URL> pair) {

Consumer c = new Consumer();
c.setHash(id);
c.setService(url.getServiceKey());
String group = url.getUrlParam().getParameter(Constants.GROUP_KEY);
String version = url.getUrlParam().getParameter(Constants.VERSION_KEY);
String service = BaseServiceMetadata.buildServiceKey(url.getServiceInterface(), group, version);
c.setService(service);
c.setAddress(url.getHost());
c.setApplication(url.getParameter(Constants.APPLICATION_KEY));
c.setParameters(url.toParameterString());
Expand Down
Loading