Skip to content

Commit

Permalink
metadata support for nacos, consul, etcd
Browse files Browse the repository at this point in the history
  • Loading branch information
cvictory committed Aug 15, 2019
1 parent 2a30b22 commit dd360f4
Show file tree
Hide file tree
Showing 20 changed files with 270 additions and 112 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
public class BaseApplicationMetadataIdentifier {
String application;

String getUniqueKey(MetadataIdentifier.KeyTypeEnum keyType, String... params) {
if (keyType == MetadataIdentifier.KeyTypeEnum.PATH) {
String getUniqueKey(KeyTypeEnum keyType, String... params) {
if (keyType == KeyTypeEnum.PATH) {
return getFilePathKey(params);
}
return getIdentifierKey(params);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package org.apache.dubbo.metadata.report.identifier;

/**
* @author cvictory ON 2019-08-15
*/
public interface BaseMetadataIdentifier {

String getUniqueKey(KeyTypeEnum keyType);

String getIdentifierKey();

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ public class BaseServiceMetadataIdentifier {
String group;
String side;

String getUniqueKey(MetadataIdentifier.KeyTypeEnum keyType, String... params) {
if (keyType == MetadataIdentifier.KeyTypeEnum.PATH) {
String getUniqueKey(KeyTypeEnum keyType, String... params) {
if (keyType == KeyTypeEnum.PATH) {
return getFilePathKey(params);
}
return getIdentifierKey(params);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package org.apache.dubbo.metadata.report.identifier;

/**
* 2019-08-15
*/
public enum KeyTypeEnum {
PATH, UNIQUE_KEY
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,15 @@
import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
import static org.apache.dubbo.metadata.MetadataConstants.KEY_SEPARATOR;

/**
* The MetadataIdentifier is used to store method descriptor.
* <p>
* The name of class is reserved because of it has been used in the previous version.
* <p>
* 2018/10/25
*/
public class MetadataIdentifier extends BaseServiceMetadataIdentifier {
public class MetadataIdentifier extends BaseServiceMetadataIdentifier implements BaseMetadataIdentifier {

private String application;

Expand Down Expand Up @@ -101,7 +102,4 @@ public void setApplication(String application) {
this.application = application;
}

public enum KeyTypeEnum {
PATH, UNIQUE_KEY
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.apache.dubbo.common.URL;

import static org.apache.dubbo.common.constants.CommonConstants.GROUP_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.REVISION_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.SIDE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.VERSION_KEY;
import static org.apache.dubbo.metadata.MetadataConstants.KEY_REVISON_PREFIX;
Expand All @@ -14,7 +13,7 @@
* <p>
* 2019-08-09
*/
public class ServiceMetadataIdentifier extends BaseServiceMetadataIdentifier {
public class ServiceMetadataIdentifier extends BaseServiceMetadataIdentifier implements BaseMetadataIdentifier {

private String revision;
private String protocol;
Expand All @@ -40,7 +39,7 @@ public ServiceMetadataIdentifier(URL url) {
this.protocol = url.getProtocol();
}

public String getUniqueKey(MetadataIdentifier.KeyTypeEnum keyType) {
public String getUniqueKey(KeyTypeEnum keyType) {
return super.getUniqueKey(keyType, protocol, KEY_REVISON_PREFIX + revision);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@
import static org.apache.dubbo.common.constants.CommonConstants.REVISION_KEY;

/**
*
* 2019-08-12
*/
public class SubscriberMetadataIdentifier extends BaseApplicationMetadataIdentifier{
public class SubscriberMetadataIdentifier extends BaseApplicationMetadataIdentifier implements BaseMetadataIdentifier {

private String revision;

Expand All @@ -27,7 +26,7 @@ public SubscriberMetadataIdentifier(URL url) {
this.revision = url.getParameter(REVISION_KEY, "");
}

public String getUniqueKey(MetadataIdentifier.KeyTypeEnum keyType) {
public String getUniqueKey(KeyTypeEnum keyType) {
return super.getUniqueKey(keyType, revision);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.apache.dubbo.metadata.definition.model.FullServiceDefinition;
import org.apache.dubbo.metadata.definition.model.ServiceDefinition;
import org.apache.dubbo.metadata.report.MetadataReport;
import org.apache.dubbo.metadata.report.identifier.KeyTypeEnum;
import org.apache.dubbo.metadata.report.identifier.MetadataIdentifier;
import org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier;
import org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier;
Expand Down Expand Up @@ -200,9 +201,9 @@ private void saveProperties(MetadataIdentifier metadataIdentifier, String value,

try {
if (add) {
properties.setProperty(metadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), value);
properties.setProperty(metadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), value);
} else {
properties.remove(metadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY));
properties.remove(metadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY));
}
long version = lastCacheChanged.incrementAndGet();
if (sync) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import org.apache.dubbo.common.URL;
import org.apache.dubbo.metadata.WritableMetadataService;

import java.util.ArrayList;
import java.util.List;
import java.util.SortedSet;
import java.util.function.BiFunction;

Expand All @@ -14,20 +12,16 @@
* @since 2.7.5
*/
public class RemoteWritableMetadataServiceDelegate implements WritableMetadataService {
List<WritableMetadataService> metadataServiceList = new ArrayList<>(2);
private WritableMetadataService defaultWritableMetadataService;
private RemoteWritableMetadataService remoteWritableMetadataService;

public RemoteWritableMetadataServiceDelegate() {
metadataServiceList.add(WritableMetadataService.getDefaultExtension());
metadataServiceList.add(new RemoteWritableMetadataService());
defaultWritableMetadataService = WritableMetadataService.getDefaultExtension();
remoteWritableMetadataService = new RemoteWritableMetadataService();
}

private WritableMetadataService getInMemoryWritableMetadataService() {
for (WritableMetadataService writableMetadataService : metadataServiceList) {
if (writableMetadataService instanceof InMemoryWritableMetadataService) {
return writableMetadataService;
}
}
return metadataServiceList.get(0);
private WritableMetadataService getDefaultWritableMetadataService() {
return defaultWritableMetadataService;
}

@Override
Expand All @@ -53,43 +47,41 @@ public boolean unsubscribeURL(URL url) {
@Override
public boolean refreshMetadata(String exportedRevision, String subscribedRevision) {
boolean result = true;
for (WritableMetadataService writableMetadataService : metadataServiceList) {
result &= writableMetadataService.refreshMetadata(exportedRevision, subscribedRevision);
}
result &= defaultWritableMetadataService.refreshMetadata(exportedRevision, subscribedRevision);
result &= remoteWritableMetadataService.refreshMetadata(exportedRevision, subscribedRevision);
return result;
}

@Override
public void publishServiceDefinition(URL providerUrl) {
for (WritableMetadataService writableMetadataService : metadataServiceList) {
writableMetadataService.publishServiceDefinition(providerUrl);
}
defaultWritableMetadataService.publishServiceDefinition(providerUrl);
remoteWritableMetadataService.publishServiceDefinition(providerUrl);
}

@Override
public SortedSet<String> getExportedURLs(String serviceInterface, String group, String version, String protocol) {
return getInMemoryWritableMetadataService().getExportedURLs(serviceInterface, group, version, protocol);
return getDefaultWritableMetadataService().getExportedURLs(serviceInterface, group, version, protocol);
}

@Override
public SortedSet<String> getSubscribedURLs() {
return getInMemoryWritableMetadataService().getSubscribedURLs();
return getDefaultWritableMetadataService().getSubscribedURLs();
}

@Override
public String getServiceDefinition(String interfaceName, String version, String group) {
return getInMemoryWritableMetadataService().getServiceDefinition(interfaceName, version, group);
return getDefaultWritableMetadataService().getServiceDefinition(interfaceName, version, group);
}

@Override
public String getServiceDefinition(String serviceKey) {
return getInMemoryWritableMetadataService().getServiceDefinition(serviceKey);
return getDefaultWritableMetadataService().getServiceDefinition(serviceKey);
}

private boolean doFunction(BiFunction<WritableMetadataService, URL, Boolean> func, URL url) {
boolean result = true;
for (WritableMetadataService writableMetadataService : metadataServiceList) {
result &= func.apply(writableMetadataService, url);
}
result &= func.apply(defaultWritableMetadataService, url);
result &= func.apply(remoteWritableMetadataService, url);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.apache.dubbo.metadata.report.identifier;

import org.apache.dubbo.metadata.MetadataConstants;
import org.apache.dubbo.metadata.report.identifier.MetadataIdentifier.KeyTypeEnum;
import org.apache.dubbo.metadata.report.identifier.KeyTypeEnum;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -42,7 +42,7 @@ public void testGetUniqueKey() {
(version == null ? "" : (version + PATH_SEPARATOR))
+ (group == null ? "" : (group + PATH_SEPARATOR)) + PROVIDER_SIDE
+ PATH_SEPARATOR + application);
Assertions.assertEquals(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY),
Assertions.assertEquals(providerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY),
interfaceName + MetadataConstants.KEY_SEPARATOR +
(version == null ? "" : version) + MetadataConstants.KEY_SEPARATOR
+ (group == null ? "" : group) + MetadataConstants.KEY_SEPARATOR
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.metadata.definition.ServiceDefinitionBuilder;
import org.apache.dubbo.metadata.definition.model.FullServiceDefinition;
import org.apache.dubbo.metadata.report.identifier.KeyTypeEnum;
import org.apache.dubbo.metadata.report.identifier.MetadataIdentifier;
import org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier;
import org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier;
Expand Down Expand Up @@ -72,7 +73,7 @@ public void testStoreProviderUsual() throws ClassNotFoundException, InterruptedE
String application = "vic";
MetadataIdentifier providerMetadataIdentifier = storePrivider(abstractMetadataReport, interfaceName, version, group, application);
Thread.sleep(1500);
Assertions.assertNotNull(abstractMetadataReport.store.get(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY)));
Assertions.assertNotNull(abstractMetadataReport.store.get(providerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY)));
}

@Test
Expand All @@ -83,7 +84,7 @@ public void testStoreProviderSync() throws ClassNotFoundException, InterruptedEx
String application = "vic";
abstractMetadataReport.syncReport = true;
MetadataIdentifier providerMetadataIdentifier = storePrivider(abstractMetadataReport, interfaceName, version, group, application);
Assertions.assertNotNull(abstractMetadataReport.store.get(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY)));
Assertions.assertNotNull(abstractMetadataReport.store.get(providerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY)));
}

@Test
Expand All @@ -102,7 +103,7 @@ public void testFileExistAfterPut() throws InterruptedException, ClassNotFoundEx

Thread.sleep(2000);
Assertions.assertTrue(singleMetadataReport.file.exists());
Assertions.assertTrue(singleMetadataReport.properties.containsKey(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY)));
Assertions.assertTrue(singleMetadataReport.properties.containsKey(providerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY)));
}

@Test
Expand Down Expand Up @@ -221,17 +222,17 @@ public void testPublishAll() throws ClassNotFoundException, InterruptedException

Assertions.assertEquals(3, abstractMetadataReport.store.size());

String v = abstractMetadataReport.store.get(providerMetadataIdentifier1.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY));
String v = abstractMetadataReport.store.get(providerMetadataIdentifier1.getUniqueKey(KeyTypeEnum.UNIQUE_KEY));
Gson gson = new Gson();
FullServiceDefinition data = gson.fromJson(v, FullServiceDefinition.class);
checkParam(data.getParameters(), application, version);

String v2 = abstractMetadataReport.store.get(providerMetadataIdentifier2.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY));
String v2 = abstractMetadataReport.store.get(providerMetadataIdentifier2.getUniqueKey(KeyTypeEnum.UNIQUE_KEY));
gson = new Gson();
data = gson.fromJson(v2, FullServiceDefinition.class);
checkParam(data.getParameters(), application, version + "_2");

String v3 = abstractMetadataReport.store.get(consumerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY));
String v3 = abstractMetadataReport.store.get(consumerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY));
gson = new Gson();
Map v3Map = gson.fromJson(v3, Map.class);
checkParam(v3Map, application, version + "_3");
Expand Down Expand Up @@ -283,12 +284,12 @@ public NewMetadataReport(URL metadataReportURL) {

@Override
protected void doStoreProviderMetadata(MetadataIdentifier providerMetadataIdentifier, String serviceDefinitions) {
store.put(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), serviceDefinitions);
store.put(providerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), serviceDefinitions);
}

@Override
protected void doStoreConsumerMetadata(MetadataIdentifier consumerMetadataIdentifier, String serviceParameterString) {
store.put(consumerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), serviceParameterString);
store.put(consumerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), serviceParameterString);
}

@Override
Expand Down Expand Up @@ -340,7 +341,7 @@ protected void doStoreProviderMetadata(MetadataIdentifier providerMetadataIdenti
if (executeTimes <= needRetryTimes) {
throw new RuntimeException("must retry:" + executeTimes);
}
store.put(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), serviceDefinitions);
store.put(providerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), serviceDefinitions);
}

@Override
Expand All @@ -349,7 +350,7 @@ protected void doStoreConsumerMetadata(MetadataIdentifier consumerMetadataIdenti
if (executeTimes <= needRetryTimes) {
throw new RuntimeException("must retry:" + executeTimes);
}
store.put(consumerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), serviceParameterString);
store.put(consumerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), serviceParameterString);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.metadata.report.identifier.KeyTypeEnum;
import org.apache.dubbo.metadata.report.identifier.MetadataIdentifier;
import org.apache.dubbo.metadata.report.identifier.ServiceMetadataIdentifier;
import org.apache.dubbo.metadata.report.identifier.SubscriberMetadataIdentifier;
Expand Down Expand Up @@ -53,12 +54,12 @@ private static String getProtocol(URL url) {

@Override
protected void doStoreProviderMetadata(MetadataIdentifier providerMetadataIdentifier, String serviceDefinitions) {
store.put(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), serviceDefinitions);
store.put(providerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), serviceDefinitions);
}

@Override
protected void doStoreConsumerMetadata(MetadataIdentifier consumerMetadataIdentifier, String serviceParameterString) {
store.put(consumerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), serviceParameterString);
store.put(consumerMetadataIdentifier.getUniqueKey(KeyTypeEnum.UNIQUE_KEY), serviceParameterString);
}

@Override
Expand Down Expand Up @@ -87,11 +88,11 @@ protected String doGetSubscribedURLs(SubscriberMetadataIdentifier metadataIdenti
}

public static String getProviderKey(URL url) {
return new MetadataIdentifier(url).getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY);
return new MetadataIdentifier(url).getUniqueKey(KeyTypeEnum.UNIQUE_KEY);
}

public static String getConsumerKey(URL url) {
return new MetadataIdentifier(url).getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY);
return new MetadataIdentifier(url).getUniqueKey(KeyTypeEnum.UNIQUE_KEY);
}

@Override
Expand Down
Loading

0 comments on commit dd360f4

Please sign in to comment.