|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.dubbo.configcenter.consul; |
| 19 | + |
| 20 | +import org.apache.dubbo.common.URL; |
| 21 | +import org.apache.dubbo.common.logger.Logger; |
| 22 | +import org.apache.dubbo.common.logger.LoggerFactory; |
| 23 | +import org.apache.dubbo.common.utils.NamedThreadFactory; |
| 24 | +import org.apache.dubbo.common.utils.StringUtils; |
| 25 | +import org.apache.dubbo.configcenter.ConfigChangeEvent; |
| 26 | +import org.apache.dubbo.configcenter.ConfigurationListener; |
| 27 | +import org.apache.dubbo.configcenter.DynamicConfiguration; |
| 28 | + |
| 29 | +import com.ecwid.consul.v1.ConsulClient; |
| 30 | +import com.ecwid.consul.v1.QueryParams; |
| 31 | +import com.ecwid.consul.v1.Response; |
| 32 | +import com.ecwid.consul.v1.kv.model.GetValue; |
| 33 | + |
| 34 | +import java.util.HashSet; |
| 35 | +import java.util.Set; |
| 36 | +import java.util.concurrent.ConcurrentHashMap; |
| 37 | +import java.util.concurrent.ConcurrentMap; |
| 38 | +import java.util.concurrent.ExecutorService; |
| 39 | + |
| 40 | +import static java.util.concurrent.Executors.newCachedThreadPool; |
| 41 | +import static org.apache.dubbo.common.Constants.CONFIG_NAMESPACE_KEY; |
| 42 | +import static org.apache.dubbo.common.Constants.PATH_SEPARATOR; |
| 43 | + |
| 44 | +/** |
| 45 | + * config center implementation for consul |
| 46 | + */ |
| 47 | +public class ConsulDynamicConfiguration implements DynamicConfiguration { |
| 48 | + private static final Logger logger = LoggerFactory.getLogger(ConsulDynamicConfiguration.class); |
| 49 | + |
| 50 | + private static final int DEFAULT_PORT = 8500; |
| 51 | + private static final int DEFAULT_WATCH_TIMEOUT = 60 * 1000; |
| 52 | + private static final String WATCH_TIMEOUT = "consul-watch-timeout"; |
| 53 | + |
| 54 | + private URL url; |
| 55 | + private String rootPath; |
| 56 | + private ConsulClient client; |
| 57 | + private int watchTimeout = -1; |
| 58 | + private ConcurrentMap<String, ConsulKVWatcher> watchers = new ConcurrentHashMap<>(); |
| 59 | + private ConcurrentMap<String, Long> consulIndexes = new ConcurrentHashMap<>(); |
| 60 | + private ExecutorService watcherService = newCachedThreadPool( |
| 61 | + new NamedThreadFactory("dubbo-consul-configuration-watcher", true)); |
| 62 | + |
| 63 | + public ConsulDynamicConfiguration(URL url) { |
| 64 | + this.url = url; |
| 65 | + this.rootPath = PATH_SEPARATOR + url.getParameter(CONFIG_NAMESPACE_KEY, DEFAULT_GROUP) + PATH_SEPARATOR + "config"; |
| 66 | + this.watchTimeout = buildWatchTimeout(url); |
| 67 | + String host = url.getHost(); |
| 68 | + int port = url.getPort() != 0 ? url.getPort() : DEFAULT_PORT; |
| 69 | + client = new ConsulClient(host, port); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public void addListener(String key, String group, ConfigurationListener listener) { |
| 74 | + logger.info("register listener " + listener.getClass() + " for config with key: " + key + ", group: " + group); |
| 75 | + ConsulKVWatcher watcher = watchers.putIfAbsent(key, new ConsulKVWatcher(key)); |
| 76 | + if (watcher == null) { |
| 77 | + watcher = watchers.get(key); |
| 78 | + watcherService.submit(watcher); |
| 79 | + } |
| 80 | + watcher.addListener(listener); |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + public void removeListener(String key, String group, ConfigurationListener listener) { |
| 85 | + logger.info("unregister listener " + listener.getClass() + " for config with key: " + key + ", group: " + group); |
| 86 | + ConsulKVWatcher watcher = watchers.get(key); |
| 87 | + if (watcher != null) { |
| 88 | + watcher.removeListener(listener); |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public String getConfig(String key, String group, long timeout) throws IllegalStateException { |
| 94 | + if (StringUtils.isNotEmpty(group)) { |
| 95 | + key = group + PATH_SEPARATOR + key; |
| 96 | + } else { |
| 97 | + int i = key.lastIndexOf("."); |
| 98 | + key = key.substring(0, i) + PATH_SEPARATOR + key.substring(i + 1); |
| 99 | + } |
| 100 | + |
| 101 | + return (String) getInternalProperty(rootPath + PATH_SEPARATOR + key); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public Object getInternalProperty(String key) { |
| 106 | + logger.info("get config from: " + key); |
| 107 | + Long currentIndex = consulIndexes.computeIfAbsent(key, k -> -1L); |
| 108 | + Response<GetValue> response = client.getKVValue(key, new QueryParams(watchTimeout, currentIndex)); |
| 109 | + GetValue value = response.getValue(); |
| 110 | + consulIndexes.put(key, response.getConsulIndex()); |
| 111 | + return value != null ? value.getDecodedValue() : null; |
| 112 | + } |
| 113 | + |
| 114 | + private int buildWatchTimeout(URL url) { |
| 115 | + return url.getParameter(WATCH_TIMEOUT, DEFAULT_WATCH_TIMEOUT) / 1000; |
| 116 | + } |
| 117 | + |
| 118 | + private class ConsulKVWatcher implements Runnable { |
| 119 | + private String key; |
| 120 | + private Set<ConfigurationListener> listeners; |
| 121 | + private boolean running = true; |
| 122 | + |
| 123 | + public ConsulKVWatcher(String key) { |
| 124 | + this.key = convertKey(key); |
| 125 | + this.listeners = new HashSet<>(); |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void run() { |
| 130 | + while (running) { |
| 131 | + Long lastIndex = consulIndexes.computeIfAbsent(key, k -> -1L); |
| 132 | + Response<GetValue> response = client.getKVValue(key, new QueryParams(watchTimeout, lastIndex)); |
| 133 | + |
| 134 | + Long currentIndex = response.getConsulIndex(); |
| 135 | + if (currentIndex == null || currentIndex <= lastIndex) { |
| 136 | + continue; |
| 137 | + } |
| 138 | + |
| 139 | + consulIndexes.put(key, currentIndex); |
| 140 | + String value = response.getValue().getDecodedValue(); |
| 141 | + logger.info("notify change for key: " + key + ", the value is: " + value); |
| 142 | + ConfigChangeEvent event = new ConfigChangeEvent(key, value); |
| 143 | + for (ConfigurationListener listener : listeners) { |
| 144 | + listener.process(event); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private void addListener(ConfigurationListener listener) { |
| 150 | + this.listeners.add(listener); |
| 151 | + } |
| 152 | + |
| 153 | + private void removeListener(ConfigurationListener listener) { |
| 154 | + this.listeners.remove(listener); |
| 155 | + } |
| 156 | + |
| 157 | + private String convertKey(String key) { |
| 158 | + int index = key.lastIndexOf('.'); |
| 159 | + return rootPath + PATH_SEPARATOR + key.substring(0, index) + PATH_SEPARATOR + key.substring(index + 1); |
| 160 | + } |
| 161 | + |
| 162 | + private void stop() { |
| 163 | + running = false; |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments