You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by zh...@apache.org on 2022/06/17 17:53:19 UTC

[incubator-shenyu] branch master updated: fix issue #3484,motan client lossless registration (#3577)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new c54de0091 fix issue #3484,motan client lossless registration (#3577)
c54de0091 is described below

commit c54de009106285978737714803258b87f881944d
Author: ShawnSiao <xi...@qq.com>
AuthorDate: Sat Jun 18 01:53:12 2022 +0800

    fix issue #3484,motan client lossless registration (#3577)
    
    * fix issue #3484,motan client lossless registration
---
 ...ocessor.java => MotanServiceEventListener.java} | 35 +++++++---------------
 .../motan/ShenyuMotanClientConfiguration.java      | 12 ++++----
 .../motan/ShenyuMotanClientConfigurationTest.java  |  8 ++---
 3 files changed, 21 insertions(+), 34 deletions(-)

diff --git a/shenyu-client/shenyu-client-motan/src/main/java/org/apache/shenyu/client/motan/MotanServiceBeanPostProcessor.java b/shenyu-client/shenyu-client-motan/src/main/java/org/apache/shenyu/client/motan/MotanServiceEventListener.java
similarity index 87%
rename from shenyu-client/shenyu-client-motan/src/main/java/org/apache/shenyu/client/motan/MotanServiceBeanPostProcessor.java
rename to shenyu-client/shenyu-client-motan/src/main/java/org/apache/shenyu/client/motan/MotanServiceEventListener.java
index 9142e12d4..496fc4e2d 100644
--- a/shenyu-client/shenyu-client-motan/src/main/java/org/apache/shenyu/client/motan/MotanServiceBeanPostProcessor.java
+++ b/shenyu-client/shenyu-client-motan/src/main/java/org/apache/shenyu/client/motan/MotanServiceEventListener.java
@@ -17,7 +17,6 @@
 
 package org.apache.shenyu.client.motan;
 
-import com.google.common.util.concurrent.ThreadFactoryBuilder;
 import com.weibo.api.motan.config.springsupport.BasicServiceConfigBean;
 import com.weibo.api.motan.config.springsupport.annotation.MotanService;
 import org.apache.commons.lang3.StringUtils;
@@ -35,9 +34,9 @@ import org.apache.shenyu.register.common.config.PropertiesConfig;
 import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
 import org.springframework.aop.support.AopUtils;
 import org.springframework.beans.BeansException;
-import org.springframework.beans.factory.config.BeanPostProcessor;
 import org.springframework.context.ApplicationContext;
-import org.springframework.context.ApplicationContextAware;
+import org.springframework.context.ApplicationListener;
+import org.springframework.context.event.ContextRefreshedEvent;
 import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
 import org.springframework.core.annotation.AnnotatedElementUtils;
 import org.springframework.lang.NonNull;
@@ -47,17 +46,16 @@ import java.lang.reflect.Method;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
 import java.util.Properties;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
 import java.util.stream.Collectors;
 
 /**
- * Motan BeanPostProcessor.
+ * Motan Service Event Listener.
  */
