Skip to content

Commit

Permalink
Polish /apache#3942 : Refactor implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
mercyblitz committed Jun 7, 2019
1 parent 3ac778c commit a8526da
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 134 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.apache.dubbo.registry.client;/*
* 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.
*/

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.SPI;

import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader;

/**
* The factory class to create an instance of {@link ServiceDiscoveryFactory} based on Event-Publishing as the default
* {@link SPI} implementation
*
* @see ServiceDiscoveryFactory
* @see EventPublishingServiceDiscovery
* @see ServiceDiscovery
* @since 2.7.3
*/
public class EventPublishingServiceDiscoveryFactory implements ServiceDiscoveryFactory {

private static final Class<ServiceDiscoveryFactory> FACTORY_CLASS = ServiceDiscoveryFactory.class;

@Override
public ServiceDiscovery create(URL connectionURL) {
String protocol = connectionURL.getProtocol();
ServiceDiscoveryFactory serviceDiscoveryFactory = loadFactoryByProtocol(protocol);
ServiceDiscovery originalServiceDiscovery = serviceDiscoveryFactory.create(connectionURL);
return new EventPublishingServiceDiscovery(originalServiceDiscovery);
}

protected ServiceDiscoveryFactory loadFactoryByProtocol(String protocol) {
return getExtensionLoader(FACTORY_CLASS).getExtension(protocol);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.SPI;

import static java.lang.Integer.compare;
import static org.apache.dubbo.common.extension.ExtensionLoader.getExtensionLoader;

/**
Expand All @@ -28,46 +27,17 @@
* @see ServiceDiscovery
* @since 2.7.3
*/
@SPI("default")
public interface ServiceDiscoveryFactory extends Comparable<ServiceDiscoveryFactory> {
@SPI("event-publishing")
public interface ServiceDiscoveryFactory {

/**
* It indicates the current implementation supports or not in the specified {@link URL connnection url}.
* Creates an instance of {@link ServiceDiscovery}.
*
* @param connectionURL the {@link URL connection url}
* @return if supports, return <code>true</code>, or <code>false</code>
*/
boolean supports(URL connectionURL);

/**
* Creates an instance of {@link ServiceDiscovery} if {@link #supports(URL)} returns <code>true</code>,
*
* @param connectionURL the {@link URL connection url}
* @return an instance of {@link ServiceDiscovery} if supported, or <code>null</code>
* @return an instance of {@link ServiceDiscovery}
*/
ServiceDiscovery create(URL connectionURL);

/**
* The priority of current {@link ServiceDiscoveryFactory}
*
* @return The {@link Integer#MIN_VALUE minimum integer} indicates the highest priority, in contrast,
* the lowest priority is {@link Integer#MAX_VALUE the maximum integer}
*/
default int getPriority() {
return Integer.MAX_VALUE;
}

/**
* Compares its priority
*
* @param that {@link ServiceDiscovery}
* @return
*/
@Override
default int compareTo(ServiceDiscoveryFactory that) {
return compare(this.getPriority(), that.getPriority());
}

/**
* Get the default extension of {@link ServiceDiscoveryFactory}
*
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
default=org.apache.dubbo.registry.client.DefaultServiceDiscoveryFactory
event-publishing=org.apache.dubbo.registry.client.EventPublishingServiceDiscoveryFactory
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@
*/
public class InMemoryServiceDiscoveryFactory implements ServiceDiscoveryFactory {

@Override
public boolean supports(URL connectionURL) {
return "in-memory".equalsIgnoreCase(connectionURL.getProtocol());
}

@Override
public ServiceDiscovery create(URL connectionURL) {
return new InMemoryServiceDiscovery();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

import static org.apache.dubbo.common.URL.valueOf;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
* {@link ServiceDiscoveryFactory} Test
Expand All @@ -46,23 +44,12 @@ public void init() {

@Test
public void testClass() {
assertEquals(DefaultServiceDiscoveryFactory.class, serviceDiscoveryFactory.getClass());
}

@Test
public void testSupports() {
assertFalse(serviceDiscoveryFactory.supports(dubboURL));
assertTrue(serviceDiscoveryFactory.supports(inMemoryURL));
assertEquals(EventPublishingServiceDiscoveryFactory.class, serviceDiscoveryFactory.getClass());
}

@Test
public void testCreate() {
ServiceDiscovery serviceDiscovery = serviceDiscoveryFactory.create(inMemoryURL);
assertEquals(EventPublishingServiceDiscovery.class, serviceDiscovery.getClass());
}

@Test
public void testPriority() {
assertEquals(Integer.MAX_VALUE, serviceDiscoveryFactory.getPriority());
}
}
Original file line number Diff line number Diff line change
@@ -1 +1 @@
org.apache.dubbo.registry.client.InMemoryServiceDiscoveryFactory
in-memory=org.apache.dubbo.registry.client.InMemoryServiceDiscoveryFactory
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@
*/
public class ZookeeperServiceDiscoveryFactory implements ServiceDiscoveryFactory {

@Override
public boolean supports(URL connectionURL) {
return "zookeeper".equalsIgnoreCase(connectionURL.getProtocol());
}

@Override
public ServiceDiscovery create(URL connectionURL) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
zookeeper=org.apache.dubbo.registry.zookeeper.ZookeeperServiceDiscoveryFactory

0 comments on commit a8526da

Please sign in to comment.