*
* registry=com.alibaba.dubbo.registry.status.RegistryStatusChecker * spring=com.alibaba.dubbo.config.spring.status.SpringStatusChecker diff --git a/dubbo-spring-boot-actuator/src/main/resources/META-INF/dubbo-endpoins-default.properties b/dubbo-spring-boot-actuator/src/main/resources/META-INF/dubbo-endpoins-default.properties index 97a012feb..99215bfcd 100644 --- a/dubbo-spring-boot-actuator/src/main/resources/META-INF/dubbo-endpoins-default.properties +++ b/dubbo-spring-boot-actuator/src/main/resources/META-INF/dubbo-endpoins-default.properties @@ -4,19 +4,19 @@ # Set enabled for Dubbo Endpoints management.endpoint.dubbo.enabled = true -management.endpoint.dubbo-shutdown.enabled = false -management.endpoint.dubbo-configs.enabled = true -management.endpoint.dubbo-services.enabled = false -management.endpoint.dubbo-references.enabled = false -management.endpoint.dubbo-properties.enabled = true +management.endpoint.dubboShutdown.enabled = false +management.endpoint.dubboConfigs.enabled = true +management.endpoint.dubboServices.enabled = false +management.endpoint.dubboReferences.enabled = false +management.endpoint.dubboProperties.enabled = true # "management.endpoints.web.base-path" should not be configured in this file # Re-defines path-mapping of Dubbo Web Endpoints -management.endpoints.web.path-mapping.dubbo-shutdown = dubbo/shutdown -management.endpoints.web.path-mapping.dubbo-configs = dubbo/configs -management.endpoints.web.path-mapping.dubbo-services = dubbo/services -management.endpoints.web.path-mapping.dubbo-references = dubbo/references -management.endpoints.web.path-mapping.dubbo-properties = dubbo/properties +management.endpoints.web.path-mapping.dubboShutdown = dubbo/shutdown +management.endpoints.web.path-mapping.dubboConfigs = dubbo/configs +management.endpoints.web.path-mapping.dubboServices = dubbo/services +management.endpoints.web.path-mapping.dubboReferences = dubbo/references +management.endpoints.web.path-mapping.dubboProperties = dubbo/properties diff --git a/dubbo-spring-boot-actuator/src/test/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboEndpointTest.java b/dubbo-spring-boot-actuator/src/test/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboEndpointTest.java index 0135ec331..ac2b04936 100644 --- a/dubbo-spring-boot-actuator/src/test/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboEndpointTest.java +++ b/dubbo-spring-boot-actuator/src/test/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboEndpointTest.java @@ -17,12 +17,12 @@ package com.alibaba.boot.dubbo.actuate.endpoint; import com.alibaba.boot.dubbo.util.DubboUtils; + import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringRunner; import java.util.Map; @@ -32,7 +32,7 @@ /** * {@link DubboEndpoint} Test * - * @author Mercy + * * @see DubboEndpoint * @since 1.0.0 */ diff --git a/dubbo-spring-boot-actuator/src/test/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboEndpointsAutoConfigurationTest.java b/dubbo-spring-boot-actuator/src/test/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboEndpointsAutoConfigurationTest.java index 9bd190be1..d01e67cdf 100644 --- a/dubbo-spring-boot-actuator/src/test/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboEndpointsAutoConfigurationTest.java +++ b/dubbo-spring-boot-actuator/src/test/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboEndpointsAutoConfigurationTest.java @@ -18,6 +18,7 @@ import com.alibaba.boot.dubbo.actuate.autoconfigure.DubboEndpointsAutoConfiguration; import com.alibaba.dubbo.config.annotation.Service; + import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -32,7 +33,7 @@ /** * {@link DubboEndpointsAutoConfiguration} Test * - * @author Mercy + * * @since 0.2.0 */ @RunWith(SpringRunner.class) @@ -57,11 +58,11 @@ "dubbo.provider.host=127.0.0.1", "dubbo.scan.basePackages=com.alibaba.boot.dubbo.actuate.endpoint", "management.endpoint.dubbo.enabled = true", - "management.endpoint.dubbo-shutdown.enabled = true", - "management.endpoint.dubbo-configs.enabled = true", - "management.endpoint.dubbo-services.enabled = true", - "management.endpoint.dubbo-references.enabled = true", - "management.endpoint.dubbo-properties.enabled = true", + "management.endpoint.dubboShutdown.enabled = true", + "management.endpoint.dubboConfigs.enabled = true", + "management.endpoint.dubboServices.enabled = true", + "management.endpoint.dubboReferences.enabled = true", + "management.endpoint.dubboProperties.enabled = true", }) @EnableAutoConfiguration public class DubboEndpointsAutoConfigurationTest { @@ -143,7 +144,7 @@ public void testServices() { Assert.assertEquals(1, services.size()); - MapdemoServiceMeta = services.get("ServiceBean:dubboEndpointsAutoConfigurationTest.DefaultDemoService:com.alibaba.boot.dubbo.actuate.endpoint.DubboEndpointsAutoConfigurationTest$DemoService:${dubbo.service.version}"); + Map demoServiceMeta = services.get("ServiceBean:com.alibaba.boot.dubbo.actuate.endpoint.DubboEndpointsAutoConfigurationTest$DemoService:1.0.0"); Assert.assertEquals("1.0.0", demoServiceMeta.get("version")); diff --git a/dubbo-spring-boot-actuator/src/test/java/com/alibaba/boot/dubbo/actuate/health/DubboHealthIndicatorTest.java b/dubbo-spring-boot-actuator/src/test/java/com/alibaba/boot/dubbo/actuate/health/DubboHealthIndicatorTest.java index 6d736f01b..41de0a8e1 100644 --- a/dubbo-spring-boot-actuator/src/test/java/com/alibaba/boot/dubbo/actuate/health/DubboHealthIndicatorTest.java +++ b/dubbo-spring-boot-actuator/src/test/java/com/alibaba/boot/dubbo/actuate/health/DubboHealthIndicatorTest.java @@ -17,6 +17,7 @@ package com.alibaba.boot.dubbo.actuate.health; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubboConfig; + import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -33,7 +34,7 @@ /** * {@link DubboHealthIndicator} Test * - * @author Mercy + * * @see DubboHealthIndicator * @since 1.0.0 */ diff --git a/dubbo-spring-boot-autoconfigure/README.md b/dubbo-spring-boot-autoconfigure/README.md index b5e9306ff..d75b8a1f4 100644 --- a/dubbo-spring-boot-autoconfigure/README.md +++ b/dubbo-spring-boot-autoconfigure/README.md @@ -23,7 +23,7 @@ You can introduce the latest `dubbo-spring-boot-autoconfigure` to your project ``` diff --git a/dubbo-spring-boot-autoconfigure/pom.xml b/dubbo-spring-boot-autoconfigure/pom.xml index 1e892fa1c..ae2bae3fd 100644 --- a/dubbo-spring-boot-autoconfigure/pom.xml +++ b/dubbo-spring-boot-autoconfigure/pom.xml @@ -38,6 +38,7 @@ com.alibaba.boot dubbo-spring-boot-autoconfigure -0.2.0 +0.2.1-SNAPSHOT spring-boot-autoconfigure true ++ + org.springframework.boot spring-boot-starter-logging @@ -58,11 +59,18 @@true + +com.alibaba.spring +spring-context-support ++ \ No newline at end of file diff --git a/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/autoconfigure/DubboAutoConfiguration.java b/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/autoconfigure/DubboAutoConfiguration.java index 486c1b917..58029f446 100644 --- a/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/autoconfigure/DubboAutoConfiguration.java +++ b/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/autoconfigure/DubboAutoConfiguration.java @@ -26,6 +26,7 @@ import com.alibaba.dubbo.config.spring.context.annotation.DubboConfigConfiguration; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo; import com.alibaba.dubbo.config.spring.context.annotation.EnableDubboConfig; + import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -38,14 +39,15 @@ import java.util.Set; -import static com.alibaba.boot.dubbo.util.DubboUtils.*; +import static com.alibaba.boot.dubbo.util.DubboUtils.BASE_PACKAGES_PROPERTY_NAME; +import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_PREFIX; +import static com.alibaba.boot.dubbo.util.DubboUtils.MULTIPLE_CONFIG_PROPERTY_NAME; import static java.util.Collections.emptySet; import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE; /** * Dubbo Auto {@link Configuration} * - * @author Mercy * @see ApplicationConfig * @see Service * @see Reference @@ -59,27 +61,6 @@ @ConditionalOnClass(AbstractConfig.class) public class DubboAutoConfiguration { - /** - * Single Dubbo Config Configuration - * - * @see EnableDubboConfig - * @see DubboConfigConfiguration.Single - */ - @EnableDubboConfig - protected static class SingleDubboConfigConfiguration { - } - - /** - * Multiple Dubbo Config Configuration , equals @EnableDubboConfig.multiple() == org.springframework.boot spring-boot-starter-test test true
- * - * @see EnableDubboConfig - * @see DubboConfigConfiguration.Multiple - */ - @ConditionalOnProperty(name = MULTIPLE_CONFIG_PROPERTY_NAME, havingValue = "true") - @EnableDubboConfig(multiple = true) - protected static class MultipleDubboConfigConfiguration { - } - /** * Creates {@link ServiceAnnotationBeanPostProcessor} Bean * @@ -112,4 +93,25 @@ public ReferenceAnnotationBeanPostProcessor referenceAnnotationBeanPostProcessor return new ReferenceAnnotationBeanPostProcessor(); } + /** + * Single Dubbo Config Configuration + * + * @see EnableDubboConfig + * @see DubboConfigConfiguration.Single + */ + @EnableDubboConfig + protected static class SingleDubboConfigConfiguration { + } + + /** + * Multiple Dubbo Config Configuration , equals @EnableDubboConfig.multiple() ==true
+ * + * @see EnableDubboConfig + * @see DubboConfigConfiguration.Multiple + */ + @ConditionalOnProperty(name = MULTIPLE_CONFIG_PROPERTY_NAME, havingValue = "true") + @EnableDubboConfig(multiple = true) + protected static class MultipleDubboConfigConfiguration { + } + } diff --git a/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/autoconfigure/RelaxedDubboConfigBinder.java b/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/autoconfigure/RelaxedDubboConfigBinder.java index def20f000..72a66e7c1 100644 --- a/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/autoconfigure/RelaxedDubboConfigBinder.java +++ b/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/autoconfigure/RelaxedDubboConfigBinder.java @@ -19,13 +19,16 @@ import com.alibaba.dubbo.config.AbstractConfig; import com.alibaba.dubbo.config.spring.context.properties.AbstractDubboConfigBinder; import com.alibaba.dubbo.config.spring.context.properties.DubboConfigBinder; + import org.springframework.boot.context.properties.bind.BindHandler; import org.springframework.boot.context.properties.bind.Bindable; import org.springframework.boot.context.properties.bind.Binder; +import org.springframework.boot.context.properties.bind.PropertySourcesPlaceholdersResolver; import org.springframework.boot.context.properties.bind.handler.IgnoreErrorsBindHandler; import org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler; import org.springframework.boot.context.properties.source.ConfigurationPropertySource; import org.springframework.boot.context.properties.source.UnboundElementsSourceFilter; +import org.springframework.core.env.PropertySource; import static org.springframework.boot.context.properties.source.ConfigurationPropertySources.from; @@ -33,7 +36,6 @@ * Spring Boot Relaxed {@link DubboConfigBinder} implementation * see org.springframework.boot.context.properties.ConfigurationPropertiesBinder * - * @author Mercy * @since 0.1.1 */ public class RelaxedDubboConfigBinder extends AbstractDubboConfigBinder { @@ -41,13 +43,15 @@ public class RelaxedDubboConfigBinder extends AbstractDubboConfigBinder { @Override publicvoid bind(String prefix, C dubboConfig) { + Iterable > propertySources = getPropertySources(); + // Converts ConfigurationPropertySources - Iterable propertySources = from(getPropertySources()); + Iterable configurationPropertySources = from(propertySources); // Wrap Bindable from DubboConfig instance Bindable bindable = Bindable.ofInstance(dubboConfig); - Binder binder = new Binder(propertySources); + Binder binder = new Binder(configurationPropertySources, new PropertySourcesPlaceholdersResolver(propertySources)); // Get BindHandler BindHandler bindHandler = getBindHandler(); diff --git a/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/context/event/AwaitingNonWebApplicationListener.java b/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/context/event/AwaitingNonWebApplicationListener.java index 55c3daa0b..b8a22348e 100644 --- a/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/context/event/AwaitingNonWebApplicationListener.java +++ b/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/context/event/AwaitingNonWebApplicationListener.java @@ -21,30 +21,68 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.WebApplicationType; import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ContextClosedEvent; +import org.springframework.context.event.SmartApplicationListener; +import org.springframework.util.ObjectUtils; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; /** * Awaiting Non-Web Spring Boot {@link ApplicationListener} * - * @author Mercy * @since 0.1.1 */ -public class AwaitingNonWebApplicationListener implements ApplicationListener { +public class AwaitingNonWebApplicationListener implements SmartApplicationListener { private static final Logger logger = LoggerFactory.getLogger(AwaitingNonWebApplicationListener.class); - private static final ExecutorService executorService = Executors.newSingleThreadExecutor(); - - private static final AtomicBoolean shutdownHookRegistered = new AtomicBoolean(false); + private static final Class extends ApplicationEvent>[] SUPPORTED_APPLICATION_EVENTS = + of(ApplicationReadyEvent.class, ContextClosedEvent.class); private static final AtomicBoolean awaited = new AtomicBoolean(false); + private final Lock lock = new ReentrantLock(); + + private final Condition condition = lock.newCondition(); + + private final ExecutorService executorService = Executors.newSingleThreadExecutor(); + + @Override + public boolean supportsEventType(Class extends ApplicationEvent> eventType) { + return ObjectUtils.containsElement(SUPPORTED_APPLICATION_EVENTS, eventType); + } + @Override - public void onApplicationEvent(ApplicationReadyEvent event) { + public boolean supportsSourceType(Class> sourceType) { + return true; + } + + private static T[] of(T... values) { + return values; + } + + @Override + public void onApplicationEvent(ApplicationEvent event) { + if (event instanceof ApplicationReadyEvent) { + onApplicationReadyEvent((ApplicationReadyEvent) event); + } else if (event instanceof ContextClosedEvent) { + onContextClosedEvent((ContextClosedEvent) event); + } + } + + @Override + public int getOrder() { + return LOWEST_PRECEDENCE; + } + + protected void onApplicationReadyEvent(ApplicationReadyEvent event) { final SpringApplication springApplication = event.getSpringApplication(); @@ -52,46 +90,64 @@ public void onApplicationEvent(ApplicationReadyEvent event) { return; } - executorService.execute(new Runnable() { - @Override - public void run() { - - synchronized (springApplication) { - if (logger.isInfoEnabled()) { - logger.info(" [Dubbo] Current Spring Boot Application is await..."); - } - while (!awaited.get()) { - try { - springApplication.wait(); - } catch (InterruptedException e) { - Thread.currentThread().interrupt(); - } - } + await(); + + } + + protected void onContextClosedEvent(ContextClosedEvent event) { + release(); + shutdown(); + } + + protected void await() { + + // has been waited, return immediately + if (awaited.get()) { + return; + } + + executorService.execute(() -> executeMutually(() -> { + while (!awaited.get()) { + if (logger.isInfoEnabled()) { + logger.info(" [Dubbo] Current Spring Boot Application is await..."); + } + try { + condition.await(); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); } } - }); + })); + } - // register ShutdownHook - if (shutdownHookRegistered.compareAndSet(false, true)) { - registerShutdownHook(new Thread(new Runnable() { - @Override - public void run() { - synchronized (springApplication) { - if (awaited.compareAndSet(false, true)) { - springApplication.notifyAll(); - if (logger.isInfoEnabled()) { - logger.info(" [Dubbo] Current Spring Boot Application is about to shutdown..."); - } - // Shutdown executorService - executorService.shutdown(); - } - } + protected void release() { + executeMutually(() -> { + while (awaited.compareAndSet(false, true)) { + if (logger.isInfoEnabled()) { + logger.info(" [Dubbo] Current Spring Boot Application is about to shutdown..."); } - })); + condition.signalAll(); + } + }); + } + + private void shutdown() { + if (!executorService.isShutdown()) { + // Shutdown executorService + executorService.shutdown(); + } + } + + private void executeMutually(Runnable runnable) { + try { + lock.lock(); + runnable.run(); + } finally { + lock.unlock(); } } - private void registerShutdownHook(Thread thread) { - Runtime.getRuntime().addShutdownHook(thread); + static AtomicBoolean getAwaited() { + return awaited; } } diff --git a/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/context/event/OverrideDubboConfigApplicationListener.java b/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/context/event/OverrideDubboConfigApplicationListener.java index 33ac29b3c..e2a236318 100644 --- a/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/context/event/OverrideDubboConfigApplicationListener.java +++ b/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/context/event/OverrideDubboConfigApplicationListener.java @@ -18,6 +18,7 @@ import com.alibaba.dubbo.common.utils.ConfigUtils; import com.alibaba.dubbo.config.AbstractConfig; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; @@ -28,7 +29,9 @@ import java.util.SortedMap; -import static com.alibaba.boot.dubbo.util.DubboUtils.*; +import static com.alibaba.boot.dubbo.util.DubboUtils.DEFAULT_OVERRIDE_CONFIG_PROPERTY_VALUE; +import static com.alibaba.boot.dubbo.util.DubboUtils.OVERRIDE_CONFIG_PROPERTY_NAME; +import static com.alibaba.boot.dubbo.util.DubboUtils.filterDubboProperties; /** * {@link ApplicationListener} to override the dubbo properties from {@link Environment}into @@ -36,7 +39,6 @@ * {@link AbstractConfig Dubbo Config} on {@link ApplicationEnvironmentPreparedEvent}. * * - * @author Mercy * @see ConfigUtils * @since 1.0.0 */ diff --git a/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/context/event/WelcomeLogoApplicationListener.java b/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/context/event/WelcomeLogoApplicationListener.java index ae9682c81..9802a3b96 100644 --- a/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/context/event/WelcomeLogoApplicationListener.java +++ b/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/context/event/WelcomeLogoApplicationListener.java @@ -17,6 +17,7 @@ package com.alibaba.boot.dubbo.context.event; import com.alibaba.dubbo.common.Version; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent; @@ -24,23 +25,32 @@ import org.springframework.context.ApplicationListener; import org.springframework.core.annotation.Order; -import static com.alibaba.boot.dubbo.util.DubboUtils.*; -import static com.alibaba.dubbo.qos.server.DubboLogo.dubbo; +import java.util.concurrent.atomic.AtomicBoolean; + +import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_GITHUB_URL; +import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_MAILING_LIST; +import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_SPRING_BOOT_GITHUB_URL; +import static com.alibaba.boot.dubbo.util.DubboUtils.LINE_SEPARATOR; /** * Dubbo Welcome Logo {@link ApplicationListener} * - * @author Mercy * @see ApplicationListener * @since 1.0.0 */ @Order(LoggingApplicationListener.DEFAULT_ORDER + 1) public class WelcomeLogoApplicationListener implements ApplicationListener
{ + private static AtomicBoolean processed = new AtomicBoolean(false); @Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { + // Skip if processed before, prevent duplicated execution in Hierarchical ApplicationContext + if (processed.get()) { + return; + } + /** * Gets Logger After LoggingSystem configuration ready * @see LoggingApplicationListener @@ -55,9 +65,10 @@ public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { System.out.print(bannerText); } + // mark processed to be true + processed.compareAndSet(false, true); } - String buildBannerText() { StringBuilder bannerTextBuilder = new StringBuilder(); @@ -71,7 +82,7 @@ String buildBannerText() { .append(" :: Dubbo (v").append(Version.getVersion()).append(") : ") .append(DUBBO_GITHUB_URL) .append(LINE_SEPARATOR) - .append(" :: Google group : ") + .append(" :: Discuss group : ") .append(DUBBO_MAILING_LIST) .append(LINE_SEPARATOR) ; diff --git a/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/env/DubboDefaultPropertiesEnvironmentPostProcessor.java b/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/env/DubboDefaultPropertiesEnvironmentPostProcessor.java new file mode 100644 index 000000000..d8a671b95 --- /dev/null +++ b/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/env/DubboDefaultPropertiesEnvironmentPostProcessor.java @@ -0,0 +1,122 @@ +/* + * 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 com.alibaba.boot.dubbo.env; + +import com.alibaba.dubbo.config.ApplicationConfig; +import com.alibaba.dubbo.config.spring.context.annotation.EnableDubboConfig; +import com.alibaba.dubbo.config.spring.context.annotation.EnableDubboConfigBinding; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.context.ContextIdApplicationContextInitializer; +import org.springframework.boot.env.EnvironmentPostProcessor; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.Ordered; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.Environment; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.PropertySource; +import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; + +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; + +/** + * The lowest precedence {@link EnvironmentPostProcessor} processes + * {@link SpringApplication#setDefaultProperties(Properties) Spring Boot default properties} for Dubbo + * as late as possible before {@link ConfigurableApplicationContext#refresh() application context refresh}. + */ +public class DubboDefaultPropertiesEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered { + + /** + * The name of default {@link PropertySource} defined in SpringApplication#configurePropertySources method. + */ + private static final String PROPERTY_SOURCE_NAME = "defaultProperties"; + + /** + * The property name of Spring Application + * + * @see ContextIdApplicationContextInitializer + */ + private static final String SPRING_APPLICATION_NAME_PROPERTY = "spring.application.name"; + + /** + * The property name of {@link ApplicationConfig} + * + * @see EnableDubboConfig + * @see EnableDubboConfigBinding + */ + private static final String DUBBO_APPLICATION_NAME_PROPERTY = "dubbo.application.name"; + + @Override + public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { + MutablePropertySources propertySources = environment.getPropertySources(); + Map defaultProperties = createDefaultProperties(environment); + if (!CollectionUtils.isEmpty(defaultProperties)) { + addOrReplace(propertySources, defaultProperties); + } + } + + @Override + public int getOrder() { + return LOWEST_PRECEDENCE; + } + + private Map createDefaultProperties(ConfigurableEnvironment environment) { + Map defaultProperties = new HashMap (); + setDubboApplicationNameProperty(environment, defaultProperties); + return defaultProperties; + } + + private void setDubboApplicationNameProperty(Environment environment, Map defaultProperties) { + String springApplicationName = environment.getProperty(SPRING_APPLICATION_NAME_PROPERTY); + if (StringUtils.hasLength(springApplicationName) + && !environment.containsProperty(DUBBO_APPLICATION_NAME_PROPERTY)) { + defaultProperties.put(DUBBO_APPLICATION_NAME_PROPERTY, springApplicationName); + } + } + + /** + * Copy from BusEnvironmentPostProcessor#addOrReplace(MutablePropertySources, Map) + * + * @param propertySources {@link MutablePropertySources} + * @param map Default Dubbo Properties + */ + private void addOrReplace(MutablePropertySources propertySources, + Map map) { + MapPropertySource target = null; + if (propertySources.contains(PROPERTY_SOURCE_NAME)) { + PropertySource> source = propertySources.get(PROPERTY_SOURCE_NAME); + if (source instanceof MapPropertySource) { + target = (MapPropertySource) source; + for (String key : map.keySet()) { + if (!target.containsProperty(key)) { + target.getSource().put(key, map.get(key)); + } + } + } + } + if (target == null) { + target = new MapPropertySource(PROPERTY_SOURCE_NAME, map); + } + if (!propertySources.contains(PROPERTY_SOURCE_NAME)) { + propertySources.addLast(target); + } + } +} \ No newline at end of file diff --git a/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/util/DubboUtils.java b/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/util/DubboUtils.java index edf4ab2cb..76ee68963 100644 --- a/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/util/DubboUtils.java +++ b/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/util/DubboUtils.java @@ -16,16 +16,16 @@ */ package com.alibaba.boot.dubbo.util; +import org.springframework.core.env.ConfigurableEnvironment; + import java.util.Collections; import java.util.Map; import java.util.SortedMap; import java.util.TreeMap; -import org.springframework.core.env.ConfigurableEnvironment; /** * The utilities class for Dubbo * - * @author Mercy * @since 1.0.0 */ public abstract class DubboUtils { @@ -61,7 +61,7 @@ public abstract class DubboUtils { * * The default value is empty set. */ - public static final String BASE_PACKAGES_PROPERTY_NAME = DUBBO_SCAN_PREFIX + PROPERTY_NAME_SEPARATOR + "basePackages"; + public static final String BASE_PACKAGES_PROPERTY_NAME = DUBBO_SCAN_PREFIX + PROPERTY_NAME_SEPARATOR + "base-packages"; /** * The property name of multiple properties binding from externalized configuration @@ -129,7 +129,7 @@ public static SortedMap
filterDubboProperties(ConfigurableEnviro String propertyName = entry.getKey(); if (propertyName.startsWith(DUBBO_PREFIX + PROPERTY_NAME_SEPARATOR) - && entry.getValue() != null) { + && entry.getValue() != null) { dubboProperties.put(propertyName, entry.getValue().toString()); } diff --git a/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/util/EnvironmentUtils.java b/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/util/EnvironmentUtils.java index 9349d8129..9b20c58f8 100644 --- a/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/util/EnvironmentUtils.java +++ b/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/util/EnvironmentUtils.java @@ -16,7 +16,12 @@ */ package com.alibaba.boot.dubbo.util; -import org.springframework.core.env.*; +import org.springframework.core.env.CompositePropertySource; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.core.env.EnumerablePropertySource; +import org.springframework.core.env.Environment; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.PropertySource; import org.springframework.util.ObjectUtils; import java.util.Collections; @@ -26,7 +31,6 @@ /** * The utilities class for {@link Environment} * - * @author Mercy * @see Environment * @since 1.0.0 */ diff --git a/dubbo-spring-boot-autoconfigure/src/main/resources/META-INF/spring-configuration-metadata.json b/dubbo-spring-boot-autoconfigure/src/main/resources/META-INF/spring-configuration-metadata.json index 078dae99d..7ce2ceec9 100644 --- a/dubbo-spring-boot-autoconfigure/src/main/resources/META-INF/spring-configuration-metadata.json +++ b/dubbo-spring-boot-autoconfigure/src/main/resources/META-INF/spring-configuration-metadata.json @@ -1090,8 +1090,8 @@ }, { "sourceType": "com.alibaba.boot.dubbo.autoconfigure.DubboScanProperties", - "name": "dubbo.scan.basePackages", - "description": "The basePackages to scan , the multiple-value is delimited by comma\n\n @see EnableDubbo#scanBasePackages()", + "name": "dubbo.scan.base-packages", + "description": "The base-packages to scan , the multiple-value is delimited by comma\n\n @see EnableDubbo#scanBasePackages()", "type": "java.util.Set " } ] diff --git a/dubbo-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories b/dubbo-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories index 52fe93ce2..4ea2a1a98 100644 --- a/dubbo-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories +++ b/dubbo-spring-boot-autoconfigure/src/main/resources/META-INF/spring.factories @@ -5,4 +5,7 @@ com.alibaba.boot.dubbo.autoconfigure.DubboAutoConfiguration org.springframework.context.ApplicationListener=\ com.alibaba.boot.dubbo.context.event.OverrideDubboConfigApplicationListener,\ com.alibaba.boot.dubbo.context.event.WelcomeLogoApplicationListener,\ -com.alibaba.boot.dubbo.context.event.AwaitingNonWebApplicationListener \ No newline at end of file +com.alibaba.boot.dubbo.context.event.AwaitingNonWebApplicationListener + +org.springframework.boot.env.EnvironmentPostProcessor=\ +com.alibaba.boot.dubbo.env.DubboDefaultPropertiesEnvironmentPostProcessor \ No newline at end of file diff --git a/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/autoconfigure/DubboAutoConfigurationOnMultipleConfigTest.java b/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/autoconfigure/DubboAutoConfigurationOnMultipleConfigTest.java index ee9350938..af38587b8 100644 --- a/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/autoconfigure/DubboAutoConfigurationOnMultipleConfigTest.java +++ b/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/autoconfigure/DubboAutoConfigurationOnMultipleConfigTest.java @@ -16,10 +16,15 @@ */ package com.alibaba.boot.dubbo.autoconfigure; -import com.alibaba.dubbo.config.*; +import com.alibaba.dubbo.config.ApplicationConfig; +import com.alibaba.dubbo.config.ConsumerConfig; +import com.alibaba.dubbo.config.ModuleConfig; +import com.alibaba.dubbo.config.MonitorConfig; +import com.alibaba.dubbo.config.ProtocolConfig; +import com.alibaba.dubbo.config.ProviderConfig; +import com.alibaba.dubbo.config.RegistryConfig; import org.junit.Assert; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; @@ -38,7 +43,7 @@ /** * {@link DubboAutoConfiguration} Test On multiple Dubbo Configuration * - * @author Mercy + * * @since 1.0.0 */ @RunWith(SpringJUnit4ClassRunner.class) diff --git a/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/autoconfigure/DubboAutoConfigurationOnSingleConfigTest.java b/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/autoconfigure/DubboAutoConfigurationOnSingleConfigTest.java index c8092d3f6..b60340e75 100644 --- a/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/autoconfigure/DubboAutoConfigurationOnSingleConfigTest.java +++ b/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/autoconfigure/DubboAutoConfigurationOnSingleConfigTest.java @@ -16,9 +16,16 @@ */ package com.alibaba.boot.dubbo.autoconfigure; -import com.alibaba.dubbo.config.*; +import com.alibaba.dubbo.config.ApplicationConfig; +import com.alibaba.dubbo.config.ConsumerConfig; +import com.alibaba.dubbo.config.ModuleConfig; +import com.alibaba.dubbo.config.MonitorConfig; +import com.alibaba.dubbo.config.ProtocolConfig; +import com.alibaba.dubbo.config.ProviderConfig; +import com.alibaba.dubbo.config.RegistryConfig; import com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor; import com.alibaba.dubbo.config.spring.beans.factory.annotation.ServiceAnnotationBeanPostProcessor; + import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -32,7 +39,7 @@ /** * {@link DubboAutoConfiguration} Test On single Dubbo Configuration * - * @author Mercy + * * @since 1.0.0 */ @RunWith(SpringJUnit4ClassRunner.class) diff --git a/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/autoconfigure/RelaxedDubboConfigBinderTest.java b/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/autoconfigure/RelaxedDubboConfigBinderTest.java index 8e080861c..0a151bab8 100644 --- a/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/autoconfigure/RelaxedDubboConfigBinderTest.java +++ b/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/autoconfigure/RelaxedDubboConfigBinderTest.java @@ -19,6 +19,7 @@ import com.alibaba.dubbo.config.ApplicationConfig; import com.alibaba.dubbo.config.ProtocolConfig; import com.alibaba.dubbo.config.RegistryConfig; + import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -30,7 +31,7 @@ /** * {@link RelaxedDubboConfigBinder} Test * - * @author Mercy + * * @since 0.1.1 */ @RunWith(SpringJUnit4ClassRunner.class) diff --git a/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/context/event/AwaitingNonWebApplicationListenerTest.java b/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/context/event/AwaitingNonWebApplicationListenerTest.java new file mode 100644 index 000000000..42710cd31 --- /dev/null +++ b/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/context/event/AwaitingNonWebApplicationListenerTest.java @@ -0,0 +1,57 @@ +/* + * 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 com.alibaba.boot.dubbo.context.event; + +import org.junit.Assert; +import org.junit.Test; +import org.springframework.boot.WebApplicationType; +import org.springframework.boot.builder.SpringApplicationBuilder; + +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * {@link AwaitingNonWebApplicationListener} Test + */ +public class AwaitingNonWebApplicationListenerTest { + + @Test + public void init() { + AtomicBoolean awaited = AwaitingNonWebApplicationListener.getAwaited(); + awaited.set(false); + + } + + @Test + public void testSingleContextNonWebApplication() { + new SpringApplicationBuilder(Object.class) + .web(WebApplicationType.NONE) + .run().close(); + AtomicBoolean awaited = AwaitingNonWebApplicationListener.getAwaited(); + Assert.assertTrue(awaited.get()); + } + + @Test + public void testMultipleContextNonWebApplication() { + new SpringApplicationBuilder(Object.class) + .parent(Object.class) + .web(WebApplicationType.NONE) + .run().close(); + AtomicBoolean awaited = AwaitingNonWebApplicationListener.getAwaited(); + Assert.assertTrue(awaited.get()); + } + +} diff --git a/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/context/event/OverrideDubboConfigApplicationListenerDisableTest.java b/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/context/event/OverrideDubboConfigApplicationListenerDisableTest.java index c425b8312..c69e5f513 100644 --- a/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/context/event/OverrideDubboConfigApplicationListenerDisableTest.java +++ b/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/context/event/OverrideDubboConfigApplicationListenerDisableTest.java @@ -17,6 +17,7 @@ package com.alibaba.boot.dubbo.context.event; import com.alibaba.dubbo.common.utils.ConfigUtils; + import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; @@ -30,7 +31,7 @@ /** * {@link OverrideDubboConfigApplicationListener} Test * - * @author Mercy + * * @see OverrideDubboConfigApplicationListener * @since 1.0.0 */ diff --git a/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/context/event/OverrideDubboConfigApplicationListenerTest.java b/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/context/event/OverrideDubboConfigApplicationListenerTest.java index c7d98d0f7..2fd7cd15f 100644 --- a/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/context/event/OverrideDubboConfigApplicationListenerTest.java +++ b/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/context/event/OverrideDubboConfigApplicationListenerTest.java @@ -17,6 +17,7 @@ package com.alibaba.boot.dubbo.context.event; import com.alibaba.dubbo.common.utils.ConfigUtils; + import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; @@ -30,7 +31,7 @@ /** * {@link OverrideDubboConfigApplicationListener} Test * - * @author Mercy + * * @see OverrideDubboConfigApplicationListener * @since 1.0.0 */ diff --git a/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/context/event/WelcomeLogoApplicationListenerTest.java b/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/context/event/WelcomeLogoApplicationListenerTest.java index b603264d2..34bb97196 100644 --- a/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/context/event/WelcomeLogoApplicationListenerTest.java +++ b/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/context/event/WelcomeLogoApplicationListenerTest.java @@ -26,7 +26,7 @@ /** * {@link WelcomeLogoApplicationListener} Test * - * @author Mercy + * * @see WelcomeLogoApplicationListener * @since 1.0.0 */ diff --git a/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/env/DubboDefaultPropertiesEnvironmentPostProcessorTest.java b/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/env/DubboDefaultPropertiesEnvironmentPostProcessorTest.java new file mode 100644 index 000000000..626f2c9c6 --- /dev/null +++ b/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/env/DubboDefaultPropertiesEnvironmentPostProcessorTest.java @@ -0,0 +1,85 @@ +/* + * 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 com.alibaba.boot.dubbo.env; + +import org.junit.Assert; +import org.junit.Test; +import org.springframework.boot.SpringApplication; +import org.springframework.core.Ordered; +import org.springframework.core.env.MapPropertySource; +import org.springframework.core.env.MutablePropertySources; +import org.springframework.core.env.PropertySource; +import org.springframework.mock.env.MockEnvironment; + +import java.util.HashMap; + +/** + * {@link DubboDefaultPropertiesEnvironmentPostProcessor} Test + */ +public class DubboDefaultPropertiesEnvironmentPostProcessorTest { + + private DubboDefaultPropertiesEnvironmentPostProcessor instance = + new DubboDefaultPropertiesEnvironmentPostProcessor(); + + private SpringApplication springApplication = new SpringApplication(); + + @Test + public void testOrder() { + Assert.assertEquals(Ordered.LOWEST_PRECEDENCE, instance.getOrder()); + } + + @Test + public void testPostProcessEnvironment() { + MockEnvironment environment = new MockEnvironment(); + // Case 1 : Not Any property + instance.postProcessEnvironment(environment, springApplication); + // Get PropertySources + MutablePropertySources propertySources = environment.getPropertySources(); + // Nothing to change + PropertySource defaultPropertySource = propertySources.get("defaultProperties"); + Assert.assertNull(defaultPropertySource); + + // Case 2 : Only set property "spring.application.name" + environment.setProperty("spring.application.name", "demo-dubbo-application"); + instance.postProcessEnvironment(environment, springApplication); + defaultPropertySource = propertySources.get("defaultProperties"); + Object dubboApplicationName = defaultPropertySource.getProperty("dubbo.application.name"); + Assert.assertEquals("demo-dubbo-application", dubboApplicationName); + + // Case 3 : Only set property "dubbo.application.name" + // Rest environment + environment = new MockEnvironment(); + propertySources = environment.getPropertySources(); + environment.setProperty("dubbo.application.name", "demo-dubbo-application"); + instance.postProcessEnvironment(environment, springApplication); + defaultPropertySource = propertySources.get("defaultProperties"); + Assert.assertNull(defaultPropertySource); + dubboApplicationName = environment.getProperty("dubbo.application.name"); + Assert.assertEquals("demo-dubbo-application", dubboApplicationName); + + // Case 4 : If "defaultProperties" PropertySource is present in PropertySources + // Rest environment + environment = new MockEnvironment(); + propertySources = environment.getPropertySources(); + propertySources.addLast(new MapPropertySource("defaultProperties", new HashMap ())); + environment.setProperty("spring.application.name", "demo-dubbo-application"); + instance.postProcessEnvironment(environment, springApplication); + defaultPropertySource = propertySources.get("defaultProperties"); + dubboApplicationName = defaultPropertySource.getProperty("dubbo.application.name"); + Assert.assertEquals("demo-dubbo-application", dubboApplicationName); + } +} diff --git a/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/util/DubboUtilsTest.java b/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/util/DubboUtilsTest.java index 94df5d0e1..066cce5a2 100644 --- a/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/util/DubboUtilsTest.java +++ b/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/util/DubboUtilsTest.java @@ -22,12 +22,25 @@ import java.util.SortedMap; -import static com.alibaba.boot.dubbo.util.DubboUtils.*; +import static com.alibaba.boot.dubbo.util.DubboUtils.BASE_PACKAGES_PROPERTY_NAME; +import static com.alibaba.boot.dubbo.util.DubboUtils.DEFAULT_MULTIPLE_CONFIG_PROPERTY_VALUE; +import static com.alibaba.boot.dubbo.util.DubboUtils.DEFAULT_OVERRIDE_CONFIG_PROPERTY_VALUE; +import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_CONFIG_PREFIX; +import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_GITHUB_URL; +import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_MAILING_LIST; +import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_PREFIX; +import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_SCAN_PREFIX; +import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_SPRING_BOOT_GITHUB_URL; +import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_SPRING_BOOT_GIT_URL; +import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_SPRING_BOOT_ISSUES_URL; +import static com.alibaba.boot.dubbo.util.DubboUtils.MULTIPLE_CONFIG_PROPERTY_NAME; +import static com.alibaba.boot.dubbo.util.DubboUtils.OVERRIDE_CONFIG_PROPERTY_NAME; +import static com.alibaba.boot.dubbo.util.DubboUtils.filterDubboProperties; /** * {@link DubboUtils} Test * - * @author Mercy + * * @see DubboUtils * @since 1.0.0 */ @@ -40,7 +53,7 @@ public void testConstants() { Assert.assertEquals("dubbo.scan", DUBBO_SCAN_PREFIX); - Assert.assertEquals("dubbo.scan.basePackages", BASE_PACKAGES_PROPERTY_NAME); + Assert.assertEquals("dubbo.scan.base-packages", BASE_PACKAGES_PROPERTY_NAME); Assert.assertEquals("dubbo.config", DUBBO_CONFIG_PREFIX); diff --git a/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/util/EnvironmentUtilsTest.java b/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/util/EnvironmentUtilsTest.java index d22ba4983..9155471a3 100644 --- a/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/util/EnvironmentUtilsTest.java +++ b/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/util/EnvironmentUtilsTest.java @@ -29,7 +29,7 @@ /** * {@link EnvironmentUtils} Test * - * @author Mercy + * * @see EnvironmentUtils * @since 1.0.0 */ diff --git a/dubbo-spring-boot-parent/pom.xml b/dubbo-spring-boot-parent/pom.xml index b22a36d4b..82ab7009e 100644 --- a/dubbo-spring-boot-parent/pom.xml +++ b/dubbo-spring-boot-parent/pom.xml @@ -37,8 +37,8 @@ 1.8 UTF-8 UTF-8 -2.0.3.RELEASE -2.6.2 +2.1.1.RELEASE +2.6.5 0.2 3.4.9 2.12.0 @@ -57,6 +57,7 @@0.12 2.5.3 2.19.1 +1.0.2 @@ -92,6 +93,13 @@ + + + +com.alibaba.spring +spring-context-support +${alibaba-spring-context-support.version} ++ org.apache.zookeeper @@ -416,6 +424,8 @@**/*.fc **/*.javascript **/*.properties +**/*.yml +**/*.yaml **/*.thrift **/*.sh **/*.bat diff --git a/dubbo-spring-boot-samples/dubbo-spring-boot-sample-consumer/pom.xml b/dubbo-spring-boot-samples/dubbo-spring-boot-sample-consumer/pom.xml index dec0ef069..962dea625 100644 --- a/dubbo-spring-boot-samples/dubbo-spring-boot-sample-consumer/pom.xml +++ b/dubbo-spring-boot-samples/dubbo-spring-boot-sample-consumer/pom.xml @@ -53,6 +53,11 @@${project.version} + +com.alibaba +dubbo ++ ${project.groupId} dubbo-spring-boot-sample-api diff --git a/dubbo-spring-boot-samples/dubbo-spring-boot-sample-consumer/src/main/java/com/alibaba/boot/dubbo/demo/consumer/bootstrap/DubboConsumerDemo.java b/dubbo-spring-boot-samples/dubbo-spring-boot-sample-consumer/src/main/java/com/alibaba/boot/dubbo/demo/consumer/bootstrap/DubboConsumerDemo.java index 705101ae5..7f10d51ea 100644 --- a/dubbo-spring-boot-samples/dubbo-spring-boot-sample-consumer/src/main/java/com/alibaba/boot/dubbo/demo/consumer/bootstrap/DubboConsumerDemo.java +++ b/dubbo-spring-boot-samples/dubbo-spring-boot-sample-consumer/src/main/java/com/alibaba/boot/dubbo/demo/consumer/bootstrap/DubboConsumerDemo.java @@ -17,8 +17,9 @@ package com.alibaba.boot.dubbo.demo.consumer.bootstrap; import com.alibaba.boot.dubbo.demo.consumer.controller.DemoConsumerController; -import org.springframework.boot.SpringApplication; + import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.builder.SpringApplicationBuilder; /** * Dubbo Consumer Demo @@ -31,9 +32,8 @@ public class DubboConsumerDemo { public static void main(String[] args) { - - SpringApplication.run(DubboConsumerDemo.class,args); - + new SpringApplicationBuilder(DubboConsumerDemo.class) + .profiles("prod") + .run(args); } - } diff --git a/dubbo-spring-boot-samples/dubbo-spring-boot-sample-consumer/src/main/java/com/alibaba/boot/dubbo/demo/consumer/controller/DemoConsumerController.java b/dubbo-spring-boot-samples/dubbo-spring-boot-sample-consumer/src/main/java/com/alibaba/boot/dubbo/demo/consumer/controller/DemoConsumerController.java index 1897979a4..99cb0fc1b 100644 --- a/dubbo-spring-boot-samples/dubbo-spring-boot-sample-consumer/src/main/java/com/alibaba/boot/dubbo/demo/consumer/controller/DemoConsumerController.java +++ b/dubbo-spring-boot-samples/dubbo-spring-boot-sample-consumer/src/main/java/com/alibaba/boot/dubbo/demo/consumer/controller/DemoConsumerController.java @@ -15,8 +15,10 @@ * limitations under the License. */ package com.alibaba.boot.dubbo.demo.consumer.controller; + import com.alibaba.boot.dubbo.demo.consumer.DemoService; import com.alibaba.dubbo.config.annotation.Reference; + import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -31,8 +33,8 @@ @RestController public class DemoConsumerController { - @Reference(version = "${demo.service.version}", - application = "${dubbo.application.id}", + @Reference( + version = "${demo.service.version}", url = "dubbo://localhost:12345") private DemoService demoService; diff --git a/dubbo-spring-boot-samples/dubbo-spring-boot-sample-consumer/src/main/resources/application.properties b/dubbo-spring-boot-samples/dubbo-spring-boot-sample-consumer/src/main/resources/application.properties deleted file mode 100644 index df91988c6..000000000 --- a/dubbo-spring-boot-samples/dubbo-spring-boot-sample-consumer/src/main/resources/application.properties +++ /dev/null @@ -1,38 +0,0 @@ -# Spring boot application -spring.application.name = dubbo-consumer-demo -server.port = 8080 -management.server.port = 8081 - -# DemoService service version -demo.service.version = 1.0.0 - -# Dubbo Config properties -## ApplicationConfig Bean -dubbo.application.id = dubbo-consumer-demo -dubbo.application.name = dubbo-consumer-demo - -## Legacy QOS Config -dubbo.qos.port = 22223 - -## ProtocolConfig Bean -dubbo.protocol.id = dubbo -dubbo.protocol.name = dubbo -dubbo.protocol.port = 12345 - -# Dubbo Endpoint (default status is disable) -endpoints.dubbo.enabled = true - -# Dubbo Health -## StatusChecker Name defaults (default : "memory", "load" ) -management.health.dubbo.status.defaults = memory - -# Enables Dubbo All Endpoints -management.endpoint.dubbo.enabled = true -management.endpoint.dubbo-shutdown.enabled = true -management.endpoint.dubbo-configs.enabled = true -management.endpoint.dubbo-services.enabled = true -management.endpoint.dubbo-references.enabled = true -management.endpoint.dubbo-properties.enabled = true - -# Exposes all web endpoints -management.endpoints.web.exposure.include = * \ No newline at end of file diff --git a/dubbo-spring-boot-samples/dubbo-spring-boot-sample-consumer/src/main/resources/application.yml b/dubbo-spring-boot-samples/dubbo-spring-boot-sample-consumer/src/main/resources/application.yml new file mode 100644 index 000000000..9aad95db6 --- /dev/null +++ b/dubbo-spring-boot-samples/dubbo-spring-boot-sample-consumer/src/main/resources/application.yml @@ -0,0 +1,37 @@ +spring: + application: + name: dubbo-consumer-demo + +server: + port: 8080 + +management: + server: + port: 8081 + +dubbo: + application: + id: ${spring.application.name} + logger: slf4j + protocol: + id: dubbo + name: dubbo + port: 12345 + +--- +spring: + profiles: prod + +endpoints: + dubbo: + enabled: true + +management: + health: + dubbo: + status: + defaults: memory + endpoints: + web: + exposure: + include: "*" \ No newline at end of file diff --git a/dubbo-spring-boot-samples/dubbo-spring-boot-sample-provider/pom.xml b/dubbo-spring-boot-samples/dubbo-spring-boot-sample-provider/pom.xml index 1dc600b53..bb4eff8e7 100644 --- a/dubbo-spring-boot-samples/dubbo-spring-boot-sample-provider/pom.xml +++ b/dubbo-spring-boot-samples/dubbo-spring-boot-sample-provider/pom.xml @@ -26,7 +26,7 @@4.0.0 dubbo-spring-boot-sample-provider -Dubbo Spring Boot Sample : Provider +Dubbo Spring Boot Sample : Provider @@ -36,6 +36,30 @@ spring-boot-starter + + +org.jboss.resteasy +resteasy-jaxrs +3.0.19.Final ++ + +javax.validation +validation-api +1.1.0.Final ++ +org.mortbay.jetty +jetty +6.1.26 ++ ++ +org.mortbay.jetty +servlet-api ++ org.springframework.boot spring-boot-starter-actuator @@ -53,6 +77,11 @@${project.version} + +com.alibaba +dubbo +${project.groupId} dubbo-spring-boot-sample-api diff --git a/dubbo-spring-boot-samples/dubbo-spring-boot-sample-provider/src/main/java/com/alibaba/boot/dubbo/demo/provider/service/DefaultDemoService.java b/dubbo-spring-boot-samples/dubbo-spring-boot-sample-provider/src/main/java/com/alibaba/boot/dubbo/demo/provider/service/DefaultDemoService.java index 48bfc22bf..eda8d4127 100644 --- a/dubbo-spring-boot-samples/dubbo-spring-boot-sample-provider/src/main/java/com/alibaba/boot/dubbo/demo/provider/service/DefaultDemoService.java +++ b/dubbo-spring-boot-samples/dubbo-spring-boot-sample-provider/src/main/java/com/alibaba/boot/dubbo/demo/provider/service/DefaultDemoService.java @@ -19,6 +19,10 @@ import com.alibaba.boot.dubbo.demo.consumer.DemoService; import com.alibaba.dubbo.config.annotation.Service; +import javax.ws.rs.GET; +import javax.ws.rs.Path; +import javax.ws.rs.QueryParam; + /** * Default {@link DemoService} * @@ -28,14 +32,15 @@ */ @Service( version = "${demo.service.version}", - application = "${dubbo.application.id}", - protocol = "${dubbo.protocol.id}", + protocol = {"dubbo", "rest"}, registry = "${dubbo.registry.id}" ) +@Path("demo") public class DefaultDemoService implements DemoService { - public String sayHello(String name) { + @GET + @Path("/say-hello") + public String sayHello(@QueryParam("name") String name) { return "Hello, " + name + " (from Spring Boot)"; } - } \ No newline at end of file diff --git a/dubbo-spring-boot-samples/dubbo-spring-boot-sample-provider/src/main/resources/application.properties b/dubbo-spring-boot-samples/dubbo-spring-boot-sample-provider/src/main/resources/application.properties index 323f72e43..b6f2c3a3e 100644 --- a/dubbo-spring-boot-samples/dubbo-spring-boot-sample-provider/src/main/resources/application.properties +++ b/dubbo-spring-boot-samples/dubbo-spring-boot-sample-provider/src/main/resources/application.properties @@ -7,21 +7,25 @@ server.port = 9090 demo.service.version = 1.0.0 # Base packages to scan Dubbo Component: @com.alibaba.dubbo.config.annotation.Service -dubbo.scan.basePackages = com.alibaba.boot.dubbo.demo.provider.service +dubbo.scan.base-packages = com.alibaba.boot.dubbo.demo.provider.service # Dubbo Config properties ## ApplicationConfig Bean -dubbo.application.id = dubbo-provider-demo -dubbo.application.name = dubbo-provider-demo -dubbo.application.qos.port=22222 +dubbo.application.qos.port=0 dubbo.application.qos.enable=true + +## Enable multiple config bindings +dubbo.config.multiple =true + ## ProtocolConfig Bean -dubbo.protocol.id = dubbo -dubbo.protocol.name = dubbo -dubbo.protocol.port = 12345 -dubbo.protocol.status = server +dubbo.protocols.dubbo.name = dubbo +dubbo.protocols.dubbo.port = 12345 +dubbo.protocols.dubbo.status = server + +dubbo.protocols.rest.name = rest +dubbo.protocols.rest.port = 8888 ## RegistryConfig Bean dubbo.registry.id = my-registry @@ -29,11 +33,11 @@ dubbo.registry.address = N/A # Enables Dubbo All Endpoints management.endpoint.dubbo.enabled = true -management.endpoint.dubbo-shutdown.enabled = true -management.endpoint.dubbo-configs.enabled = true -management.endpoint.dubbo-services.enabled = true -management.endpoint.dubbo-references.enabled = true -management.endpoint.dubbo-properties.enabled = true +management.endpoint.dubboShutdown.enabled = true +management.endpoint.dubboConfigs.enabled = true +management.endpoint.dubboServices.enabled = true +management.endpoint.dubboReferences.enabled = true +management.endpoint.dubboProperties.enabled = true # Dubbo Health ## StatusChecker Name defaults (default : "memory", "load" ) diff --git a/dubbo-spring-boot-starter/pom.xml b/dubbo-spring-boot-starter/pom.xml index 993366029..f5e7dafed 100644 --- a/dubbo-spring-boot-starter/pom.xml +++ b/dubbo-spring-boot-starter/pom.xml @@ -36,24 +36,20 @@- - - org.springframework.boot spring-boot-starter -provided -- com.alibaba -dubbo +true org.apache.zookeeper zookeeper +true org.apache.curator curator-framework +true