-public class MotanServiceBeanPostProcessor implements BeanPostProcessor, ApplicationContextAware {
+public class MotanServiceEventListener implements ApplicationListener<ContextRefreshedEvent> {
 
     private static final String BASE_SERVICE_CONFIG = "baseServiceConfig";
 
@@ -69,8 +67,6 @@ public class MotanServiceBeanPostProcessor implements BeanPostProcessor, Applica
 
     private ApplicationContext applicationContext;
 
-    private final ExecutorService executorService;
-
     private final String contextPath;
 
     private final String appName;
@@ -83,7 +79,7 @@ public class MotanServiceBeanPostProcessor implements BeanPostProcessor, Applica
 
     private Integer timeout;
 
-    public MotanServiceBeanPostProcessor(final PropertiesConfig clientConfig, final ShenyuClientRegisterRepository shenyuClientRegisterRepository) {
+    public MotanServiceEventListener(final PropertiesConfig clientConfig, final ShenyuClientRegisterRepository shenyuClientRegisterRepository) {
         Properties props = clientConfig.getProps();
         String contextPath = props.getProperty(ShenyuClientConstants.CONTEXT_PATH);
         String appName = props.getProperty(ShenyuClientConstants.APP_NAME);
@@ -94,21 +90,16 @@ public class MotanServiceBeanPostProcessor implements BeanPostProcessor, Applica
         this.appName = appName;
         this.host = props.getProperty(ShenyuClientConstants.HOST);
         this.port = props.getProperty(ShenyuClientConstants.PORT);
-        executorService = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("shenyu-motan-client-thread-pool-%d").build());
         publisher.start(shenyuClientRegisterRepository);
     }
 
     @Override
-    public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException {
-        Class<?> clazz = bean.getClass();
-        if (AopUtils.isAopProxy(bean)) {
-            clazz = AopUtils.getTargetClass(bean);
+    public void onApplicationEvent(final ContextRefreshedEvent contextRefreshedEvent) throws BeansException {
+        applicationContext = contextRefreshedEvent.getApplicationContext();
+        Map<String, Object> beans = applicationContext.getBeansWithAnnotation(ShenyuMotanClient.class);
+        for (Map.Entry<String, Object> entry : beans.entrySet()) {
+            handler(entry.getValue());
         }
-        MotanService service = clazz.getAnnotation(MotanService.class);
-        if (service != null) {
-            executorService.execute(() -> handler(bean));
-        }
-        return bean;
     }
 
     private void handler(final Object bean) {
@@ -223,8 +214,4 @@ public class MotanServiceBeanPostProcessor implements BeanPostProcessor, Applica
         return result.toString();
     }
 
-    @Override
-    public void setApplicationContext(final ApplicationContext applicationContext) throws BeansException {
-        this.applicationContext = applicationContext;
-    }
 }
diff --git a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-motan/src/main/java/org/apache/shenyu/springboot/starter/client/motan/ShenyuMotanClientConfiguration.java b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-motan/src/main/java/org/apache/shenyu/springboot/starter/client/motan/ShenyuMotanClientConfiguration.java
index 6ba52352a..e054284e2 100644
--- a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-motan/src/main/java/org/apache/shenyu/springboot/starter/client/motan/ShenyuMotanClientConfiguration.java
+++ b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-motan/src/main/java/org/apache/shenyu/springboot/starter/client/motan/ShenyuMotanClientConfiguration.java
@@ -18,7 +18,7 @@
 
 package org.apache.shenyu.springboot.starter.client.motan;
 
-import org.apache.shenyu.client.motan.MotanServiceBeanPostProcessor;
+import org.apache.shenyu.client.motan.MotanServiceEventListener;
 import org.apache.shenyu.common.enums.RpcTypeEnum;
 import org.apache.shenyu.register.client.api.ShenyuClientRegisterRepository;
 import org.apache.shenyu.register.common.config.ShenyuClientConfig;
@@ -28,21 +28,21 @@ import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Configuration;
 
 /**
- * Motan type client bean postprocessor.
+ * Motan type client event listener.
  */
 @Configuration
 @ImportAutoConfiguration(ShenyuClientCommonBeanConfiguration.class)
 public class ShenyuMotanClientConfiguration {
 
     /**
-     * Motan service bean post processor.
+     * Motan service event listener.
      *
      * @param clientConfig the client config
      * @param shenyuClientRegisterRepository the shenyuClientRegisterRepository
-     * @return the motan service bean post processor
+     * @return the motan service event listener
      */
     @Bean
-    public MotanServiceBeanPostProcessor motanServiceBeanPostProcessor(final ShenyuClientConfig clientConfig, final ShenyuClientRegisterRepository shenyuClientRegisterRepository) {
-        return new MotanServiceBeanPostProcessor(clientConfig.getClient().get(RpcTypeEnum.MOTAN.getName()), shenyuClientRegisterRepository);
+    public MotanServiceEventListener motanServiceEventListener(final ShenyuClientConfig clientConfig, final ShenyuClientRegisterRepository shenyuClientRegisterRepository) {
+        return new MotanServiceEventListener(clientConfig.getClient().get(RpcTypeEnum.MOTAN.getName()), shenyuClientRegisterRepository);
     }
 }
diff --git a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-motan/src/test/java/org/apache/shenyu/springboot/starter/client/motan/ShenyuMotanClientConfigurationTest.java b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-motan/src/test/java/org/apache/shenyu/springboot/starter/client/motan/ShenyuMotanClientConfigurationTest.java
index 50f952eeb..6db754611 100644
--- a/shenyu-spring-boot-starter/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-motan/src/test/java/org/apache/shenyu/springboot/starter/client/motan/ShenyuMotanClientConfigurationTest.java
+++ b/shenyu-spring-boot-starter/shenyu-spring-boot-starter-client/shenyu-spring-boot-starter-client-motan/src/test/java/org/apache/shenyu/springboot/starter/client/motan/ShenyuMotanClientConfigurationTest.java
@@ -18,7 +18,7 @@
 
 package org.apache.shenyu.springboot.starter.client.motan;
 
-import org.apache.shenyu.client.motan.MotanServiceBeanPostProcessor;
+import org.apache.shenyu.client.motan.MotanServiceEventListener;
 import org.apache.shenyu.register.client.http.utils.RegisterUtils;
 import org.junit.jupiter.api.Test;
 import org.mockito.MockedStatic;
@@ -41,7 +41,7 @@ import static org.mockito.Mockito.mockStatic;
 public class ShenyuMotanClientConfigurationTest {
 
     @Test
-    public void testMotanServiceBeanPostProcessor() {
+    public void testMotanServiceEventListener() {
         MockedStatic<RegisterUtils> registerUtilsMockedStatic = mockStatic(RegisterUtils.class);
         registerUtilsMockedStatic.when(() -> RegisterUtils.doLogin(any(), any(), any())).thenReturn(Optional.ofNullable("token"));
         new ApplicationContextRunner()
@@ -59,8 +59,8 @@ public class ShenyuMotanClientConfigurationTest {
                 "shenyu.client.motan.props[port]=8081"
             )
             .run(context -> {
-                MotanServiceBeanPostProcessor processor = context.getBean("motanServiceBeanPostProcessor", MotanServiceBeanPostProcessor.class);
-                assertNotNull(processor);
+                MotanServiceEventListener motanServiceEventListener = context.getBean("motanServiceEventListener", MotanServiceEventListener.class);
+                assertNotNull(motanServiceEventListener);
             });
         registerUtilsMockedStatic.close();
     }