Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@
import static org.apache.dubbo.common.constants.LoggerCodeConstants.CONFIG_FAILED_LOAD_ENV_VARIABLE;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.CONFIG_NO_METHOD_FOUND;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.CONFIG_PROPERTY_CONFLICT;
import static org.apache.dubbo.common.constants.LoggerCodeConstants.INTERNAL_ERROR;
import static org.apache.dubbo.common.constants.RegistryConstants.PROVIDED_BY;
import static org.apache.dubbo.common.constants.RegistryConstants.SUBSCRIBED_SERVICE_NAMES_KEY;
import static org.apache.dubbo.common.utils.NetUtils.isInvalidLocalHost;
Expand Down Expand Up @@ -349,20 +350,30 @@ protected void init(boolean check) {
initServiceMetadata(consumer);

serviceMetadata.setServiceType(getServiceInterfaceClass());
// TODO, uncomment this line once service key is unified
boolean useIdl = false;

if (DubboStub.class.isAssignableFrom(interfaceClass) && ServiceNameConfig.useIdl(getScopeModel())) {
try {
ServiceDescriptor idlDescriptor = StubSuppliers.getServiceDescriptor(interfaceName);
if (idlDescriptor != null && StringUtils.isNotEmpty(idlDescriptor.getInterfaceName())) {
useIdl = true;
serviceMetadata.setServiceInterfaceName(idlDescriptor.getInterfaceName());
}
} catch (Throwable t) {
logger.warn(INTERNAL_ERROR, "unknown", "", "Failed to resolve IDL service name.", t);
}
}
serviceMetadata.generateServiceKey();

Map<String, String> referenceParameters = appendConfig();

ModuleServiceRepository repository = getScopeModel().getServiceRepository();
ServiceDescriptor serviceDescriptor;
if (CommonConstants.NATIVE_STUB.equals(getProxy())) {
serviceDescriptor = StubSuppliers.getServiceDescriptor(interfaceName);
repository.registerService(serviceDescriptor);
setInterface(serviceDescriptor.getInterfaceName());
} else {
serviceDescriptor = repository.registerService(interfaceClass);
if (useIdl) {
referenceParameters.put(CommonConstants.INTERFACE_KEY, serviceMetadata.getServiceInterfaceName());
}

ModuleServiceRepository repository = getScopeModel().getServiceRepository();
ServiceDescriptor serviceDescriptor = repository.registerService(interfaceClass);

consumerModel = new ConsumerModel(
serviceMetadata.getServiceKey(),
proxy,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,14 @@
import org.apache.dubbo.rpc.RpcInvocation;
import org.apache.dubbo.rpc.ServerService;
import org.apache.dubbo.rpc.cluster.ConfiguratorFactory;
import org.apache.dubbo.rpc.model.DubboStub;
import org.apache.dubbo.rpc.model.ModuleModel;
import org.apache.dubbo.rpc.model.ModuleServiceRepository;
import org.apache.dubbo.rpc.model.ProviderModel;
import org.apache.dubbo.rpc.model.ScopeModel;
import org.apache.dubbo.rpc.model.ServiceDescriptor;
import org.apache.dubbo.rpc.service.GenericService;
import org.apache.dubbo.rpc.stub.StubSuppliers;

import java.beans.Transient;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -318,6 +320,35 @@ public void init() {
initServiceMetadata(provider);
serviceMetadata.setServiceType(getInterfaceClass());
serviceMetadata.setTarget(getRef());

// This handles scenarios where the package differs from the generated Java package.
if (DubboStub.class.isAssignableFrom(getInterfaceClass()) && ServiceNameConfig.useIdl(getScopeModel())) {
try {
ServiceDescriptor sd = StubSuppliers.getServiceDescriptor(interfaceName);
if (sd != null && StringUtils.isNotEmpty(sd.getInterfaceName())) {

String idlName = sd.getInterfaceName();

// Update the Service Metadata to use the IDL service name instead of the Java class name.
serviceMetadata.setServiceInterfaceName(idlName);

// Set IDL name into URL parameters so the registry registers this service using the
// IDL-based name
Map<String, String> params = this.getParameters();
if (params == null) {
params = new HashMap<>();
} else {
params = new HashMap<>(params);
}
params.put(CommonConstants.INTERFACE_KEY, idlName);
this.setParameters(params);

logger.info("Using IDL service name '" + idlName + "' for registration.");
}
} catch (Throwable t) {
logger.warn(INTERNAL_ERROR, "unknown", "", "Failed to resolve IDL service name", t);
}
}
serviceMetadata.generateServiceKey();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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.config;

import org.apache.dubbo.common.config.ConfigurationUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.model.ScopeModel;

/**
* This class controls how Dubbo determines the logical service name
* which is used for service discovery.
* This is introduced to support Dubbo IDL services.
* A legacy configuration key is also supported to ease migration from
* earlier versions.
*/
public class ServiceNameConfig {

// It defines how the service name is determined.
public enum Mode {
JAVA,
IDL,
BOTH
}

private static final String CONFIG_KEY = "dubbo.service.name.mode";
private static final String LEGACY_KEY = "dubbo.application.use-idl-package-as-service-name";

// Resolves the configured Mode from the environment.
public static Mode getMode(ScopeModel scopeModel) {
String value = ConfigurationUtils.getProperty(
scopeModel, CONFIG_KEY, ConfigurationUtils.getProperty(scopeModel, LEGACY_KEY, "JAVA"));

if (StringUtils.isEmpty(value)) {
return Mode.JAVA;
}

try {
return Mode.valueOf(value.trim().toUpperCase());
} catch (IllegalArgumentException e) {
return Mode.JAVA;
}
}

// Check if IDL service naming should be used.
public static boolean useIdl(ScopeModel scopeModel) {
Mode mode = getMode(scopeModel);
return mode == Mode.IDL || mode == Mode.BOTH;
}
}
Loading