shutdownData = new TreeMap<>();
78 | shutdownData.put("shutdown.count", shutdownCountData);
79 | return shutdownData;
80 | }
81 |
82 | }
--------------------------------------------------------------------------------
/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/autoconfigure/DubboEndpointsAutoConfiguration.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alibaba.boot.dubbo.actuate.autoconfigure;
18 |
19 | import com.alibaba.boot.dubbo.actuate.endpoint.DubboConfigsMetadataEndpoint;
20 | import com.alibaba.boot.dubbo.actuate.endpoint.DubboEndpoint;
21 | import com.alibaba.boot.dubbo.actuate.endpoint.DubboPropertiesEndpoint;
22 | import com.alibaba.boot.dubbo.actuate.endpoint.DubboReferencesMetadataEndpoint;
23 | import com.alibaba.boot.dubbo.actuate.endpoint.DubboServicesMetadataEndpoint;
24 | import com.alibaba.boot.dubbo.actuate.endpoint.DubboShutdownEndpoint;
25 |
26 | import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
27 | import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
28 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
29 | import org.springframework.context.annotation.Bean;
30 | import org.springframework.context.annotation.Configuration;
31 | import org.springframework.context.annotation.PropertySource;
32 |
33 | /**
34 | * Dubbo {@link Endpoint} Auto-{@link Configuration}
35 | *
36 | *
37 | * @see Endpoint
38 | * @see Configuration
39 | * @since 0.2.0
40 | */
41 | @Configuration
42 | @PropertySource(
43 | name = "Dubbo Endpoints Default Properties",
44 | value = "classpath:/META-INF/dubbo-endpoins-default.properties") // 导入该配置文件
45 | public class DubboEndpointsAutoConfiguration {
46 |
47 | @Bean
48 | @ConditionalOnMissingBean
49 | @ConditionalOnEnabledEndpoint
50 | public DubboEndpoint dubboEndpoint() {
51 | return new DubboEndpoint();
52 | }
53 |
54 | @Bean
55 | @ConditionalOnMissingBean
56 | @ConditionalOnEnabledEndpoint
57 | public DubboConfigsMetadataEndpoint dubboConfigsMetadataEndpoint() {
58 | return new DubboConfigsMetadataEndpoint();
59 | }
60 |
61 | @Bean
62 | @ConditionalOnMissingBean
63 | @ConditionalOnEnabledEndpoint
64 | public DubboPropertiesEndpoint dubboPropertiesEndpoint() {
65 | return new DubboPropertiesEndpoint();
66 | }
67 |
68 | @Bean
69 | @ConditionalOnMissingBean
70 | @ConditionalOnEnabledEndpoint
71 | public DubboReferencesMetadataEndpoint dubboReferencesMetadataEndpoint() {
72 | return new DubboReferencesMetadataEndpoint();
73 | }
74 |
75 | @Bean
76 | @ConditionalOnMissingBean
77 | @ConditionalOnEnabledEndpoint
78 | public DubboServicesMetadataEndpoint dubboServicesMetadataEndpoint() {
79 | return new DubboServicesMetadataEndpoint();
80 | }
81 |
82 | @Bean
83 | @ConditionalOnMissingBean
84 | @ConditionalOnEnabledEndpoint
85 | public DubboShutdownEndpoint dubboShutdownEndpoint() {
86 | return new DubboShutdownEndpoint();
87 | }
88 |
89 | }
--------------------------------------------------------------------------------
/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/context/event/OverrideDubboConfigApplicationListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alibaba.boot.dubbo.context.event;
18 |
19 | import com.alibaba.dubbo.common.utils.ConfigUtils;
20 | import com.alibaba.dubbo.config.AbstractConfig;
21 |
22 | import org.slf4j.Logger;
23 | import org.slf4j.LoggerFactory;
24 | import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
25 | import org.springframework.context.ApplicationListener;
26 | import org.springframework.core.annotation.Order;
27 | import org.springframework.core.env.ConfigurableEnvironment;
28 | import org.springframework.core.env.Environment;
29 |
30 | import java.util.SortedMap;
31 |
32 | import static com.alibaba.boot.dubbo.util.DubboUtils.DEFAULT_OVERRIDE_CONFIG_PROPERTY_VALUE;
33 | import static com.alibaba.boot.dubbo.util.DubboUtils.OVERRIDE_CONFIG_PROPERTY_NAME;
34 | import static com.alibaba.boot.dubbo.util.DubboUtils.filterDubboProperties;
35 |
36 | /**
37 | * {@link ApplicationListener} to override the dubbo properties from {@link Environment}into
38 | * {@link ConfigUtils#getProperties() Dubbo Config}.
39 | * {@link AbstractConfig Dubbo Config} on {@link ApplicationEnvironmentPreparedEvent}.
40 | *
41 | *
42 | * @see ConfigUtils
43 | * @since 1.0.0
44 | */
45 | @Order // LOWEST_PRECEDENCE Make sure last execution
46 | public class OverrideDubboConfigApplicationListener implements ApplicationListener {
47 |
48 | @Override
49 | public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
50 | // 获得 Logger 对象
51 | /**
52 | * Gets Logger After LoggingSystem configuration ready
53 | * @see LoggingApplicationListener
54 | */
55 | final Logger logger = LoggerFactory.getLogger(getClass());
56 |
57 | // 获得 "dubbo.config.override" 属性对应的值。默认情况下为 true
58 | ConfigurableEnvironment environment = event.getEnvironment();
59 | boolean override = environment.getProperty(OVERRIDE_CONFIG_PROPERTY_NAME, boolean.class, DEFAULT_OVERRIDE_CONFIG_PROPERTY_VALUE);
60 | // 如果要重写,则覆盖添加到 Dubbo Properties 中
61 | if (override) {
62 | // 从 environment 中,提取 "dubbo." 开头的配置
63 | SortedMap dubboProperties = filterDubboProperties(environment);
64 | // 添加到 Dubbo Properties 中
65 | ConfigUtils.getProperties().putAll(dubboProperties);
66 | if (logger.isInfoEnabled()) {
67 | logger.info("Dubbo Config was overridden by externalized configuration {}", dubboProperties);
68 | }
69 | } else {
70 | if (logger.isInfoEnabled()) {
71 | logger.info("Disable override Dubbo Config caused by property {} = {}", OVERRIDE_CONFIG_PROPERTY_NAME, override);
72 | }
73 | }
74 |
75 | }
76 |
77 | }
--------------------------------------------------------------------------------
/dubbo-spring-boot-samples/dubbo-registry-nacos-samples/consumer-sample/pom.xml:
--------------------------------------------------------------------------------
1 |
17 |
20 |
21 | com.alibaba.boot.samples
22 | dubbo-spring-boot-registry-nacos-samples
23 | ${revision}
24 | ../pom.xml
25 |
26 | 4.0.0
27 |
28 | dubbo-spring-boot-registry-nacos-consumer-sample
29 | Dubbo Spring Boot Samples : Registry Nacos :: Consumer Sample
30 |
31 |
32 |
33 |
34 | org.springframework.boot
35 | spring-boot-starter
36 |
37 |
38 |
39 | com.alibaba.boot
40 | dubbo-spring-boot-starter
41 | ${revision}
42 |
43 |
44 |
45 |
46 | com.alibaba
47 | dubbo
48 |
49 |
50 |
51 | com.alibaba.boot.samples
52 | dubbo-spring-boot-sample-api
53 | ${revision}
54 |
55 |
56 |
57 |
58 | io.netty
59 | netty-all
60 |
61 |
62 |
63 |
64 | com.alibaba
65 | dubbo-registry-nacos
66 |
67 |
68 |
69 |
70 | com.alibaba.nacos
71 | nacos-client
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 | org.springframework.boot
80 | spring-boot-maven-plugin
81 | ${spring-boot.version}
82 |
83 |
84 |
85 | repackage
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
--------------------------------------------------------------------------------
/dubbo-spring-boot-samples/dubbo-registry-nacos-samples/provider-sample/pom.xml:
--------------------------------------------------------------------------------
1 |
17 |
20 |
21 | com.alibaba.boot.samples
22 | dubbo-spring-boot-registry-nacos-samples
23 | ${revision}
24 | ../pom.xml
25 |
26 | 4.0.0
27 |
28 | dubbo-spring-boot-registry-nacos-provider-sample
29 | Dubbo Spring Boot Samples : Registry Nacos :: Provider Sample
30 |
31 |
32 |
33 |
34 |
35 | org.springframework.boot
36 | spring-boot-starter-web
37 |
38 |
39 |
40 | com.alibaba.boot
41 | dubbo-spring-boot-starter
42 | ${revision}
43 |
44 |
45 |
46 |
47 | com.alibaba
48 | dubbo
49 |
50 |
51 |
52 | com.alibaba.boot.samples
53 | dubbo-spring-boot-sample-api
54 | ${revision}
55 |
56 |
57 |
58 |
59 | io.netty
60 | netty-all
61 |
62 |
63 |
64 |
65 | com.alibaba
66 | dubbo-registry-nacos
67 |
68 |
69 |
70 |
71 | com.alibaba.nacos
72 | nacos-client
73 |
74 |
75 |
76 |
77 |
78 |
79 | org.springframework.boot
80 | spring-boot-maven-plugin
81 | ${spring-boot.version}
82 |
83 |
84 |
85 | repackage
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
--------------------------------------------------------------------------------
/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboReferencesMetadataEndpoint.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alibaba.boot.dubbo.actuate.endpoint;
18 |
19 | import com.alibaba.dubbo.config.annotation.Reference;
20 | import com.alibaba.dubbo.config.spring.ReferenceBean;
21 | import com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor;
22 |
23 | import org.springframework.beans.factory.annotation.InjectionMetadata;
24 | import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
25 | import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
26 |
27 | import java.util.LinkedHashMap;
28 | import java.util.Map;
29 |
30 | /**
31 | * Dubbo {@link Reference} Metadata {@link Endpoint}
32 | *
33 | *
34 | * @since 1.0.0
35 | */
36 | @Endpoint(id = "dubboreferences")
37 | public class DubboReferencesMetadataEndpoint extends AbstractDubboEndpoint {
38 |
39 | @ReadOperation
40 | public Map> references() {
41 | // 创建 Map
42 | // KEY:Bean 的名字
43 | // VALUE:Bean 的元数据
44 | Map> referencesMetadata = new LinkedHashMap<>();
45 |
46 | // 获得 ReferenceAnnotationBeanPostProcessor Bean 对象
47 | ReferenceAnnotationBeanPostProcessor beanPostProcessor = super.getReferenceAnnotationBeanPostProcessor();
48 |
49 | // injected Field ReferenceBean Cache
50 | referencesMetadata.putAll(buildReferencesMetadata(beanPostProcessor.getInjectedFieldReferenceBeanMap()));
51 | // injected Method ReferenceBean Cache
52 | referencesMetadata.putAll(buildReferencesMetadata(beanPostProcessor.getInjectedMethodReferenceBeanMap()));
53 | return referencesMetadata;
54 | }
55 |
56 | private Map> buildReferencesMetadata(Map> injectedElementReferenceBeanMap) {
57 | // 创建 Map
58 | // KEY:Bean 的名字
59 | // VALUE:Bean 的元数据
60 | Map> referencesMetadata = new LinkedHashMap<>();
61 | // 遍历 injectedElementReferenceBeanMap 元素
62 | for (Map.Entry> entry : injectedElementReferenceBeanMap.entrySet()) {
63 | InjectionMetadata.InjectedElement injectedElement = entry.getKey();
64 | // 获得 ReferenceBean 对象
65 | ReferenceBean> referenceBean = entry.getValue();
66 | // 获得 Bean 元数据
67 | Map beanMetadata = super.resolveBeanMetadata(referenceBean);
68 | // 获得 invoker 属性
69 | beanMetadata.put("invoker", super.resolveBeanMetadata(referenceBean.get()));
70 | // 添加到 referencesMetadata 中
71 | referencesMetadata.put(String.valueOf(injectedElement.getMember()), beanMetadata);
72 | }
73 | return referencesMetadata;
74 | }
75 |
76 | }
--------------------------------------------------------------------------------
/dubbo-spring-boot-actuator/src/test/java/com/alibaba/boot/dubbo/actuate/health/DubboHealthIndicatorTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alibaba.boot.dubbo.actuate.health;
18 |
19 | import com.alibaba.dubbo.config.spring.context.annotation.EnableDubboConfig;
20 |
21 | import org.junit.Assert;
22 | import org.junit.Test;
23 | import org.junit.runner.RunWith;
24 | import org.springframework.beans.factory.annotation.Autowired;
25 | import org.springframework.boot.actuate.health.Health;
26 | import org.springframework.boot.actuate.health.Status;
27 | import org.springframework.boot.context.properties.EnableConfigurationProperties;
28 | import org.springframework.boot.test.context.SpringBootTest;
29 | import org.springframework.test.context.TestPropertySource;
30 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
31 |
32 | import java.util.Map;
33 |
34 | /**
35 | * {@link DubboHealthIndicator} Test
36 | *
37 | *
38 | * @see DubboHealthIndicator
39 | * @since 1.0.0
40 | */
41 | @RunWith(SpringJUnit4ClassRunner.class)
42 | @TestPropertySource(properties = {
43 | "dubbo.protocol.id = dubbo-protocol",
44 | "dubbo.protocol.name = dubbo",
45 | "dubbo.protocol.port = 12345",
46 | "dubbo.protocol.status = registry",
47 | "dubbo.provider.id = dubbo-provider",
48 | "dubbo.provider.status = server",
49 | "management.health.dubbo.status.defaults = memory",
50 | "management.health.dubbo.status.extras = load,threadpool"
51 | })
52 | @SpringBootTest(
53 | classes = {
54 | DubboHealthIndicator.class,
55 | DubboHealthIndicatorTest.class
56 | }
57 | )
58 | @EnableConfigurationProperties(DubboHealthIndicatorProperties.class)
59 | @EnableDubboConfig
60 | public class DubboHealthIndicatorTest {
61 |
62 | @Autowired
63 | private DubboHealthIndicator dubboHealthIndicator;
64 |
65 | @Test
66 | public void testResolveStatusCheckerNamesMap() {
67 |
68 | Map statusCheckerNamesMap = dubboHealthIndicator.resolveStatusCheckerNamesMap();
69 |
70 | Assert.assertEquals(5, statusCheckerNamesMap.size());
71 |
72 | Assert.assertEquals("dubbo-protocol@ProtocolConfig.getStatus()", statusCheckerNamesMap.get("registry"));
73 | Assert.assertEquals("dubbo-provider@ProviderConfig.getStatus()", statusCheckerNamesMap.get("server"));
74 | Assert.assertEquals("management.health.dubbo.status.defaults", statusCheckerNamesMap.get("memory"));
75 | Assert.assertEquals("management.health.dubbo.status.extras", statusCheckerNamesMap.get("load"));
76 | Assert.assertEquals("management.health.dubbo.status.extras", statusCheckerNamesMap.get("threadpool"));
77 |
78 | }
79 |
80 | @Test
81 | public void testHealth() {
82 |
83 | Health health = dubboHealthIndicator.health();
84 |
85 | Assert.assertEquals(Status.UNKNOWN, health.getStatus());
86 |
87 | }
88 | }
89 |
--------------------------------------------------------------------------------
/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/DubboConfigsMetadataEndpoint.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alibaba.boot.dubbo.actuate.endpoint;
18 |
19 | import com.alibaba.dubbo.config.*;
20 | import org.springframework.beans.factory.BeanFactoryUtils;
21 | import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
22 | import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
23 |
24 | import java.util.LinkedHashMap;
25 | import java.util.Map;
26 | import java.util.TreeMap;
27 |
28 | /**
29 | * Dubbo Configs Metadata {@link Endpoint}
30 | *
31 | *
32 | * @since 0.2.0
33 | */
34 | @Endpoint(id = "dubboconfigs")
35 | public class DubboConfigsMetadataEndpoint extends AbstractDubboEndpoint {
36 |
37 | @ReadOperation
38 | public Map>> configs() {
39 | // 创建 Map
40 | // KEY:获得类的简称。例如:ApplicationConfig、ConsumerConfig
41 | // KEY2:Bean 的名称
42 | // VALUE:Bean 的元数据
43 | Map>> configsMap = new LinkedHashMap<>();
44 |
45 | // 遍历每个配置类,添加其的 Bean 们,到 configsMap 中
46 | addDubboConfigBeans(ApplicationConfig.class, configsMap);
47 | addDubboConfigBeans(ConsumerConfig.class, configsMap);
48 | addDubboConfigBeans(MethodConfig.class, configsMap);
49 | addDubboConfigBeans(ModuleConfig.class, configsMap);
50 | addDubboConfigBeans(MonitorConfig.class, configsMap);
51 | addDubboConfigBeans(ProtocolConfig.class, configsMap);
52 | addDubboConfigBeans(ProviderConfig.class, configsMap);
53 | addDubboConfigBeans(ReferenceConfig.class, configsMap);
54 | addDubboConfigBeans(RegistryConfig.class, configsMap);
55 | addDubboConfigBeans(ServiceConfig.class, configsMap);
56 | return configsMap;
57 | }
58 |
59 | private void addDubboConfigBeans(Class extends AbstractConfig> dubboConfigClass, Map>> configsMap) {
60 | // 获得指定类 dubboConfigClass 的 Map
61 | Map dubboConfigBeans = BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, dubboConfigClass);
62 | // 获得类的简称。例如:ApplicationConfig、ConsumerConfig
63 | String name = dubboConfigClass.getSimpleName();
64 | // 创建 Map
65 | Map> beansMetadata = new TreeMap<>();
66 | // 遍历 dubboConfigBeans 数组
67 | for (Map.Entry entry : dubboConfigBeans.entrySet()) {
68 | // 获得 Bean 的名字
69 | String beanName = entry.getKey();
70 | // 获得 Bean 的元数据
71 | AbstractConfig configBean = entry.getValue();
72 | Map configBeanMeta = super.resolveBeanMetadata(configBean);
73 | // 添加到 beansMetadata 中
74 | beansMetadata.put(beanName, configBeanMeta);
75 | }
76 | // 添加到 configsMap 中
77 | configsMap.put(name, beansMetadata);
78 | }
79 |
80 | }
--------------------------------------------------------------------------------
/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/health/DubboHealthIndicatorProperties.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alibaba.boot.dubbo.actuate.health;
18 |
19 | import com.alibaba.dubbo.common.status.StatusChecker;
20 |
21 | import org.springframework.boot.actuate.health.HealthIndicator;
22 | import org.springframework.boot.context.properties.ConfigurationProperties;
23 |
24 | import java.util.Arrays;
25 | import java.util.LinkedHashSet;
26 | import java.util.Set;
27 |
28 | import static com.alibaba.boot.dubbo.actuate.health.DubboHealthIndicatorProperties.PREFIX;
29 |
30 | /**
31 | * Dubbo {@link HealthIndicator} Properties
32 | *
33 | *
34 | * @see HealthIndicator
35 | * @since 1.0.0
36 | */
37 | @ConfigurationProperties(prefix = PREFIX, ignoreUnknownFields = false) // "management.health.dubbo" 开头的配置
38 | public class DubboHealthIndicatorProperties {
39 |
40 | /**
41 | * The prefix of {@link DubboHealthIndicatorProperties}
42 | */
43 | public static final String PREFIX = "management.health.dubbo";
44 |
45 | private Status status = new Status();
46 |
47 | public Status getStatus() {
48 | return status;
49 | }
50 |
51 | public void setStatus(Status status) {
52 | this.status = status;
53 | }
54 |
55 | /**
56 | * The nested class for {@link StatusChecker}'s names
57 | *
58 | * registry=com.alibaba.dubbo.registry.status.RegistryStatusChecker
59 | * spring=com.alibaba.dubbo.config.spring.status.SpringStatusChecker
60 | * datasource=com.alibaba.dubbo.config.spring.status.DataSourceStatusChecker
61 | * memory=com.alibaba.dubbo.common.status.support.MemoryStatusChecker
62 | * load=com.alibaba.dubbo.common.status.support.LoadStatusChecker
63 | * server=com.alibaba.dubbo.rpc.protocol.dubbo.status.ServerStatusChecker
64 | * threadpool=com.alibaba.dubbo.rpc.protocol.dubbo.status.ThreadPoolStatusChecker
65 | *
66 | *
67 | * @see StatusChecker
68 | */
69 | public static class Status {
70 |
71 | /**
72 | * The defaults names of {@link StatusChecker}
73 | *
74 | * The defaults : "memory", "load"
75 | */
76 | private Set defaults = new LinkedHashSet<>(Arrays.asList("memory", "load"));
77 |
78 | /**
79 | * The extra names of {@link StatusChecker}
80 | *
81 | * 配置的 "management.health.dubbo.extras" 集合
82 | *
83 | * 每个元素,是 StatusChecker 的实现类
84 | */
85 | private Set extras = new LinkedHashSet<>();
86 |
87 | public Set getDefaults() {
88 | return defaults;
89 | }
90 |
91 | public void setDefaults(Set defaults) {
92 | this.defaults = defaults;
93 | }
94 |
95 | public Set getExtras() {
96 | return extras;
97 | }
98 |
99 | public void setExtras(Set extras) {
100 | this.extras = extras;
101 | }
102 | }
103 |
104 | }
--------------------------------------------------------------------------------
/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/autoconfigure/RelaxedDubboConfigBinder.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alibaba.boot.dubbo.autoconfigure;
18 |
19 | import com.alibaba.dubbo.config.AbstractConfig;
20 | import com.alibaba.dubbo.config.spring.context.properties.AbstractDubboConfigBinder;
21 | import com.alibaba.dubbo.config.spring.context.properties.DubboConfigBinder;
22 |
23 | import org.springframework.boot.context.properties.bind.BindHandler;
24 | import org.springframework.boot.context.properties.bind.Bindable;
25 | import org.springframework.boot.context.properties.bind.Binder;
26 | import org.springframework.boot.context.properties.bind.PropertySourcesPlaceholdersResolver;
27 | import org.springframework.boot.context.properties.bind.handler.IgnoreErrorsBindHandler;
28 | import org.springframework.boot.context.properties.bind.handler.NoUnboundElementsBindHandler;
29 | import org.springframework.boot.context.properties.source.ConfigurationPropertySource;
30 | import org.springframework.boot.context.properties.source.UnboundElementsSourceFilter;
31 | import org.springframework.core.env.PropertySource;
32 |
33 | import static org.springframework.boot.context.properties.source.ConfigurationPropertySources.from;
34 |
35 | /**
36 | * Spring Boot Relaxed {@link DubboConfigBinder} implementation
37 | * see org.springframework.boot.context.properties.ConfigurationPropertiesBinder
38 | *
39 | * @since 0.1.1
40 | */
41 | public class RelaxedDubboConfigBinder extends AbstractDubboConfigBinder {
42 |
43 | @Override
44 | public void bind(String prefix, C dubboConfig) {
45 | // 获得 PropertySource 数组
46 | Iterable> propertySources = getPropertySources();
47 | // Converts ConfigurationPropertySources
48 | // 转换成 ConfigurationPropertySource 数组
49 | Iterable configurationPropertySources = from(propertySources);
50 |
51 | // Wrap Bindable from DubboConfig instance
52 | // 将 dubboConfig 包装成 Bindable 对象
53 | Bindable bindable = Bindable.ofInstance(dubboConfig);
54 |
55 | // 创建 Binder 对象
56 | Binder binder = new Binder(configurationPropertySources, new PropertySourcesPlaceholdersResolver(propertySources));
57 | // Get BindHandler
58 | // 获得 BindHandler 对象
59 | BindHandler bindHandler = getBindHandler();
60 | // Bind
61 | // 执行绑定,会将 propertySources 属性,注入到 dubboConfig 对象中
62 | binder.bind(prefix, bindable, bindHandler);
63 | }
64 |
65 | private BindHandler getBindHandler() {
66 | // 获得默认的 BindHandler 处理器
67 | BindHandler handler = BindHandler.DEFAULT;
68 | // 进一步包装成 IgnoreErrorsBindHandler 对象
69 | if (isIgnoreInvalidFields()) {
70 | handler = new IgnoreErrorsBindHandler(handler);
71 | }
72 | // 进一步包装成 NoUnboundElementsBindHandler 对象
73 | if (!isIgnoreUnknownFields()) {
74 | UnboundElementsSourceFilter filter = new UnboundElementsSourceFilter();
75 | handler = new NoUnboundElementsBindHandler(handler, filter);
76 | }
77 | return handler;
78 | }
79 |
80 | }
81 |
--------------------------------------------------------------------------------
/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/context/event/WelcomeLogoApplicationListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alibaba.boot.dubbo.context.event;
18 |
19 | import com.alibaba.dubbo.common.Version;
20 |
21 | import org.slf4j.Logger;
22 | import org.slf4j.LoggerFactory;
23 | import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
24 | import org.springframework.boot.context.logging.LoggingApplicationListener;
25 | import org.springframework.context.ApplicationListener;
26 | import org.springframework.core.annotation.Order;
27 |
28 | import java.util.concurrent.atomic.AtomicBoolean;
29 |
30 | import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_GITHUB_URL;
31 | import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_MAILING_LIST;
32 | import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_SPRING_BOOT_GITHUB_URL;
33 | import static com.alibaba.boot.dubbo.util.DubboUtils.LINE_SEPARATOR;
34 |
35 | /**
36 | * Dubbo Welcome Logo {@link ApplicationListener}
37 | *
38 | * @see ApplicationListener
39 | * @since 1.0.0
40 | */
41 | @Order(LoggingApplicationListener.DEFAULT_ORDER + 1)
42 | public class WelcomeLogoApplicationListener implements ApplicationListener {
43 |
44 | /**
45 | * 是否执行过
46 | *
47 | * 通过该变量,保证有且仅处理一次 ApplicationEnvironmentPreparedEvent 事件
48 | */
49 | private static AtomicBoolean processed = new AtomicBoolean(false);
50 |
51 | @Override
52 | public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
53 | // Skip if processed before, prevent duplicated execution in Hierarchical ApplicationContext
54 | // 如果已经处理,则直接跳过
55 | if (processed.get()) {
56 | return;
57 | }
58 |
59 | // 获得 Logger 对象
60 | /**
61 | * Gets Logger After LoggingSystem configuration ready
62 | * @see LoggingApplicationListener
63 | */
64 | final Logger logger = LoggerFactory.getLogger(getClass());
65 |
66 | // 获得 Dubbo Banner 文本
67 | String bannerText = buildBannerText();
68 | if (logger.isInfoEnabled()) {
69 | logger.info(bannerText);
70 | } else {
71 | System.out.print(bannerText);
72 | }
73 |
74 | // mark processed to be true
75 | // 标记已执行
76 | processed.compareAndSet(false, true);
77 | }
78 |
79 | String buildBannerText() {
80 | StringBuilder bannerTextBuilder = new StringBuilder();
81 | bannerTextBuilder
82 | .append(LINE_SEPARATOR)
83 | .append(LINE_SEPARATOR)
84 | .append(" :: Dubbo Spring Boot (v").append(Version.getVersion(getClass(), "1.0.0")).append(") : ")
85 | .append(DUBBO_SPRING_BOOT_GITHUB_URL)
86 | .append(LINE_SEPARATOR)
87 | .append(" :: Dubbo (v").append(Version.getVersion()).append(") : ")
88 | .append(DUBBO_GITHUB_URL)
89 | .append(LINE_SEPARATOR)
90 | .append(" :: Discuss group : ")
91 | .append(DUBBO_MAILING_LIST)
92 | .append(LINE_SEPARATOR);
93 | return bannerTextBuilder.toString();
94 | }
95 |
96 | }
--------------------------------------------------------------------------------
/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/util/DubboUtilsTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alibaba.boot.dubbo.util;
18 |
19 | import org.junit.Assert;
20 | import org.junit.Test;
21 | import org.springframework.mock.env.MockEnvironment;
22 |
23 | import java.util.SortedMap;
24 |
25 | import static com.alibaba.boot.dubbo.util.DubboUtils.BASE_PACKAGES_PROPERTY_NAME;
26 | import static com.alibaba.boot.dubbo.util.DubboUtils.DEFAULT_MULTIPLE_CONFIG_PROPERTY_VALUE;
27 | import static com.alibaba.boot.dubbo.util.DubboUtils.DEFAULT_OVERRIDE_CONFIG_PROPERTY_VALUE;
28 | import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_CONFIG_PREFIX;
29 | import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_GITHUB_URL;
30 | import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_MAILING_LIST;
31 | import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_PREFIX;
32 | import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_SCAN_PREFIX;
33 | import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_SPRING_BOOT_GITHUB_URL;
34 | import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_SPRING_BOOT_GIT_URL;
35 | import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_SPRING_BOOT_ISSUES_URL;
36 | import static com.alibaba.boot.dubbo.util.DubboUtils.MULTIPLE_CONFIG_PROPERTY_NAME;
37 | import static com.alibaba.boot.dubbo.util.DubboUtils.OVERRIDE_CONFIG_PROPERTY_NAME;
38 | import static com.alibaba.boot.dubbo.util.DubboUtils.filterDubboProperties;
39 |
40 | /**
41 | * {@link DubboUtils} Test
42 | *
43 | *
44 | * @see DubboUtils
45 | * @since 1.0.0
46 | */
47 | public class DubboUtilsTest {
48 |
49 | @Test
50 | public void testConstants() {
51 |
52 | Assert.assertEquals("dubbo", DUBBO_PREFIX);
53 |
54 | Assert.assertEquals("dubbo.scan", DUBBO_SCAN_PREFIX);
55 |
56 | Assert.assertEquals("dubbo.scan.base-packages", BASE_PACKAGES_PROPERTY_NAME);
57 |
58 | Assert.assertEquals("dubbo.config", DUBBO_CONFIG_PREFIX);
59 |
60 | Assert.assertEquals("dubbo.config.multiple", MULTIPLE_CONFIG_PROPERTY_NAME);
61 |
62 | Assert.assertEquals("dubbo.config.override", OVERRIDE_CONFIG_PROPERTY_NAME);
63 |
64 | Assert.assertEquals("https://github.com/apache/incubator-dubbo-spring-boot-project", DUBBO_SPRING_BOOT_GITHUB_URL);
65 | Assert.assertEquals("https://github.com/apache/incubator-dubbo-spring-boot-project.git", DUBBO_SPRING_BOOT_GIT_URL);
66 | Assert.assertEquals("https://github.com/apache/incubator-dubbo-spring-boot-project/issues", DUBBO_SPRING_BOOT_ISSUES_URL);
67 |
68 | Assert.assertEquals("https://github.com/apache/incubator-dubbo", DUBBO_GITHUB_URL);
69 |
70 | Assert.assertEquals("dev@dubbo.apache.org", DUBBO_MAILING_LIST);
71 |
72 | Assert.assertFalse(DEFAULT_MULTIPLE_CONFIG_PROPERTY_VALUE);
73 |
74 | Assert.assertTrue(DEFAULT_OVERRIDE_CONFIG_PROPERTY_VALUE);
75 |
76 | }
77 |
78 |
79 | @Test
80 | public void testFilterDubboProperties() {
81 |
82 | MockEnvironment environment = new MockEnvironment();
83 | environment.setProperty("message", "Hello,World");
84 | environment.setProperty(MULTIPLE_CONFIG_PROPERTY_NAME, "true");
85 | environment.setProperty(OVERRIDE_CONFIG_PROPERTY_NAME, "true");
86 |
87 | SortedMap dubboProperties = filterDubboProperties(environment);
88 |
89 | Assert.assertEquals("true",dubboProperties.get(MULTIPLE_CONFIG_PROPERTY_NAME));
90 | Assert.assertEquals("true",dubboProperties.get(OVERRIDE_CONFIG_PROPERTY_NAME));
91 |
92 | }
93 |
94 | }
95 |
--------------------------------------------------------------------------------
/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/util/EnvironmentUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alibaba.boot.dubbo.util;
18 |
19 | import org.springframework.core.env.CompositePropertySource;
20 | import org.springframework.core.env.ConfigurableEnvironment;
21 | import org.springframework.core.env.EnumerablePropertySource;
22 | import org.springframework.core.env.Environment;
23 | import org.springframework.core.env.MutablePropertySources;
24 | import org.springframework.core.env.PropertySource;
25 | import org.springframework.util.ObjectUtils;
26 |
27 | import java.util.Collections;
28 | import java.util.LinkedHashMap;
29 | import java.util.Map;
30 |
31 | /**
32 | * The utilities class for {@link Environment}
33 | *
34 | * @see Environment
35 | * @since 1.0.0
36 | */
37 | public abstract class EnvironmentUtils {
38 |
39 | /**
40 | * Extras The properties from {@link ConfigurableEnvironment}
41 | *
42 | * @param environment {@link ConfigurableEnvironment}
43 | * @return Read-only Map
44 | */
45 | public static Map extractProperties(ConfigurableEnvironment environment) {
46 | return Collections.unmodifiableMap(doExtraProperties(environment));
47 | }
48 |
49 | // /**
50 | // * Gets {@link PropertySource} Map , the {@link PropertySource#getName()} as key
51 | // *
52 | // * @param environment {@link ConfigurableEnvironment}
53 | // * @return Read-only Map
54 | // */
55 | // public static Map> getPropertySources(ConfigurableEnvironment environment) {
56 | // return Collections.unmodifiableMap(doGetPropertySources(environment));
57 | // }
58 |
59 | private static Map doExtraProperties(ConfigurableEnvironment environment) {
60 | // 注意,是顺序的 HashMap
61 | Map properties = new LinkedHashMap<>(); // orderly
62 | // 获得所有 PropertySource 的 Map
63 | Map> map = doGetPropertySources(environment);
64 | for (PropertySource> source : map.values()) {
65 | // 如果是 EnumerablePropertySource 类型
66 | if (source instanceof EnumerablePropertySource) {
67 | EnumerablePropertySource propertySource = (EnumerablePropertySource) source;
68 | // 忽略无属性值
69 | String[] propertyNames = propertySource.getPropertyNames();
70 | if (ObjectUtils.isEmpty(propertyNames)) {
71 | continue;
72 | }
73 | // 遍历 propertyNames 数组,添加对应属性到 properties 中
74 | for (String propertyName : propertyNames) {
75 | if (!properties.containsKey(propertyName)) { // put If absent
76 | properties.put(propertyName, propertySource.getProperty(propertyName));
77 | }
78 | }
79 | }
80 |
81 | }
82 |
83 | return properties;
84 | }
85 |
86 | private static Map> doGetPropertySources(ConfigurableEnvironment environment) {
87 | Map> map = new LinkedHashMap>();
88 | MutablePropertySources sources = environment.getPropertySources();
89 | // 遍历 MutablePropertySource 数组,转换成 Map 集合
90 | for (PropertySource> source : sources) {
91 | extract("", map, source);
92 | }
93 | return map;
94 | }
95 |
96 | private static void extract(String root, Map> map, PropertySource> source) {
97 | if (source instanceof CompositePropertySource) {
98 | for (PropertySource> nest : ((CompositePropertySource) source).getPropertySources()) {
99 | extract(source.getName() + ":", map, nest);
100 | }
101 | } else {
102 | map.put(root + source.getName(), source);
103 | }
104 | }
105 |
106 | }
107 |
--------------------------------------------------------------------------------
/dubbo-spring-boot-distribution/pom.xml:
--------------------------------------------------------------------------------
1 |
17 |
20 |
21 | com.alibaba.boot
22 | dubbo-spring-boot-parent
23 | ${revision}
24 | ../dubbo-spring-boot-parent/pom.xml
25 |
26 | 4.0.0
27 |
28 | dubbo-spring-boot-distribution
29 | pom
30 | Dubbo Spring Boot Distribution
31 | Dubbo Spring Boot Distribution
32 |
33 |
34 |
35 |
36 |
37 | com.alibaba.boot
38 | dubbo-spring-boot-actuator
39 | ${revision}
40 |
41 |
42 |
43 | com.alibaba.boot
44 | dubbo-spring-boot-autoconfigure
45 | ${revision}
46 |
47 |
48 |
49 | com.alibaba.boot
50 | dubbo-spring-boot-starter
51 | ${revision}
52 |
53 |
54 |
55 |
56 |
57 |
58 | release
59 |
60 | apache-dubbo-spring-boot-project-incubating-${project.version}
61 |
62 |
63 | maven-assembly-plugin
64 | 3.1.0
65 |
66 |
67 | bin-release
68 | package
69 |
70 | single
71 |
72 |
73 |
74 | assembly/bin-release.xml
75 |
76 |
77 |
78 |
79 | source-release
80 | package
81 |
82 | single
83 |
84 |
85 |
86 | assembly/source-release.xml
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 | org.apache.maven.plugins
102 | maven-deploy-plugin
103 | 2.8.2
104 |
105 | true
106 |
107 |
108 |
109 |
110 |
111 |
112 |
113 |
--------------------------------------------------------------------------------
/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/env/DubboDefaultPropertiesEnvironmentPostProcessorTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alibaba.boot.dubbo.env;
18 |
19 | import org.junit.Assert;
20 | import org.junit.Test;
21 | import org.springframework.boot.SpringApplication;
22 | import org.springframework.core.Ordered;
23 | import org.springframework.core.env.MapPropertySource;
24 | import org.springframework.core.env.MutablePropertySources;
25 | import org.springframework.core.env.PropertySource;
26 | import org.springframework.mock.env.MockEnvironment;
27 |
28 | import java.util.HashMap;
29 |
30 | /**
31 | * {@link DubboDefaultPropertiesEnvironmentPostProcessor} Test
32 | */
33 | public class DubboDefaultPropertiesEnvironmentPostProcessorTest {
34 |
35 | private DubboDefaultPropertiesEnvironmentPostProcessor instance =
36 | new DubboDefaultPropertiesEnvironmentPostProcessor();
37 |
38 | private SpringApplication springApplication = new SpringApplication();
39 |
40 | @Test
41 | public void testOrder() {
42 | Assert.assertEquals(Ordered.LOWEST_PRECEDENCE, instance.getOrder());
43 | }
44 |
45 | @Test
46 | public void testPostProcessEnvironment() {
47 | MockEnvironment environment = new MockEnvironment();
48 | // Case 1 : Not Any property
49 | instance.postProcessEnvironment(environment, springApplication);
50 | // Get PropertySources
51 | MutablePropertySources propertySources = environment.getPropertySources();
52 | // Nothing to change
53 | PropertySource defaultPropertySource = propertySources.get("defaultProperties");
54 | Assert.assertNotNull(defaultPropertySource);
55 | Assert.assertEquals("true", defaultPropertySource.getProperty("dubbo.config.multiple"));
56 | Assert.assertEquals("false", defaultPropertySource.getProperty("dubbo.application.qos-enable"));
57 |
58 | // Case 2 : Only set property "spring.application.name"
59 | environment.setProperty("spring.application.name", "demo-dubbo-application");
60 | instance.postProcessEnvironment(environment, springApplication);
61 | defaultPropertySource = propertySources.get("defaultProperties");
62 | Object dubboApplicationName = defaultPropertySource.getProperty("dubbo.application.name");
63 | Assert.assertEquals("demo-dubbo-application", dubboApplicationName);
64 |
65 | // Case 3 : Only set property "dubbo.application.name"
66 | // Rest environment
67 | environment = new MockEnvironment();
68 | propertySources = environment.getPropertySources();
69 | environment.setProperty("dubbo.application.name", "demo-dubbo-application");
70 | instance.postProcessEnvironment(environment, springApplication);
71 | defaultPropertySource = propertySources.get("defaultProperties");
72 | Assert.assertNotNull(defaultPropertySource);
73 | dubboApplicationName = environment.getProperty("dubbo.application.name");
74 | Assert.assertEquals("demo-dubbo-application", dubboApplicationName);
75 |
76 | // Case 4 : If "defaultProperties" PropertySource is present in PropertySources
77 | // Rest environment
78 | environment = new MockEnvironment();
79 | propertySources = environment.getPropertySources();
80 | propertySources.addLast(new MapPropertySource("defaultProperties", new HashMap()));
81 | environment.setProperty("spring.application.name", "demo-dubbo-application");
82 | instance.postProcessEnvironment(environment, springApplication);
83 | defaultPropertySource = propertySources.get("defaultProperties");
84 | dubboApplicationName = defaultPropertySource.getProperty("dubbo.application.name");
85 | Assert.assertEquals("demo-dubbo-application", dubboApplicationName);
86 |
87 | // Case 5 : Rest dubbo.config.multiple and dubbo.application.qos-enable
88 | environment = new MockEnvironment();
89 | propertySources = environment.getPropertySources();
90 | propertySources.addLast(new MapPropertySource("defaultProperties", new HashMap()));
91 | environment.setProperty("dubbo.config.multiple", "false");
92 | environment.setProperty("dubbo.application.qos-enable", "true");
93 | instance.postProcessEnvironment(environment, springApplication);
94 | Assert.assertEquals("false", environment.getProperty("dubbo.config.multiple"));
95 | Assert.assertEquals("true", environment.getProperty("dubbo.application.qos-enable"));
96 | }
97 | }
--------------------------------------------------------------------------------
/dubbo-spring-boot-actuator/src/main/java/com/alibaba/boot/dubbo/actuate/endpoint/AbstractDubboEndpoint.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alibaba.boot.dubbo.actuate.endpoint;
18 |
19 | import com.alibaba.dubbo.config.ProtocolConfig;
20 | import com.alibaba.dubbo.config.spring.ServiceBean;
21 | import com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor;
22 | import org.springframework.beans.BeansException;
23 | import org.springframework.beans.factory.BeanFactoryUtils;
24 | import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
25 | import org.springframework.context.ApplicationContext;
26 | import org.springframework.context.ApplicationContextAware;
27 | import org.springframework.context.EnvironmentAware;
28 | import org.springframework.core.env.ConfigurableEnvironment;
29 | import org.springframework.core.env.Environment;
30 |
31 | import java.beans.BeanInfo;
32 | import java.beans.Introspector;
33 | import java.beans.PropertyDescriptor;
34 | import java.lang.reflect.Method;
35 | import java.math.BigDecimal;
36 | import java.math.BigInteger;
37 | import java.net.URL;
38 | import java.util.Date;
39 | import java.util.LinkedHashMap;
40 | import java.util.Map;
41 |
42 | import static org.springframework.util.ClassUtils.isPrimitiveOrWrapper;
43 |
44 | /**
45 | * Abstract Dubbo {@link Endpoint @Endpoint}
46 | *
47 | *
48 | * @since 0.2.0
49 | */
50 | public abstract class AbstractDubboEndpoint implements ApplicationContextAware, EnvironmentAware {
51 |
52 | protected ApplicationContext applicationContext;
53 |
54 | protected ConfigurableEnvironment environment;
55 |
56 | @Override
57 | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
58 | this.applicationContext = applicationContext;
59 | }
60 |
61 | @Override
62 | public void setEnvironment(Environment environment) {
63 | if (environment instanceof ConfigurableEnvironment) {
64 | this.environment = (ConfigurableEnvironment) environment;
65 | }
66 | }
67 |
68 | /**
69 | * 解析 Bean 的元数据
70 | *
71 | * @param bean Bean 对象
72 | * @return 元数据
73 | */
74 | protected Map resolveBeanMetadata(final Object bean) {
75 | // 创建 Map
76 | final Map beanMetadata = new LinkedHashMap<>();
77 | try {
78 | // 获得 BeanInfo 对象
79 | BeanInfo beanInfo = Introspector.getBeanInfo(bean.getClass());
80 | // 获得 PropertyDescriptor 数组
81 | PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
82 | // 遍历 PropertyDescriptor 数组
83 | for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
84 | // 获得 Method 对象
85 | Method readMethod = propertyDescriptor.getReadMethod();
86 | // 读取属性,添加到 beanMetadata 中
87 | if (readMethod != null && isSimpleType(propertyDescriptor.getPropertyType())) {
88 | String name = Introspector.decapitalize(propertyDescriptor.getName());
89 | Object value = readMethod.invoke(bean);
90 | beanMetadata.put(name, value);
91 | }
92 | }
93 | } catch (Exception e) {
94 | throw new RuntimeException(e);
95 | }
96 | return beanMetadata;
97 | }
98 |
99 | protected Map getServiceBeansMap() {
100 | return BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ServiceBean.class);
101 | }
102 |
103 | protected ReferenceAnnotationBeanPostProcessor getReferenceAnnotationBeanPostProcessor() {
104 | return applicationContext.getBean(ReferenceAnnotationBeanPostProcessor.BEAN_NAME, ReferenceAnnotationBeanPostProcessor.class);
105 | }
106 |
107 | protected Map getProtocolConfigsBeanMap() {
108 | return BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, ProtocolConfig.class);
109 | }
110 |
111 | private static boolean isSimpleType(Class> type) {
112 | return isPrimitiveOrWrapper(type) // 基本类型 or 包装类型
113 | || type == String.class
114 | || type == BigDecimal.class
115 | || type == BigInteger.class
116 | || type == Date.class
117 | || type == URL.class
118 | || type == Class.class
119 | ;
120 | }
121 |
122 | }
--------------------------------------------------------------------------------
/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/util/DubboUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alibaba.boot.dubbo.util;
18 |
19 | import org.springframework.core.env.ConfigurableEnvironment;
20 |
21 | import java.util.Collections;
22 | import java.util.Map;
23 | import java.util.SortedMap;
24 | import java.util.TreeMap;
25 |
26 | /**
27 | * The utilities class for Dubbo
28 | *
29 | * @since 1.0.0
30 | */
31 | public abstract class DubboUtils {
32 |
33 | /**
34 | * line separator
35 | */
36 | public static final String LINE_SEPARATOR = System.getProperty("line.separator");
37 |
38 |
39 | /**
40 | * The separator of property name
41 | */
42 | public static final String PROPERTY_NAME_SEPARATOR = ".";
43 |
44 | /**
45 | * The prefix of property name of Dubbo
46 | */
47 | public static final String DUBBO_PREFIX = "dubbo";
48 |
49 | /**
50 | * The prefix of property name for Dubbo scan
51 | */
52 | public static final String DUBBO_SCAN_PREFIX = DUBBO_PREFIX + PROPERTY_NAME_SEPARATOR + "scan";
53 |
54 | /**
55 | * The prefix of property name for Dubbo Config.ØØ
56 | */
57 | public static final String DUBBO_CONFIG_PREFIX = DUBBO_PREFIX + PROPERTY_NAME_SEPARATOR + "config";
58 |
59 | /**
60 | * The property name of base packages to scan
61 | *
62 | * The default value is empty set.
63 | */
64 | public static final String BASE_PACKAGES_PROPERTY_NAME = DUBBO_SCAN_PREFIX + PROPERTY_NAME_SEPARATOR + "base-packages";
65 |
66 | /**
67 | * The property name of multiple properties binding from externalized configuration
68 | *
69 | * The default value is {@link #DEFAULT_MULTIPLE_CONFIG_PROPERTY_VALUE}
70 | */
71 | public static final String MULTIPLE_CONFIG_PROPERTY_NAME = DUBBO_CONFIG_PREFIX + PROPERTY_NAME_SEPARATOR + "multiple";
72 |
73 | /**
74 | * The default value of multiple properties binding from externalized configuration
75 | */
76 | public static final boolean DEFAULT_MULTIPLE_CONFIG_PROPERTY_VALUE = false;
77 |
78 | /**
79 | * The property name of override Dubbo config
80 | *
81 | * The default value is {@link #DEFAULT_OVERRIDE_CONFIG_PROPERTY_VALUE}
82 | */
83 | public static final String OVERRIDE_CONFIG_PROPERTY_NAME = DUBBO_CONFIG_PREFIX + PROPERTY_NAME_SEPARATOR + "override";
84 |
85 | /**
86 | * The default property value of override Dubbo config
87 | */
88 | public static final boolean DEFAULT_OVERRIDE_CONFIG_PROPERTY_VALUE = true;
89 |
90 |
91 | /**
92 | * The github URL of Dubbo Spring Boot
93 | */
94 | public static final String DUBBO_SPRING_BOOT_GITHUB_URL = "https://github.com/apache/incubator-dubbo-spring-boot-project";
95 |
96 | /**
97 | * The git URL of Dubbo Spring Boot
98 | */
99 | public static final String DUBBO_SPRING_BOOT_GIT_URL = "https://github.com/apache/incubator-dubbo-spring-boot-project.git";
100 |
101 | /**
102 | * The issues of Dubbo Spring Boot
103 | */
104 | public static final String DUBBO_SPRING_BOOT_ISSUES_URL = "https://github.com/apache/incubator-dubbo-spring-boot-project/issues";
105 |
106 | /**
107 | * The github URL of Dubbo
108 | */
109 | public static final String DUBBO_GITHUB_URL = "https://github.com/apache/incubator-dubbo";
110 |
111 | /**
112 | * The google group URL of Dubbo
113 | */
114 | public static final String DUBBO_MAILING_LIST = "dev@dubbo.apache.org";
115 |
116 | /**
117 | * Filters Dubbo Properties from {@link ConfigurableEnvironment}
118 | *
119 | * @param environment {@link ConfigurableEnvironment}
120 | * @return Read-only SortedMap
121 | */
122 | public static SortedMap filterDubboProperties(ConfigurableEnvironment environment) {
123 | SortedMap dubboProperties = new TreeMap<>();
124 | // 获得所有的配置
125 | Map properties = EnvironmentUtils.extractProperties(environment);
126 | // 遍历配置,如果以 "dubbo." 开头,则添加到 dubboProperties 中
127 | for (Map.Entry entry : properties.entrySet()) {
128 | String propertyName = entry.getKey();
129 | if (propertyName.startsWith(DUBBO_PREFIX + PROPERTY_NAME_SEPARATOR)
130 | && entry.getValue() != null) {
131 | dubboProperties.put(propertyName, entry.getValue().toString());
132 | }
133 | }
134 | // 返回 dubboProperties
135 | return Collections.unmodifiableSortedMap(dubboProperties);
136 | }
137 |
138 | }
139 |
--------------------------------------------------------------------------------
/mvnw.cmd:
--------------------------------------------------------------------------------
1 | @REM ----------------------------------------------------------------------------
2 | @REM Licensed to the Apache Software Foundation (ASF) under one
3 | @REM or more contributor license agreements. See the NOTICE file
4 | @REM distributed with this work for additional information
5 | @REM regarding copyright ownership. The ASF licenses this file
6 | @REM to you under the Apache License, Version 2.0 (the
7 | @REM "License"); you may not use this file except in compliance
8 | @REM with the License. You may obtain a copy of the License at
9 | @REM
10 | @REM http://www.apache.org/licenses/LICENSE-2.0
11 | @REM
12 | @REM Unless required by applicable law or agreed to in writing,
13 | @REM software distributed under the License is distributed on an
14 | @REM "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 | @REM KIND, either express or implied. See the License for the
16 | @REM specific language governing permissions and limitations
17 | @REM under the License.
18 | @REM ----------------------------------------------------------------------------
19 |
20 | @REM ----------------------------------------------------------------------------
21 | @REM Maven2 Start Up Batch script
22 | @REM
23 | @REM Required ENV vars:
24 | @REM JAVA_HOME - location of a JDK home dir
25 | @REM
26 | @REM Optional ENV vars
27 | @REM M2_HOME - location of maven2's installed home dir
28 | @REM MAVEN_BATCH_ECHO - set to 'on' to enable the echoing of the batch commands
29 | @REM MAVEN_BATCH_PAUSE - set to 'on' to wait for a key stroke before ending
30 | @REM MAVEN_OPTS - parameters passed to the Java VM when running Maven
31 | @REM e.g. to debug Maven itself, use
32 | @REM set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
33 | @REM MAVEN_SKIP_RC - flag to disable loading of mavenrc files
34 | @REM ----------------------------------------------------------------------------
35 |
36 | @REM Begin all REM lines with '@' in case MAVEN_BATCH_ECHO is 'on'
37 | @echo off
38 | @REM enable echoing my setting MAVEN_BATCH_ECHO to 'on'
39 | @if "%MAVEN_BATCH_ECHO%" == "on" echo %MAVEN_BATCH_ECHO%
40 |
41 | @REM set %HOME% to equivalent of $HOME
42 | if "%HOME%" == "" (set "HOME=%HOMEDRIVE%%HOMEPATH%")
43 |
44 | @REM Execute a user defined script before this one
45 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPre
46 | @REM check for pre script, once with legacy .bat ending and once with .cmd ending
47 | if exist "%HOME%\mavenrc_pre.bat" call "%HOME%\mavenrc_pre.bat"
48 | if exist "%HOME%\mavenrc_pre.cmd" call "%HOME%\mavenrc_pre.cmd"
49 | :skipRcPre
50 |
51 | @setlocal
52 |
53 | set ERROR_CODE=0
54 |
55 | @REM To isolate internal variables from possible post scripts, we use another setlocal
56 | @setlocal
57 |
58 | @REM ==== START VALIDATION ====
59 | if not "%JAVA_HOME%" == "" goto OkJHome
60 |
61 | echo.
62 | echo Error: JAVA_HOME not found in your environment. >&2
63 | echo Please set the JAVA_HOME variable in your environment to match the >&2
64 | echo location of your Java installation. >&2
65 | echo.
66 | goto error
67 |
68 | :OkJHome
69 | if exist "%JAVA_HOME%\bin\java.exe" goto init
70 |
71 | echo.
72 | echo Error: JAVA_HOME is set to an invalid directory. >&2
73 | echo JAVA_HOME = "%JAVA_HOME%" >&2
74 | echo Please set the JAVA_HOME variable in your environment to match the >&2
75 | echo location of your Java installation. >&2
76 | echo.
77 | goto error
78 |
79 | @REM ==== END VALIDATION ====
80 |
81 | :init
82 |
83 | @REM Find the project base dir, i.e. the directory that contains the folder ".mvn".
84 | @REM Fallback to current working directory if not found.
85 |
86 | set MAVEN_PROJECTBASEDIR=%MAVEN_BASEDIR%
87 | IF NOT "%MAVEN_PROJECTBASEDIR%"=="" goto endDetectBaseDir
88 |
89 | set EXEC_DIR=%CD%
90 | set WDIR=%EXEC_DIR%
91 | :findBaseDir
92 | IF EXIST "%WDIR%"\.mvn goto baseDirFound
93 | cd ..
94 | IF "%WDIR%"=="%CD%" goto baseDirNotFound
95 | set WDIR=%CD%
96 | goto findBaseDir
97 |
98 | :baseDirFound
99 | set MAVEN_PROJECTBASEDIR=%WDIR%
100 | cd "%EXEC_DIR%"
101 | goto endDetectBaseDir
102 |
103 | :baseDirNotFound
104 | set MAVEN_PROJECTBASEDIR=%EXEC_DIR%
105 | cd "%EXEC_DIR%"
106 |
107 | :endDetectBaseDir
108 |
109 | IF NOT EXIST "%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config" goto endReadAdditionalConfig
110 |
111 | @setlocal EnableExtensions EnableDelayedExpansion
112 | for /F "usebackq delims=" %%a in ("%MAVEN_PROJECTBASEDIR%\.mvn\jvm.config") do set JVM_CONFIG_MAVEN_PROPS=!JVM_CONFIG_MAVEN_PROPS! %%a
113 | @endlocal & set JVM_CONFIG_MAVEN_PROPS=%JVM_CONFIG_MAVEN_PROPS%
114 |
115 | :endReadAdditionalConfig
116 |
117 | SET MAVEN_JAVA_EXE="%JAVA_HOME%\bin\java.exe"
118 |
119 | set WRAPPER_JAR="%MAVEN_PROJECTBASEDIR%\.mvn\wrapper\maven-wrapper.jar"
120 | set WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
121 |
122 | %MAVEN_JAVA_EXE% %JVM_CONFIG_MAVEN_PROPS% %MAVEN_OPTS% %MAVEN_DEBUG_OPTS% -classpath %WRAPPER_JAR% "-Dmaven.multiModuleProjectDirectory=%MAVEN_PROJECTBASEDIR%" %WRAPPER_LAUNCHER% %MAVEN_CONFIG% %*
123 | if ERRORLEVEL 1 goto error
124 | goto end
125 |
126 | :error
127 | set ERROR_CODE=1
128 |
129 | :end
130 | @endlocal & set ERROR_CODE=%ERROR_CODE%
131 |
132 | if not "%MAVEN_SKIP_RC%" == "" goto skipRcPost
133 | @REM check for post script, once with legacy .bat ending and once with .cmd ending
134 | if exist "%HOME%\mavenrc_post.bat" call "%HOME%\mavenrc_post.bat"
135 | if exist "%HOME%\mavenrc_post.cmd" call "%HOME%\mavenrc_post.cmd"
136 | :skipRcPost
137 |
138 | @REM pause the script if MAVEN_BATCH_PAUSE is set to 'on'
139 | if "%MAVEN_BATCH_PAUSE%" == "on" pause
140 |
141 | if "%MAVEN_TERMINATE_CMD%" == "on" exit %ERROR_CODE%
142 |
143 | exit /B %ERROR_CODE%
144 |
145 |
--------------------------------------------------------------------------------
/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/autoconfigure/DubboAutoConfiguration.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alibaba.boot.dubbo.autoconfigure;
18 |
19 | import com.alibaba.dubbo.config.AbstractConfig;
20 | import com.alibaba.dubbo.config.ApplicationConfig;
21 | import com.alibaba.dubbo.config.annotation.Reference;
22 | import com.alibaba.dubbo.config.annotation.Service;
23 | import com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor;
24 | import com.alibaba.dubbo.config.spring.beans.factory.annotation.ServiceAnnotationBeanPostProcessor;
25 | import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
26 | import com.alibaba.dubbo.config.spring.context.annotation.DubboConfigConfiguration;
27 | import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
28 | import com.alibaba.dubbo.config.spring.context.annotation.EnableDubboConfig;
29 |
30 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
31 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
32 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
33 | import org.springframework.boot.context.properties.bind.Binder;
34 | import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
35 | import org.springframework.context.annotation.Bean;
36 | import org.springframework.context.annotation.Configuration;
37 | import org.springframework.context.annotation.Scope;
38 | import org.springframework.core.env.Environment;
39 |
40 | import java.util.Set;
41 |
42 | import static com.alibaba.boot.dubbo.util.DubboUtils.BASE_PACKAGES_PROPERTY_NAME;
43 | import static com.alibaba.boot.dubbo.util.DubboUtils.DUBBO_PREFIX;
44 | import static com.alibaba.boot.dubbo.util.DubboUtils.MULTIPLE_CONFIG_PROPERTY_NAME;
45 | import static java.util.Collections.emptySet;
46 | import static org.springframework.beans.factory.config.ConfigurableBeanFactory.SCOPE_PROTOTYPE;
47 |
48 | /**
49 | * Dubbo Auto {@link Configuration}
50 | *
51 | * @see ApplicationConfig
52 | * @see Service
53 | * @see Reference
54 | * @see DubboComponentScan
55 | * @see EnableDubboConfig
56 | * @see EnableDubbo
57 | * @since 1.0.0
58 | */
59 | @Configuration // 配置类
60 | @ConditionalOnProperty(prefix = DUBBO_PREFIX, name = "enabled", matchIfMissing = true, havingValue = "true") // 要求配置了 "dubbo.enabled=true" 或者,"dubbo.enabled" 未配置
61 | @ConditionalOnClass(AbstractConfig.class) // AbstractConfig 类存在的时候,即用于判断有 dubbo 库
62 | public class DubboAutoConfiguration {
63 |
64 | /**
65 | * Creates {@link ServiceAnnotationBeanPostProcessor} Bean
66 | *
67 | * @param environment {@link Environment} Bean
68 | * @return {@link ServiceAnnotationBeanPostProcessor}
69 | */
70 | @ConditionalOnProperty(name = BASE_PACKAGES_PROPERTY_NAME) // 配置了 "dubbo.scan.base-package" 属性,即要扫描 Dubbo 注解的包
71 | @ConditionalOnClass(ConfigurationPropertySources.class) // 有 Spring Boot 配置加载的功能
72 | @Bean
73 | public ServiceAnnotationBeanPostProcessor serviceAnnotationBeanPostProcessor(Environment environment) {
74 | // 获得 "dubbo.scan.base-package" 属性
75 | Set packagesToScan = environment.getProperty(BASE_PACKAGES_PROPERTY_NAME, Set.class, emptySet());
76 | // 创建 ServiceAnnotationBeanPostProcessor 对象
77 | return new ServiceAnnotationBeanPostProcessor(packagesToScan);
78 | }
79 |
80 | @ConditionalOnClass(Binder.class) // 存在 Binder 类的时候
81 | @Bean
82 | @Scope(scopeName = SCOPE_PROTOTYPE) // 多例
83 | public RelaxedDubboConfigBinder relaxedDubboConfigBinder() {
84 | return new RelaxedDubboConfigBinder();
85 | }
86 |
87 | /**
88 | * Creates {@link ReferenceAnnotationBeanPostProcessor} Bean if Absent
89 | *
90 | * @return {@link ReferenceAnnotationBeanPostProcessor}
91 | */
92 | @ConditionalOnMissingBean // 不存在 ReferenceAnnotationBeanPostProcessor Bean 的时候
93 | @Bean(name = ReferenceAnnotationBeanPostProcessor.BEAN_NAME) // Bean 的名字是 referenceAnnotationBeanPostProcessor
94 | public ReferenceAnnotationBeanPostProcessor referenceAnnotationBeanPostProcessor() {
95 | return new ReferenceAnnotationBeanPostProcessor();
96 | }
97 |
98 | /**
99 | * Single Dubbo Config Configuration
100 | *
101 | * @see EnableDubboConfig
102 | * @see DubboConfigConfiguration.Single
103 | */
104 | @EnableDubboConfig
105 | protected static class SingleDubboConfigConfiguration {
106 | }
107 |
108 | /**
109 | * Multiple Dubbo Config Configuration , equals @EnableDubboConfig.multiple() == true
110 | *
111 | * @see EnableDubboConfig
112 | * @see DubboConfigConfiguration.Multiple
113 | */
114 | @ConditionalOnProperty(name = MULTIPLE_CONFIG_PROPERTY_NAME, havingValue = "true") // 要求配置 "dubbo.config.multiple=true" 。默认情况下,Dubbo 自带 "dubbo.config.multiple=true"
115 | @EnableDubboConfig(multiple = true)
116 | protected static class MultipleDubboConfigConfiguration {
117 | }
118 |
119 | }
120 |
--------------------------------------------------------------------------------
/dubbo-spring-boot-autoconfigure/src/test/java/com/alibaba/boot/dubbo/autoconfigure/DubboAutoConfigurationOnSingleConfigTest.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alibaba.boot.dubbo.autoconfigure;
18 |
19 | import com.alibaba.dubbo.config.ApplicationConfig;
20 | import com.alibaba.dubbo.config.ConsumerConfig;
21 | import com.alibaba.dubbo.config.ModuleConfig;
22 | import com.alibaba.dubbo.config.MonitorConfig;
23 | import com.alibaba.dubbo.config.ProtocolConfig;
24 | import com.alibaba.dubbo.config.ProviderConfig;
25 | import com.alibaba.dubbo.config.RegistryConfig;
26 | import com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor;
27 | import com.alibaba.dubbo.config.spring.beans.factory.annotation.ServiceAnnotationBeanPostProcessor;
28 |
29 | import org.junit.Assert;
30 | import org.junit.Test;
31 | import org.junit.runner.RunWith;
32 | import org.springframework.beans.factory.annotation.Autowired;
33 | import org.springframework.boot.test.context.SpringBootTest;
34 | import org.springframework.context.ApplicationContext;
35 | import org.springframework.core.env.Environment;
36 | import org.springframework.test.context.TestPropertySource;
37 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
38 |
39 | /**
40 | * {@link DubboAutoConfiguration} Test On single Dubbo Configuration
41 | *
42 | * @since 1.0.0
43 | */
44 | @RunWith(SpringJUnit4ClassRunner.class)
45 | @TestPropertySource(
46 | properties = {
47 | "dubbo.application.name = dubbo-demo-application",
48 | "dubbo.module.name = dubbo-demo-module",
49 | "dubbo.registry.address = zookeeper://192.168.99.100:32770",
50 | "dubbo.protocol.name=dubbo",
51 | "dubbo.protocol.port=20880",
52 | "dubbo.monitor.address=zookeeper://127.0.0.1:32770",
53 | "dubbo.provider.host=127.0.0.1",
54 | "dubbo.consumer.client=netty"
55 | }
56 | )
57 | @SpringBootTest(
58 | classes = {DubboAutoConfiguration.class}
59 | )
60 | public class DubboAutoConfigurationOnSingleConfigTest {
61 |
62 | @Autowired
63 | private ApplicationConfig applicationConfig;
64 |
65 | @Autowired
66 | private ModuleConfig moduleConfig;
67 |
68 | @Autowired
69 | private RegistryConfig registryConfig;
70 |
71 | @Autowired
72 | private MonitorConfig monitorConfig;
73 |
74 | @Autowired
75 | private ProviderConfig providerConfig;
76 |
77 | @Autowired
78 | private ConsumerConfig consumerConfig;
79 |
80 | @Autowired
81 | private ProtocolConfig protocolConfig;
82 |
83 | @Autowired(required = false)
84 | private DubboAutoConfiguration.MultipleDubboConfigConfiguration multipleDubboConfigConfiguration;
85 |
86 | @Autowired
87 | private DubboAutoConfiguration.SingleDubboConfigConfiguration singleDubboConfigConfiguration;
88 |
89 | @Autowired
90 | private Environment environment;
91 |
92 | @Autowired
93 | private ApplicationContext applicationContext;
94 |
95 | @Autowired(required = false)
96 | private ServiceAnnotationBeanPostProcessor serviceAnnotationBeanPostProcessor;
97 |
98 | @Autowired
99 | private ReferenceAnnotationBeanPostProcessor referenceAnnotationBeanPostProcessor;
100 |
101 | @Test
102 | public void testApplicationConfig() {
103 |
104 | Assert.assertEquals("dubbo-demo-application", applicationConfig.getName());
105 |
106 | }
107 |
108 | @Test
109 | public void testModuleConfig() {
110 |
111 | Assert.assertEquals("dubbo-demo-module", moduleConfig.getName());
112 |
113 | }
114 |
115 | @Test
116 | public void testRegistryConfig() {
117 |
118 | Assert.assertEquals("zookeeper://192.168.99.100:32770", registryConfig.getAddress());
119 |
120 | }
121 |
122 | @Test
123 | public void testMonitorConfig() {
124 |
125 | Assert.assertEquals("zookeeper://127.0.0.1:32770", monitorConfig.getAddress());
126 |
127 | }
128 |
129 | @Test
130 | public void testProtocolConfig() {
131 |
132 | Assert.assertEquals("dubbo", protocolConfig.getName());
133 | Assert.assertEquals(Integer.valueOf(20880), protocolConfig.getPort());
134 |
135 | }
136 |
137 | @Test
138 | public void testProviderConfig() {
139 |
140 | Assert.assertEquals("127.0.0.1", providerConfig.getHost());
141 |
142 | }
143 |
144 | @Test
145 | public void testConsumerConfig() {
146 |
147 | Assert.assertEquals("netty", consumerConfig.getClient());
148 |
149 | }
150 |
151 | @Test
152 | public void testMultipleDubboConfigConfiguration() {
153 | Assert.assertNotNull(multipleDubboConfigConfiguration);
154 | }
155 |
156 | @Test
157 | public void testSingleDubboConfigConfiguration() {
158 | Assert.assertNotNull(singleDubboConfigConfiguration);
159 | }
160 |
161 | @Test
162 | public void testServiceAnnotationBeanPostProcessor() {
163 | Assert.assertNotNull(multipleDubboConfigConfiguration);
164 | }
165 |
166 | }
167 |
--------------------------------------------------------------------------------
/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/context/event/AwaitingNonWebApplicationListener.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alibaba.boot.dubbo.context.event;
18 |
19 | import org.slf4j.Logger;
20 | import org.slf4j.LoggerFactory;
21 | import org.springframework.boot.SpringApplication;
22 | import org.springframework.boot.WebApplicationType;
23 | import org.springframework.boot.context.event.ApplicationReadyEvent;
24 | import org.springframework.context.ApplicationEvent;
25 | import org.springframework.context.ApplicationListener;
26 | import org.springframework.context.event.ContextClosedEvent;
27 | import org.springframework.context.event.SmartApplicationListener;
28 | import org.springframework.util.ObjectUtils;
29 |
30 | import java.util.concurrent.ExecutorService;
31 | import java.util.concurrent.Executors;
32 | import java.util.concurrent.atomic.AtomicBoolean;
33 | import java.util.concurrent.locks.Condition;
34 | import java.util.concurrent.locks.Lock;
35 | import java.util.concurrent.locks.ReentrantLock;
36 |
37 | /**
38 | * Awaiting Non-Web Spring Boot {@link ApplicationListener}
39 | *
40 | * @since 0.1.1
41 | */
42 | public class AwaitingNonWebApplicationListener implements SmartApplicationListener {
43 |
44 | private static final Logger logger = LoggerFactory.getLogger(AwaitingNonWebApplicationListener.class);
45 |
46 | private static final Class extends ApplicationEvent>[] SUPPORTED_APPLICATION_EVENTS =
47 | of(ApplicationReadyEvent.class, ContextClosedEvent.class);
48 |
49 | /**
50 | * 是否已经等待完成
51 | */
52 | private static final AtomicBoolean awaited = new AtomicBoolean(false);
53 |
54 | private final Lock lock = new ReentrantLock();
55 |
56 | private final Condition condition = lock.newCondition();
57 |
58 | private final ExecutorService executorService = Executors.newSingleThreadExecutor();
59 |
60 | @Override
61 | public boolean supportsEventType(Class extends ApplicationEvent> eventType) {
62 | return ObjectUtils.containsElement(SUPPORTED_APPLICATION_EVENTS, eventType);
63 | }
64 |
65 | @Override
66 | public boolean supportsSourceType(Class> sourceType) {
67 | return true;
68 | }
69 |
70 | private static T[] of(T... values) {
71 | return values;
72 | }
73 |
74 | @Override
75 | public void onApplicationEvent(ApplicationEvent event) {
76 | if (event instanceof ApplicationReadyEvent) {
77 | onApplicationReadyEvent((ApplicationReadyEvent) event);
78 | } else if (event instanceof ContextClosedEvent) {
79 | onContextClosedEvent((ContextClosedEvent) event);
80 | }
81 | }
82 |
83 | @Override
84 | public int getOrder() {
85 | return LOWEST_PRECEDENCE;
86 | }
87 |
88 | protected void onApplicationReadyEvent(ApplicationReadyEvent event) {
89 | // 如果是 Web 环境,则直接返回
90 | final SpringApplication springApplication = event.getSpringApplication();
91 | if (!WebApplicationType.NONE.equals(springApplication.getWebApplicationType())) {
92 | return;
93 | }
94 | // 启动一个用户线程,从而实现等待
95 | await();
96 | }
97 |
98 | protected void onContextClosedEvent(ContextClosedEvent event) {
99 | // 释放
100 | release();
101 | // 关闭线程池
102 | shutdown();
103 | }
104 |
105 | protected void await() {
106 | // has been waited, return immediately
107 | // 如果已经处于阻塞等待,直接返回
108 | if (awaited.get()) {
109 | return;
110 | }
111 |
112 | // 创建任务,实现阻塞
113 | executorService.execute(() -> executeMutually(() -> {
114 | while (!awaited.get()) {
115 | if (logger.isInfoEnabled()) {
116 | logger.info(" [Dubbo] Current Spring Boot Application is await...");
117 | }
118 | try {
119 | condition.await();
120 | System.out.println("123");
121 | } catch (InterruptedException e) {
122 | Thread.currentThread().interrupt();
123 | }
124 | }
125 | }));
126 | }
127 |
128 | protected void release() {
129 | executeMutually(() -> {
130 | // CAS 设置 awaited 为 true
131 | while (awaited.compareAndSet(false, true)) {
132 | if (logger.isInfoEnabled()) {
133 | logger.info(" [Dubbo] Current Spring Boot Application is about to shutdown...");
134 | }
135 | // 通知 Condition
136 | condition.signalAll();
137 | }
138 | });
139 | }
140 |
141 | private void shutdown() {
142 | if (!executorService.isShutdown()) {
143 | // Shutdown executorService
144 | executorService.shutdown();
145 | }
146 | }
147 |
148 | private void executeMutually(Runnable runnable) {
149 | try {
150 | lock.lock();
151 | // 执行 Runnable
152 | runnable.run();
153 | } finally {
154 | lock.unlock();
155 | }
156 | }
157 |
158 | static AtomicBoolean getAwaited() {
159 | return awaited;
160 | }
161 | }
162 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
17 |
19 | 4.0.0
20 |
21 |
22 | org.sonatype.oss
23 | oss-parent
24 | 7
25 |
26 |
27 | com.alibaba.boot
28 | dubbo-spring-boot-project
29 | ${revision}
30 |
31 | pom
32 |
33 | Dubbo Spring Boot Project
34 | Dubbo Spring Boot Project
35 | https://github.com/apache/incubator-dubbo-spring-boot-project
36 |
37 |
38 | 3.0.1
39 | 2.19.1
40 | 0.2.1
41 |
42 |
43 |
44 | dubbo-spring-boot-parent
45 | dubbo-spring-boot-distribution
46 | dubbo-spring-boot-autoconfigure
47 | dubbo-spring-boot-starter
48 | dubbo-spring-boot-samples
49 | dubbo-spring-boot-actuator
50 |
51 |
52 |
53 | The Apache Software Foundation
54 | http://www.apache.org/
55 |
56 |
57 |
58 | https://github.com/apache/incubator-dubbo-spring-boot-project
59 | scm:git:git:////github.com/apache/incubator-dubbo-spring-boot-project.git
60 | scm:git:ssh://git@//github.com/apache/incubator-dubbo-spring-boot-project.git
61 |
62 |
63 |
64 |
65 | Github
66 | https://github.com/apache/incubator-dubbo-spring-boot-project/issues
67 |
68 |
69 |
70 |
71 | Development List
72 | dev-subscribe@dubbo.apache.org
73 | dev-unsubscribe@dubbo.apache.org
74 | dev@dubbo.apache.org
75 |
76 |
77 | Commits List
78 | commits-subscribe@dubbo.apache.org
79 | commits-unsubscribe@dubbo.apache.org
80 | commits@dubbo.apache.org
81 |
82 |
83 | Issues List
84 | issues-subscribe@dubbo.apache.org
85 | issues-unsubscribe@dubbo.apache.org
86 | issues@dubbo.apache.org
87 |
88 |
89 |
90 |
91 | Apache Dubbo (incubating)
92 | The Apache Dubbo (incubating) Project Contributors
93 | dev@dubbo.apache.org
94 | http://dubbo.apache.org
95 |
96 |
97 |
98 |
99 |
100 | release
101 |
102 |
103 |
104 | org.apache.maven.plugins
105 | maven-javadoc-plugin
106 |
107 |
108 | package
109 |
110 | jar
111 |
112 |
113 |
114 |
115 |
116 |
117 | org.apache.maven.plugins
118 | maven-gpg-plugin
119 |
120 |
121 | verify
122 |
123 | sign
124 |
125 |
126 |
127 |
128 |
129 |
130 | org.codehaus.mojo
131 | flatten-maven-plugin
132 | 1.1.0
133 |
134 | true
135 | resolveCiFriendliesOnly
136 |
137 |
138 |
139 | flatten
140 | process-resources
141 |
142 | flatten
143 |
144 |
145 |
146 | flatten.clean
147 | clean
148 |
149 | clean
150 |
151 |
152 |
153 |
154 |
155 |
156 |
157 |
158 |
159 |
160 |
--------------------------------------------------------------------------------
/dubbo-spring-boot-autoconfigure/src/main/java/com/alibaba/boot/dubbo/env/DubboDefaultPropertiesEnvironmentPostProcessor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
3 | * contributor license agreements. See the NOTICE file distributed with
4 | * this work for additional information regarding copyright ownership.
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
6 | * (the "License"); you may not use this file except in compliance with
7 | * the License. You may obtain a copy of the License at
8 | *
9 | * http://www.apache.org/licenses/LICENSE-2.0
10 | *
11 | * Unless required by applicable law or agreed to in writing, software
12 | * distributed under the License is distributed on an "AS IS" BASIS,
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 | * See the License for the specific language governing permissions and
15 | * limitations under the License.
16 | */
17 | package com.alibaba.boot.dubbo.env;
18 |
19 | import com.alibaba.dubbo.config.ApplicationConfig;
20 | import com.alibaba.dubbo.config.spring.context.annotation.EnableDubboConfig;
21 | import com.alibaba.dubbo.config.spring.context.annotation.EnableDubboConfigBinding;
22 |
23 | import org.springframework.boot.SpringApplication;
24 | import org.springframework.boot.context.ContextIdApplicationContextInitializer;
25 | import org.springframework.boot.env.EnvironmentPostProcessor;
26 | import org.springframework.context.ConfigurableApplicationContext;
27 | import org.springframework.core.Ordered;
28 | import org.springframework.core.env.ConfigurableEnvironment;
29 | import org.springframework.core.env.Environment;
30 | import org.springframework.core.env.MapPropertySource;
31 | import org.springframework.core.env.MutablePropertySources;
32 | import org.springframework.core.env.PropertySource;
33 | import org.springframework.util.CollectionUtils;
34 | import org.springframework.util.StringUtils;
35 |
36 | import java.util.HashMap;
37 | import java.util.Map;
38 | import java.util.Properties;
39 |
40 | /**
41 | * The lowest precedence {@link EnvironmentPostProcessor} processes
42 | * {@link SpringApplication#setDefaultProperties(Properties) Spring Boot default properties} for Dubbo
43 | * as late as possible before {@link ConfigurableApplicationContext#refresh() application context refresh}.
44 | */
45 | public class DubboDefaultPropertiesEnvironmentPostProcessor implements EnvironmentPostProcessor, Ordered {
46 |
47 | /**
48 | * The name of default {@link PropertySource} defined in SpringApplication#configurePropertySources method.
49 | */
50 | private static final String PROPERTY_SOURCE_NAME = "defaultProperties";
51 |
52 | /**
53 | * The property name of Spring Application
54 | *
55 | * @see ContextIdApplicationContextInitializer
56 | */
57 | private static final String SPRING_APPLICATION_NAME_PROPERTY = "spring.application.name";
58 |
59 | /**
60 | * The property name of {@link ApplicationConfig}
61 | *
62 | * @see EnableDubboConfig
63 | * @see EnableDubboConfigBinding
64 | */
65 | private static final String DUBBO_APPLICATION_NAME_PROPERTY = "dubbo.application.name";
66 |
67 | /**
68 | * The property name of {@link EnableDubboConfig#multiple() @EnableDubboConfig.multiple()}
69 | */
70 | private static final String DUBBO_CONFIG_MULTIPLE_PROPERTY = "dubbo.config.multiple";
71 |
72 | /**
73 | * The property name of {@link ApplicationConfig#getQosEnable() application's QOS enable}
74 | */
75 | private static final String DUBBO_APPLICATION_QOS_ENABLE_PROPERTY = "dubbo.application.qos-enable";
76 |
77 | @Override
78 | public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
79 | MutablePropertySources propertySources = environment.getPropertySources();
80 | // 生成 Dubbo 默认配置
81 | Map defaultProperties = createDefaultProperties(environment);
82 | // 有默认配置,则添加到 environment 中
83 | if (!CollectionUtils.isEmpty(defaultProperties)) {
84 | addOrReplace(propertySources, defaultProperties);
85 | }
86 | }
87 |
88 | @Override
89 | public int getOrder() {
90 | return LOWEST_PRECEDENCE;
91 | }
92 |
93 | private Map createDefaultProperties(ConfigurableEnvironment environment) {
94 | Map defaultProperties = new HashMap();
95 | // "dubbo.application.name"
96 | setDubboApplicationNameProperty(environment, defaultProperties);
97 | // "dubbo.config.multiple"
98 | setDubboConfigMultipleProperty(defaultProperties);
99 | // "dubbo.application.qos-enable"
100 | setDubboApplicationQosEnableProperty(defaultProperties);
101 | return defaultProperties;
102 | }
103 |
104 | private void setDubboApplicationNameProperty(Environment environment, Map defaultProperties) {
105 | String springApplicationName = environment.getProperty(SPRING_APPLICATION_NAME_PROPERTY);
106 | if (StringUtils.hasLength(springApplicationName)
107 | && !environment.containsProperty(DUBBO_APPLICATION_NAME_PROPERTY)) {
108 | defaultProperties.put(DUBBO_APPLICATION_NAME_PROPERTY, springApplicationName);
109 | }
110 | }
111 |
112 | private void setDubboConfigMultipleProperty(Map defaultProperties) {
113 | defaultProperties.put(DUBBO_CONFIG_MULTIPLE_PROPERTY, Boolean.TRUE.toString());
114 | }
115 |
116 | private void setDubboApplicationQosEnableProperty(Map defaultProperties) {
117 | defaultProperties.put(DUBBO_APPLICATION_QOS_ENABLE_PROPERTY, Boolean.FALSE.toString());
118 | }
119 |
120 | /**
121 | * Copy from BusEnvironmentPostProcessor#addOrReplace(MutablePropertySources, Map)
122 | *
123 | * @param propertySources {@link MutablePropertySources}
124 | * @param map Default Dubbo Properties
125 | */
126 | private void addOrReplace(MutablePropertySources propertySources, Map map) {
127 | // 情况一,获得到 "defaultProperties" 对应的 PropertySource 对象,则进行替换
128 | MapPropertySource target = null;
129 | if (propertySources.contains(PROPERTY_SOURCE_NAME)) {
130 | PropertySource> source = propertySources.get(PROPERTY_SOURCE_NAME);
131 | if (source instanceof MapPropertySource) { // 找到
132 | target = (MapPropertySource) source;
133 | // 遍历 map 数组,进行替换到 "defaultProperties" 中
134 | for (String key : map.keySet()) {
135 | if (!target.containsProperty(key)) {
136 | target.getSource().put(key, map.get(key));
137 | }
138 | }
139 | }
140 | }
141 | // 情况二,不存在 "defaultProperties" 对应的 PropertySource 对象,则进行添加
142 | if (target == null) {
143 | target = new MapPropertySource(PROPERTY_SOURCE_NAME, map);
144 | }
145 | if (!propertySources.contains(PROPERTY_SOURCE_NAME)) {
146 | propertySources.addLast(target);
147 | }
148 | }
149 |
150 | }
--------------------------------------------------------------------------------
/mvnw:
--------------------------------------------------------------------------------
1 | #!/bin/sh
2 | # ----------------------------------------------------------------------------
3 | # Licensed to the Apache Software Foundation (ASF) under one
4 | # or more contributor license agreements. See the NOTICE file
5 | # distributed with this work for additional information
6 | # regarding copyright ownership. The ASF licenses this file
7 | # to you under the Apache License, Version 2.0 (the
8 | # "License"); you may not use this file except in compliance
9 | # with the License. You may obtain a copy of the License at
10 | #
11 | # http://www.apache.org/licenses/LICENSE-2.0
12 | #
13 | # Unless required by applicable law or agreed to in writing,
14 | # software distributed under the License is distributed on an
15 | # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 | # KIND, either express or implied. See the License for the
17 | # specific language governing permissions and limitations
18 | # under the License.
19 | # ----------------------------------------------------------------------------
20 |
21 | # ----------------------------------------------------------------------------
22 | # Maven2 Start Up Batch script
23 | #
24 | # Required ENV vars:
25 | # ------------------
26 | # JAVA_HOME - location of a JDK home dir
27 | #
28 | # Optional ENV vars
29 | # -----------------
30 | # M2_HOME - location of maven2's installed home dir
31 | # MAVEN_OPTS - parameters passed to the Java VM when running Maven
32 | # e.g. to debug Maven itself, use
33 | # set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
34 | # MAVEN_SKIP_RC - flag to disable loading of mavenrc files
35 | # ----------------------------------------------------------------------------
36 |
37 | if [ -z "$MAVEN_SKIP_RC" ] ; then
38 |
39 | if [ -f /etc/mavenrc ] ; then
40 | . /etc/mavenrc
41 | fi
42 |
43 | if [ -f "$HOME/.mavenrc" ] ; then
44 | . "$HOME/.mavenrc"
45 | fi
46 |
47 | fi
48 |
49 | # OS specific support. $var _must_ be set to either true or false.
50 | cygwin=false;
51 | darwin=false;
52 | mingw=false
53 | case "`uname`" in
54 | CYGWIN*) cygwin=true ;;
55 | MINGW*) mingw=true;;
56 | Darwin*) darwin=true
57 | # Use /usr/libexec/java_home if available, otherwise fall back to /Library/Java/Home
58 | # See https://developer.apple.com/library/mac/qa/qa1170/_index.html
59 | if [ -z "$JAVA_HOME" ]; then
60 | if [ -x "/usr/libexec/java_home" ]; then
61 | export JAVA_HOME="`/usr/libexec/java_home`"
62 | else
63 | export JAVA_HOME="/Library/Java/Home"
64 | fi
65 | fi
66 | ;;
67 | esac
68 |
69 | if [ -z "$JAVA_HOME" ] ; then
70 | if [ -r /etc/gentoo-release ] ; then
71 | JAVA_HOME=`java-config --jre-home`
72 | fi
73 | fi
74 |
75 | if [ -z "$M2_HOME" ] ; then
76 | ## resolve links - $0 may be a link to maven's home
77 | PRG="$0"
78 |
79 | # need this for relative symlinks
80 | while [ -h "$PRG" ] ; do
81 | ls=`ls -ld "$PRG"`
82 | link=`expr "$ls" : '.*-> \(.*\)$'`
83 | if expr "$link" : '/.*' > /dev/null; then
84 | PRG="$link"
85 | else
86 | PRG="`dirname "$PRG"`/$link"
87 | fi
88 | done
89 |
90 | saveddir=`pwd`
91 |
92 | M2_HOME=`dirname "$PRG"`/..
93 |
94 | # make it fully qualified
95 | M2_HOME=`cd "$M2_HOME" && pwd`
96 |
97 | cd "$saveddir"
98 | # echo Using m2 at $M2_HOME
99 | fi
100 |
101 | # For Cygwin, ensure paths are in UNIX format before anything is touched
102 | if $cygwin ; then
103 | [ -n "$M2_HOME" ] &&
104 | M2_HOME=`cygpath --unix "$M2_HOME"`
105 | [ -n "$JAVA_HOME" ] &&
106 | JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
107 | [ -n "$CLASSPATH" ] &&
108 | CLASSPATH=`cygpath --path --unix "$CLASSPATH"`
109 | fi
110 |
111 | # For Mingw, ensure paths are in UNIX format before anything is touched
112 | if $mingw ; then
113 | [ -n "$M2_HOME" ] &&
114 | M2_HOME="`(cd "$M2_HOME"; pwd)`"
115 | [ -n "$JAVA_HOME" ] &&
116 | JAVA_HOME="`(cd "$JAVA_HOME"; pwd)`"
117 | # TODO classpath?
118 | fi
119 |
120 | if [ -z "$JAVA_HOME" ]; then
121 | javaExecutable="`which javac`"
122 | if [ -n "$javaExecutable" ] && ! [ "`expr \"$javaExecutable\" : '\([^ ]*\)'`" = "no" ]; then
123 | # readlink(1) is not available as standard on Solaris 10.
124 | readLink=`which readlink`
125 | if [ ! `expr "$readLink" : '\([^ ]*\)'` = "no" ]; then
126 | if $darwin ; then
127 | javaHome="`dirname \"$javaExecutable\"`"
128 | javaExecutable="`cd \"$javaHome\" && pwd -P`/javac"
129 | else
130 | javaExecutable="`readlink -f \"$javaExecutable\"`"
131 | fi
132 | javaHome="`dirname \"$javaExecutable\"`"
133 | javaHome=`expr "$javaHome" : '\(.*\)/bin'`
134 | JAVA_HOME="$javaHome"
135 | export JAVA_HOME
136 | fi
137 | fi
138 | fi
139 |
140 | if [ -z "$JAVACMD" ] ; then
141 | if [ -n "$JAVA_HOME" ] ; then
142 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
143 | # IBM's JDK on AIX uses strange locations for the executables
144 | JAVACMD="$JAVA_HOME/jre/sh/java"
145 | else
146 | JAVACMD="$JAVA_HOME/bin/java"
147 | fi
148 | else
149 | JAVACMD="`which java`"
150 | fi
151 | fi
152 |
153 | if [ ! -x "$JAVACMD" ] ; then
154 | echo "Error: JAVA_HOME is not defined correctly." >&2
155 | echo " We cannot execute $JAVACMD" >&2
156 | exit 1
157 | fi
158 |
159 | if [ -z "$JAVA_HOME" ] ; then
160 | echo "Warning: JAVA_HOME environment variable is not set."
161 | fi
162 |
163 | CLASSWORLDS_LAUNCHER=org.codehaus.plexus.classworlds.launcher.Launcher
164 |
165 | # traverses directory structure from process work directory to filesystem root
166 | # first directory with .mvn subdirectory is considered project base directory
167 | find_maven_basedir() {
168 |
169 | if [ -z "$1" ]
170 | then
171 | echo "Path not specified to find_maven_basedir"
172 | return 1
173 | fi
174 |
175 | basedir="$1"
176 | wdir="$1"
177 | while [ "$wdir" != '/' ] ; do
178 | if [ -d "$wdir"/.mvn ] ; then
179 | basedir=$wdir
180 | break
181 | fi
182 | # workaround for JBEAP-8937 (on Solaris 10/Sparc)
183 | if [ -d "${wdir}" ]; then
184 | wdir=`cd "$wdir/.."; pwd`
185 | fi
186 | # end of workaround
187 | done
188 | echo "${basedir}"
189 | }
190 |
191 | # concatenates all lines of a file
192 | concat_lines() {
193 | if [ -f "$1" ]; then
194 | echo "$(tr -s '\n' ' ' < "$1")"
195 | fi
196 | }
197 |
198 | BASE_DIR=`find_maven_basedir "$(pwd)"`
199 | if [ -z "$BASE_DIR" ]; then
200 | exit 1;
201 | fi
202 |
203 | export MAVEN_PROJECTBASEDIR=${MAVEN_BASEDIR:-"$BASE_DIR"}
204 | if [ "$MVNW_VERBOSE" = true ]; then
205 | echo $MAVEN_PROJECTBASEDIR
206 | fi
207 | MAVEN_OPTS="$(concat_lines "$MAVEN_PROJECTBASEDIR/.mvn/jvm.config") $MAVEN_OPTS"
208 |
209 | # For Cygwin, switch paths to Windows format before running java
210 | if $cygwin; then
211 | [ -n "$M2_HOME" ] &&
212 | M2_HOME=`cygpath --path --windows "$M2_HOME"`
213 | [ -n "$JAVA_HOME" ] &&
214 | JAVA_HOME=`cygpath --path --windows "$JAVA_HOME"`
215 | [ -n "$CLASSPATH" ] &&
216 | CLASSPATH=`cygpath --path --windows "$CLASSPATH"`
217 | [ -n "$MAVEN_PROJECTBASEDIR" ] &&
218 | MAVEN_PROJECTBASEDIR=`cygpath --path --windows "$MAVEN_PROJECTBASEDIR"`
219 | fi
220 |
221 | WRAPPER_LAUNCHER=org.apache.maven.wrapper.MavenWrapperMain
222 |
223 | exec "$JAVACMD" \
224 | $MAVEN_OPTS \
225 | -classpath "$MAVEN_PROJECTBASEDIR/.mvn/wrapper/maven-wrapper.jar" \
226 | "-Dmaven.home=${M2_HOME}" "-Dmaven.multiModuleProjectDirectory=${MAVEN_PROJECTBASEDIR}" \
227 | ${WRAPPER_LAUNCHER} $MAVEN_CONFIG "$@"
228 |
229 |
--------------------------------------------------------------------------------