Skip to content

Commit

Permalink
[Dubbo-3653] Enable etcd to support metadata center (#3943)
Browse files Browse the repository at this point in the history
* Support etcd as metadata center

* Fix key format error for consul/redis and UT failure

* Fix UT failure

* Fix UT failure

* Fix UT failure
  • Loading branch information
ralf0131 authored and beiwei30 committed May 5, 2019
1 parent 74a491b commit a206dde
Show file tree
Hide file tree
Showing 21 changed files with 452 additions and 48 deletions.
7 changes: 7 additions & 0 deletions dubbo-all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,13 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metadata-report-etcd</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>

<!-- Transitive dependencies -->
<dependency>
Expand Down
5 changes: 5 additions & 0 deletions dubbo-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,11 @@
<artifactId>dubbo-metadata-report-consul</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metadata-report-etcd</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ public String getValue() {

private void put(String key, String value) {
try {

client.getKVClient().put(ByteSequence.from(key, UTF_8), ByteSequence.from(value, UTF_8)).get();
} catch (Exception e) {
System.out.println("Error put value to etcd.");
Expand Down
50 changes: 50 additions & 0 deletions dubbo-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,56 @@
<artifactId>dubbo-registry-sofa</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-zookeeper</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-apollo</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-consul</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-etcd</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metadata-report-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metadata-report-zookeeper</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metadata-report-redis</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metadata-report-consul</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metadata-report-etcd</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-monitor-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
* 2018/10/25
*/
public class MetadataIdentifier {

public static final String SEPARATOR = ":";
final static String DEFAULT_PATH_TAG = "metadata";
final static String META_DATA_STORE_TAG = ".metaData";

private String serviceInterface;
private String version;
Expand Down Expand Up @@ -53,9 +55,9 @@ public MetadataIdentifier(URL url) {

public String getUniqueKey(KeyTypeEnum keyType) {
if (keyType == KeyTypeEnum.PATH) {
return getFilePathKey();
return getFilePathKey() + Constants.PATH_SEPARATOR + DEFAULT_PATH_TAG;
}
return getIdentifierKey();
return getIdentifierKey() + META_DATA_STORE_TAG;
}

public String getIdentifierKey() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
*/
public interface MetadataReport {

public static final String META_DATA_STORE_TAG = ".metaData";


void storeProviderMetadata(MetadataIdentifier providerMetadataIdentifier, FullServiceDefinition serviceDefinition);

void storeConsumerMetadata(MetadataIdentifier consumerMetadataIdentifier, Map<String, String> serviceParameterMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
*/
public abstract class AbstractMetadataReport implements MetadataReport {

protected final static String DEFAULT_ROOT = "dubbo";

private static final int ONE_DAY_IN_MIll = 60 * 24 * 60 * 1000;
private static final int FOUR_HOURS_IN_MIll = 60 * 4 * 60 * 1000;
Expand Down Expand Up @@ -216,12 +217,7 @@ public void storeProviderMetadata(MetadataIdentifier providerMetadataIdentifier,
if (syncReport) {
storeProviderMetadataTask(providerMetadataIdentifier, serviceDefinition);
} else {
reportCacheExecutor.execute(new Runnable() {
@Override
public void run() {
storeProviderMetadataTask(providerMetadataIdentifier, serviceDefinition);
}
});
reportCacheExecutor.execute(() -> storeProviderMetadataTask(providerMetadataIdentifier, serviceDefinition));
}
}

Expand Down Expand Up @@ -249,12 +245,7 @@ public void storeConsumerMetadata(MetadataIdentifier consumerMetadataIdentifier,
if (syncReport) {
storeConsumerMetadataTask(consumerMetadataIdentifier, serviceParameterMap);
} else {
reportCacheExecutor.execute(new Runnable() {
@Override
public void run() {
storeConsumerMetadataTask(consumerMetadataIdentifier, serviceParameterMap);
}
});
reportCacheExecutor.execute(() -> storeConsumerMetadataTask(consumerMetadataIdentifier, serviceParameterMap));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.apache.dubbo.metadata.identifier.MetadataIdentifier.META_DATA_STORE_TAG;

/**
* 2019/1/7
*/
Expand All @@ -34,10 +36,16 @@ public void testGetUniqueKey() {
String application = "vic.zk.md";
MetadataIdentifier providerMetadataIdentifier = new MetadataIdentifier(interfaceName, version, group, Constants.PROVIDER_SIDE, application);
System.out.println(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.PATH));
Assertions.assertEquals(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.PATH), "metadata" + Constants.PATH_SEPARATOR + interfaceName + Constants.PATH_SEPARATOR + (version == null ? "" : (version + Constants.PATH_SEPARATOR))
+ (group == null ? "" : (group + Constants.PATH_SEPARATOR)) + Constants.PROVIDER_SIDE + Constants.PATH_SEPARATOR + application);
Assertions.assertEquals(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.PATH),
"metadata" + Constants.PATH_SEPARATOR + interfaceName + Constants.PATH_SEPARATOR +
(version == null ? "" : (version + Constants.PATH_SEPARATOR))
+ (group == null ? "" : (group + Constants.PATH_SEPARATOR)) + Constants.PROVIDER_SIDE
+ Constants.PATH_SEPARATOR + application + Constants.PATH_SEPARATOR + "metadata");
System.out.println(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY));
Assertions.assertEquals(providerMetadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY),
interfaceName + MetadataIdentifier.SEPARATOR + (version == null ? "" : version + MetadataIdentifier.SEPARATOR) + (group == null ? "" : group + MetadataIdentifier.SEPARATOR) + Constants.PROVIDER_SIDE + MetadataIdentifier.SEPARATOR + application);
interfaceName + MetadataIdentifier.SEPARATOR +
(version == null ? "" : version + MetadataIdentifier.SEPARATOR)
+ (group == null ? "" : group + MetadataIdentifier.SEPARATOR)
+ Constants.PROVIDER_SIDE + MetadataIdentifier.SEPARATOR + application + META_DATA_STORE_TAG);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected void doStoreProviderMetadata(MetadataIdentifier providerMetadataIdenti

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

public static String getProviderKey(URL url) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected void doStoreConsumerMetadata(MetadataIdentifier consumerMetadataIdenti

private void storeMetadata(MetadataIdentifier identifier, String v) {
try {
client.setKVValue(identifier.getIdentifierKey() + META_DATA_STORE_TAG, v);
client.setKVValue(identifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.UNIQUE_KEY), v);
} catch (Throwable t) {
logger.error("Failed to put " + identifier + " to consul " + v + ", cause: " + t.getMessage(), t);
throw new RpcException("Failed to put " + identifier + " to consul " + v + ", cause: " + t.getMessage(), t);
Expand Down
53 changes: 53 additions & 0 deletions dubbo-metadata-report/dubbo-metadata-report-etcd/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>dubbo-metadata-report</artifactId>
<groupId>org.apache.dubbo</groupId>
<version>2.7.2-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>dubbo-metadata-report-etcd</artifactId>

<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metadata-report-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-remoting-etcd3</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>io.etcd</groupId>
<artifactId>jetcd-launcher</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* 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.
*/

/*
* 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.metadata.store.etcd;

import org.apache.dubbo.common.Constants;
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.identifier.MetadataIdentifier;
import org.apache.dubbo.metadata.support.AbstractMetadataReport;
import org.apache.dubbo.remoting.etcd.jetcd.JEtcdClient;

/**
* Report Metadata to Etcd
*/
public class EtcdMetadataReport extends AbstractMetadataReport {

private final static Logger logger = LoggerFactory.getLogger(EtcdMetadataReport.class);

private final String root;

/**
* The etcd client
*/
private final JEtcdClient etcdClient;

public EtcdMetadataReport(URL url) {
super(url);
if (url.isAnyHost()) {
throw new IllegalStateException("registry address == null");
}
String group = url.getParameter(Constants.GROUP_KEY, DEFAULT_ROOT);
if (!group.startsWith(Constants.PATH_SEPARATOR)) {
group = Constants.PATH_SEPARATOR + group;
}
this.root = group;
etcdClient = new JEtcdClient(url);
}

@Override
protected void doStoreProviderMetadata(MetadataIdentifier providerMetadataIdentifier, String serviceDefinitions) {
storeMetadata(providerMetadataIdentifier, serviceDefinitions);
}

@Override
protected void doStoreConsumerMetadata(MetadataIdentifier consumerMetadataIdentifier, String value) {
storeMetadata(consumerMetadataIdentifier, value);
}

private void storeMetadata(MetadataIdentifier identifier, String v) {
String key = getNodeKey(identifier);
if (!etcdClient.put(key, v)) {
logger.error("Failed to put " + identifier + " to etcd, value: " + v);
}
}

String getNodeKey(MetadataIdentifier identifier) {
return toRootDir() + identifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.PATH);
}

String toRootDir() {
if (root.equals(Constants.PATH_SEPARATOR)) {
return root;
}
return root + Constants.PATH_SEPARATOR;
}
}
Loading

0 comments on commit a206dde

Please sign in to comment.