Skip to content

Commit

Permalink
Enhance register type of ServiceConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbumenJ committed Jun 22, 2023
1 parent f28c2fe commit f7ad294
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 47 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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.common.constants;

/**
* Indicate that a service need to be registered to registry or not
*/
public enum RegisterTypeEnum {

/**
* Never register. Cannot be registered by any command(like QoS-online).
*/
NEVER_REGISTER,

/**
* Manual register. Can be registered by command(like QoS-online), but not register by default.
*/
MANUAL_REGISTER,

/**
* (INTERNAL) Auto register by deployer. Will be registered after deployer started.
* (Delay publish when starting. Prevent service from being invoked before all services are started)
*/
AUTO_REGISTER_BY_DEPLOYER,

/**
* Auto register. Will be registered when one service is exported.
*/
AUTO_REGISTER;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.apache.dubbo.config;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.constants.RegisterTypeEnum;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Service;
Expand Down Expand Up @@ -417,7 +418,7 @@ public Boolean shouldExportAsync() {
* export service and auto start application instance
*/
public final void export() {
export(true);
export(RegisterTypeEnum.AUTO_REGISTER);
}

public abstract void unexport();
Expand All @@ -429,10 +430,9 @@ public final void export() {
/**
* Export service to network
*
* @param register Whether register service to registry. If false, can be registered manually
* through the {@link ServiceConfigBase#register(boolean)} API.
* @param registerType register type of current export action.
*/
public abstract void export(boolean register);
public abstract void export(RegisterTypeEnum registerType);

/**
* Register delay published service to registry.
Expand All @@ -444,7 +444,7 @@ public final void register() {
/**
* Register delay published service to registry.
*
* @param onlyDefault only register those services that export with configured register false
* @param byDeployer register by deployer or not.
*/
public abstract void register(boolean onlyDefault);
public abstract void register(boolean byDeployer);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.dubbo.common.Version;
import org.apache.dubbo.common.config.ConfigurationUtils;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.constants.RegisterTypeEnum;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.common.logger.ErrorTypeAwareLogger;
import org.apache.dubbo.common.logger.LoggerFactory;
Expand Down Expand Up @@ -57,11 +58,14 @@
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -99,7 +103,6 @@
import static org.apache.dubbo.config.Constants.DUBBO_PORT_TO_REGISTRY;
import static org.apache.dubbo.config.Constants.SCOPE_NONE;
import static org.apache.dubbo.registry.Constants.REGISTER_KEY;
import static org.apache.dubbo.registry.Constants.REGISTER_ORIGIN_KEY;
import static org.apache.dubbo.remoting.Constants.BIND_IP_KEY;
import static org.apache.dubbo.remoting.Constants.BIND_PORT_KEY;
import static org.apache.dubbo.remoting.Constants.IS_PU_SERVER_KEY;
Expand Down Expand Up @@ -149,7 +152,7 @@ public class ServiceConfig<T> extends ServiceConfigBase<T> {
/**
* The exported services
*/
private final List<Exporter<?>> exporters = new ArrayList<Exporter<?>>();
private final Map<RegisterTypeEnum, List<Exporter<?>>> exporters = new ConcurrentHashMap<>();

private final List<ServiceListener> serviceListeners = new ArrayList<>();

Expand Down Expand Up @@ -197,19 +200,23 @@ public void unexport() {
return;
}
if (!exporters.isEmpty()) {
for (Exporter<?> exporter : exporters) {
try {
exporter.unregister();
} catch (Throwable t) {
logger.warn(CONFIG_UNEXPORT_ERROR, "", "", "Unexpected error occurred when unexport " + exporter, t);
for (List<Exporter<?>> es : exporters.values()) {
for (Exporter<?> exporter : es) {
try {
exporter.unregister();
} catch (Throwable t) {
logger.warn(CONFIG_UNEXPORT_ERROR, "", "", "Unexpected error occurred when unexport " + exporter, t);
}
}
}
waitForIdle();
for (Exporter<?> exporter : exporters) {
try {
exporter.unexport();
} catch (Throwable t) {
logger.warn(CONFIG_UNEXPORT_ERROR, "", "", "Unexpected error occurred when unexport " + exporter, t);
for (List<Exporter<?>> es : exporters.values()) {
for (Exporter<?> exporter : es) {
try {
exporter.unexport();
} catch (Throwable t) {
logger.warn(CONFIG_UNEXPORT_ERROR, "", "", "Unexpected error occurred when unexport " + exporter, t);
}
}
}
exporters.clear();
Expand Down Expand Up @@ -281,7 +288,7 @@ public void init() {
}

@Override
public void export(boolean register) {
public void export(RegisterTypeEnum registerType) {
if (this.exported) {
return;
}
Expand All @@ -302,16 +309,19 @@ public void export(boolean register) {

if (shouldDelay()) {
// should register if delay export
doDelayExport(true);
doDelayExport();
} if (Integer.valueOf(-1).equals(getDelay())) {
// should not register by default
doExport(RegisterTypeEnum.MANUAL_REGISTER);
} else {
doExport(register);
doExport(registerType);
}
}
}
}

@Override
public void register(boolean onlyDefault) {
public void register(boolean byDeployer) {
if (!this.exported) {
return;
}
Expand All @@ -321,19 +331,23 @@ public void register(boolean onlyDefault) {
return;
}

for (Exporter<?> exporter : exporters) {
if (!onlyDefault || exporter.getInvoker().getUrl().getParameter(REGISTER_ORIGIN_KEY, true)) {
for (Exporter<?> exporter : exporters.getOrDefault(RegisterTypeEnum.AUTO_REGISTER, Collections.emptyList())) {
exporter.register();
}

if (byDeployer) {
for (Exporter<?> exporter : exporters.getOrDefault(RegisterTypeEnum.AUTO_REGISTER_BY_DEPLOYER, Collections.emptyList())) {
exporter.register();
}
}
}
}

protected void doDelayExport(boolean register) {
protected void doDelayExport() {
ExecutorRepository.getInstance(getScopeModel().getApplicationModel()).getServiceExportExecutor()
.schedule(() -> {
try {
doExport(register);
doExport(RegisterTypeEnum.AUTO_REGISTER);
} catch (Exception e) {
logger.error(CONFIG_FAILED_EXPORT_SERVICE, "configuration server disconnected", "", "Failed to (async)export service config: " + interfaceName, e);
}
Expand Down Expand Up @@ -460,7 +474,7 @@ protected void postProcessRefresh() {
checkAndUpdateSubConfigs();
}

protected synchronized void doExport(boolean register) {
protected synchronized void doExport(RegisterTypeEnum registerType) {
if (unexported) {
throw new IllegalStateException("The service " + interfaceClass.getName() + " has already unexported!");
}
Expand All @@ -471,12 +485,12 @@ protected synchronized void doExport(boolean register) {
if (StringUtils.isEmpty(path)) {
path = interfaceName;
}
doExportUrls(register);
doExportUrls(registerType);
exported();
}

@SuppressWarnings({"unchecked", "rawtypes"})
private void doExportUrls(boolean register) {
private void doExportUrls(RegisterTypeEnum registerType) {
ModuleServiceRepository repository = getScopeModel().getServiceRepository();
ServiceDescriptor serviceDescriptor;
final boolean serverService = ref instanceof ServerService;
Expand Down Expand Up @@ -508,10 +522,10 @@ private void doExportUrls(boolean register) {
.orElse(path), group, version);
// stub service will use generated service name
if (!serverService) {
// In case user specified path, register service one more time to map it to path.
// In case user specified path, registerImmediately service one more time to map it to path.
repository.registerService(pathKey, interfaceClass);
}
doExportUrlsFor1Protocol(protocolConfig, registryURLs, register);
doExportUrlsFor1Protocol(protocolConfig, registryURLs, registerType);
}
return null;
}
Expand All @@ -520,7 +534,7 @@ private void doExportUrls(boolean register) {
providerModel.setServiceUrls(urls);
}

private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List<URL> registryURLs, boolean register) {
private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List<URL> registryURLs, RegisterTypeEnum registerType) {
Map<String, String> map = buildAttributes(protocolConfig);

// remove null key and null value
Expand All @@ -532,7 +546,7 @@ private void doExportUrlsFor1Protocol(ProtocolConfig protocolConfig, List<URL> r

processServiceExecutor(url);

exportUrl(url, registryURLs, register);
exportUrl(url, registryURLs, registerType);
}

private void processServiceExecutor(URL url) {
Expand Down Expand Up @@ -714,7 +728,7 @@ private URL buildUrl(ProtocolConfig protocolConfig, Map<String, String> params)
return url;
}

private void exportUrl(URL url, List<URL> registryURLs, boolean register) {
private void exportUrl(URL url, List<URL> registryURLs, RegisterTypeEnum registerType) {
String scope = url.getParameter(SCOPE_KEY);
// don't export when none is configured
if (!SCOPE_NONE.equalsIgnoreCase(scope)) {
Expand All @@ -738,7 +752,7 @@ private void exportUrl(URL url, List<URL> registryURLs, boolean register) {
build();
}

url = exportRemote(url, registryURLs, register);
url = exportRemote(url, registryURLs, registerType);
if (!isGeneric(generic) && !getScopeModel().isInternal()) {
MetadataUtils.publishServiceDefinition(url, providerModel.getServiceModel(), getApplicationModel());
}
Expand All @@ -753,7 +767,7 @@ private void exportUrl(URL url, List<URL> registryURLs, boolean register) {
URL localUrl = URLBuilder.from(url).
setProtocol(protocol).
build();
localUrl = exportRemote(localUrl, registryURLs, register);
localUrl = exportRemote(localUrl, registryURLs, registerType);
if (!isGeneric(generic) && !getScopeModel().isInternal()) {
MetadataUtils.publishServiceDefinition(localUrl, providerModel.getServiceModel(), getApplicationModel());
}
Expand All @@ -765,8 +779,8 @@ private void exportUrl(URL url, List<URL> registryURLs, boolean register) {
this.urls.add(url);
}

private URL exportRemote(URL url, List<URL> registryURLs, boolean register) {
if (CollectionUtils.isNotEmpty(registryURLs)) {
private URL exportRemote(URL url, List<URL> registryURLs, RegisterTypeEnum registerType) {
if (CollectionUtils.isNotEmpty(registryURLs) && registerType != RegisterTypeEnum.NEVER_REGISTER) {
for (URL registryURL : registryURLs) {
if (SERVICE_REGISTRY_PROTOCOL.equals(registryURL.getProtocol())) {
url = url.addParameterIfAbsent(SERVICE_NAME_MAPPING_KEY, "true");
Expand Down Expand Up @@ -797,7 +811,7 @@ private URL exportRemote(URL url, List<URL> registryURLs, boolean register) {
}
}

doExportUrl(registryURL.putAttribute(EXPORT_KEY, url), true, register);
doExportUrl(registryURL.putAttribute(EXPORT_KEY, url), true, registerType);
}

} else {
Expand All @@ -806,25 +820,30 @@ private URL exportRemote(URL url, List<URL> registryURLs, boolean register) {
logger.info("Export dubbo service " + interfaceClass.getName() + " to url " + url);
}

doExportUrl(url, true, register);
doExportUrl(url, true, registerType);
}


return url;
}

@SuppressWarnings({"unchecked", "rawtypes"})
private void doExportUrl(URL url, boolean withMetaData, boolean register) {
if (!register) {
url = url.addParameter(REGISTER_ORIGIN_KEY, url.getParameter(REGISTER_KEY, true));
private void doExportUrl(URL url, boolean withMetaData, RegisterTypeEnum registerType) {
if (!url.getParameter(REGISTER_KEY, true)) {
registerType = RegisterTypeEnum.MANUAL_REGISTER;
}
if (registerType == RegisterTypeEnum.NEVER_REGISTER ||
registerType == RegisterTypeEnum.MANUAL_REGISTER ||
registerType == RegisterTypeEnum.AUTO_REGISTER_BY_DEPLOYER) {
url = url.addParameter(REGISTER_KEY, false);
}

Invoker<?> invoker = proxyFactory.getInvoker(ref, (Class) interfaceClass, url);
if (withMetaData) {
invoker = new DelegateProviderMetaDataInvoker(invoker, this);
}
Exporter<?> exporter = protocolSPI.export(invoker);
exporters.add(exporter);
exporters.computeIfAbsent(registerType, k -> new CopyOnWriteArrayList<>()).add(exporter);
}


Expand All @@ -840,7 +859,7 @@ private void exportLocal(URL url) {
local = local.setScopeModel(getScopeModel())
.setServiceModel(providerModel);
local = local.addParameter(EXPORTER_LISTENER_KEY, LOCAL_PROTOCOL);
doExportUrl(local, false, true);
doExportUrl(local, false, RegisterTypeEnum.AUTO_REGISTER);
logger.info("Export dubbo service " + interfaceClass.getName() + " to local registry url : " + local);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.apache.dubbo.common.config.ReferenceCache;
import org.apache.dubbo.common.constants.LoggerCodeConstants;
import org.apache.dubbo.common.constants.RegisterTypeEnum;
import org.apache.dubbo.common.deploy.AbstractDeployer;
import org.apache.dubbo.common.deploy.ApplicationDeployer;
import org.apache.dubbo.common.deploy.DeployListener;
Expand Down Expand Up @@ -427,7 +428,7 @@ private void exportServiceInternal(ServiceConfigBase sc) {
asyncExportingFutures.add(future);
} else {
if (!sc.isExported()) {
sc.export(false);
sc.export(RegisterTypeEnum.AUTO_REGISTER_BY_DEPLOYER);
exportedServices.add(sc);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public interface Constants {
String REGISTER_IP_KEY = "register.ip";

String REGISTER_KEY = "register";
String REGISTER_ORIGIN_KEY = "register_origin";

String SUBSCRIBE_KEY = "subscribe";

Expand Down
Loading

0 comments on commit f7ad294

Please sign in to comment.