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

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


The following commit(s) were added to refs/heads/master by this push:
     new 23b100775 [type Refactor]extractor Multi-client registration is 
supported (#4790)
23b100775 is described below

commit 23b100775653af0736c7e3e556f68c720d85086e
Author: likeguo <[email protected]>
AuthorDate: Tue Jul 4 09:23:15 2023 +0800

    [type Refactor]extractor Multi-client registration is supported (#4790)
    
    * feature/extractor
    
    * feature/extractor
    
    * feature/extractor
    
    * feature/extractor
    
    * feature/extractor
    
    * feature/extractor
    
    * feature/extractor
    
    * feature/extractor
    
    * Update MultiClientApiBeansExtractorImpl.java
    
    * Update ApiBean.java
---
 .../shenyu/client/core/register/ApiBean.java       | 17 ++++++++++
 .../register/ClientApiRefreshedEventListener.java  | 38 +++++++++++++---------
 .../core/register/extractor/ApiBeansExtractor.java |  6 ----
 .../extractor/BaseAnnotationApiBeansExtractor.java |  2 +-
 .../register/extractor/BaseApiBeansExtractor.java  |  2 +-
 ....java => MultiClientApiBeansExtractorImpl.java} | 38 +++++++++-------------
 ...ansExtractor.java => RpcApiBeansExtractor.java} | 15 +--------
 .../core/register/registrar/ApiRegistrar.java      | 15 ++++++++-
 .../register/SpringCloudApiBeansExtractor.java     |  4 +--
 .../register/SpringMvcApiBeansExtractor.java       |  4 +--
 10 files changed, 76 insertions(+), 65 deletions(-)

diff --git 
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/ApiBean.java
 
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/ApiBean.java
index 1adcabcc9..516686a9e 100644
--- 
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/ApiBean.java
+++ 
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/ApiBean.java
@@ -26,6 +26,7 @@ import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Objects;
 import java.util.Optional;
 import java.util.Properties;
 import java.util.stream.Collectors;
@@ -215,6 +216,22 @@ public class ApiBean {
         this.beanPath = beanPath;
     }
     
+    /**
+     * deep copy.
+     *
+     * @return ApiBean
+     */
+    public ApiBean copy() {
+        final ApiBean copy = new ApiBean(clientName, beanName, beanInstance, 
beanPath);
+        beanProperties.forEach((k, v) -> copy.addProperties(k.toString(), 
Objects.toString(v)));
+        for (ApiDefinition definition : apiDefinitions) {
+            final ApiDefinition newDefinition = new ApiDefinition(this, 
definition.apiMethod, definition.methodPath);
+            definition.apiProperties.forEach((k, v) -> 
newDefinition.addProperties(k.toString(), Objects.toString(v)));
+            copy.apiDefinitions.add(newDefinition);
+        }
+        return copy;
+    }
+    
     /**
      * API corresponds to an accessible interface proxy service, which can be 
an http service, RPC service, TCP service.
      */
diff --git 
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/ClientApiRefreshedEventListener.java
 
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/ClientApiRefreshedEventListener.java
index b3f5d4dc0..9102838f4 100644
--- 
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/ClientApiRefreshedEventListener.java
+++ 
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/ClientApiRefreshedEventListener.java
@@ -19,34 +19,42 @@ package org.apache.shenyu.client.core.register;
 
 import org.apache.shenyu.client.core.register.extractor.ApiBeansExtractor;
 import org.apache.shenyu.client.core.register.registrar.ApiRegistrar;
-import org.springframework.context.ApplicationContext;
+import org.jetbrains.annotations.NotNull;
 import org.springframework.context.ApplicationListener;
 import org.springframework.context.event.ContextRefreshedEvent;
 
 import java.util.List;
+import java.util.stream.Collectors;
 
 public final class ClientApiRefreshedEventListener implements 
ApplicationListener<ContextRefreshedEvent> {
-
+    
     private final List<ApiRegistrar> apiRegistrars;
-
+    
     private final ApiBeansExtractor apiBeanExtractor;
-
+    
     public ClientApiRefreshedEventListener(final List<ApiRegistrar> 
apiRegistrars, final ApiBeansExtractor apiBeanExtractor) {
-
         this.apiBeanExtractor = apiBeanExtractor;
-
         this.apiRegistrars = apiRegistrars;
     }
-
+    
     @Override
     public void onApplicationEvent(final ContextRefreshedEvent event) {
-
-        ApplicationContext applicationContext = event.getApplicationContext();
-
-        List<ApiBean> apiBeans = apiBeanExtractor.extract(applicationContext);
-
-        apiBeans.forEach(apiBean ->
-                apiRegistrars.forEach(registrar -> registrar.register(apiBean))
-        );
+        // Collect all types of RPC client APIs
+        List<ApiBean> apiBeans = 
apiBeanExtractor.extract(event.getApplicationContext());
+        // Register different metadata
+        for (ApiRegistrar registrar : apiRegistrars) {
+            // Optimization point: parallel registration
+            // Each registrar holds a copy of the full API information,
+            // which is not complete and can be modified during the 
registration process
+            
+            registrar.register(copy(apiBeans));
+        }
+    }
+    
+    @NotNull
+    private static List<ApiBean> copy(final List<ApiBean> apiBeans) {
+        return apiBeans.stream()
+                .map(ApiBean::copy)
+                .collect(Collectors.toList());
     }
 }
