Skip to content

Commit

Permalink
fix #3179. Make registry, configcenter, matadata-report share a zooke… (
Browse files Browse the repository at this point in the history
#3182)

* fix #3179. Make registry, configcenter, matadata-report share a zookeeper connection

* fix #3179. optimize registry, configcenter, matadata-report share a zookeeper connection

* fix #3179. add licence

* fix some review issue: just optimize constuctor order and refactor some code

* modify HashMap to ConcurrentHashMap

* add some comments

* remove ZookeeperClientData and originalURLs

* remove ZookeeperClientData and originalURLs

* fix #3205 . add group into MetadataReport

* remove SOURCE_URL_KEY from Constants

* fix #3179 , #3205, #3218.  modify review issue; when sharing zookeeper connection, it should judge zookeeperClient.isConnected()
  • Loading branch information
cvictory authored and beiwei30 committed Jan 14, 2019
1 parent 8e838d5 commit 2f74354
Show file tree
Hide file tree
Showing 17 changed files with 471 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -482,12 +482,14 @@ public class Constants {

/**
* simple the registry for provider.
*
* @since 2.7.0
*/
public static final String SIMPLIFIED_KEY = "simplified";

/**
* After simplify the registry, should add some paramter individually for provider.
*
* @since 2.7.0
*/
public static final String EXTRA_KEYS_KEY = "extra-keys";
Expand Down
14 changes: 12 additions & 2 deletions dubbo-common/src/main/java/org/apache/dubbo/common/URL.java
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ public static URL valueOf(String url) {
// see https://howdoesinternetwork.com/2013/ipv6-zone-id
// ignore
} else {
port = Integer.parseInt(url.substring(i+1));
port = Integer.parseInt(url.substring(i + 1));
url = url.substring(0, i);
}
}
Expand Down Expand Up @@ -364,7 +364,7 @@ public URL setHost(String host) {

/**
* Fetch IP address for this URL.
*
* <p>
* Pls. note that IP should be used instead of Host when to compare with socket's address or to search in a map
* which use address as its key.
*
Expand Down Expand Up @@ -498,6 +498,15 @@ public String[] getParameter(String key, String[] defaultValue) {
return Constants.COMMA_SPLIT_PATTERN.split(value);
}

public List<String> getParameter(String key, List<String> defaultValue) {
String value = getParameter(key);
if (value == null || value.length() == 0) {
return defaultValue;
}
String[] strArray = Constants.COMMA_SPLIT_PATTERN.split(value);
return Arrays.asList(strArray);
}

private Map<String, Number> getNumbers() {
if (numbers == null) { // concurrent initialization is tolerant
numbers = new ConcurrentHashMap<String, Number>();
Expand Down Expand Up @@ -1267,6 +1276,7 @@ private String buildString(boolean appendUser, boolean appendParameter, boolean
buf.append("/");
buf.append(path);
}

if (appendParameter) {
buildParameters(buf, true, parameters);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package org.apache.dubbo.common;

import org.apache.dubbo.common.utils.CollectionUtils;

import org.junit.Test;

import java.io.File;
Expand Down Expand Up @@ -124,7 +123,7 @@ public void test_valueOf_noHost() throws Exception {
assertEquals("home/user1/router.js", url.getPath());
assertEquals(0, url.getParameters().size());

// Caution!!
// Caution!!
url = URL.valueOf("file://home/user1/router.js");
// ^^ only tow slash!
assertEquals("file", url.getProtocol());
Expand Down Expand Up @@ -680,4 +679,4 @@ public void testIpV6AddressWithScopeId(){
assertEquals("1.0.0", url.getParameter("version"));
assertEquals("morgan", url.getParameter("application"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import java.util.Map;
import java.util.Set;

import static org.apache.dubbo.common.Constants.APPLICATION_KEY;
import static org.apache.dubbo.common.config.ConfigurationUtils.parseProperties;
import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader;

Expand Down Expand Up @@ -376,7 +375,6 @@ private URL loadMetadataReporterURL() {
return null;
}
Map<String, String> map = new HashMap<String, String>();
map.put(APPLICATION_KEY, application.getName());
appendParameters(map, metadataReportConfig);
return UrlUtils.parseURL(address, map);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public class MetadataReportConfig extends AbstractConfig {
// Request timeout in milliseconds for register center
private Integer timeout;

/**
* The group the metadata in . It is the same as registry
*/
private String group;

// Customized parameters
private Map<String, String> parameters;

Expand All @@ -73,6 +78,7 @@ public MetadataReportConfig(String address) {
setAddress(address);
}

@Parameter(excluded = true)
public String getAddress() {
return address;
}
Expand Down Expand Up @@ -160,4 +166,12 @@ public String getPrefix() {
public boolean isValid() {
return StringUtils.isNotEmpty(address);
}

public String getGroup() {
return group;
}

public void setGroup(String group) {
this.group = group;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,12 @@
<xsd:documentation><![CDATA[ The request timeout. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="group" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[ The group of metadata-report. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>

<xsd:attribute name="retry-times" type="xsd:integer" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[ if fail, retry times. ]]></xsd:documentation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,11 @@
<xsd:documentation><![CDATA[ The request timeout. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="group" type="xsd:string" use="optional">
<xsd:annotation>
<xsd:documentation><![CDATA[ The group of metadata-report. ]]></xsd:documentation>
</xsd:annotation>
</xsd:attribute>

<xsd:attribute name="retry-times" type="xsd:integer" use="optional">
<xsd:annotation>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ private boolean doHandleMetadataCollection(Map<MetadataIdentifier, Object> metad
* not private. just for unittest.
*/
void publishAll() {
logger.info("start to publish all metadata.");
this.doHandleMetadataCollection(allMetadataReports);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ public abstract class AbstractMetadataReportFactory implements MetadataReportFac
@Override
public MetadataReport getMetadataReport(URL url) {
url = url.setPath(MetadataReport.class.getName())
.addParameter(Constants.INTERFACE_KEY, MetadataReport.class.getName())
.removeParameters(Constants.EXPORT_KEY, Constants.REFER_KEY);
String key = url.toServiceString();
// Lock the registry access process to ensure a single instance of the registry
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,18 @@
*/
package org.apache.dubbo.remoting.zookeeper.curator;

import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.remoting.zookeeper.ChildListener;
import org.apache.dubbo.remoting.zookeeper.StateListener;
import org.apache.dubbo.remoting.zookeeper.support.AbstractZookeeperClient;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.api.CuratorWatcher;
import org.apache.curator.framework.state.ConnectionState;
import org.apache.curator.framework.state.ConnectionStateListener;
import org.apache.curator.retry.RetryNTimes;
import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.remoting.zookeeper.ChildListener;
import org.apache.dubbo.remoting.zookeeper.StateListener;
import org.apache.dubbo.remoting.zookeeper.support.AbstractZookeeperClient;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException.NoNodeException;
import org.apache.zookeeper.KeeperException.NodeExistsException;
Expand All @@ -43,6 +42,7 @@ public class CuratorZookeeperClient extends AbstractZookeeperClient<CuratorWatch
private final Charset charset = Charset.forName("UTF-8");
private final CuratorFramework client;


public CuratorZookeeperClient(URL url) {
super(url);
try {
Expand Down Expand Up @@ -147,6 +147,7 @@ public boolean checkExists(String path) {
}
return false;
}

@Override
public boolean isConnected() {
return client.getZookeeperClient().isConnected();
Expand All @@ -156,12 +157,9 @@ public boolean isConnected() {
public String doGetContent(String path) {
try {
byte[] dataBytes = client.getData().forPath(path);
if(dataBytes == null || dataBytes.length == 0){
return null;
}
return new String(dataBytes, charset);
} catch (NodeExistsException e) {
return (dataBytes == null || dataBytes.length == 0) ? null : new String(dataBytes, charset);
} catch (NoNodeException e) {
// ignore NoNode Exception.
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

import org.apache.dubbo.common.URL;
import org.apache.dubbo.remoting.zookeeper.ZookeeperClient;
import org.apache.dubbo.remoting.zookeeper.ZookeeperTransporter;
import org.apache.dubbo.remoting.zookeeper.support.AbstractZookeeperTransporter;

public class CuratorZookeeperTransporter implements ZookeeperTransporter {
public class CuratorZookeeperTransporter extends AbstractZookeeperTransporter {

@Override
public ZookeeperClient connect(URL url) {
public ZookeeperClient createZookeeperClient(URL url) {
return new CuratorZookeeperClient(url);
}


}
Loading

0 comments on commit 2f74354

Please sign in to comment.