Feature/dubbo3.3 add reserence providedby by springcloud feignclient (#12823)

This commit is contained in:
suncairong163 2023-08-01 15:43:20 +08:00 committed by GitHub
parent ce65317207
commit 6c5a876f31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 120 additions and 6 deletions

View File

@ -132,6 +132,39 @@ public interface AnnotationUtils {
return getAttribute(annotation, "value");
}
/**
* Get the attribute from the specified {@link Annotation annotation}
*
* @param annotation the specified {@link Annotation annotation}
* @param attributeNames the multiply attribute name arrays
* @param <T> the type of attribute
* @return the attribute value
* @throws IllegalArgumentException If the attribute name can't be found
*/
static <T> T getAttribute(Annotation annotation, String... attributeNames) throws IllegalArgumentException {
if (attributeNames == null || attributeNames.length == 0) {
return null;
}
for (String attributeName : attributeNames) {
T attribute = getAttribute(annotation, attributeName);
if (attribute == null) {
continue;
}
// exclude string attribute default is empty
if ((attribute instanceof String) && ((String) attribute).length() == 0) {
continue;
}
return attribute;
}
return null;
}
/**
* Get the {@link Annotation} from the specified {@link AnnotatedElement the annotated element} and
* {@link Annotation annotation} class name

View File

@ -212,6 +212,20 @@ public class ClassUtils {
return forName(name, getClassLoader());
}
/**
* find class and don`t expect to throw exception
* @param name
* @return
*/
public static Class<?> forNameAndTryCatch(String name) {
try {
return forName(name, getClassLoader());
} catch (Throwable e) {
return null;
}
}
/**
* Replacement for <code>Class.forName()</code> that also returns Class
* instances for primitives (like "int") and array class names (like

View File

@ -34,6 +34,7 @@ import org.apache.dubbo.common.utils.UrlUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.dubbo.config.support.Parameter;
import org.apache.dubbo.config.utils.ConfigValidationUtils;
import org.apache.dubbo.config.utils.FeignClientAnnotationUtil;
import org.apache.dubbo.registry.client.metadata.MetadataUtils;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Protocol;
@ -418,6 +419,8 @@ public class ReferenceConfig<T> extends ReferenceConfigBase<T> {
AbstractConfig.appendParameters(map, consumer);
AbstractConfig.appendParameters(map, this);
appendMetricsCompatible(map);
// after interface metadata set
FeignClientAnnotationUtil.appendParametersFromInterfaceClassMetadata(this.interfaceClass,map);
String hostToRegistry = ConfigUtils.getSystemProperty(DUBBO_IP_TO_REGISTRY);
if (StringUtils.isEmpty(hostToRegistry)) {

View File

@ -0,0 +1,64 @@
/*
* 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.utils;
import org.apache.dubbo.common.utils.AnnotationUtils;
import org.apache.dubbo.common.utils.ClassUtils;
import org.apache.dubbo.common.utils.StringUtils;
import java.lang.annotation.Annotation;
import java.util.Map;
import static org.apache.dubbo.common.constants.RegistryConstants.PROVIDED_BY;
public class FeignClientAnnotationUtil {
/**
* append parameters from interface class metadata
* such as FeignClient service as dubbo providedBy
*
* @param paramtetersMap
*/
public static void appendParametersFromInterfaceClassMetadata(Class<?> interfaceClass, Map<String, String> paramtetersMap) {
if (interfaceClass == null) {
return;
}
Class<? extends Annotation> feignClientAnno = (Class<? extends Annotation>) ClassUtils.forNameAndTryCatch("org.springframework.cloud.openfeign.FeignClient");
if (feignClientAnno == null || !AnnotationUtils.isAnnotationPresent(interfaceClass, feignClientAnno)) {
return;
}
Annotation annotation = interfaceClass.getAnnotation(feignClientAnno);
// get feign client service name
String serviceName = AnnotationUtils.getAttribute(annotation, "name", "value");
if (StringUtils.isEmpty(serviceName)) {
return;
}
// append old value
serviceName = paramtetersMap.containsKey(PROVIDED_BY) ? paramtetersMap.get(PROVIDED_BY) + "," + serviceName : serviceName;
// cover old value
paramtetersMap.put(PROVIDED_BY, serviceName);
}
}

View File

@ -34,7 +34,7 @@ public class ServiceDeployerManager {
private static final ConcurrentMap<String, ServiceDeployer> serviceDeployers = new ConcurrentHashMap<>();
public static URL deploy(final URL currentURL, ServiceRestMetadata serviceRestMetadata, Invoker invoker) {
public static URL deploy(URL currentURL, ServiceRestMetadata serviceRestMetadata, Invoker invoker) {
AtomicBoolean isNewCreate = new AtomicBoolean();
@ -52,6 +52,11 @@ public class ServiceDeployerManager {
// register exception mapper
newServiceDeployer.registerExtension(currentURL);
// passing ServiceDeployer to PortUnificationServer through URL
// add attribute for server build
currentURL = currentURL.putAttribute(REST_SERVICE_DEPLOYER_URL_ATTRIBUTE_KEY, newServiceDeployer);
// not new URL
if (!isNewCreate.get()) {
return currentURL;
@ -64,11 +69,6 @@ public class ServiceDeployerManager {
tmp = tmp.addParameter(SERVER_KEY, PORT_UNIFICATION_NETTY4_SERVER);
}
// passing ServiceDeployer to PortUnificationServer through URL
// add attribute for server build
tmp = tmp.putAttribute(REST_SERVICE_DEPLOYER_URL_ATTRIBUTE_KEY, newServiceDeployer);
return tmp;
}