diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboLifecycleComponentsApplicationListener.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboLifecycleComponentApplicationListener.java similarity index 97% rename from dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboLifecycleComponentsApplicationListener.java rename to dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboLifecycleComponentApplicationListener.java index 19365d7c572..11dd51c11e3 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboLifecycleComponentsApplicationListener.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/DubboLifecycleComponentApplicationListener.java @@ -42,7 +42,7 @@ * @see SmartApplicationListener * @since 2.7.4 */ -public class DubboLifecycleComponentsApplicationListener implements ApplicationListener { +public class DubboLifecycleComponentApplicationListener implements ApplicationListener { private List lifecycleComponents = emptyList(); diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboLifecycleComponentRegistrar.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboLifecycleComponentRegistrar.java new file mode 100644 index 00000000000..5c7cab41565 --- /dev/null +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/DubboLifecycleComponentRegistrar.java @@ -0,0 +1,39 @@ +/* + * 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.spring.context.annotation; + +import org.apache.dubbo.common.context.Lifecycle; +import org.apache.dubbo.config.spring.context.DubboLifecycleComponentApplicationListener; + +import org.springframework.beans.factory.support.BeanDefinitionRegistry; +import org.springframework.context.annotation.ImportBeanDefinitionRegistrar; +import org.springframework.core.type.AnnotationMetadata; + +import static org.apache.dubbo.config.spring.util.AnnotatedBeanDefinitionRegistryUtils.registerBeans; + +/** + * A {@link ImportBeanDefinitionRegistrar register} for the {@link Lifecycle Dubbo Lifecycle} components + * + * @since 2.7.4 + */ +public class DubboLifecycleComponentRegistrar implements ImportBeanDefinitionRegistrar { + + @Override + public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) { + registerBeans(registry, DubboLifecycleComponentApplicationListener.class); + } +} diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboLifecycle.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboLifecycle.java index 0abd18e405b..74ca3f3c6a7 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboLifecycle.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/context/annotation/EnableDubboLifecycle.java @@ -16,8 +16,6 @@ */ package org.apache.dubbo.config.spring.context.annotation; -import org.apache.dubbo.config.spring.context.DubboLifecycleComponentsApplicationListener; - import org.springframework.context.Lifecycle; import org.springframework.context.annotation.Import; @@ -37,6 +35,6 @@ @Retention(RetentionPolicy.RUNTIME) @Inherited @Documented -@Import(DubboLifecycleComponentsApplicationListener.class) +@Import(DubboLifecycleComponentRegistrar.class) public @interface EnableDubboLifecycle { } diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/DubboNamespaceHandler.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/DubboNamespaceHandler.java index ddafd031586..4f3f086c180 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/DubboNamespaceHandler.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/schema/DubboNamespaceHandler.java @@ -30,6 +30,8 @@ import org.apache.dubbo.config.spring.ReferenceBean; import org.apache.dubbo.config.spring.ServiceBean; import org.apache.dubbo.config.spring.beans.factory.config.ConfigurableSourceBeanMetadataElement; +import org.apache.dubbo.config.spring.context.DubboLifecycleComponentApplicationListener; +import org.apache.dubbo.config.spring.util.AnnotatedBeanDefinitionRegistryUtils; import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionRegistry; @@ -38,6 +40,8 @@ import org.springframework.context.annotation.AnnotationConfigUtils; import org.w3c.dom.Element; +import static org.apache.dubbo.config.spring.util.AnnotatedBeanDefinitionRegistryUtils.registerBeans; + /** * DubboNamespaceHandler * @@ -76,18 +80,34 @@ public void init() { */ @Override public BeanDefinition parse(Element element, ParserContext parserContext) { - registerAnnotationConfigProcessors(parserContext); + BeanDefinitionRegistry registry = parserContext.getRegistry(); + registerAnnotationConfigProcessors(registry); + registerDubboLifecycleComponentApplicationListener(registry); BeanDefinition beanDefinition = super.parse(element, parserContext); setSource(beanDefinition); return beanDefinition; } /** - * @param parserContext {@link ParserContext} + * Register {@link DubboLifecycleComponentApplicationListener} as a Spring Bean + * + * @param registry {@link BeanDefinitionRegistry} + * @see DubboLifecycleComponentApplicationListener + * @see AnnotatedBeanDefinitionRegistryUtils#registerBeans(BeanDefinitionRegistry, Class[]) * @since 2.7.4 */ - private void registerAnnotationConfigProcessors(ParserContext parserContext) { - BeanDefinitionRegistry registry = parserContext.getRegistry(); + private void registerDubboLifecycleComponentApplicationListener(BeanDefinitionRegistry registry) { + registerBeans(registry, DubboLifecycleComponentApplicationListener.class); + } + + /** + * Register the processors for the Spring Annotation-Driven features + * + * @param registry {@link BeanDefinitionRegistry} + * @see AnnotationConfigUtils + * @since 2.7.4 + */ + private void registerAnnotationConfigProcessors(BeanDefinitionRegistry registry) { AnnotationConfigUtils.registerAnnotationConfigProcessors(registry); } } diff --git a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtils.java b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtils.java index 3753b61d461..fffda69d802 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtils.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtils.java @@ -26,9 +26,10 @@ import org.springframework.util.ObjectUtils; import java.lang.annotation.Annotation; -import java.util.ArrayList; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.Objects; +import java.util.Set; import static java.lang.String.format; import static java.util.Arrays.asList; @@ -94,8 +95,10 @@ public static void registerBeans(BeanDefinitionRegistry registry, Class... an return; } + Set> classesToRegister = new LinkedHashSet<>(asList(annotatedClasses)); + // Remove all annotated-classes that have been registered - Iterator> iterator = new ArrayList<>(asList(annotatedClasses)).iterator(); + Iterator> iterator = classesToRegister.iterator(); while (iterator.hasNext()) { Class annotatedClass = iterator.next(); @@ -110,7 +113,9 @@ public static void registerBeans(BeanDefinitionRegistry registry, Class... an logger.debug(registry.getClass().getSimpleName() + " will register annotated classes : " + asList(annotatedClasses) + " ."); } - reader.register(annotatedClasses); + reader.register(classesToRegister.toArray(new Class[0])); + // clear + classesToRegister.clear(); } } diff --git a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtilsTest.java b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtilsTest.java index 05c4a78a7bc..847cee04b54 100644 --- a/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtilsTest.java +++ b/dubbo-config/dubbo-config-spring/src/test/java/org/apache/dubbo/config/spring/util/AnnotatedBeanDefinitionRegistryUtilsTest.java @@ -18,6 +18,7 @@ import org.junit.Assert; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.stereotype.Service; @@ -36,6 +37,11 @@ public class AnnotatedBeanDefinitionRegistryUtilsTest { private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); + @BeforeEach + public void init() { + context.setAllowBeanDefinitionOverriding(false); + } + @AfterEach public void destroy() { context.close(); @@ -59,6 +65,9 @@ public void testRegisterBeans() { registerBeans(context, A.class, A.class); + registerBeans(context, A.class); + + context.refresh(); A a = context.getBean(A.class); diff --git a/dubbo-dependencies-bom/pom.xml b/dubbo-dependencies-bom/pom.xml index ea55a6507d0..c335104160b 100644 --- a/dubbo-dependencies-bom/pom.xml +++ b/dubbo-dependencies-bom/pom.xml @@ -140,6 +140,9 @@ 0.6 + + 1.9.12 + 2.2.7 1.2.0 1.11.2 @@ -256,6 +259,17 @@ embedded-consul ${consul_process_version} + + + com.netflix.eureka + eureka-client + ${eureka.version} + + + com.netflix.eureka + eureka-core + ${eureka.version} + com.googlecode.xmemcached xmemcached diff --git a/dubbo-registry/dubbo-registry-eureka/pom.xml b/dubbo-registry/dubbo-registry-eureka/pom.xml new file mode 100644 index 00000000000..94dbd74e508 --- /dev/null +++ b/dubbo-registry/dubbo-registry-eureka/pom.xml @@ -0,0 +1,45 @@ + + + 4.0.0 + + org.apache.dubbo + dubbo-registry + ${revision} + ../pom.xml + + + dubbo-registry-eureka + jar + ${project.artifactId} + The Eureka registry module of Dubbo project + + + + + org.apache.dubbo + dubbo-registry-api + ${project.parent.version} + + + + + com.netflix.eureka + eureka-client + true + + + javax.inject + javax.inject + 1 + true + + + com.netflix.eureka + eureka-core + true + + + + \ No newline at end of file diff --git a/dubbo-registry/pom.xml b/dubbo-registry/pom.xml index b32ad5e8871..d141b2999ff 100644 --- a/dubbo-registry/pom.xml +++ b/dubbo-registry/pom.xml @@ -40,5 +40,6 @@ dubbo-registry-nacos dubbo-registry-multiple dubbo-registry-sofa + dubbo-registry-eureka