diff --git 
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/ApiBeansExtractor.java
 
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/ApiBeansExtractor.java
index 8924cdc89..b70c4a29b 100644
--- 
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/ApiBeansExtractor.java
+++ 
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/ApiBeansExtractor.java
@@ -43,10 +43,4 @@ public interface ApiBeansExtractor {
      */
     List<ApiBean> extract(ApplicationContext applicationContext);
     
-    /**
-     * client name.
-     *
-     * @return name
-     */
-    String clientName();
 }
diff --git 
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/BaseAnnotationApiBeansExtractor.java
 
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/BaseAnnotationApiBeansExtractor.java
index 8bd5cd2f8..d89d41b5b 100644
--- 
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/BaseAnnotationApiBeansExtractor.java
+++ 
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/BaseAnnotationApiBeansExtractor.java
@@ -32,7 +32,7 @@ import java.util.stream.Collectors;
  * AnnotationApiBeansExtractor.<br>
  * API extraction converter that supports annotation.
  */
-public abstract class BaseAnnotationApiBeansExtractor extends 
BaseApiBeansExtractor implements ApiBeansExtractor {
+public abstract class BaseAnnotationApiBeansExtractor extends 
BaseApiBeansExtractor implements RpcApiBeansExtractor {
     
     @Override
     protected Map<String, Object> extractSupportBeans(final ApplicationContext 
applicationContext) {
diff --git 
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/BaseApiBeansExtractor.java
 
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/BaseApiBeansExtractor.java
index 7189b2da5..4a6d72415 100644
--- 
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/BaseApiBeansExtractor.java
+++ 
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/BaseApiBeansExtractor.java
@@ -37,7 +37,7 @@ import java.util.stream.Collectors;
  * BaseApiBeansExtractor.<br>
  * Used to demonstrate the basic behavior of the extractor.
  */
-public abstract class BaseApiBeansExtractor implements ApiBeansExtractor {
+public abstract class BaseApiBeansExtractor implements RpcApiBeansExtractor {
     
     protected static final Logger LOG = 
LoggerFactory.getLogger(BaseApiBeansExtractor.class);
     
diff --git 
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/ApiBeansExtractor.java
 
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/MultiClientApiBeansExtractorImpl.java
similarity index 56%
copy from 
shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/ApiBeansExtractor.java
copy to 
shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/MultiClientApiBeansExtractorImpl.java
index 8924cdc89..84d96236e 100644
--- 
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/ApiBeansExtractor.java
+++ 
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/MultiClientApiBeansExtractorImpl.java
@@ -21,32 +21,24 @@ import org.apache.shenyu.client.core.register.ApiBean;
 import org.springframework.context.ApplicationContext;
 
 import java.util.List;
+import java.util.stream.Collectors;
 
 /**
- * Main responsibilities: Get the possible API class classes and corresponding 
methods,
- * and initialize and resolve them Different.<br>
- * <ul>
- *     <li>clients correspond to different implementations
- *     <li>In the Spring web scenario, collect controller
- *     <li> java EE web scenarios, collect servlet path Dubbo
- *     <li> scenarios, and collect Dubbo Service APIs
- *     <li> In other RPC scenarios, collect RPC Service APIs
- * </ul>
+ * MultiClientApiBeansExtractorImpl.
+ * Multi-client collector
  */
-public interface ApiBeansExtractor {
+public class MultiClientApiBeansExtractorImpl implements ApiBeansExtractor {
     
-    /**
-     * Extract apiBeans from applicationContext.
-     *
-     * @param applicationContext applicationContext
-     * @return apiBeans
-     */
-    List<ApiBean> extract(ApplicationContext applicationContext);
+    private final List<RpcApiBeansExtractor> rpcApiBeansExtractors;
     
-    /**
-     * client name.
-     *
-     * @return name
-     */
-    String clientName();
+    public MultiClientApiBeansExtractorImpl(final List<RpcApiBeansExtractor> 
rpcApiBeansExtractors) {
+        this.rpcApiBeansExtractors = rpcApiBeansExtractors;
+    }
+    
+    @Override
+    public List<ApiBean> extract(final ApplicationContext applicationContext) {
+        return rpcApiBeansExtractors.stream()
+                .flatMap(e -> e.extract(applicationContext).stream())
+                .collect(Collectors.toList());
+    }
 }
diff --git 
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/ApiBeansExtractor.java
 
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/RpcApiBeansExtractor.java
similarity index 78%
copy from 
shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/ApiBeansExtractor.java
copy to 
shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/RpcApiBeansExtractor.java
index 8924cdc89..6be0c764b 100644
--- 
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/ApiBeansExtractor.java
+++ 
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/extractor/RpcApiBeansExtractor.java
@@ -17,11 +17,6 @@
 
 package org.apache.shenyu.client.core.register.extractor;
 
-import org.apache.shenyu.client.core.register.ApiBean;
-import org.springframework.context.ApplicationContext;
-
-import java.util.List;
-
 /**
  * Main responsibilities: Get the possible API class classes and corresponding 
methods,
  * and initialize and resolve them Different.<br>
@@ -33,15 +28,7 @@ import java.util.List;
  *     <li> In other RPC scenarios, collect RPC Service APIs
  * </ul>
  */
-public interface ApiBeansExtractor {
-    
-    /**
-     * Extract apiBeans from applicationContext.
-     *
-     * @param applicationContext applicationContext
-     * @return apiBeans
-     */
-    List<ApiBean> extract(ApplicationContext applicationContext);
+public interface RpcApiBeansExtractor extends ApiBeansExtractor {
     
     /**
      * client name.
diff --git 
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/registrar/ApiRegistrar.java
 
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/registrar/ApiRegistrar.java
index baf676b36..7124b6bec 100644
--- 
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/registrar/ApiRegistrar.java
+++ 
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/register/registrar/ApiRegistrar.java
@@ -19,8 +19,21 @@ package org.apache.shenyu.client.core.register.registrar;
 
 import org.apache.shenyu.client.core.register.ApiBean;
 
-public interface ApiRegistrar {
+import java.util.List;
 
+public interface ApiRegistrar {
+    
+    /**
+     * Registers ApiBean.
+     *
+     * @param beans apiBean to register
+     */
+    default void register(List<ApiBean> beans) {
+        for (ApiBean bean : beans) {
+            register(bean);
+        }
+    }
+    
     /**
      * Registers ApiBean.
      *
diff --git 
a/shenyu-client/shenyu-client-http/shenyu-client-springcloud/src/main/java/org/apache/shenyu/client/springcloud/register/SpringCloudApiBeansExtractor.java
 
b/shenyu-client/shenyu-client-http/shenyu-client-springcloud/src/main/java/org/apache/shenyu/client/springcloud/register/SpringCloudApiBeansExtractor.java
index 3153269a3..782d6c98b 100644
--- 
a/shenyu-client/shenyu-client-http/shenyu-client-springcloud/src/main/java/org/apache/shenyu/client/springcloud/register/SpringCloudApiBeansExtractor.java
+++ 
b/shenyu-client/shenyu-client-http/shenyu-client-springcloud/src/main/java/org/apache/shenyu/client/springcloud/register/SpringCloudApiBeansExtractor.java
@@ -20,7 +20,7 @@ package org.apache.shenyu.client.springcloud.register;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.shenyu.client.core.register.ApiBean;
 import 
org.apache.shenyu.client.core.register.extractor.BaseAnnotationApiBeansExtractor;
-import org.apache.shenyu.client.core.register.extractor.ApiBeansExtractor;
+import org.apache.shenyu.client.core.register.extractor.RpcApiBeansExtractor;
 import org.apache.shenyu.common.enums.RpcTypeEnum;
 import org.jetbrains.annotations.NotNull;
 import org.springframework.lang.NonNull;
@@ -36,7 +36,7 @@ import java.util.Objects;
  * Support for Spring Cloud. <br>
  * Should inherit from SpringMvcApiBeansExtractor.
  */
-public class SpringCloudApiBeansExtractor extends 
BaseAnnotationApiBeansExtractor implements ApiBeansExtractor {
+public class SpringCloudApiBeansExtractor extends 
BaseAnnotationApiBeansExtractor implements RpcApiBeansExtractor {
     
     private final List<Class<? extends Annotation>> supportedApiAnnotations = 
new ArrayList<>(1);
     
diff --git 
a/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/register/SpringMvcApiBeansExtractor.java
 
b/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/register/SpringMvcApiBeansExtractor.java
index 9778d49fd..e2b2005a7 100644
--- 
a/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/register/SpringMvcApiBeansExtractor.java
+++ 
b/shenyu-client/shenyu-client-http/shenyu-client-springmvc/src/main/java/org/apache/shenyu/client/springmvc/register/SpringMvcApiBeansExtractor.java
@@ -20,7 +20,7 @@ package org.apache.shenyu.client.springmvc.register;
 import org.apache.commons.lang3.ArrayUtils;
 import org.apache.shenyu.client.core.register.ApiBean;
 import 
org.apache.shenyu.client.core.register.extractor.BaseAnnotationApiBeansExtractor;
-import org.apache.shenyu.client.core.register.extractor.ApiBeansExtractor;
+import org.apache.shenyu.client.core.register.extractor.RpcApiBeansExtractor;
 import org.apache.shenyu.common.enums.RpcTypeEnum;
 import org.jetbrains.annotations.NotNull;
 import org.springframework.lang.NonNull;
@@ -32,7 +32,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.Objects;
 
-public class SpringMvcApiBeansExtractor extends 
BaseAnnotationApiBeansExtractor implements ApiBeansExtractor {
+public class SpringMvcApiBeansExtractor extends 
BaseAnnotationApiBeansExtractor implements RpcApiBeansExtractor {
     
     private final List<Class<? extends Annotation>> supportedApiAnnotations = 
new ArrayList<>(1);
     

Reply via email to