Dubbo-聊聊注册中心的设计( 三 )

创建/获取getRegistry是对RegistryFactory实现,如果没有在缓存中,则进行创建实例对象createRegistry,createRegistry是抽象方法,为了让子类重写该方法,比如说redis实现的注册中心和zookeeper实现的注册中心创建方式肯定不同,而他们相同的一些操作都已经在AbstractRegistryFactory中实现,所以只要关注且实现该抽象方法即可 。
    //抽象的createRegistry方法    protected abstract Registry createRegistry(URL url);    //获取实例    public Registry getRegistry(URL url) {        Registry defaultNopRegistry = getDefaultNopRegistryIfDestroyed();        if (null != defaultNopRegistry) {            return defaultNopRegistry;        }        //构建key        url = URLBuilder.from(url)                .setPath(RegistryService.class.getName())                .addParameter(INTERFACE_KEY, RegistryService.class.getName())                .removeParameters(EXPORT_KEY, REFER_KEY)                .build();        String key = createRegistryCacheKey(url);        // Lock the registry access process to ensure a single instance of the registry        LOCK.lock();        try {            // double check            // fix https://github.com/apache/dubbo/issues/7265.            defaultNopRegistry = getDefaultNopRegistryIfDestroyed();            if (null != defaultNopRegistry) {                return defaultNopRegistry;            }            //获取实例对象            Registry registry = REGISTRIES.get(key);            if (registry != null) {                return registry;            }            //没有获取到就创建            registry = createRegistry(url);            if (registry == null) {                throw new IllegalStateException("Can not create registry " + url);            }            //放入Map集合中            REGISTRIES.put(key, registry);            return registry;        } finally {            // Release the lock            LOCK.unlock();        }    }

经验总结扩展阅读