You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by me...@apache.org on 2020/04/28 03:25:17 UTC

[dubbo] branch master updated: @Reference和@Service Naming issues (#6074)

This is an automated email from the ASF dual-hosted git repository.

mercyblitz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new 78aeadc  @Reference和@Service Naming issues (#6074)
78aeadc is described below

commit 78aeadc48348f5b36862db6a16adf8226d23a2f4
Author: Mercy Ma <me...@gmail.com>
AuthorDate: Tue Apr 28 11:25:04 2020 +0800

    @Reference和@Service Naming issues (#6074)
    
    * Polish apache/dubbo#6007 : [Discussion] @Reference和@Service Naming issues
    
    * Polish apache/dubbo#6007 : [Discussion] @Reference和@Service Naming issues
    
    * Polish apache/dubbo#6007 : [Discussion] @Reference和@Service Naming issues
---
 .../{Reference.java => DubboReference.java}        | 24 +++++++--------
 .../annotation/{Service.java => DubboService.java} | 11 ++++---
 .../apache/dubbo/config/annotation/Reference.java  | 21 +++++++------
 .../apache/dubbo/config/annotation/Service.java    |  8 +++--
 .../dubbo/common/utils/AnnotationUtilsTest.java    | 20 ++++++------
 .../ReferenceAnnotationBeanPostProcessor.java      | 10 ++++--
 .../annotation/ServiceClassPostProcessor.java      | 36 +++++++++++++---------
 .../consumer/test/TestConsumerConfiguration.java   |  5 +--
 .../annotation/provider/DefaultHelloService.java   |  3 +-
 9 files changed, 82 insertions(+), 56 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/annotation/Reference.java b/dubbo-common/src/main/java/org/apache/dubbo/config/annotation/DubboReference.java
similarity index 97%
copy from dubbo-common/src/main/java/org/apache/dubbo/config/annotation/Reference.java
copy to dubbo-common/src/main/java/org/apache/dubbo/config/annotation/DubboReference.java
index 4fd1a3f..985a4d9 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/config/annotation/Reference.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/config/annotation/DubboReference.java
@@ -23,15 +23,15 @@ import java.lang.annotation.RetentionPolicy;
 import java.lang.annotation.Target;
 
 /**
- * Reference
+ * An annotation used for referencing a Dubbo service
  *
- * @export
- * @since 2.7.0
+ * @since 2.7.7
  */
 @Documented
 @Retention(RetentionPolicy.RUNTIME)
 @Target({ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
-public @interface Reference {
+public @interface DubboReference {
+
     /**
      * Interface class, default value is void.class
      */
@@ -89,7 +89,7 @@ public @interface Reference {
 
     /**
      * Export an stub service for event dispatch, default value is false.
-     *
+     * <p>
      * see org.apache.dubbo.rpc.Constants#STUB_EVENT_METHODS_KEY
      */
     boolean stubevent() default false;
@@ -97,14 +97,14 @@ public @interface Reference {
     /**
      * Whether to reconnect if connection is lost, if not specify, reconnect is enabled by default, and the interval
      * for retry connecting is 2000 ms
-     *
+     * <p>
      * see org.apache.dubbo.remoting.Constants#DEFAULT_RECONNECT_PERIOD
      */
     String reconnect() default "";
 
     /**
      * Whether to stick to the same node in the cluster, the default value is false
-     *
+     * <p>
      * see Constants#DEFAULT_CLUSTER_STICKY
      */
     boolean sticky() default false;
@@ -131,7 +131,7 @@ public @interface Reference {
 
     /**
      * The callback instance limit peer connection
-     *
+     * <p>
      * see org.apache.dubbo.rpc.Constants#DEFAULT_CALLBACK_INSTANCES
      */
     int callbacks() default 0;
@@ -158,14 +158,14 @@ public @interface Reference {
 
     /**
      * Service invocation retry times
-     *
+     * <p>
      * see Constants#DEFAULT_RETRIES
      */
     int retries() default 2;
 
     /**
      * Load balance strategy, legal values include: random, roundrobin, leastactive
-     *
+     * <p>
      * see Constants#DEFAULT_LOADBALANCE
      */
     String loadbalance() default "";
@@ -207,14 +207,14 @@ public @interface Reference {
 
     /**
      * Filters for service invocation
-     *
+     * <p>
      * see Filter
      */
     String[] filter() default {};
 
     /**
      * Listeners for service exporting and unexporting
-     *
+     * <p>
      * see ExporterListener
      */
     String[] listener() default {};
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/annotation/Service.java b/dubbo-common/src/main/java/org/apache/dubbo/config/annotation/DubboService.java
similarity index 97%
copy from dubbo-common/src/main/java/org/apache/dubbo/config/annotation/Service.java
copy to dubbo-common/src/main/java/org/apache/dubbo/config/annotation/DubboService.java
index 425814d..3b27d04 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/config/annotation/Service.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/config/annotation/DubboService.java
@@ -28,15 +28,15 @@ import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_LOADBALA
 import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_RETRIES;
 
 /**
- * Service annotation
+ * Class-level annotation used for declaring Dubbo service
  *
- * @export
+ * @since 2.7.7
  */
 @Documented
 @Retention(RetentionPolicy.RUNTIME)
 @Target({ElementType.TYPE})
 @Inherited
-public @interface Service {
+public @interface DubboService {
 
     /**
      * Interface class, default value is void.class
@@ -114,7 +114,7 @@ public @interface Service {
     int delay() default 0;
 
     /**
-     * @see Service#stub()
+     * @see DubboService#stub()
      * @deprecated
      */
     String local() default "";
@@ -141,7 +141,7 @@ public @interface Service {
 
     /**
      * The callback instance limit peer connection
-     *
+     * <p>
      * see org.apache.dubbo.rpc.Constants#DEFAULT_CALLBACK_INSTANCES
      */
     int callbacks() default org.apache.dubbo.common.constants.CommonConstants.DEFAULT_CALLBACK_INSTANCES;
@@ -271,6 +271,7 @@ public @interface Service {
 
     /**
      * methods support
+     *
      * @return
      */
     Method[] methods() default {};
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/annotation/Reference.java b/dubbo-common/src/main/java/org/apache/dubbo/config/annotation/Reference.java
index 4fd1a3f..abb250f 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/config/annotation/Reference.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/config/annotation/Reference.java
@@ -24,13 +24,16 @@ import java.lang.annotation.Target;
 
 /**
  * Reference
+ * <p>
  *
- * @export
+ * @see DubboReference
  * @since 2.7.0
+ * @deprecated Recommend {@link DubboReference} as the substitute
  */
 @Documented
 @Retention(RetentionPolicy.RUNTIME)
 @Target({ElementType.FIELD, ElementType.METHOD, ElementType.ANNOTATION_TYPE})
+@Deprecated
 public @interface Reference {
     /**
      * Interface class, default value is void.class
@@ -89,7 +92,7 @@ public @interface Reference {
 
     /**
      * Export an stub service for event dispatch, default value is false.
-     *
+     * <p>
      * see org.apache.dubbo.rpc.Constants#STUB_EVENT_METHODS_KEY
      */
     boolean stubevent() default false;
@@ -97,14 +100,14 @@ public @interface Reference {
     /**
      * Whether to reconnect if connection is lost, if not specify, reconnect is enabled by default, and the interval
      * for retry connecting is 2000 ms
-     *
+     * <p>
      * see org.apache.dubbo.remoting.Constants#DEFAULT_RECONNECT_PERIOD
      */
     String reconnect() default "";
 
     /**
      * Whether to stick to the same node in the cluster, the default value is false
-     *
+     * <p>
      * see Constants#DEFAULT_CLUSTER_STICKY
      */
     boolean sticky() default false;
@@ -131,7 +134,7 @@ public @interface Reference {
 
     /**
      * The callback instance limit peer connection
-     *
+     * <p>
      * see org.apache.dubbo.rpc.Constants#DEFAULT_CALLBACK_INSTANCES
      */
     int callbacks() default 0;
@@ -158,14 +161,14 @@ public @interface Reference {
 
     /**
      * Service invocation retry times
-     *
+     * <p>
      * see Constants#DEFAULT_RETRIES
      */
     int retries() default 2;
 
     /**
      * Load balance strategy, legal values include: random, roundrobin, leastactive
-     *
+     * <p>
      * see Constants#DEFAULT_LOADBALANCE
      */
     String loadbalance() default "";
@@ -207,14 +210,14 @@ public @interface Reference {
 
     /**
      * Filters for service invocation
-     *
+     * <p>
      * see Filter
      */
     String[] filter() default {};
 
     /**
      * Listeners for service exporting and unexporting
-     *
+     * <p>
      * see ExporterListener
      */
     String[] listener() default {};
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/annotation/Service.java b/dubbo-common/src/main/java/org/apache/dubbo/config/annotation/Service.java
index 425814d..e46e075 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/config/annotation/Service.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/config/annotation/Service.java
@@ -30,12 +30,15 @@ import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_RETRIES;
 /**
  * Service annotation
  *
- * @export
+ * @see DubboService
+ * @since 2.7.0
+ * @deprecated Recommend {@link DubboService} as the substitute
  */
 @Documented
 @Retention(RetentionPolicy.RUNTIME)
 @Target({ElementType.TYPE})
 @Inherited
+@Deprecated
 public @interface Service {
 
     /**
@@ -141,7 +144,7 @@ public @interface Service {
 
     /**
      * The callback instance limit peer connection
-     *
+     * <p>
      * see org.apache.dubbo.rpc.Constants#DEFAULT_CALLBACK_INSTANCES
      */
     int callbacks() default org.apache.dubbo.common.constants.CommonConstants.DEFAULT_CALLBACK_INSTANCES;
@@ -271,6 +274,7 @@ public @interface Service {
 
     /**
      * methods support
+     *
      * @return
      */
     Method[] methods() default {};
diff --git a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/AnnotationUtilsTest.java b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/AnnotationUtilsTest.java
index 0401763..86dff96 100644
--- a/dubbo-common/src/test/java/org/apache/dubbo/common/utils/AnnotationUtilsTest.java
+++ b/dubbo-common/src/test/java/org/apache/dubbo/common/utils/AnnotationUtilsTest.java
@@ -17,6 +17,7 @@
 package org.apache.dubbo.common.utils;
 
 import org.apache.dubbo.common.extension.Adaptive;
+import org.apache.dubbo.config.annotation.DubboService;
 import org.apache.dubbo.config.annotation.Service;
 
 import org.junit.jupiter.api.Test;
@@ -148,8 +149,9 @@ public class AnnotationUtilsTest {
         assertEquals(Inherited.class, metaAnnotations.get(0).annotationType());
 
         metaAnnotations = getMetaAnnotations(Service.class);
-        assertEquals(1, metaAnnotations.size());
+        assertEquals(2, metaAnnotations.size());
         assertEquals(Inherited.class, metaAnnotations.get(0).annotationType());
+        assertEquals(Deprecated.class, metaAnnotations.get(1).annotationType());
     }
 
     @Test
@@ -164,7 +166,7 @@ public class AnnotationUtilsTest {
         assertEquals(Inherited.class, metaAnnotations.get(offset++).annotationType());
         assertEquals(Service2.class, metaAnnotations.get(offset++).annotationType());
         assertEquals(Inherited.class, metaAnnotations.get(offset++).annotationType());
-        assertEquals(Service.class, metaAnnotations.get(offset++).annotationType());
+        assertEquals(DubboService.class, metaAnnotations.get(offset++).annotationType());
         assertEquals(Inherited.class, metaAnnotations.get(offset++).annotationType());
 
         metaAnnotations = getAllMetaAnnotations(MyAdaptive.class);
@@ -218,14 +220,14 @@ public class AnnotationUtilsTest {
 
     @Test
     public void testFindMetaAnnotations() {
-        List<Service> services = findMetaAnnotations(B.class, Service.class);
+        List<DubboService> services = findMetaAnnotations(B.class, DubboService.class);
         assertEquals(1, services.size());
 
-        Service service = services.get(0);
+        DubboService service = services.get(0);
         assertEquals("", service.interfaceName());
         assertEquals(Cloneable.class, service.interfaceClass());
 
-        services = findMetaAnnotations(Service5.class, Service.class);
+        services = findMetaAnnotations(Service5.class, DubboService.class);
         assertEquals(1, services.size());
 
         service = services.get(0);
@@ -235,13 +237,13 @@ public class AnnotationUtilsTest {
 
     @Test
     public void testFindMetaAnnotation() {
-        Service service = findMetaAnnotation(B.class, Service.class);
+        DubboService service = findMetaAnnotation(B.class, DubboService.class);
         assertEquals(Cloneable.class, service.interfaceClass());
 
-        service = findMetaAnnotation(B.class, "org.apache.dubbo.config.annotation.Service");
+        service = findMetaAnnotation(B.class, "org.apache.dubbo.config.annotation.DubboService");
         assertEquals(Cloneable.class, service.interfaceClass());
 
-        service = findMetaAnnotation(Service5.class, Service.class);
+        service = findMetaAnnotation(Service5.class, DubboService.class);
         assertEquals(Cloneable.class, service.interfaceClass());
     }
 
@@ -262,7 +264,7 @@ public class AnnotationUtilsTest {
     @Retention(RetentionPolicy.RUNTIME)
     @Target({ElementType.TYPE})
     @Inherited
-    @Service(interfaceClass = Cloneable.class)
+    @DubboService(interfaceClass = Cloneable.class)
     @interface Service2 {
 
 
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java
index 3fea6ec..05c8139 100644
--- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java
+++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ReferenceAnnotationBeanPostProcessor.java
@@ -16,6 +16,7 @@
  */
 package org.apache.dubbo.config.spring.beans.factory.annotation;
 
+import org.apache.dubbo.config.annotation.DubboReference;
 import org.apache.dubbo.config.annotation.Reference;
 import org.apache.dubbo.config.annotation.Service;
 import org.apache.dubbo.config.spring.ReferenceBean;
@@ -53,6 +54,9 @@ import static org.springframework.util.StringUtils.hasText;
  * {@link org.springframework.beans.factory.config.BeanPostProcessor} implementation
  * that Consumer service {@link Reference} annotated fields
  *
+ * @see DubboReference
+ * @see Reference
+ * @see com.alibaba.dubbo.config.annotation.Reference
  * @since 2.5.7
  */
 public class ReferenceAnnotationBeanPostProcessor extends AbstractAnnotationBeanPostProcessor implements
@@ -83,10 +87,12 @@ public class ReferenceAnnotationBeanPostProcessor extends AbstractAnnotationBean
     private ApplicationContext applicationContext;
 
     /**
-     * To support the legacy annotation that is @com.alibaba.dubbo.config.annotation.Reference since 2.7.3
+     * {@link com.alibaba.dubbo.config.annotation.Reference @com.alibaba.dubbo.config.annotation.Reference} has been supported since 2.7.3
+     * <p>
+     * {@link DubboReference @DubboReference} has been supported since 2.7.7
      */
     public ReferenceAnnotationBeanPostProcessor() {
-        super(Reference.class, com.alibaba.dubbo.config.annotation.Reference.class);
+        super(DubboReference.class, Reference.class, com.alibaba.dubbo.config.annotation.Reference.class);
     }
 
     /**
diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceClassPostProcessor.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceClassPostProcessor.java
index acda5f0..ed73be4 100644
--- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceClassPostProcessor.java
+++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/beans/factory/annotation/ServiceClassPostProcessor.java
@@ -20,6 +20,7 @@ import org.apache.dubbo.common.logger.Logger;
 import org.apache.dubbo.common.logger.LoggerFactory;
 import org.apache.dubbo.common.utils.ArrayUtils;
 import org.apache.dubbo.config.MethodConfig;
+import org.apache.dubbo.config.annotation.DubboService;
 import org.apache.dubbo.config.annotation.Method;
 import org.apache.dubbo.config.annotation.Service;
 import org.apache.dubbo.config.spring.ServiceBean;
@@ -64,6 +65,7 @@ import java.util.HashMap;
 import java.util.LinkedHashSet;
 import java.util.List;
 import java.util.Map;
+import java.util.Objects;
 import java.util.Set;
 
 import static com.alibaba.spring.util.AnnotatedBeanDefinitionRegistryUtils.registerBeans;
@@ -88,10 +90,20 @@ import static org.springframework.util.ClassUtils.resolveClassName;
 public class ServiceClassPostProcessor implements BeanDefinitionRegistryPostProcessor, EnvironmentAware,
         ResourceLoaderAware, BeanClassLoaderAware {
 
+    private final static List<Class<? extends Annotation>> serviceAnnotationTypes = asList(
+            // @since 2.7.7 Add the @DubboService , the issue : https://github.com/apache/dubbo/issues/6007
+            DubboService.class,
+            // @since 2.7.0 the substitute @com.alibaba.dubbo.config.annotation.Service
+            Service.class,
+            // @since 2.7.3 Add the compatibility for legacy Dubbo's @Service , the issue : https://github.com/apache/dubbo/issues/4330
+            com.alibaba.dubbo.config.annotation.Service.class
+    );
+
     private final Logger logger = LoggerFactory.getLogger(getClass());
 
     protected final Set<String> packagesToScan;
 
+
     private Environment environment;
 
     private ResourceLoader resourceLoader;
@@ -143,15 +155,10 @@ public class ServiceClassPostProcessor implements BeanDefinitionRegistryPostProc
 
         scanner.setBeanNameGenerator(beanNameGenerator);
 
-        scanner.addIncludeFilter(new AnnotationTypeFilter(Service.class));
-
-        /**
-         * Add the compatibility for legacy Dubbo's @Service
-         *
-         * The issue : https://github.com/apache/dubbo/issues/4330
-         * @since 2.7.3
-         */
-        scanner.addIncludeFilter(new AnnotationTypeFilter(com.alibaba.dubbo.config.annotation.Service.class));
+        // refactor @since 2.7.7
+        serviceAnnotationTypes.forEach(annotationType -> {
+            scanner.addIncludeFilter(new AnnotationTypeFilter(annotationType));
+        });
 
         for (String packageToScan : packagesToScan) {
 
@@ -316,11 +323,12 @@ public class ServiceClassPostProcessor implements BeanDefinitionRegistryPostProc
      * @since 2.7.3
      */
     private Annotation findServiceAnnotation(Class<?> beanClass) {
-        Annotation service = findMergedAnnotation(beanClass, Service.class);
-        if (service == null) {
-            service = findMergedAnnotation(beanClass, com.alibaba.dubbo.config.annotation.Service.class);
-        }
-        return service;
+        return serviceAnnotationTypes
+                .stream()
+                .map(annotationType -> findMergedAnnotation(beanClass, annotationType))
+                .filter(Objects::nonNull)
+                .findFirst()
+                .orElse(null);
     }
 
     /**
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/consumer/test/TestConsumerConfiguration.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/consumer/test/TestConsumerConfiguration.java
index 4f67396..b5b0aa5 100644
--- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/consumer/test/TestConsumerConfiguration.java
+++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/consumer/test/TestConsumerConfiguration.java
@@ -16,6 +16,7 @@
  */
 package org.apache.dubbo.config.spring.context.annotation.consumer.test;
 
+import org.apache.dubbo.config.annotation.DubboReference;
 import org.apache.dubbo.config.annotation.Reference;
 import org.apache.dubbo.config.spring.api.DemoService;
 import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
@@ -63,7 +64,7 @@ public class TestConsumerConfiguration {
 
     public static abstract class Ancestor {
 
-        @Reference(version = "2.5.7", url = remoteURL, application = "dubbo-demo-application")
+        @DubboReference(version = "2.5.7", url = remoteURL, application = "dubbo-demo-application")
         private DemoService demoServiceFromAncestor;
 
         public DemoService getDemoServiceFromAncestor() {
@@ -83,7 +84,7 @@ public class TestConsumerConfiguration {
             return demoServiceFromParent;
         }
 
-        @Reference(version = "2.5.7", url = remoteURL, application = "dubbo-demo-application")
+        @com.alibaba.dubbo.config.annotation.Reference(version = "2.5.7", url = remoteURL, application = "dubbo-demo-application")
         public void setDemoServiceFromParent(DemoService demoServiceFromParent) {
             this.demoServiceFromParent = demoServiceFromParent;
         }
diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/DefaultHelloService.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/DefaultHelloService.java
index f792364..0384baf 100644
--- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/DefaultHelloService.java
+++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/context/annotation/provider/DefaultHelloService.java
@@ -16,6 +16,7 @@
  */
 package org.apache.dubbo.config.spring.context.annotation.provider;
 
+import org.apache.dubbo.config.annotation.DubboService;
 import org.apache.dubbo.config.spring.api.HelloService;
 
 import org.springframework.stereotype.Service;
@@ -27,7 +28,7 @@ import org.springframework.stereotype.Service;
  * @since TODO
  */
 @Service
-@org.apache.dubbo.config.annotation.Service
+@DubboService
 public class DefaultHelloService implements HelloService {
 
     @Override