diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/com/alibaba/dubbo/registry/support/AbstractRegistryFactory.java b/dubbo-registry/dubbo-registry-api/src/main/java/com/alibaba/dubbo/registry/support/AbstractRegistryFactory.java index ae2f7fed11a..fe76ebb4807 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/com/alibaba/dubbo/registry/support/AbstractRegistryFactory.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/com/alibaba/dubbo/registry/support/AbstractRegistryFactory.java @@ -1,103 +1,105 @@ -/* - * Copyright 1999-2011 Alibaba Group. - * - * Licensed 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 com.alibaba.dubbo.registry.support; - -import java.util.Collection; -import java.util.Collections; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.locks.ReentrantLock; - -import com.alibaba.dubbo.common.URL; -import com.alibaba.dubbo.common.logger.Logger; -import com.alibaba.dubbo.common.logger.LoggerFactory; -import com.alibaba.dubbo.registry.Registry; -import com.alibaba.dubbo.registry.RegistryFactory; - -/** - * AbstractRegistryFactory. (SPI, Singleton, ThreadSafe) - * - * @see com.alibaba.dubbo.registry.RegistryFactory - * @author william.liangf - */ -public abstract class AbstractRegistryFactory implements RegistryFactory { - - // 日志输出 - private static final Logger LOGGER = LoggerFactory.getLogger(AbstractRegistryFactory.class); - - // 注册中心获取过程锁 - private static final ReentrantLock LOCK = new ReentrantLock(); - - // 注册中心集合 Map - private static final Map REGISTRIES = new ConcurrentHashMap(); - - /** - * 获取所有注册中心 - * - * @return 所有注册中心 - */ - public static Collection getRegistries() { - return Collections.unmodifiableCollection(REGISTRIES.values()); - } - - /** - * 关闭所有已创建注册中心 - */ - public static void destroyAll() { - if (LOGGER.isInfoEnabled()) { - LOGGER.info("Close all registries " + getRegistries()); - } - // 锁定注册中心关闭过程 - LOCK.lock(); - try { - for (Registry registry : getRegistries()) { - try { - registry.destroy(); - } catch (Throwable e) { - LOGGER.error(e.getMessage(), e); - } - } - REGISTRIES.clear(); - } finally { - // 释放锁 - LOCK.unlock(); - } - } - - public Registry getRegistry(URL url) { - // 锁定注册中心获取过程,保证注册中心单一实例 - LOCK.lock(); - try { - String key = url.getProtocol() + "://" + url.getUsername() + ":" + url.getPassword() + "@" + url.getIp() + ":" + url.getPort(); - Registry registry = REGISTRIES.get(key); - if (registry != null) { - return registry; - } - registry = createRegistry(url); - if (registry == null) { - throw new IllegalStateException("Can not create registry " + url); - } - REGISTRIES.put(key, registry); - return registry; - } finally { - // 释放锁 - LOCK.unlock(); - } - } - - protected abstract Registry createRegistry(URL url); - +/* + * Copyright 1999-2011 Alibaba Group. + * + * Licensed 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 com.alibaba.dubbo.registry.support; + +import com.alibaba.dubbo.common.Constants; +import com.alibaba.dubbo.common.URL; +import com.alibaba.dubbo.common.logger.Logger; +import com.alibaba.dubbo.common.logger.LoggerFactory; +import com.alibaba.dubbo.registry.Registry; +import com.alibaba.dubbo.registry.RegistryFactory; + +import java.util.Collection; +import java.util.Collections; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.locks.ReentrantLock; + +/** + * AbstractRegistryFactory. (SPI, Singleton, ThreadSafe) + * + * @see com.alibaba.dubbo.registry.RegistryFactory + * @author william.liangf + */ +public abstract class AbstractRegistryFactory implements RegistryFactory { + + // 日志输出 + private static final Logger LOGGER = LoggerFactory.getLogger(AbstractRegistryFactory.class); + + // 注册中心获取过程锁 + private static final ReentrantLock LOCK = new ReentrantLock(); + + // 注册中心集合 Map + private static final Map REGISTRIES = new ConcurrentHashMap(); + + /** + * 获取所有注册中心 + * + * @return 所有注册中心 + */ + public static Collection getRegistries() { + return Collections.unmodifiableCollection(REGISTRIES.values()); + } + + /** + * 关闭所有已创建注册中心 + */ + public static void destroyAll() { + if (LOGGER.isInfoEnabled()) { + LOGGER.info("Close all registries " + getRegistries()); + } + // 锁定注册中心关闭过程 + LOCK.lock(); + try { + for (Registry registry : getRegistries()) { + try { + registry.destroy(); + } catch (Throwable e) { + LOGGER.error(e.getMessage(), e); + } + } + REGISTRIES.clear(); + } finally { + // 释放锁 + LOCK.unlock(); + } + } + + public Registry getRegistry(URL url) { + // 锁定注册中心获取过程,保证注册中心单一实例 + LOCK.lock(); + try { + String key = url.getProtocol() + "://" + url.getUsername() + ":" + url.getPassword() + "@" + url.getIp() + ":" + url.getPort() + + url.getParameter(Constants.GROUP_KEY, ""); + Registry registry = REGISTRIES.get(key); + if (registry != null) { + return registry; + } + registry = createRegistry(url); + if (registry == null) { + throw new IllegalStateException("Can not create registry " + url); + } + REGISTRIES.put(key, registry); + return registry; + } finally { + // 释放锁 + LOCK.unlock(); + } + } + + protected abstract Registry createRegistry(URL url); + } \ No newline at end of file