Skip to content

Commit

Permalink
Merge pull request #3593, Consul support for Registry and Metadata.
Browse files Browse the repository at this point in the history
  • Loading branch information
beiwei30 authored and chickenlj committed Mar 11, 2019
1 parent 10ec77f commit 096d1da
Show file tree
Hide file tree
Showing 19 changed files with 810 additions and 3 deletions.
24 changes: 24 additions & 0 deletions dubbo-all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,13 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-consul</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-monitor-api</artifactId>
Expand Down Expand Up @@ -346,6 +353,13 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-consul</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-compatible</artifactId>
Expand Down Expand Up @@ -381,6 +395,13 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-metadata-report-consul</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
<optional>true</optional>
</dependency>

<!-- Transitive dependencies -->
<dependency>
Expand Down Expand Up @@ -468,6 +489,7 @@
<include>org.apache.dubbo:dubbo-registry-multicast</include>
<include>org.apache.dubbo:dubbo-registry-zookeeper</include>
<include>org.apache.dubbo:dubbo-registry-redis</include>
<include>org.apache.dubbo:dubbo-registry-consul</include>
<include>org.apache.dubbo:dubbo-monitor-api</include>
<include>org.apache.dubbo:dubbo-monitor-default</include>
<include>org.apache.dubbo:dubbo-config-api</include>
Expand All @@ -488,10 +510,12 @@
<include>org.apache.dubbo:dubbo-configcenter-definition</include>
<include>org.apache.dubbo:dubbo-configcenter-apollo</include>
<include>org.apache.dubbo:dubbo-configcenter-zookeeper</include>
<include>org.apache.dubbo:dubbo-configcenter-consul</include>
<include>org.apache.dubbo:dubbo-metadata-report-api</include>
<include>org.apache.dubbo:dubbo-metadata-definition</include>
<include>org.apache.dubbo:dubbo-metadata-report-redis</include>
<include>org.apache.dubbo:dubbo-metadata-report-zookeeper</include>
<include>org.apache.dubbo:dubbo-metadata-report-consul</include>
</includes>
</artifactSet>
<transformers>
Expand Down
15 changes: 15 additions & 0 deletions dubbo-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,11 @@
<artifactId>dubbo-registry-redis</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-consul</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-monitor-api</artifactId>
Expand Down Expand Up @@ -303,6 +308,11 @@
<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-configcenter-api</artifactId>
Expand All @@ -318,6 +328,11 @@
<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-metadata-definition</artifactId>
Expand Down
44 changes: 44 additions & 0 deletions dubbo-configcenter/dubbo-configcenter-consul/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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-configcenter</artifactId>
<groupId>org.apache.dubbo</groupId>
<version>2.7.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>dubbo-configcenter-consul</artifactId>

<dependencies>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-api</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>com.ecwid.consul</groupId>
<artifactId>consul-api</artifactId>
</dependency>
</dependencies>


</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* 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.configcenter.consul;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.NamedThreadFactory;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.configcenter.ConfigChangeEvent;
import org.apache.dubbo.configcenter.ConfigurationListener;
import org.apache.dubbo.configcenter.DynamicConfiguration;

import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.kv.model.GetValue;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;

import static java.util.concurrent.Executors.newCachedThreadPool;
import static org.apache.dubbo.common.Constants.CONFIG_NAMESPACE_KEY;
import static org.apache.dubbo.common.Constants.PATH_SEPARATOR;

/**
* config center implementation for consul
*/
public class ConsulDynamicConfiguration implements DynamicConfiguration {
private static final Logger logger = LoggerFactory.getLogger(ConsulDynamicConfiguration.class);

private static final int DEFAULT_PORT = 8500;
private static final int DEFAULT_WATCH_TIMEOUT = 60 * 1000;
private static final String WATCH_TIMEOUT = "consul-watch-timeout";

private URL url;
private String rootPath;
private ConsulClient client;
private int watchTimeout = -1;
private ConcurrentMap<String, ConsulKVWatcher> watchers = new ConcurrentHashMap<>();
private ConcurrentMap<String, Long> consulIndexes = new ConcurrentHashMap<>();
private ExecutorService watcherService = newCachedThreadPool(
new NamedThreadFactory("dubbo-consul-configuration-watcher", true));

public ConsulDynamicConfiguration(URL url) {
this.url = url;
this.rootPath = PATH_SEPARATOR + url.getParameter(CONFIG_NAMESPACE_KEY, DEFAULT_GROUP) + PATH_SEPARATOR + "config";
this.watchTimeout = buildWatchTimeout(url);
String host = url.getHost();
int port = url.getPort() != 0 ? url.getPort() : DEFAULT_PORT;
client = new ConsulClient(host, port);
}

@Override
public void addListener(String key, String group, ConfigurationListener listener) {
logger.info("register listener " + listener.getClass() + " for config with key: " + key + ", group: " + group);
ConsulKVWatcher watcher = watchers.putIfAbsent(key, new ConsulKVWatcher(key));
if (watcher == null) {
watcher = watchers.get(key);
watcherService.submit(watcher);
}
watcher.addListener(listener);
}

@Override
public void removeListener(String key, String group, ConfigurationListener listener) {
logger.info("unregister listener " + listener.getClass() + " for config with key: " + key + ", group: " + group);
ConsulKVWatcher watcher = watchers.get(key);
if (watcher != null) {
watcher.removeListener(listener);
}
}

@Override
public String getConfig(String key, String group, long timeout) throws IllegalStateException {
if (StringUtils.isNotEmpty(group)) {
key = group + PATH_SEPARATOR + key;
} else {
int i = key.lastIndexOf(".");
key = key.substring(0, i) + PATH_SEPARATOR + key.substring(i + 1);
}

return (String) getInternalProperty(rootPath + PATH_SEPARATOR + key);
}

@Override
public Object getInternalProperty(String key) {
logger.info("get config from: " + key);
Long currentIndex = consulIndexes.computeIfAbsent(key, k -> -1L);
Response<GetValue> response = client.getKVValue(key, new QueryParams(watchTimeout, currentIndex));
GetValue value = response.getValue();
consulIndexes.put(key, response.getConsulIndex());
return value != null ? value.getDecodedValue() : null;
}

private int buildWatchTimeout(URL url) {
return url.getParameter(WATCH_TIMEOUT, DEFAULT_WATCH_TIMEOUT) / 1000;
}

private class ConsulKVWatcher implements Runnable {
private String key;
private Set<ConfigurationListener> listeners;
private boolean running = true;

public ConsulKVWatcher(String key) {
this.key = convertKey(key);
this.listeners = new HashSet<>();
}

@Override
public void run() {
while (running) {
Long lastIndex = consulIndexes.computeIfAbsent(key, k -> -1L);
Response<GetValue> response = client.getKVValue(key, new QueryParams(watchTimeout, lastIndex));

Long currentIndex = response.getConsulIndex();
if (currentIndex == null || currentIndex <= lastIndex) {
continue;
}

consulIndexes.put(key, currentIndex);
String value = response.getValue().getDecodedValue();
logger.info("notify change for key: " + key + ", the value is: " + value);
ConfigChangeEvent event = new ConfigChangeEvent(key, value);
for (ConfigurationListener listener : listeners) {
listener.process(event);
}
}
}

private void addListener(ConfigurationListener listener) {
this.listeners.add(listener);
}

private void removeListener(ConfigurationListener listener) {
this.listeners.remove(listener);
}

private String convertKey(String key) {
int index = key.lastIndexOf('.');
return rootPath + PATH_SEPARATOR + key.substring(0, index) + PATH_SEPARATOR + key.substring(index + 1);
}

private void stop() {
running = false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.configcenter.consul;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.configcenter.AbstractDynamicConfigurationFactory;
import org.apache.dubbo.configcenter.DynamicConfiguration;

/**
* Config center factory for consul
*/
public class ConsulDynamicConfigurationFactory extends AbstractDynamicConfigurationFactory {
@Override
protected DynamicConfiguration createDynamicConfiguration(URL url) {
return new ConsulDynamicConfiguration(url);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
consul=org.apache.dubbo.configcenter.consul.ConsulDynamicConfigurationFactory
3 changes: 2 additions & 1 deletion dubbo-configcenter/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@
<module>dubbo-configcenter-api</module>
<module>dubbo-configcenter-zookeeper</module>
<module>dubbo-configcenter-apollo</module>
<module>dubbo-configcenter-consul</module>
</modules>
</project>
</project>
6 changes: 6 additions & 0 deletions dubbo-dependencies-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
<curator_version>4.0.1</curator_version>
<curator_test_version>2.12.0</curator_test_version>
<jedis_version>2.9.0</jedis_version>
<consul_version>1.4.2</consul_version>
<xmemcached_version>1.3.6</xmemcached_version>
<cxf_version>3.1.15</cxf_version>
<thrift_version>0.8.0</thrift_version>
Expand Down Expand Up @@ -232,6 +233,11 @@
<artifactId>jedis</artifactId>
<version>${jedis_version}</version>
</dependency>
<dependency>
<groupId>com.ecwid.consul</groupId>
<artifactId>consul-api</artifactId>
<version>${consul_version}</version>
</dependency>
<dependency>
<groupId>com.googlecode.xmemcached</groupId>
<artifactId>xmemcached</artifactId>
Expand Down
Loading

0 comments on commit 096d1da

Please sign in to comment.