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

improve:change serviceMetadata order #3754

Merged
merged 3 commits into from
Apr 23, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ConsumerMethodModel;
import org.apache.dubbo.rpc.model.ConsumerModel;
import org.apache.dubbo.rpc.model.ServiceMetadata;
import org.apache.dubbo.rpc.protocol.injvm.InjvmProtocol;
import org.apache.dubbo.rpc.service.GenericService;
import org.apache.dubbo.rpc.support.ProtocolUtils;
Expand Down Expand Up @@ -153,6 +154,8 @@ public class ReferenceConfig<T> extends AbstractReferenceConfig {
*/
private transient volatile boolean destroyed;

private ServiceMetadata serviceMetadata;

@SuppressWarnings("unused")
private final Object finalizerGuardian = new Object() {
@Override
Expand All @@ -174,9 +177,13 @@ protected void finalize() throws Throwable {
};

public ReferenceConfig() {
serviceMetadata = new ServiceMetadata();
serviceMetadata.addAttribute("ORIGIN_CONFIG", this);
}

public ReferenceConfig(Reference reference) {
serviceMetadata = new ServiceMetadata();
serviceMetadata.addAttribute("ORIGIN_CONFIG", this);
appendAnnotation(Reference.class, reference);
setMethods(MethodConfig.constructMethodConfig(reference.methods()));
}
Expand Down Expand Up @@ -265,6 +272,14 @@ private void init() {
checkStubAndLocal(interfaceClass);
checkMock(interfaceClass);

//init serivceMetadata
serviceMetadata.setVersion(version);
serviceMetadata.setGroup(group);
serviceMetadata.setDefaultGroup(group);
serviceMetadata.setServiceType(interfaceClass);
serviceMetadata.setServiceInterfaceName(interfaceName);


ConsumerModel consumerModel = new ConsumerModel(interfaceName, group, version, interfaceClass);
ApplicationModel.initConsumerModel(URL.buildKey(interfaceName, group, version), consumerModel);

Expand Down Expand Up @@ -316,8 +331,11 @@ private void init() {
}
map.put(Constants.REGISTER_IP_KEY, hostToRegistry);

serviceMetadata.getAttachments().putAll(map);

ref = createProxy(map);

serviceMetadata.setTarget(ref);
consumerModel.getServiceMetadata().addAttribute(Constants.PROXY_CLASS_REF, ref);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.apache.dubbo.rpc.cluster.ConfiguratorFactory;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.apache.dubbo.rpc.model.ProviderModel;
import org.apache.dubbo.rpc.model.ServiceMetadata;
import org.apache.dubbo.rpc.service.GenericService;
import org.apache.dubbo.rpc.support.ProtocolUtils;

Expand Down Expand Up @@ -165,10 +166,16 @@ public class ServiceConfig<T> extends AbstractServiceConfig {
*/
private volatile String generic;

private ServiceMetadata serviceMetadata;

public ServiceConfig() {
serviceMetadata = new ServiceMetadata();
serviceMetadata.addAttribute("ORIGIN_CONFIG", this);
}

public ServiceConfig(Service service) {
serviceMetadata = new ServiceMetadata();
serviceMetadata.addAttribute("ORIGIN_CONFIG", this);
appendAnnotation(Service.class, service);
setMethods(MethodConfig.constructMethodConfig(service.methods()));
}
Expand Down Expand Up @@ -334,6 +341,17 @@ private void appendParameters() {
public synchronized void export() {
checkAndUpdateSubConfigs();

//init serviceMetadata
serviceMetadata.setVersion(version);
serviceMetadata.setGroup(group);
serviceMetadata.setDefaultGroup(group);
serviceMetadata.setServiceType(interfaceClass);
serviceMetadata.setServiceInterfaceName(interfaceName);
serviceMetadata.setTarget(ref);

ProviderModel providerModel = new ProviderModel(ref, serviceMetadata);
ApplicationModel.initProviderModel(URL.buildKey(interfaceName, group, version), providerModel);

if (!shouldExport()) {
return;
}
Expand Down Expand Up @@ -379,8 +397,7 @@ protected synchronized void doExport() {
if (StringUtils.isEmpty(path)) {
path = interfaceName;
}
ProviderModel providerModel = new ProviderModel(interfaceName, group, version, ref, interfaceClass);
ApplicationModel.initProviderModel(URL.buildKey(interfaceName, group, version), providerModel);

doExportUrls();
}

Expand Down Expand Up @@ -518,6 +535,9 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List<URL> r
map.put(Constants.TOKEN_KEY, token);
}
}
//init serviceMetadata attachments
serviceMetadata.getAttachments().putAll(map);

// export service
String host = this.findConfigedHosts(protocolConfig, registryURLs, map);
Integer port = this.findConfigedPorts(protocolConfig, name, map);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ public ConsumerModel(String serviceName, String group, String version, Class<?>
}
}

public ConsumerModel(ServiceMetadata serviceMetadata) {
this.serviceMetadata = serviceMetadata;
Method[] methods = serviceMetadata.getServiceType().getMethods();
for (Method method : methods) {
methodModels.put(method, new ConsumerMethodModel(method));
}
}

/**
* @return serviceMetadata
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -29,6 +30,7 @@
public class ProviderModel {
private final Object serviceInstance;
private final ServiceMetadata serviceMetadata;
private final String serivceKey;
private final Map<String, List<ProviderMethodModel>> methods = new HashMap<String, List<ProviderMethodModel>>();

public ProviderModel(String serviceName, String group, String version, Object serviceInstance, Class<?> serviceInterfaceClass) {
Expand All @@ -38,9 +40,16 @@ public ProviderModel(String serviceName, String group, String version, Object se

this.serviceInstance = serviceInstance;
this.serviceMetadata = new ServiceMetadata(serviceName, group, version, serviceInterfaceClass);
this.serivceKey = serviceMetadata.getServiceKey();
initMethod(serviceInterfaceClass);
}

public ProviderModel(Object serviceInstance, ServiceMetadata serviceMetadata) {
this.serviceInstance = serviceInstance;
this.serviceMetadata = serviceMetadata;
this.serivceKey = serviceMetadata.getServiceKey();
initMethod(serviceMetadata.getServiceType());
}

public String getServiceName() {
return this.serviceMetadata.getServiceKey();
Expand Down Expand Up @@ -75,20 +84,21 @@ public ProviderMethodModel getMethodModel(String methodName, String[] argTypes)
}

public List<ProviderMethodModel> getMethodModelList(String methodName) {
return methods.get(methodName);
List<ProviderMethodModel> resultList = methods.get(methodName);
return resultList == null ? Collections.emptyList() : resultList;
}


private void initMethod(Class<?> serviceInterfaceClass) {
Method[] methodsToExport = null;
Method[] methodsToExport;
methodsToExport = serviceInterfaceClass.getMethods();

for (Method method : methodsToExport) {
method.setAccessible(true);

List<ProviderMethodModel> methodModels = methods.get(method.getName());
if (methodModels == null) {
methodModels = new ArrayList<ProviderMethodModel>(1);
methodModels = new ArrayList<ProviderMethodModel>();
methods.put(method.getName(), methodModels);
}
methodModels.add(new ProviderMethodModel(method));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@
*/
public class ServiceMetadata {

private final String serviceKey;
private final String serviceInterfaceName;
private final String defaultGroup;
private final String version;
private final Class<?> serviceType;
private String serviceKey;
private String serviceInterfaceName;
private String defaultGroup;
private String version;
private Class<?> serviceType;

private volatile String group;

private Object target;

/* will be transferred to remote side */
private final Map<String, Object> attachments = new ConcurrentHashMap<String, Object>();
/* used locally*/
Expand All @@ -47,17 +49,20 @@ public ServiceMetadata(String serviceInterfaceName, String group, String version
this.serviceType = serviceType;
}

public ServiceMetadata() {
}

public String getServiceKey() {
return serviceKey;
return serviceInterfaceName + ":" + version;
}

public Map<String, Object> getAttachments() {
return attachments;
}

// public Map<String, Object> getAttributeMap() {
// return attributeMap;
// }
public Map<String, Object> getAttributeMap() {
return attributeMap;
}

public Object getAttribute(String key) {
return attributeMap.get(key);
Expand Down Expand Up @@ -94,4 +99,32 @@ public String getGroup() {
public void setGroup(String group) {
this.group = group;
}

public void setServiceInterfaceName(String serviceInterfaceName) {
this.serviceInterfaceName = serviceInterfaceName;
}

public void setDefaultGroup(String defaultGroup) {
this.defaultGroup = defaultGroup;
}

public void setVersion(String version) {
this.version = version;
}

public void setServiceType(Class<?> serviceType) {
this.serviceType = serviceType;
}

public void setServiceKey(String serviceKey) {
this.serviceKey = serviceKey;
}

public Object getTarget() {
return target;
}

public void setTarget(Object target) {
this.target = target;
}
}