Skip to content

Commit

Permalink
Code clean at metadata service. (apache#8161)
Browse files Browse the repository at this point in the history
* code clean at metadata service.

* code clean.

* code clean.

* code clean. publish service definition is not provider side particular.
  • Loading branch information
horizonzy authored Jul 2, 2021
1 parent bb6d62b commit 23f19ce
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ConfigItem {
/**
* version information corresponding to the current configure item.
*/
private Object stat;
private Object ticket;

public String getContent() {
return content;
Expand All @@ -39,17 +39,17 @@ public void setContent(String content) {
this.content = content;
}

public Object getStat() {
return stat;
public Object getTicket() {
return ticket;
}

public void setStat(Object stat) {
this.stat = stat;
public void setTicket(Object ticket) {
this.ticket = ticket;
}

public ConfigItem(String content, Object stat) {
public ConfigItem(String content, Object ticket) {
this.content = content;
this.stat = stat;
this.ticket = ticket;
}

public ConfigItem() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ public void testPublishConfigCas() {
String group = "org.apache.dubbo.service.UserService";
String content = "test";
ConfigItem configItem = configuration.getConfigItem(key, group);
assertTrue(configuration.publishConfigCas(key, group, content, configItem.getStat()));
assertTrue(configuration.publishConfigCas(key, group, content, configItem.getTicket()));
configItem = configuration.getConfigItem(key, group);
assertEquals("test", configItem.getContent());
assertTrue(configuration.publishConfigCas(key, group, "newtest", configItem.getStat()));
assertFalse(configuration.publishConfigCas(key, group, "newtest2", configItem.getStat()));
assertTrue(configuration.publishConfigCas(key, group, "newtest", configItem.getTicket()));
assertFalse(configuration.publishConfigCas(key, group, "newtest2", configItem.getTicket()));
assertEquals("newtest", configuration.getConfigItem(key, group).getContent());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ default String serviceName() {
*/
boolean unsubscribeURL(URL url);

void publishServiceDefinition(URL providerUrl);
void publishServiceDefinition(URL url);

default void setMetadataServiceURL(URL url) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void map(URL url) {
}
newConfigContent = oldConfigContent + COMMA_SEPARATOR + getName();
}
success = metadataReport.registerServiceAppMapping(serviceInterface, DEFAULT_MAPPING_GROUP, newConfigContent, configItem.getStat());
success = metadataReport.registerServiceAppMapping(serviceInterface, DEFAULT_MAPPING_GROUP, newConfigContent, configItem.getTicket());
} while (!success && currentRetryTimes++ <= CAS_RETRY_TIMES);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,25 +201,25 @@ public boolean unsubscribeURL(URL url) {
}

@Override
public void publishServiceDefinition(URL providerUrl) {
public void publishServiceDefinition(URL url) {
try {
String interfaceName = providerUrl.getServiceInterface();
String interfaceName = url.getServiceInterface();
if (StringUtils.isNotEmpty(interfaceName)
&& !ProtocolUtils.isGeneric(providerUrl.getParameter(GENERIC_KEY))) {
&& !ProtocolUtils.isGeneric(url.getParameter(GENERIC_KEY))) {
Class interfaceClass = Class.forName(interfaceName);
ServiceDefinition serviceDefinition = ServiceDefinitionBuilder.build(interfaceClass);
Gson gson = new Gson();
String data = gson.toJson(serviceDefinition);
serviceDefinitions.put(providerUrl.getServiceKey(), data);
serviceDefinitions.put(url.getServiceKey(), data);
return;
} else if (CONSUMER_SIDE.equalsIgnoreCase(providerUrl.getParameter(SIDE_KEY))) {
} else if (CONSUMER_SIDE.equalsIgnoreCase(url.getParameter(SIDE_KEY))) {
//to avoid consumer generic invoke style error
return;
}
logger.error("publishProvider interfaceName is empty . providerUrl: " + providerUrl.toFullString());
logger.error("publish service definition interfaceName is empty. url: " + url.toFullString());
} catch (Throwable e) {
//ignore error
logger.error("publishProvider getServiceDescriptor error. providerUrl: " + providerUrl.toFullString(), e);
logger.error("publish service definition getServiceDescriptor error. url: " + url.toFullString(), e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier;
import org.apache.dubbo.registry.client.ServiceInstance;
import org.apache.dubbo.registry.client.metadata.ServiceInstanceMetadataUtils;
import org.apache.dubbo.remoting.Constants;
import org.apache.dubbo.rpc.RpcException;

import java.util.HashMap;
Expand All @@ -41,6 +40,9 @@
import static org.apache.dubbo.common.constants.CommonConstants.PROVIDER_SIDE;
import static org.apache.dubbo.common.constants.CommonConstants.TIMESTAMP_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.REGISTRY_CLUSTER_KEY;
import static org.apache.dubbo.registry.Constants.REGISTER_IP_KEY;
import static org.apache.dubbo.remoting.Constants.BIND_IP_KEY;
import static org.apache.dubbo.remoting.Constants.BIND_PORT_KEY;

public class RemoteMetadataServiceImpl {
protected final Logger logger = LoggerFactory.getLogger(getClass());
Expand Down Expand Up @@ -72,8 +74,7 @@ public void publishMetadata(String serviceName) {
metadataReport.publishAppMetadata(identifier, metadataInfo);
} else {
if (logger.isInfoEnabled()) {
logger.info("Remote Metadata Report Server not hasn't been configured. " +
"Only publish Metadata to local.");
logger.info("Remote Metadata Report Server not hasn't been configured. Only publish Metadata to local.");
}
}
metadataInfo.markReported();
Expand All @@ -83,7 +84,7 @@ public void publishMetadata(String serviceName) {

public MetadataInfo getMetadata(ServiceInstance instance) {
SubscriberMetadataIdentifier identifier = new SubscriberMetadataIdentifier(instance.getServiceName(),
ServiceInstanceMetadataUtils.getExportedServicesRevision(instance));
ServiceInstanceMetadataUtils.getExportedServicesRevision(instance));

String registryCluster = instance.getRegistryCluster();

Expand All @@ -102,18 +103,17 @@ public MetadataInfo getMetadata(ServiceInstance instance) {

private void checkRemoteConfigured() {
if (getMetadataReports().size() == 0) {
String msg = "Remote Metadata Report Server not hasn't been configured. " +
"Unable to get Metadata from remote!";
String msg = "Remote Metadata Report Server not hasn't been configured. Unable to get Metadata from remote!";
logger.error(msg);
throw new IllegalStateException(msg);
}
}

public void publishServiceDefinition(URL url) {
String side = url.getSide();

checkRemoteConfigured();

String side = url.getSide();

if (PROVIDER_SIDE.equalsIgnoreCase(side)) {
//TODO, the params part is duplicate with that stored by exportURL(url), can be further optimized in the future.
publishProvider(url);
Expand All @@ -126,37 +126,35 @@ public void publishServiceDefinition(URL url) {
private void publishProvider(URL providerUrl) throws RpcException {
//first add into the list
// remove the individual param
providerUrl = providerUrl.removeParameters(PID_KEY, TIMESTAMP_KEY, Constants.BIND_IP_KEY,
Constants.BIND_PORT_KEY, TIMESTAMP_KEY);
providerUrl = providerUrl.removeParameters(PID_KEY, TIMESTAMP_KEY, BIND_IP_KEY, BIND_PORT_KEY);

try {
String interfaceName = providerUrl.getServiceInterface();
if (StringUtils.isNotEmpty(interfaceName)) {
Class interfaceClass = Class.forName(interfaceName);
FullServiceDefinition fullServiceDefinition = ServiceDefinitionBuilder.buildFullDefinition(interfaceClass,
providerUrl.getParameters());
providerUrl.getParameters());
for (Map.Entry<String, MetadataReport> entry : getMetadataReports().entrySet()) {
MetadataReport metadataReport = entry.getValue();
metadataReport.storeProviderMetadata(new MetadataIdentifier(providerUrl.getServiceInterface(),
providerUrl.getVersion(), providerUrl.getGroup(),
PROVIDER_SIDE, providerUrl.getApplication()), fullServiceDefinition);
providerUrl.getVersion(), providerUrl.getGroup(),
PROVIDER_SIDE, providerUrl.getApplication()), fullServiceDefinition);
}
return;
}
logger.error("publishProvider interfaceName is empty . providerUrl: " + providerUrl.toFullString());
logger.error("publishProvider interfaceName is empty. providerUrl: " + providerUrl.toFullString());
} catch (ClassNotFoundException e) {
//ignore error
logger.error("publishProvider getServiceDescriptor error. providerUrl: " + providerUrl.toFullString(), e);
}
}

private void publishConsumer(URL consumerURL) throws RpcException {
final URL url = consumerURL.removeParameters(PID_KEY, TIMESTAMP_KEY, Constants.BIND_IP_KEY,
Constants.BIND_PORT_KEY, TIMESTAMP_KEY);
final URL url = consumerURL.removeParameters(PID_KEY, TIMESTAMP_KEY, BIND_IP_KEY, BIND_PORT_KEY, REGISTER_IP_KEY);
getMetadataReports().forEach((registryKey, config) -> {
config.storeConsumerMetadata(new MetadataIdentifier(url.getServiceInterface(),
url.getVersion(), url.getGroup(), CONSUMER_SIDE,
url.getApplication()), url.getParameters());
url.getVersion(), url.getGroup(), CONSUMER_SIDE,
url.getApplication()), url.getParameters());
});
}

Expand Down

0 comments on commit 23f19ce

Please sign in to comment.