diff --git a/dubbo-spring-boot-actuator/README.md b/dubbo-spring-boot-actuator/README.md index 401729256..1410bca39 100644 --- a/dubbo-spring-boot-actuator/README.md +++ b/dubbo-spring-boot-actuator/README.md @@ -23,7 +23,7 @@ For now, `dubbo-spring-boot-actuator` will separate two versions for Spring Boot * `0.1.x` is a legacy version for maintaining Spring Boot 1.x - ​ + ## Integrate with Maven @@ -503,10 +503,10 @@ Dubbo Spring Boot providers actuator endpoints , however some of them are disabl ```properties # Enables Dubbo All Endpoints management.endpoint.dubbo.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 +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 ``` diff --git a/dubbo-spring-boot-actuator/pom.xml b/dubbo-spring-boot-actuator/pom.xml index 70e8f2011..4e41bf245 100644 --- a/dubbo-spring-boot-actuator/pom.xml +++ b/dubbo-spring-boot-actuator/pom.xml @@ -32,15 +32,6 @@ - - - javax.servlet - javax.servlet-api - 3.1.0 - provided - true - - org.springframework.boot spring-boot-starter-web diff --git a/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/autoconfigure/DubboEndpointsAutoConfiguration.java b/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/autoconfigure/DubboEndpointsAutoConfiguration.java index 038e0bf51..efa5b4153 100644 --- a/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/autoconfigure/DubboEndpointsAutoConfiguration.java +++ b/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/autoconfigure/DubboEndpointsAutoConfiguration.java @@ -16,7 +16,13 @@ */ package com.alibaba.boot.dubbo.actuate.autoconfigure; -import com.alibaba.boot.dubbo.actuate.endpoint.*; +import com.alibaba.boot.dubbo.actuate.endpoint.DubboConfigsMetadataEndpoint; +import com.alibaba.boot.dubbo.actuate.endpoint.DubboEndpoint; +import com.alibaba.boot.dubbo.actuate.endpoint.DubboPropertiesEndpoint; +import com.alibaba.boot.dubbo.actuate.endpoint.DubboReferencesMetadataEndpoint; +import com.alibaba.boot.dubbo.actuate.endpoint.DubboServicesMetadataEndpoint; +import com.alibaba.boot.dubbo.actuate.endpoint.DubboShutdownEndpoint; + import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -27,7 +33,7 @@ /** * Dubbo {@link Endpoint} Auto-{@link Configuration} * - * @author Mercy + * * @see Endpoint * @see Configuration * @since 0.2.0 diff --git a/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/autoconfigure/DubboHealthIndicatorAutoConfiguration.java b/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/autoconfigure/DubboHealthIndicatorAutoConfiguration.java index c53c958df..20d2cf1ff 100644 --- a/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/autoconfigure/DubboHealthIndicatorAutoConfiguration.java +++ b/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/autoconfigure/DubboHealthIndicatorAutoConfiguration.java @@ -19,6 +19,7 @@ import com.alibaba.boot.dubbo.actuate.health.DubboHealthIndicator; import com.alibaba.boot.dubbo.actuate.health.DubboHealthIndicatorProperties; import com.alibaba.boot.dubbo.autoconfigure.DubboAutoConfiguration; + import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.health.ConditionalOnEnabledHealthIndicator; import org.springframework.boot.actuate.health.HealthIndicator; @@ -33,7 +34,7 @@ /** * Dubbo {@link DubboHealthIndicator} Auto Configuration * - * @author Mercy + * * @see HealthIndicator * @since 1.0.0 */ diff --git a/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/AbstractDubboEndpoint.java b/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/AbstractDubboEndpoint.java index 95b865589..45a0cba56 100644 --- a/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/AbstractDubboEndpoint.java +++ b/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/AbstractDubboEndpoint.java @@ -19,6 +19,7 @@ import com.alibaba.dubbo.config.ProtocolConfig; import com.alibaba.dubbo.config.spring.ServiceBean; import com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor; + import org.springframework.beans.BeansException; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.context.ApplicationContext; @@ -45,7 +46,7 @@ /** * Abstract Dubbo {@link Endpoint @Endpoint} * - * @author Mercy + * * @since 0.2.0 */ public abstract class AbstractDubboEndpoint implements ApplicationContextAware, EnvironmentAware { diff --git a/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboConfigsMetadataEndpoint.java b/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboConfigsMetadataEndpoint.java index 3bdccb33a..28b276a1e 100644 --- a/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboConfigsMetadataEndpoint.java +++ b/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboConfigsMetadataEndpoint.java @@ -40,10 +40,10 @@ /** * Dubbo Configs Metadata {@link Endpoint} * - * @author Mercy + * * @since 0.2.0 */ -@Endpoint(id = "dubboConfigs") +@Endpoint(id = "dubboconfigs") public class DubboConfigsMetadataEndpoint extends AbstractDubboEndpoint { @ReadOperation diff --git a/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboEndpoint.java b/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboEndpoint.java index 792374613..eba7b5666 100644 --- a/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboEndpoint.java +++ b/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboEndpoint.java @@ -34,7 +34,7 @@ /** * Actuator {@link Endpoint} to expose Dubbo Meta Data * - * @author Mercy + * * @see Endpoint * @since 1.0.0 */ diff --git a/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboPropertiesEndpoint.java b/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboPropertiesEndpoint.java index 23fad4e8c..f9b9d1a55 100644 --- a/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboPropertiesEndpoint.java +++ b/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboPropertiesEndpoint.java @@ -26,10 +26,10 @@ /** * Dubbo Properties {@link Endpoint} * - * @author Mercy + * * @since 1.0.0 */ -@Endpoint(id = "dubboProperties") +@Endpoint(id = "dubboproperties") public class DubboPropertiesEndpoint extends AbstractDubboEndpoint { @ReadOperation diff --git a/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboReferencesMetadataEndpoint.java b/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboReferencesMetadataEndpoint.java index 8a712c6f1..3d4bbf6ed 100644 --- a/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboReferencesMetadataEndpoint.java +++ b/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboReferencesMetadataEndpoint.java @@ -30,10 +30,10 @@ /** * Dubbo {@link Reference} Metadata {@link Endpoint} * - * @author Mercy + * * @since 1.0.0 */ -@Endpoint(id = "dubboReferences") +@Endpoint(id = "dubboreferences") public class DubboReferencesMetadataEndpoint extends AbstractDubboEndpoint { @ReadOperation diff --git a/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboServicesMetadataEndpoint.java b/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboServicesMetadataEndpoint.java index 3b1c5cb52..0c4c7886f 100644 --- a/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboServicesMetadataEndpoint.java +++ b/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboServicesMetadataEndpoint.java @@ -28,10 +28,10 @@ /** * Dubbo {@link Service} Metadata {@link Endpoint} * - * @author Mercy + * * @since 0.2.0 */ -@Endpoint(id = "dubboServices") +@Endpoint(id = "dubboservices") public class DubboServicesMetadataEndpoint extends AbstractDubboEndpoint { @ReadOperation diff --git a/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboShutdownEndpoint.java b/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboShutdownEndpoint.java index 6c6e26a8c..10148003f 100644 --- a/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboShutdownEndpoint.java +++ b/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboShutdownEndpoint.java @@ -32,10 +32,9 @@ /** * Dubbo Shutdown * - * @author Mercy * @since 0.2.0 */ -@Endpoint(id = "dubboShutdown") +@Endpoint(id = "dubboshutdown") public class DubboShutdownEndpoint extends AbstractDubboEndpoint { diff --git a/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/health/DubboHealthIndicator.java b/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/health/DubboHealthIndicator.java index 4de90222f..e84a24e98 100644 --- a/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/health/DubboHealthIndicator.java +++ b/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/health/DubboHealthIndicator.java @@ -20,6 +20,7 @@ import com.alibaba.dubbo.common.status.StatusChecker; import com.alibaba.dubbo.config.ProtocolConfig; import com.alibaba.dubbo.config.ProviderConfig; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.actuate.health.AbstractHealthIndicator; import org.springframework.boot.actuate.health.Health; @@ -37,7 +38,7 @@ /** * Dubbo {@link HealthIndicator} * - * @author Mercy + * * @see HealthIndicator * @since 1.0.0 */ diff --git a/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/health/DubboHealthIndicatorProperties.java b/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/health/DubboHealthIndicatorProperties.java index 2ba7da2db..e6db92811 100644 --- a/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/health/DubboHealthIndicatorProperties.java +++ b/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/health/DubboHealthIndicatorProperties.java @@ -30,7 +30,7 @@ /** * Dubbo {@link HealthIndicator} Properties * - * @author Mercy + * * @see HealthIndicator * @since 1.0.0 */ 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 99215bfcd..ffcf18814 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,18 @@ # Set enabled for Dubbo Endpoints management.endpoint.dubbo.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.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.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 - +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 \ No newline at end of file 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 e1aa531f0..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 @@ -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 3576dbafe..29e2af649 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 @@ -19,21 +19,25 @@ import com.alibaba.boot.dubbo.actuate.autoconfigure.DubboEndpointsAutoConfiguration; import com.alibaba.dubbo.config.annotation.Service; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.web.client.RestTemplate; import java.util.Map; import java.util.SortedMap; +import java.util.function.Supplier; /** * {@link DubboEndpointsAutoConfiguration} Test * - * @author Mercy * @since 0.2.0 */ @RunWith(SpringRunner.class) @@ -58,11 +62,12 @@ "dubbo.provider.host=127.0.0.1", "dubbo.scan.basePackages=com.alibaba.boot.dubbo.actuate.endpoint", "management.endpoint.dubbo.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", + "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", + "management.endpoints.web.exposure.include = *", }) @EnableAutoConfiguration public class DubboEndpointsAutoConfigurationTest { @@ -85,6 +90,13 @@ public class DubboEndpointsAutoConfigurationTest { @Autowired private DubboShutdownEndpoint dubboShutdownEndpoint; + private RestTemplate restTemplate = new RestTemplate(); + + @Autowired + private ObjectMapper objectMapper; + + @Value("http://127.0.0.1:${local.management.port}${management.endpoints.web.base-path:/actuator}") + private String actuatorBaseURL; @Test public void testShutdown() throws Exception { @@ -178,20 +190,35 @@ public void testProperties() { Assert.assertEquals("com.alibaba.boot.dubbo.actuate.endpoint", properties.get("dubbo.scan.basePackages")); } + @Test + public void testHttpEndpoints() throws JsonProcessingException { +// testHttpEndpoint("/dubbo", dubboEndpoint::invoke); + testHttpEndpoint("/dubbo/configs", dubboConfigsMetadataEndpoint::configs); + testHttpEndpoint("/dubbo/services", dubboServicesMetadataEndpoint::services); + testHttpEndpoint("/dubbo/references", dubboReferencesMetadataEndpoint::references); + testHttpEndpoint("/dubbo/properties", dubboPropertiesEndpoint::properties); + } -@Service( - version = "${dubbo.service.version}", - application = "${dubbo.application.id}", - protocol = "${dubbo.protocol.id}", - registry = "${dubbo.registry.id}" -) -static class DefaultDemoService implements DemoService { - - public String sayHello(String name) { - return "Hello, " + name + " (from Spring Boot)"; + private void testHttpEndpoint(String actuatorURI, Supplier resultsSupplier) throws JsonProcessingException { + String actuatorURL = actuatorBaseURL + actuatorURI; + String response = restTemplate.getForObject(actuatorURL, String.class); + Assert.assertEquals(objectMapper.writeValueAsString(resultsSupplier.get()), response); } -} + + @Service( + version = "${dubbo.service.version}", + application = "${dubbo.application.id}", + protocol = "${dubbo.protocol.id}", + registry = "${dubbo.registry.id}" + ) + static class DefaultDemoService implements DemoService { + + public String sayHello(String name) { + return "Hello, " + name + " (from Spring Boot)"; + } + + } interface DemoService { String sayHello(String name); 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/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() == 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 51a6ab72f..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 @@ -36,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 { 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[] 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 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 99cf9808a..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 @@ -35,7 +35,6 @@ /** * Dubbo Welcome Logo {@link ApplicationListener} * - * @author Mercy * @see ApplicationListener * @since 1.0.0 */ 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 7bf2b6966..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 @@ -26,7 +26,6 @@ /** * The utilities class for Dubbo * - * @author Mercy * @since 1.0.0 */ public abstract class DubboUtils { @@ -130,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/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/util/DubboUtilsTest.java b/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/util/DubboUtilsTest.java index af961134e..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 @@ -40,7 +40,7 @@ /** * {@link DubboUtils} Test * - * @author Mercy + * * @see DubboUtils * @since 1.0.0 */ 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 */