From d4d03cf6626ee4bf87c14a51d0422047cf32eb75 Mon Sep 17 00:00:00 2001 From: flystar32 Date: Mon, 8 Oct 2018 20:29:39 +0800 Subject: [PATCH 01/63] update version to 0.2.0 --- pom.xml | 2 +- spring-cloud-alibaba-dependencies/pom.xml | 2 +- .../nacos-example/nacos-config-example/pom.xml | 2 +- .../nacos-discovery-consumer-example/pom.xml | 2 +- .../nacos-discovery-provider-example/pom.xml | 2 +- .../nacos-example/nacos-discovery-example/pom.xml | 2 +- spring-cloud-alibaba-examples/oss-example/pom.xml | 2 +- spring-cloud-alibaba-examples/pom.xml | 2 +- .../sentinel-example/sentinel-core-example/pom.xml | 2 +- .../sentinel-dubbo-example/sentinel-dubbo-api/pom.xml | 2 +- .../sentinel-dubbo-consumer-example/pom.xml | 2 +- .../sentinel-dubbo-provider-example/pom.xml | 2 +- spring-cloud-alibaba-nacos-config/pom.xml | 2 +- spring-cloud-alibaba-nacos-discovery/pom.xml | 2 +- spring-cloud-alibaba-sentinel/pom.xml | 2 +- spring-cloud-alibaba-test/core-support/pom.xml | 2 +- spring-cloud-alibaba-test/pom.xml | 2 +- spring-cloud-alibaba-test/sentinel-test-support/pom.xml | 2 +- spring-cloud-alicloud-oss/pom.xml | 2 +- spring-cloud-starter-alibaba/pom.xml | 2 +- .../spring-cloud-starter-alibaba-nacos-config/pom.xml | 2 +- .../spring-cloud-starter-alibaba-nacos-discovery/pom.xml | 2 +- .../spring-cloud-starter-alibaba-sentinel/pom.xml | 2 +- spring-cloud-starter-alicloud/pom.xml | 2 +- .../spring-cloud-starter-alicloud-oss/pom.xml | 2 +- 25 files changed, 25 insertions(+), 25 deletions(-) diff --git a/pom.xml b/pom.xml index a8388a1ec..1353ad3e7 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0 pom Spring Cloud Alibaba diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index 2ff41fded..058625b51 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -10,7 +10,7 @@ spring-cloud-alibaba-dependencies - 0.2.0.BUILD-SNAPSHOT + 0.2.0 pom Spring Cloud Alibaba Dependencies Spring Cloud Alibaba Dependencies diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/pom.xml index f7cedc41a..a011d829a 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0.BUILD-SNAPSHOT + 0.2.0 ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/pom.xml index ec343d5c5..65fbbfb0d 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud nacos-discovery-example - 0.2.0.BUILD-SNAPSHOT + 0.2.0 4.0.0 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/pom.xml index 294a44b94..c2e5952aa 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud nacos-discovery-example - 0.2.0.BUILD-SNAPSHOT + 0.2.0 4.0.0 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml index e56d92d4a..d4041f669 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0.BUILD-SNAPSHOT + 0.2.0 ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/oss-example/pom.xml b/spring-cloud-alibaba-examples/oss-example/pom.xml index cfa08a115..33442b3ad 100644 --- a/spring-cloud-alibaba-examples/oss-example/pom.xml +++ b/spring-cloud-alibaba-examples/oss-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0.BUILD-SNAPSHOT + 0.2.0 4.0.0 diff --git a/spring-cloud-alibaba-examples/pom.xml b/spring-cloud-alibaba-examples/pom.xml index ed4acb014..f02eadbb3 100644 --- a/spring-cloud-alibaba-examples/pom.xml +++ b/spring-cloud-alibaba-examples/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml index ec35b244d..314ee8f82 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0.BUILD-SNAPSHOT + 0.2.0 ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api/pom.xml index 24cde890d..6c989f5da 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0.BUILD-SNAPSHOT + 0.2.0 ../../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/pom.xml index 890656ea8..ba88981a0 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0.BUILD-SNAPSHOT + 0.2.0 ../../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example/pom.xml index 87adbbaf7..c59f4ba61 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0.BUILD-SNAPSHOT + 0.2.0 ../../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-nacos-config/pom.xml b/spring-cloud-alibaba-nacos-config/pom.xml index 12ff873c1..6bbc12173 100644 --- a/spring-cloud-alibaba-nacos-config/pom.xml +++ b/spring-cloud-alibaba-nacos-config/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0 4.0.0 diff --git a/spring-cloud-alibaba-nacos-discovery/pom.xml b/spring-cloud-alibaba-nacos-discovery/pom.xml index 3c6bd31af..e0aaa47d1 100644 --- a/spring-cloud-alibaba-nacos-discovery/pom.xml +++ b/spring-cloud-alibaba-nacos-discovery/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0 4.0.0 diff --git a/spring-cloud-alibaba-sentinel/pom.xml b/spring-cloud-alibaba-sentinel/pom.xml index 1ac5ef319..74b58421b 100644 --- a/spring-cloud-alibaba-sentinel/pom.xml +++ b/spring-cloud-alibaba-sentinel/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0 4.0.0 diff --git a/spring-cloud-alibaba-test/core-support/pom.xml b/spring-cloud-alibaba-test/core-support/pom.xml index 26712634f..72d30a988 100644 --- a/spring-cloud-alibaba-test/core-support/pom.xml +++ b/spring-cloud-alibaba-test/core-support/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-test - 0.2.0.BUILD-SNAPSHOT + 0.2.0 4.0.0 diff --git a/spring-cloud-alibaba-test/pom.xml b/spring-cloud-alibaba-test/pom.xml index 289cc1bb2..1261226d7 100644 --- a/spring-cloud-alibaba-test/pom.xml +++ b/spring-cloud-alibaba-test/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0 4.0.0 diff --git a/spring-cloud-alibaba-test/sentinel-test-support/pom.xml b/spring-cloud-alibaba-test/sentinel-test-support/pom.xml index 3a7b21dc7..9197c5f82 100644 --- a/spring-cloud-alibaba-test/sentinel-test-support/pom.xml +++ b/spring-cloud-alibaba-test/sentinel-test-support/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-test - 0.2.0.BUILD-SNAPSHOT + 0.2.0 4.0.0 diff --git a/spring-cloud-alicloud-oss/pom.xml b/spring-cloud-alicloud-oss/pom.xml index d486d2c02..09e114540 100644 --- a/spring-cloud-alicloud-oss/pom.xml +++ b/spring-cloud-alicloud-oss/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0 4.0.0 diff --git a/spring-cloud-starter-alibaba/pom.xml b/spring-cloud-starter-alibaba/pom.xml index 877646ae2..303e785f7 100644 --- a/spring-cloud-starter-alibaba/pom.xml +++ b/spring-cloud-starter-alibaba/pom.xml @@ -4,7 +4,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0 spring-cloud-starter-alibaba pom diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config/pom.xml index 2befd40c8..60c0e4891 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config/pom.xml +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-starter-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0 spring-cloud-starter-alibaba-nacos-config Spring Cloud Starter Alibaba Nacos Config diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-discovery/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-discovery/pom.xml index 2ce41a2fd..08bcf547d 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-discovery/pom.xml +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-discovery/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-starter-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0 spring-cloud-starter-alibaba-nacos-discovery Spring Cloud Starter Alibaba Nacos Discovery diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml index 70300f790..32aab630f 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-starter-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0 spring-cloud-starter-alibaba-sentinel Spring Cloud Starter Alibaba Sentinel diff --git a/spring-cloud-starter-alicloud/pom.xml b/spring-cloud-starter-alicloud/pom.xml index f44393a4c..a28fc12b4 100644 --- a/spring-cloud-starter-alicloud/pom.xml +++ b/spring-cloud-starter-alicloud/pom.xml @@ -4,7 +4,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0 spring-cloud-starter-alicloud pom diff --git a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-oss/pom.xml b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-oss/pom.xml index e52d22728..74ed72f33 100644 --- a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-oss/pom.xml +++ b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-oss/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-starter-alicloud - 0.2.0.BUILD-SNAPSHOT + 0.2.0 spring-cloud-starter-alicloud-oss Spring Cloud Starter Alibaba Cloud OSS From 0db10356ee63133192d624c4935e2bd8e4677dbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=A6=E7=9B=8F?= Date: Tue, 9 Oct 2018 20:29:45 +0800 Subject: [PATCH 02/63] Fixes #32 support fileExtension yaml and yml --- .../client/NacosPropertySourceBuilder.java | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java index 6fb8ffa97..4e5d63b67 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java @@ -28,6 +28,8 @@ import com.alibaba.nacos.api.exception.NacosException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; +import org.springframework.core.io.ByteArrayResource; import org.springframework.util.StringUtils; /** @@ -83,11 +85,22 @@ public class NacosPropertySourceBuilder { data = configService.getConfig(dataId, group, timeout); // todo add content type yaml support if (!StringUtils.isEmpty(data)) { - Properties properties = new Properties(); logger.info(String.format("Loading nacos data, dataId: '%s', group: '%s'", dataId, group)); - properties.load(new StringReader(data)); - return properties; + + if (fileExtension.equalsIgnoreCase("properties")) { + Properties properties = new Properties(); + + properties.load(new StringReader(data)); + return properties; + } + else if (fileExtension.equalsIgnoreCase("yaml") + || fileExtension.equalsIgnoreCase("yml")) { + YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean(); + yamlFactory.setResources(new ByteArrayResource(data.getBytes())); + return yamlFactory.getObject(); + } + } } catch (NacosException e) { From 2f77e717c2eee7e05471e3ff1b2d5bd55fe2b0ae Mon Sep 17 00:00:00 2001 From: flystar32 Date: Thu, 11 Oct 2018 21:48:35 +0800 Subject: [PATCH 03/63] update sentinel version to 0.2.0 --- spring-cloud-alibaba-dependencies/pom.xml | 2 +- .../examples/JsonFlowRuleListParser.java | 9 +++++---- .../cloud/examples/ServiceApplication.java | 4 ++-- .../sentinel/datasource/DataSourceLoader.java | 20 +++++++++---------- .../SentinelDataSourcePostProcessor.java | 10 +++++----- .../ApolloDataSourceFactoryBean.java | 14 ++++++------- .../FileRefreshableDataSourceFactoryBean.java | 14 ++++++------- .../NacosDataSourceFactoryBean.java | 14 ++++++------- .../ZookeeperDataSourceFactoryBean.java | 20 +++++++++---------- 9 files changed, 54 insertions(+), 53 deletions(-) diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index 058625b51..167f71e9d 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -16,7 +16,7 @@ Spring Cloud Alibaba Dependencies - 0.1.1 + 0.2.0 3.1.0 0.2.1 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/JsonFlowRuleListParser.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/JsonFlowRuleListParser.java index 255eb7892..b205c96c5 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/JsonFlowRuleListParser.java +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/JsonFlowRuleListParser.java @@ -2,7 +2,7 @@ package org.springframework.cloud.alibaba.cloud.examples; import java.util.List; -import com.alibaba.csp.sentinel.datasource.ConfigParser; +import com.alibaba.csp.sentinel.datasource.Converter; import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.TypeReference; @@ -10,9 +10,10 @@ import com.alibaba.fastjson.TypeReference; /** * @author fangjian */ -public class JsonFlowRuleListParser implements ConfigParser> { +public class JsonFlowRuleListParser implements Converter> { @Override - public List parse(String source) { - return JSON.parseObject(source, new TypeReference>() {}); + public List convert(String source) { + return JSON.parseObject(source, new TypeReference>() { + }); } } diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ServiceApplication.java b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ServiceApplication.java index 0dccda2f8..fe00e033e 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ServiceApplication.java +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ServiceApplication.java @@ -1,6 +1,6 @@ package org.springframework.cloud.alibaba.cloud.examples; -import com.alibaba.csp.sentinel.datasource.ConfigParser; +import com.alibaba.csp.sentinel.datasource.Converter; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.alibaba.sentinel.annotation.SentinelProtect; @@ -25,7 +25,7 @@ public class ServiceApplication { } @Bean - public ConfigParser myParser() { + public Converter myParser() { return new JsonFlowRuleListParser(); } diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/DataSourceLoader.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/DataSourceLoader.java index 7ac89dfc9..068717660 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/DataSourceLoader.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/DataSourceLoader.java @@ -34,10 +34,10 @@ import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; -import com.alibaba.csp.sentinel.datasource.DataSource; +import com.alibaba.csp.sentinel.datasource.ReadableDataSource; /** - * {@link DataSource} Loader + * {@link ReadableDataSource} Loader * * @author fangjian */ @@ -50,20 +50,20 @@ public class DataSourceLoader { private final static String ALL_PROPERTIES_RESOURCES_LOCATION = CLASSPATH_ALL_URL_PREFIX + PROPERTIES_RESOURCE_LOCATION; - private final static ConcurrentMap> dataSourceClassesCache = new ConcurrentHashMap>( + private final static ConcurrentMap> dataSourceClassesCache = new ConcurrentHashMap>( 4); static void loadAllDataSourceClassesCache() { - Map> dataSourceClassesMap = loadAllDataSourceClassesCache( + Map> dataSourceClassesMap = loadAllDataSourceClassesCache( ALL_PROPERTIES_RESOURCES_LOCATION); dataSourceClassesCache.putAll(dataSourceClassesMap); } - static Map> loadAllDataSourceClassesCache( + static Map> loadAllDataSourceClassesCache( String resourcesLocation) { - Map> dataSourcesMap = new HashMap>( + Map> dataSourcesMap = new HashMap>( 4); ClassLoader classLoader = DataSourceLoader.class.getClassLoader(); @@ -102,10 +102,10 @@ public class DataSourceLoader { Class dataSourceClass = ClassUtils.resolveClassName(className, classLoader); - Assert.isAssignable(DataSource.class, dataSourceClass); + Assert.isAssignable(ReadableDataSource.class, dataSourceClass); dataSourcesMap.put(type, - (Class) dataSourceClass); + (Class) dataSourceClass); if (logger.isDebugEnabled()) { logger.debug("Sentinel DataSource implementation [ type : " @@ -126,10 +126,10 @@ public class DataSourceLoader { return dataSourcesMap; } - public static Class loadClass(String type) + public static Class loadClass(String type) throws IllegalArgumentException { - Class dataSourceClass = dataSourceClassesCache.get(type); + Class dataSourceClass = dataSourceClassesCache.get(type); if (dataSourceClass == null) { if (dataSourceClassesCache.isEmpty()) { diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java index 05914b7d2..7f81899a4 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java @@ -44,20 +44,20 @@ import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; -import com.alibaba.csp.sentinel.datasource.ConfigParser; +import com.alibaba.csp.sentinel.datasource.Converter; /** * {@link SentinelDataSource @SentinelDataSource} Post Processor * * @author fangjian - * @see com.alibaba.csp.sentinel.datasource.DataSource + * @see com.alibaba.csp.sentinel.datasource.ReadableDataSource * @see SentinelDataSource */ public class SentinelDataSourcePostProcessor extends InstantiationAwareBeanPostProcessorAdapter implements MergedBeanDefinitionPostProcessor { - private static final Logger logger = LoggerFactory + private static final Logger logger = LoggerFactory .getLogger(SentinelDataSourcePostProcessor.class); @Autowired @@ -170,8 +170,8 @@ public class SentinelDataSourcePostProcessor for (String propertyName : propertyMap.keySet()) { Field field = ReflectionUtils.findField(targetClass, propertyName); if (field != null) { - if (field.getType().isAssignableFrom(ConfigParser.class)) { - // ConfigParser get from ApplicationContext + if (field.getType().isAssignableFrom(Converter.class)) { + // Converter get from ApplicationContext builder.addPropertyReference(propertyName, propertyMap.get(propertyName).toString()); } diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ApolloDataSourceFactoryBean.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ApolloDataSourceFactoryBean.java index 48458f82b..09b39bb44 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ApolloDataSourceFactoryBean.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ApolloDataSourceFactoryBean.java @@ -2,7 +2,7 @@ package org.springframework.cloud.alibaba.sentinel.datasource.factorybean; import org.springframework.beans.factory.FactoryBean; -import com.alibaba.csp.sentinel.datasource.ConfigParser; +import com.alibaba.csp.sentinel.datasource.Converter; import com.alibaba.csp.sentinel.datasource.apollo.ApolloDataSource; /** @@ -14,12 +14,12 @@ public class ApolloDataSourceFactoryBean implements FactoryBean private String serverAddr; private String groupId; private String dataId; - private ConfigParser configParser; + private Converter converter; @Override public NacosDataSource getObject() throws Exception { - return new NacosDataSource(serverAddr, groupId, dataId, configParser); + return new NacosDataSource(serverAddr, groupId, dataId, converter); } @Override @@ -50,11 +50,11 @@ public class NacosDataSourceFactoryBean implements FactoryBean this.dataId = dataId; } - public ConfigParser getConfigParser() { - return configParser; + public Converter getConverter() { + return converter; } - public void setConfigParser(ConfigParser configParser) { - this.configParser = configParser; + public void setConverter(Converter Converter) { + this.converter = Converter; } } diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ZookeeperDataSourceFactoryBean.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ZookeeperDataSourceFactoryBean.java index b0ea5aaa0..aa10270a7 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ZookeeperDataSourceFactoryBean.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ZookeeperDataSourceFactoryBean.java @@ -3,7 +3,7 @@ package org.springframework.cloud.alibaba.sentinel.datasource.factorybean; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.FactoryBean; -import com.alibaba.csp.sentinel.datasource.ConfigParser; +import com.alibaba.csp.sentinel.datasource.Converter; import com.alibaba.csp.sentinel.datasource.zookeeper.ZookeeperDataSource; /** @@ -19,17 +19,17 @@ public class ZookeeperDataSourceFactoryBean implements FactoryBean Date: Tue, 16 Oct 2018 15:39:23 +0800 Subject: [PATCH 04/63] add management info to registry metadata --- .../nacos/registry/NacosRegistration.java | 36 +++++++++++++++---- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosRegistration.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosRegistration.java index 10620415d..4d923facf 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosRegistration.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosRegistration.java @@ -20,8 +20,11 @@ import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.ManagementServerPortUtils; import org.springframework.cloud.client.serviceregistry.Registration; +import org.springframework.context.ApplicationContext; import org.springframework.core.env.Environment; +import org.springframework.util.StringUtils; import java.net.URI; import java.util.Map; @@ -40,17 +43,23 @@ import static com.alibaba.nacos.api.PropertyKeyConst.*; */ public class NacosRegistration implements Registration, ServiceInstance { + private static final String MANAGEMENT_PORT = "management.port"; + private static final String MANAGEMENT_CONTEXT_PATH = "management.context-path"; + private static final String MANAGEMENT_ADDRESS = "management.address"; + @Autowired private NacosDiscoveryProperties nacosDiscoveryProperties; - private NamingService nacosNamingService; - @Autowired - private Environment environment; + private ApplicationContext context; + + private NamingService nacosNamingService; @PostConstruct public void init() { - nacosDiscoveryProperties.overrideFromEnv(environment); + + Environment env = context.getEnvironment(); + nacosDiscoveryProperties.overrideFromEnv(context.getEnvironment()); Properties properties = new Properties(); properties.put(SERVER_ADDR, nacosDiscoveryProperties.getServerAddr()); @@ -67,6 +76,21 @@ public class NacosRegistration implements Registration, ServiceInstance { catch (Exception e) { } + + Integer managementPort = ManagementServerPortUtils.getPort(context); + if (null != managementPort) { + Map metadata = nacosDiscoveryProperties.getMetadata(); + metadata.put(MANAGEMENT_PORT, managementPort.toString()); + String contextPath = env + .getProperty("management.server.servlet.context-path"); + String address = env.getProperty("management.server.address"); + if (!StringUtils.isEmpty(contextPath)) { + metadata.put(MANAGEMENT_CONTEXT_PATH, contextPath); + } + if (!StringUtils.isEmpty(address)) { + metadata.put(MANAGEMENT_ADDRESS, address); + } + } } @Override @@ -85,9 +109,7 @@ public class NacosRegistration implements Registration, ServiceInstance { } public void setPort(int port) { - if (nacosDiscoveryProperties.getPort() < 0) { - this.nacosDiscoveryProperties.setPort(port); - } + this.nacosDiscoveryProperties.setPort(port); } @Override From a13bc2eea308b802a55975376b99c9a683e176b2 Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Tue, 16 Oct 2018 16:39:47 +0800 Subject: [PATCH 05/63] update comments and docs --- .../sentinel-core-example/readme-zh.md | 6 +-- .../sentinel-core-example/readme.md | 6 +-- .../annotation/SentinelDataSource.java | 22 +++++--- .../SentinelDataSourceRegistry.java | 50 ++++++++++--------- 4 files changed, 46 insertions(+), 38 deletions(-) diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md index a4b4d87ac..47b4a51dc 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md @@ -204,7 +204,7 @@ Sentinel starter 整合了目前存在的几类 DataSource。只需要在配置 spring.cloud.sentinel.datasource.recommendRefreshMs=2000 spring.cloud.sentinel.datasource.bufSize=2048 spring.cloud.sentinel.datasource.charset=utf-8 - spring.cloud.sentinel.datasource.configParser=myParser + spring.cloud.sentinel.datasource.converter=myParser spring.cloud.sentinel.datasource.file=/Users/you/rule.json 然后使用`@SentinelDataSource` 注解修饰 DataSource 即可注入: @@ -220,7 +220,7 @@ Sentinel starter 整合了目前存在的几类 DataSource。只需要在配置 `spring.cloud.sentinel.datasource.recommendRefreshMs` 里的 `recommendRefreshMs` 对应相关 DataSource 的属性。 -`spring.cloud.sentinel.datasource.configParser`代表 `ConfigParser` 在 Spring 容器里的 name。如果没找到,会抛出异常。 +`spring.cloud.sentinel.datasource.converter`代表 `Converter` 在 Spring 容器里的 name。如果没找到,会抛出异常。 type目前支持file, nacos, zk, apollo。 @@ -260,7 +260,7 @@ type目前支持file, nacos, zk, apollo。 spring.cloud.sentinel.datasource.fieldA = valueA spring.cloud.sentinel.datasource.fieldB = valueB - 注意:由于目前Sentinel的AbstractDataSource需要有个ConfigParser作为构造函数中的参数,并且它的子类的构造都是通过多个参数的构造函数构造的。 + 注意:由于目前Sentinel的AbstractDataSource需要有个Converter作为构造函数中的参数,并且它的子类的构造都是通过多个参数的构造函数构造的。 所以目前所有的Sentinel starter中的DataSource都是基于FactoryBean并且通过设置属性构造的。如果有这方面的需求,需要再多加一个registerFactoryBean过程。 SentinelDataSourceRegistry.registerFactoryBean("custeom", CustomDataSourceFactoryBean.class); diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme.md b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme.md index daaa3e8fb..8d838ebcd 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme.md +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme.md @@ -177,7 +177,7 @@ If you want to define FileRefreshableDataSource: spring.cloud.sentinel.datasource.recommendRefreshMs=2000 spring.cloud.sentinel.datasource.bufSize=2048 spring.cloud.sentinel.datasource.charset=utf-8 - spring.cloud.sentinel.datasource.configParser=myParser + spring.cloud.sentinel.datasource.converter=myParser spring.cloud.sentinel.datasource.file=/Users/you/rule.json then use `@SentinelDataSource` to annotate DataSource: @@ -191,7 +191,7 @@ spring.cloud.sentinel.datasource.type means the type of DataSource. spring.cloud.sentinel.datasource.recommendRefreshMs means the recommendRefreshMs property of specified DataSource. -spring.cloud.sentinel.datasource.configParser means the name of spring bean that type is ConfigParser. If the bean is not exists, will throw exception. +spring.cloud.sentinel.datasource.converter means the name of spring bean that type is Converter. If the bean is not exists, will throw exception. Now datasource type support 4 categories: file, nacos, zk, apollo. @@ -230,7 +230,7 @@ User-defined DataSource need 2 steps. spring.cloud.sentinel.datasource.fieldA = valueA spring.cloud.sentinel.datasource.fieldB = valueB -Note: The AbstractDataSource of Sentinel need a ConfigParser as a constructor param and the subclass of AbstractDataSource was construct by multi-param constructor. +Note: The AbstractDataSource of Sentinel need a Converter as a constructor param and the subclass of AbstractDataSource was construct by multi-param constructor. Now All DataSources in starter was construct by FactoryBean. If you want to do it in this way, you should register FactoryBean by SentinelDataSourceRegistry. SentinelDataSourceRegistry.registerFactoryBean("custeom", CustomDataSourceFactoryBean.class); diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/annotation/SentinelDataSource.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/annotation/SentinelDataSource.java index ef09eb0ef..93ef1bdb9 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/annotation/SentinelDataSource.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/annotation/SentinelDataSource.java @@ -16,28 +16,34 @@ package org.springframework.cloud.alibaba.sentinel.annotation; -import java.lang.annotation.*; +import java.lang.annotation.Documented; +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import com.alibaba.csp.sentinel.datasource.ReadableDataSource; import org.springframework.core.annotation.AliasFor; /** - * An annotation to inject {@link com.alibaba.csp.sentinel.datasource.DataSource} instance + * An annotation to inject {@link ReadableDataSource} instance * into a Spring Bean. The Properties of DataSource bean get from config files with * specific prefix. * * @author fangjian - * @see com.alibaba.csp.sentinel.datasource.DataSource + * @see ReadableDataSource */ -@Target({ ElementType.FIELD }) +@Target({ElementType.FIELD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface SentinelDataSource { - @AliasFor("prefix") - String value() default ""; + @AliasFor("prefix") + String value() default ""; - @AliasFor("value") - String prefix() default ""; + @AliasFor("value") + String prefix() default ""; String name() default ""; // spring bean name diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourceRegistry.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourceRegistry.java index 9bd92eee5..ceb90ee1f 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourceRegistry.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourceRegistry.java @@ -18,6 +18,8 @@ package org.springframework.cloud.alibaba.sentinel.datasource; import java.util.concurrent.ConcurrentHashMap; +import com.alibaba.csp.sentinel.datasource.ReadableDataSource; + import org.springframework.beans.factory.FactoryBean; import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.ApolloDataSourceFactoryBean; import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.FileRefreshableDataSourceFactoryBean; @@ -28,7 +30,7 @@ import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.Zookeep * Registry to save DataSource FactoryBean * * @author fangjian - * @see com.alibaba.csp.sentinel.datasource.DataSource + * @see ReadableDataSource * @see FileRefreshableDataSourceFactoryBean * @see ZookeeperDataSourceFactoryBean * @see NacosDataSourceFactoryBean @@ -36,32 +38,32 @@ import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.Zookeep */ public class SentinelDataSourceRegistry { - private static ConcurrentHashMap> cache = new ConcurrentHashMap<>( - 32); + private static ConcurrentHashMap> cache = new ConcurrentHashMap<>( + 32); - static { - SentinelDataSourceRegistry.registerFactoryBean("file", - FileRefreshableDataSourceFactoryBean.class); - SentinelDataSourceRegistry.registerFactoryBean("zk", - ZookeeperDataSourceFactoryBean.class); - SentinelDataSourceRegistry.registerFactoryBean("nacos", - NacosDataSourceFactoryBean.class); - SentinelDataSourceRegistry.registerFactoryBean("apollo", - ApolloDataSourceFactoryBean.class); - } + static { + SentinelDataSourceRegistry.registerFactoryBean("file", + FileRefreshableDataSourceFactoryBean.class); + SentinelDataSourceRegistry.registerFactoryBean("zk", + ZookeeperDataSourceFactoryBean.class); + SentinelDataSourceRegistry.registerFactoryBean("nacos", + NacosDataSourceFactoryBean.class); + SentinelDataSourceRegistry.registerFactoryBean("apollo", + ApolloDataSourceFactoryBean.class); + } - public static synchronized void registerFactoryBean(String alias, - Class clazz) { - cache.putIfAbsent(alias, clazz); - cache.put(alias, clazz); - } + public static synchronized void registerFactoryBean(String alias, + Class clazz) { + cache.putIfAbsent(alias, clazz); + cache.put(alias, clazz); + } - public static Class getFactoryBean(String alias) { - return cache.get(alias); - } + public static Class getFactoryBean(String alias) { + return cache.get(alias); + } - public static boolean checkFactoryBean(String alias) { - return cache.containsKey(alias); - } + public static boolean checkFactoryBean(String alias) { + return cache.containsKey(alias); + } } From 50d3ad218a6ee68e50409bf022b38f429de3dd89 Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Wed, 17 Oct 2018 11:05:43 +0800 Subject: [PATCH 06/63] Fixes #43 add rules automation --- .../sentinel-core-example/readme-zh.md | 8 +-- .../sentinel-core-example/readme.md | 8 +-- .../SentinelDataSourcePostProcessor.java | 49 ++++++++++++++++++- 3 files changed, 56 insertions(+), 9 deletions(-) diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md index 47b4a51dc..4c0b2aeba 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md @@ -192,9 +192,9 @@ Sentinel 控制台支持实时监控查看,您可以通过 Sentinel 控制台

-## DataSource 支持 +## ReadableDataSource 支持 -Sentinel 内部提供了[动态规则的扩展实现 DataSource](https://github.com/alibaba/Sentinel/wiki/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%99%E6%89%A9%E5%B1%95#datasource-%E6%89%A9%E5%B1%95)。 +Sentinel 内部提供了[动态规则的扩展实现 ReadableDataSource](https://github.com/alibaba/Sentinel/wiki/%E5%8A%A8%E6%80%81%E8%A7%84%E5%88%99%E6%89%A9%E5%B1%95#datasource-%E6%89%A9%E5%B1%95)。 Sentinel starter 整合了目前存在的几类 DataSource。只需要在配置文件中进行相关配置,即可在 Spring 容器中自动注册 DataSource。 @@ -210,7 +210,7 @@ Sentinel starter 整合了目前存在的几类 DataSource。只需要在配置 然后使用`@SentinelDataSource` 注解修饰 DataSource 即可注入: @SentinelDataSource("spring.cloud.sentinel.datasource") - private DataSource dataSource; + private ReadableDataSource dataSource; `@SentinelDataSource` 注解的 value 属性可以不填。默认值就是 `spring.cloud.sentinel.datasource`。 @@ -230,7 +230,7 @@ type目前支持file, nacos, zk, apollo。 1. 定义DataSource - public class CustomDataSource implements DataSource { + public class CustomDataSource implements ReadableDataSource { private String fieldA; private String fieldB; ... diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme.md b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme.md index 8d838ebcd..a51ed38ad 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme.md +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme.md @@ -165,9 +165,9 @@ To see the metrics, click **实时监控(Real-time Monitoring)** in the left-sid

-## DataSource +## ReadableDataSource -Sentinel provide [DataSource](https://github.com/alibaba/Sentinel/blob/master/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/DataSource.java) to manage dynamic rules. +Sentinel provide [ReadableDataSource](https://github.com/alibaba/Sentinel/blob/master/sentinel-extension/sentinel-datasource-extension/src/main/java/com/alibaba/csp/sentinel/datasource/ReadableDataSource.java) to manage dynamic rules. Sentinel starter integrated 4 DataSources provided by Sentinel. It will be register into Spring Context if you write some configs in `application.properties`. @@ -183,7 +183,7 @@ If you want to define FileRefreshableDataSource: then use `@SentinelDataSource` to annotate DataSource: @SentinelDataSource("spring.cloud.sentinel.datasource") - private DataSource dataSource; + private ReadableDataSource dataSource; The value() of `@SentinelDataSource` is not required, it means the prefix of configuration. Default value is `spring.cloud.sentinel.datasource`. @@ -201,7 +201,7 @@ User-defined DataSource need 2 steps. 1. Define DataSource - public class CustomDataSource implements DataSource { + public class CustomDataSource implements ReadableDataSource { private String fieldA; private String fieldB; ... diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java index 7f81899a4..627c5be03 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java @@ -36,15 +36,27 @@ import org.springframework.beans.factory.support.BeanDefinitionBuilder; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor; import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.boot.context.event.ApplicationStartedEvent; import org.springframework.cloud.alibaba.sentinel.SentinelConstants; import org.springframework.cloud.alibaba.sentinel.annotation.SentinelDataSource; import org.springframework.cloud.alibaba.sentinel.util.PropertySourcesUtils; import org.springframework.context.ApplicationContext; +import org.springframework.context.event.EventListener; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; import com.alibaba.csp.sentinel.datasource.Converter; +import com.alibaba.csp.sentinel.datasource.ReadableDataSource; +import com.alibaba.csp.sentinel.property.SentinelProperty; +import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule; +import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager; +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager; +import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; +import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; +import com.alibaba.csp.sentinel.slots.system.SystemRule; +import com.alibaba.csp.sentinel.slots.system.SystemRuleManager; /** * {@link SentinelDataSource @SentinelDataSource} Post Processor @@ -187,7 +199,42 @@ public class SentinelDataSourcePostProcessor beanFactory.registerBeanDefinition(beanName, builder.getBeanDefinition()); } - class SentinelDataSourceField { + + @EventListener(classes = ApplicationStartedEvent.class) + public void appStartedListener(ApplicationStartedEvent event) throws Exception { + Map dataSourceMap = event.getApplicationContext().getBeansOfType(ReadableDataSource.class); + if(dataSourceMap.size() == 1) { + ReadableDataSource dataSource = dataSourceMap.values().iterator().next(); + Object ruleConfig = dataSource.loadConfig(); + SentinelProperty sentinelProperty = dataSource.getProperty(); + if(checkRuleType(ruleConfig, FlowRule.class)) { + FlowRuleManager.register2Property(sentinelProperty); + } + if(checkRuleType(ruleConfig, DegradeRule.class)) { + DegradeRuleManager.register2Property(sentinelProperty); + } + if(checkRuleType(ruleConfig, SystemRule.class)) { + SystemRuleManager.register2Property(sentinelProperty); + } + if(checkRuleType(ruleConfig, AuthorityRule.class)) { + AuthorityRuleManager.register2Property(sentinelProperty); + } + } + } + + private boolean checkRuleType(Object ruleConfig, Class type) { + if(ruleConfig.getClass() == type) { + return true; + } else if(ruleConfig instanceof List) { + List ruleList = (List)ruleConfig; + if(ruleList.stream().filter(rule -> rule.getClass() == type).toArray().length == ruleList.size()) { + return true; + } + } + return false; + } + + class SentinelDataSourceField { private SentinelDataSource sentinelDataSource; private Field field; From 3c2a10f35e73176e1642cdc1f020592f1d3960c3 Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Wed, 17 Oct 2018 16:04:44 +0800 Subject: [PATCH 07/63] converter property fix --- .../src/main/resources/application.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/application.properties index 2e6342de2..fd8d3b6da 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/application.properties +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/src/main/resources/application.properties @@ -10,5 +10,5 @@ spring.cloud.sentinel.datasource.type=file spring.cloud.sentinel.datasource.recommendRefreshMs=3000 spring.cloud.sentinel.datasource.bufSize=4056196 spring.cloud.sentinel.datasource.charset=utf-8 -spring.cloud.sentinel.datasource.configParser=myParser +spring.cloud.sentinel.datasource.converter=myParser spring.cloud.sentinel.datasource.file=/Users/you/rule.json \ No newline at end of file From dfd2ca58340b4d4f86797a44706b2930ee4cd882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=A6=E7=9B=8F?= Date: Wed, 17 Oct 2018 17:49:19 +0800 Subject: [PATCH 08/63] separate registry and discovery --- .../NacosDiscoveryAutoConfiguration.java | 6 -- .../alibaba/nacos/NacosDiscoveryClient.java | 25 ++++-- ...NacosDiscoveryClientAutoConfiguration.java | 6 ++ .../nacos/NacosDiscoveryProperties.java | 35 ++++++++ .../endpoint/NacosDiscoveryEndpoint.java | 6 +- ...cosDiscoveryEndpointAutoConfiguration.java | 1 - .../nacos/registry/NacosRegistration.java | 22 +---- .../alibaba/nacos/ribbon/NacosServerList.java | 6 +- .../NacosDiscoveryAutoConfigurationTests.java | 3 +- .../NacosRibbonClientConfigurationTests.java | 83 +++++++++---------- 10 files changed, 109 insertions(+), 84 deletions(-) diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfiguration.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfiguration.java index b61801448..21a8d303c 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfiguration.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfiguration.java @@ -48,12 +48,6 @@ public class NacosDiscoveryAutoConfiguration { return new NacosServiceRegistry(); } - @Bean - @ConditionalOnMissingBean - public NacosDiscoveryProperties nacosProperties() { - return new NacosDiscoveryProperties(); - } - @Bean @ConditionalOnBean(AutoServiceRegistrationProperties.class) public NacosRegistration nacosRegistration() { diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java index d32a6b0d3..59ea1ec73 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java @@ -19,12 +19,15 @@ package org.springframework.cloud.alibaba.nacos; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cloud.alibaba.nacos.registry.NacosRegistration; +import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.core.env.Environment; import java.util.*; +import javax.annotation.PostConstruct; + import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ListView; @@ -39,7 +42,18 @@ public class NacosDiscoveryClient implements DiscoveryClient { public static final String DESCRIPTION = "Spring Cloud Nacos Discovery Client"; @Autowired - private NacosRegistration nacosRegistration; + private NacosDiscoveryProperties discoveryProperties; + + @Autowired + private Environment environment; + + private NamingService namingService; + + @PostConstruct + public void init() { + discoveryProperties.overrideFromEnv(environment); + namingService = discoveryProperties.getNamingService(); + } @Override public String description() { @@ -49,7 +63,6 @@ public class NacosDiscoveryClient implements DiscoveryClient { @Override public List getInstances(String serviceId) { try { - NamingService namingService = nacosRegistration.getNacosNamingService(); List instances = namingService.getAllInstances(serviceId); return hostToServiceInstanceList(instances, serviceId); } @@ -79,7 +92,7 @@ public class NacosDiscoveryClient implements DiscoveryClient { List instances, String serviceId) { List result = new ArrayList(instances.size()); for (Instance instance : instances) { - if(instance.isHealthy()) { + if (instance.isHealthy()) { result.add(hostToServiceInstance(instance, serviceId)); } } @@ -90,7 +103,6 @@ public class NacosDiscoveryClient implements DiscoveryClient { public List getServices() { try { - NamingService namingService = nacosRegistration.getNacosNamingService(); ListView services = namingService.getServicesOfServer(1, Integer.MAX_VALUE); return services.getData(); @@ -101,4 +113,7 @@ public class NacosDiscoveryClient implements DiscoveryClient { } } + public NamingService getNamingService() { + return namingService; + } } diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClientAutoConfiguration.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClientAutoConfiguration.java index 2563cbe6d..05049f4b3 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClientAutoConfiguration.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClientAutoConfiguration.java @@ -36,4 +36,10 @@ public class NacosDiscoveryClientAutoConfiguration { return new NacosDiscoveryClient(); } + @Bean + @ConditionalOnMissingBean + public NacosDiscoveryProperties nacosProperties() { + return new NacosDiscoveryProperties(); + } + } diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryProperties.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryProperties.java index 43b6a5c7e..aa9271a8d 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryProperties.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryProperties.java @@ -16,6 +16,8 @@ package org.springframework.cloud.alibaba.nacos; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; @@ -33,6 +35,18 @@ import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Objects; +import java.util.Properties; + +import com.alibaba.nacos.api.NacosFactory; +import com.alibaba.nacos.api.naming.NamingService; +import com.alibaba.nacos.client.naming.utils.UtilAndComs; + +import static com.alibaba.nacos.api.PropertyKeyConst.ACCESS_KEY; +import static com.alibaba.nacos.api.PropertyKeyConst.CLUSTER_NAME; +import static com.alibaba.nacos.api.PropertyKeyConst.ENDPOINT; +import static com.alibaba.nacos.api.PropertyKeyConst.NAMESPACE; +import static com.alibaba.nacos.api.PropertyKeyConst.SECRET_KEY; +import static com.alibaba.nacos.api.PropertyKeyConst.SERVER_ADDR; /** * @author dungu.zpf @@ -42,6 +56,9 @@ import java.util.Objects; @ConfigurationProperties("spring.cloud.nacos.discovery") public class NacosDiscoveryProperties { + private static final Logger LOGGER = LoggerFactory + .getLogger(NacosDiscoveryProperties.class); + /** * nacos discovery server address */ @@ -333,4 +350,22 @@ public class NacosDiscoveryProperties { } } + public NamingService getNamingService() { + Properties properties = new Properties(); + properties.put(SERVER_ADDR, serverAddr); + properties.put(NAMESPACE, namespace); + properties.put(UtilAndComs.NACOS_NAMING_LOG_NAME, logName); + properties.put(ENDPOINT, endpoint); + properties.put(ACCESS_KEY, accessKey); + properties.put(SECRET_KEY, secretKey); + properties.put(CLUSTER_NAME, clusterName); + try { + return NacosFactory.createNamingService(properties); + } + catch (Exception e) { + LOGGER.error("create naming service error!properties={},e=,", this, e); + return null; + } + } + } diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosDiscoveryEndpoint.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosDiscoveryEndpoint.java index da49d6c29..db384df5e 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosDiscoveryEndpoint.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosDiscoveryEndpoint.java @@ -29,8 +29,8 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; +import org.springframework.cloud.alibaba.nacos.NacosDiscoveryClient; import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties; -import org.springframework.cloud.alibaba.nacos.registry.NacosRegistration; /** * Endpoint for nacos discovery, get nacos properties and subscribed services @@ -46,7 +46,7 @@ public class NacosDiscoveryEndpoint { private NacosDiscoveryProperties nacosDiscoveryProperties; @Autowired - private NacosRegistration nacosRegistration; + private NacosDiscoveryClient discoveryClient; /** * @return nacos discovery endpoint @@ -56,7 +56,7 @@ public class NacosDiscoveryEndpoint { Map result = new HashMap<>(); result.put("NacosDiscoveryProperties", nacosDiscoveryProperties); - NamingService namingService = nacosRegistration.getNacosNamingService(); + NamingService namingService = discoveryClient.getNamingService(); List subscribe = Collections.emptyList(); try { diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosDiscoveryEndpointAutoConfiguration.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosDiscoveryEndpointAutoConfiguration.java index 2b6c46352..c8716ad41 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosDiscoveryEndpointAutoConfiguration.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosDiscoveryEndpointAutoConfiguration.java @@ -28,7 +28,6 @@ import org.springframework.context.annotation.Configuration; * @author xiaojing */ @Configuration -@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true) @ConditionalOnClass(Endpoint.class) public class NacosDiscoveryEndpointAutoConfiguration { diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosRegistration.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosRegistration.java index 4d923facf..639dad981 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosRegistration.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosRegistration.java @@ -28,15 +28,10 @@ import org.springframework.util.StringUtils; import java.net.URI; import java.util.Map; -import java.util.Properties; import javax.annotation.PostConstruct; -import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.naming.NamingService; -import com.alibaba.nacos.client.naming.utils.UtilAndComs; - -import static com.alibaba.nacos.api.PropertyKeyConst.*; /** * @author xiaojing @@ -60,22 +55,7 @@ public class NacosRegistration implements Registration, ServiceInstance { Environment env = context.getEnvironment(); nacosDiscoveryProperties.overrideFromEnv(context.getEnvironment()); - - Properties properties = new Properties(); - properties.put(SERVER_ADDR, nacosDiscoveryProperties.getServerAddr()); - properties.put(NAMESPACE, nacosDiscoveryProperties.getNamespace()); - properties.put(UtilAndComs.NACOS_NAMING_LOG_NAME, - nacosDiscoveryProperties.getLogName()); - properties.put(ENDPOINT, nacosDiscoveryProperties.getEndpoint()); - properties.put(ACCESS_KEY, nacosDiscoveryProperties.getAccessKey()); - properties.put(SECRET_KEY, nacosDiscoveryProperties.getSecretKey()); - properties.put(CLUSTER_NAME, nacosDiscoveryProperties.getClusterName()); - try { - nacosNamingService = NacosFactory.createNamingService(properties); - } - catch (Exception e) { - - } + nacosNamingService = nacosDiscoveryProperties.getNamingService(); Integer managementPort = ManagementServerPortUtils.getPort(context); if (null != managementPort) { diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServerList.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServerList.java index 3eb43bbcc..261456622 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServerList.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServerList.java @@ -19,7 +19,7 @@ package org.springframework.cloud.alibaba.nacos.ribbon; import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.AbstractServerList; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cloud.alibaba.nacos.registry.NacosRegistration; +import org.springframework.cloud.alibaba.nacos.NacosDiscoveryClient; import java.util.ArrayList; import java.util.List; @@ -32,7 +32,7 @@ import com.alibaba.nacos.api.naming.pojo.Instance; public class NacosServerList extends AbstractServerList { @Autowired - private NacosRegistration registration; + private NacosDiscoveryClient discoveryClient; private String serviceId; @@ -55,7 +55,7 @@ public class NacosServerList extends AbstractServerList { private List getServers() { try { - List instances = registration.getNacosNamingService() + List instances = discoveryClient.getNamingService() .getAllInstances(serviceId); return instancesToServerList(instances); } diff --git a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfigurationTests.java b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfigurationTests.java index b7e5d8016..3d15a8fc2 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfigurationTests.java +++ b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryAutoConfigurationTests.java @@ -37,7 +37,8 @@ public class NacosDiscoveryAutoConfigurationTests { private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() .withConfiguration( AutoConfigurations.of(NacosDiscoveryTestConfiguration.class, - NacosDiscoveryAutoConfiguration.class)) + NacosDiscoveryAutoConfiguration.class, + NacosDiscoveryClientAutoConfiguration.class)) .withPropertyValues("spring.cloud.nacos.discovery.server-addr=127.0.0.1:8080") .withPropertyValues("spring.cloud.nacos.discovery.port=18080") .withPropertyValues("spring.cloud.nacos.discovery.service=myapp"); diff --git a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosRibbonClientConfigurationTests.java b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosRibbonClientConfigurationTests.java index 8c6da4290..ad5da1b24 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosRibbonClientConfigurationTests.java +++ b/spring-cloud-alibaba-nacos-discovery/src/test/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosRibbonClientConfigurationTests.java @@ -6,6 +6,7 @@ import org.junit.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.runner.WebApplicationContextRunner; +import org.springframework.cloud.alibaba.nacos.NacosDiscoveryClientAutoConfiguration; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; import org.springframework.cloud.client.loadbalancer.LoadBalanced; import org.springframework.context.annotation.Bean; @@ -19,49 +20,43 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class NacosRibbonClientConfigurationTests { - - private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() - .withConfiguration( - AutoConfigurations.of(NacosRibbonTestConfiguration.class, - NacosRibbonClientConfiguration.class, - RibbonNacosAutoConfiguration.class)) - .withPropertyValues("spring.cloud.nacos.discovery.server-addr=127.0.0.1:8080") - .withPropertyValues("spring.cloud.nacos.discovery.port=18080") - .withPropertyValues("spring.cloud.nacos.discovery.service=myapp"); - - - - @Test - public void testProperties() { - - this.contextRunner.run(context -> { - NacosServerList serverList = context.getBean(NacosServerList.class); - assertThat(serverList.getServiceId()).isEqualTo("myapp"); - }); - } - - - @Configuration - @EnableAutoConfiguration - @EnableDiscoveryClient - static class NacosRibbonTestConfiguration { - - - @Bean - IClientConfig iClientConfig(){ - //return new IClientConfig.Builder().s.build(); - DefaultClientConfigImpl config = new DefaultClientConfigImpl(); - config.setClientName("myapp"); - return config; - } - - - @Bean - @LoadBalanced - RestTemplate restTemplate(){ - return new RestTemplate(); - } - - } + private WebApplicationContextRunner contextRunner = new WebApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(NacosRibbonTestConfiguration.class, + NacosRibbonClientConfiguration.class, + NacosDiscoveryClientAutoConfiguration.class, + RibbonNacosAutoConfiguration.class)) + .withPropertyValues("spring.cloud.nacos.discovery.server-addr=127.0.0.1:8080") + .withPropertyValues("spring.cloud.nacos.discovery.port=18080") + .withPropertyValues("spring.cloud.nacos.discovery.service=myapp"); + + @Test + public void testProperties() { + + this.contextRunner.run(context -> { + NacosServerList serverList = context.getBean(NacosServerList.class); + assertThat(serverList.getServiceId()).isEqualTo("myapp"); + }); + } + + @Configuration + @EnableAutoConfiguration + @EnableDiscoveryClient + static class NacosRibbonTestConfiguration { + + @Bean + IClientConfig iClientConfig() { + // return new IClientConfig.Builder().s.build(); + DefaultClientConfigImpl config = new DefaultClientConfigImpl(); + config.setClientName("myapp"); + return config; + } + + @Bean + @LoadBalanced + RestTemplate restTemplate() { + return new RestTemplate(); + } + + } } From 64c6a0b39eb9fa8bb3e4f36352da8ca07f030b6c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BA=A6=E7=9B=8F?= Date: Mon, 22 Oct 2018 18:38:39 +0800 Subject: [PATCH 09/63] update nacos version to 0.3.0-RC1 --- spring-cloud-alibaba-dependencies/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index 167f71e9d..53ce47c01 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -18,7 +18,7 @@ 0.2.0 3.1.0 - 0.2.1 + 0.3.0-RC1 From 0acc4e1fe3459186b4227637436d3f7d0992cd28 Mon Sep 17 00:00:00 2001 From: flystar32 Date: Mon, 22 Oct 2018 19:37:50 +0800 Subject: [PATCH 10/63] separate namingservice from nacosregistration and discoveryclient --- .../alibaba/nacos/NacosDiscoveryClient.java | 24 ++++--------------- .../nacos/NacosDiscoveryProperties.java | 17 +++++++++++-- .../endpoint/NacosDiscoveryEndpoint.java | 6 +---- .../nacos/registry/NacosRegistration.java | 14 ++--------- 4 files changed, 23 insertions(+), 38 deletions(-) diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java index 59ea1ec73..1d22cd67d 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java @@ -19,15 +19,11 @@ package org.springframework.cloud.alibaba.nacos; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cloud.client.DefaultServiceInstance; import org.springframework.cloud.client.ServiceInstance; import org.springframework.cloud.client.discovery.DiscoveryClient; -import org.springframework.core.env.Environment; import java.util.*; -import javax.annotation.PostConstruct; - import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ListView; @@ -44,17 +40,6 @@ public class NacosDiscoveryClient implements DiscoveryClient { @Autowired private NacosDiscoveryProperties discoveryProperties; - @Autowired - private Environment environment; - - private NamingService namingService; - - @PostConstruct - public void init() { - discoveryProperties.overrideFromEnv(environment); - namingService = discoveryProperties.getNamingService(); - } - @Override public String description() { return DESCRIPTION; @@ -63,7 +48,8 @@ public class NacosDiscoveryClient implements DiscoveryClient { @Override public List getInstances(String serviceId) { try { - List instances = namingService.getAllInstances(serviceId); + List instances = discoveryProperties.namingServiceInstance() + .getAllInstances(serviceId); return hostToServiceInstanceList(instances, serviceId); } catch (Exception e) { @@ -103,8 +89,8 @@ public class NacosDiscoveryClient implements DiscoveryClient { public List getServices() { try { - ListView services = namingService.getServicesOfServer(1, - Integer.MAX_VALUE); + ListView services = discoveryProperties.namingServiceInstance() + .getServicesOfServer(1, Integer.MAX_VALUE); return services.getData(); } catch (Exception e) { @@ -114,6 +100,6 @@ public class NacosDiscoveryClient implements DiscoveryClient { } public NamingService getNamingService() { - return namingService; + return discoveryProperties.namingServiceInstance(); } } diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryProperties.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryProperties.java index aa9271a8d..fdb0d0e93 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryProperties.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryProperties.java @@ -142,6 +142,11 @@ public class NacosDiscoveryProperties { @Autowired private InetUtils inetUtils; + @Autowired + private Environment environment; + + private NamingService namingService; + @PostConstruct public void init() throws SocketException { @@ -180,6 +185,8 @@ public class NacosDiscoveryProperties { } } + + this.overrideFromEnv(environment); } public String getEndpoint() { @@ -350,7 +357,12 @@ public class NacosDiscoveryProperties { } } - public NamingService getNamingService() { + public NamingService namingServiceInstance() { + + if (null != namingService) { + return namingService; + } + Properties properties = new Properties(); properties.put(SERVER_ADDR, serverAddr); properties.put(NAMESPACE, namespace); @@ -360,7 +372,8 @@ public class NacosDiscoveryProperties { properties.put(SECRET_KEY, secretKey); properties.put(CLUSTER_NAME, clusterName); try { - return NacosFactory.createNamingService(properties); + namingService = NacosFactory.createNamingService(properties); + return namingService; } catch (Exception e) { LOGGER.error("create naming service error!properties={},e=,", this, e); diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosDiscoveryEndpoint.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosDiscoveryEndpoint.java index db384df5e..1cbcfb87d 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosDiscoveryEndpoint.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosDiscoveryEndpoint.java @@ -29,7 +29,6 @@ import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; -import org.springframework.cloud.alibaba.nacos.NacosDiscoveryClient; import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties; /** @@ -45,9 +44,6 @@ public class NacosDiscoveryEndpoint { @Autowired private NacosDiscoveryProperties nacosDiscoveryProperties; - @Autowired - private NacosDiscoveryClient discoveryClient; - /** * @return nacos discovery endpoint */ @@ -56,7 +52,7 @@ public class NacosDiscoveryEndpoint { Map result = new HashMap<>(); result.put("NacosDiscoveryProperties", nacosDiscoveryProperties); - NamingService namingService = discoveryClient.getNamingService(); + NamingService namingService = nacosDiscoveryProperties.namingServiceInstance(); List subscribe = Collections.emptyList(); try { diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosRegistration.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosRegistration.java index 639dad981..865358178 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosRegistration.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/registry/NacosRegistration.java @@ -48,15 +48,10 @@ public class NacosRegistration implements Registration, ServiceInstance { @Autowired private ApplicationContext context; - private NamingService nacosNamingService; - @PostConstruct public void init() { Environment env = context.getEnvironment(); - nacosDiscoveryProperties.overrideFromEnv(context.getEnvironment()); - nacosNamingService = nacosDiscoveryProperties.getNamingService(); - Integer managementPort = ManagementServerPortUtils.getPort(context); if (null != managementPort) { Map metadata = nacosDiscoveryProperties.getMetadata(); @@ -124,11 +119,7 @@ public class NacosRegistration implements Registration, ServiceInstance { } public NamingService getNacosNamingService() { - return nacosNamingService; - } - - public void setNacosNamingService(NamingService nacosNamingService) { - this.nacosNamingService = nacosNamingService; + return nacosDiscoveryProperties.namingServiceInstance(); } public void setNacosDiscoveryProperties( @@ -139,7 +130,6 @@ public class NacosRegistration implements Registration, ServiceInstance { @Override public String toString() { return "NacosRegistration{" + "nacosDiscoveryProperties=" - + nacosDiscoveryProperties + ", nacosNamingService=" + nacosNamingService - + '}'; + + nacosDiscoveryProperties + '}'; } } From 5e4cfd887d01b0ef82fc65da1b7eed964a873a35 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Tue, 23 Oct 2018 14:45:58 -0400 Subject: [PATCH 11/63] Updates to snapshot versions --- pom.xml | 73 ++++++++++++++++++- spring-cloud-alibaba-dependencies/pom.xml | 2 +- .../nacos-config-example/pom.xml | 2 +- .../nacos-discovery-consumer-example/pom.xml | 2 +- .../nacos-discovery-provider-example/pom.xml | 2 +- .../nacos-discovery-example/pom.xml | 2 +- .../oss-example/pom.xml | 2 +- spring-cloud-alibaba-examples/pom.xml | 2 +- .../sentinel-core-example/pom.xml | 2 +- .../sentinel-dubbo-api/pom.xml | 2 +- .../sentinel-dubbo-consumer-example/pom.xml | 2 +- .../sentinel-dubbo-provider-example/pom.xml | 2 +- spring-cloud-alibaba-nacos-config/pom.xml | 2 +- spring-cloud-alibaba-nacos-discovery/pom.xml | 2 +- spring-cloud-alibaba-sentinel/pom.xml | 2 +- .../core-support/pom.xml | 2 +- spring-cloud-alibaba-test/pom.xml | 2 +- .../sentinel-test-support/pom.xml | 2 +- spring-cloud-alicloud-oss/pom.xml | 2 +- spring-cloud-starter-alibaba/pom.xml | 2 +- .../pom.xml | 2 +- .../pom.xml | 2 +- .../pom.xml | 2 +- spring-cloud-starter-alicloud/pom.xml | 2 +- .../spring-cloud-starter-alicloud-oss/pom.xml | 2 +- 25 files changed, 93 insertions(+), 28 deletions(-) diff --git a/pom.xml b/pom.xml index 1353ad3e7..604de7541 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0 + 0.2.0.BUILD-SNAPSHOT pom Spring Cloud Alibaba @@ -28,12 +28,12 @@ - https://github.com/spring-cloud-incubator/spring-cloud-alibabacloud + https://github.com/spring-cloud-incubator/spring-cloud-alibaba - scm:git:git://github.com/spring-cloud-incubator/spring-cloud-alibabacloud.git + scm:git:git://github.com/spring-cloud-incubator/spring-cloud-alibaba.git - scm:git:ssh://git@github.com/spring-cloud-incubator/spring-cloud-alibabacloud.git + scm:git:ssh://git@github.com/spring-cloud-incubator/spring-cloud-alibaba.git HEAD @@ -154,4 +154,69 @@ + + + + spring + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/libs-snapshot-local + + true + + + false + + + + spring-milestones + Spring Milestones + https://repo.spring.io/libs-milestone-local + + false + + + + spring-releases + Spring Releases + https://repo.spring.io/release + + false + + + + + + spring-snapshots + Spring Snapshots + https://repo.spring.io/libs-snapshot-local + + true + + + false + + + + spring-milestones + Spring Milestones + https://repo.spring.io/libs-milestone-local + + false + + + + spring-releases + Spring Releases + https://repo.spring.io/libs-release-local + + false + + + + + + \ No newline at end of file diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index 53ce47c01..425a23358 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -10,7 +10,7 @@ spring-cloud-alibaba-dependencies - 0.2.0 + 0.2.0.BUILD-SNAPSHOT pom Spring Cloud Alibaba Dependencies Spring Cloud Alibaba Dependencies diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/pom.xml index a011d829a..f7cedc41a 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0 + 0.2.0.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/pom.xml index 65fbbfb0d..ec343d5c5 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud nacos-discovery-example - 0.2.0 + 0.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/pom.xml index c2e5952aa..294a44b94 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud nacos-discovery-example - 0.2.0 + 0.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml index d4041f669..e56d92d4a 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0 + 0.2.0.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/oss-example/pom.xml b/spring-cloud-alibaba-examples/oss-example/pom.xml index 33442b3ad..cfa08a115 100644 --- a/spring-cloud-alibaba-examples/oss-example/pom.xml +++ b/spring-cloud-alibaba-examples/oss-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0 + 0.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-examples/pom.xml b/spring-cloud-alibaba-examples/pom.xml index f02eadbb3..ed4acb014 100644 --- a/spring-cloud-alibaba-examples/pom.xml +++ b/spring-cloud-alibaba-examples/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0 + 0.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml index 314ee8f82..ec35b244d 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0 + 0.2.0.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api/pom.xml index 6c989f5da..24cde890d 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0 + 0.2.0.BUILD-SNAPSHOT ../../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/pom.xml index ba88981a0..890656ea8 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0 + 0.2.0.BUILD-SNAPSHOT ../../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example/pom.xml index c59f4ba61..87adbbaf7 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0 + 0.2.0.BUILD-SNAPSHOT ../../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-nacos-config/pom.xml b/spring-cloud-alibaba-nacos-config/pom.xml index 6bbc12173..12ff873c1 100644 --- a/spring-cloud-alibaba-nacos-config/pom.xml +++ b/spring-cloud-alibaba-nacos-config/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0 + 0.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-nacos-discovery/pom.xml b/spring-cloud-alibaba-nacos-discovery/pom.xml index e0aaa47d1..3c6bd31af 100644 --- a/spring-cloud-alibaba-nacos-discovery/pom.xml +++ b/spring-cloud-alibaba-nacos-discovery/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0 + 0.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-sentinel/pom.xml b/spring-cloud-alibaba-sentinel/pom.xml index 74b58421b..1ac5ef319 100644 --- a/spring-cloud-alibaba-sentinel/pom.xml +++ b/spring-cloud-alibaba-sentinel/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0 + 0.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-test/core-support/pom.xml b/spring-cloud-alibaba-test/core-support/pom.xml index 72d30a988..26712634f 100644 --- a/spring-cloud-alibaba-test/core-support/pom.xml +++ b/spring-cloud-alibaba-test/core-support/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-test - 0.2.0 + 0.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-test/pom.xml b/spring-cloud-alibaba-test/pom.xml index 1261226d7..289cc1bb2 100644 --- a/spring-cloud-alibaba-test/pom.xml +++ b/spring-cloud-alibaba-test/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0 + 0.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-test/sentinel-test-support/pom.xml b/spring-cloud-alibaba-test/sentinel-test-support/pom.xml index 9197c5f82..3a7b21dc7 100644 --- a/spring-cloud-alibaba-test/sentinel-test-support/pom.xml +++ b/spring-cloud-alibaba-test/sentinel-test-support/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-test - 0.2.0 + 0.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alicloud-oss/pom.xml b/spring-cloud-alicloud-oss/pom.xml index 09e114540..d486d2c02 100644 --- a/spring-cloud-alicloud-oss/pom.xml +++ b/spring-cloud-alicloud-oss/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0 + 0.2.0.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-starter-alibaba/pom.xml b/spring-cloud-starter-alibaba/pom.xml index 303e785f7..877646ae2 100644 --- a/spring-cloud-starter-alibaba/pom.xml +++ b/spring-cloud-starter-alibaba/pom.xml @@ -4,7 +4,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0 + 0.2.0.BUILD-SNAPSHOT spring-cloud-starter-alibaba pom diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config/pom.xml index 60c0e4891..2befd40c8 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config/pom.xml +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-starter-alibaba - 0.2.0 + 0.2.0.BUILD-SNAPSHOT spring-cloud-starter-alibaba-nacos-config Spring Cloud Starter Alibaba Nacos Config diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-discovery/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-discovery/pom.xml index 08bcf547d..2ce41a2fd 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-discovery/pom.xml +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-discovery/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-starter-alibaba - 0.2.0 + 0.2.0.BUILD-SNAPSHOT spring-cloud-starter-alibaba-nacos-discovery Spring Cloud Starter Alibaba Nacos Discovery diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml index 32aab630f..70300f790 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-starter-alibaba - 0.2.0 + 0.2.0.BUILD-SNAPSHOT spring-cloud-starter-alibaba-sentinel Spring Cloud Starter Alibaba Sentinel diff --git a/spring-cloud-starter-alicloud/pom.xml b/spring-cloud-starter-alicloud/pom.xml index a28fc12b4..f44393a4c 100644 --- a/spring-cloud-starter-alicloud/pom.xml +++ b/spring-cloud-starter-alicloud/pom.xml @@ -4,7 +4,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0 + 0.2.0.BUILD-SNAPSHOT spring-cloud-starter-alicloud pom diff --git a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-oss/pom.xml b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-oss/pom.xml index 74ed72f33..e52d22728 100644 --- a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-oss/pom.xml +++ b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-oss/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-starter-alicloud - 0.2.0 + 0.2.0.BUILD-SNAPSHOT spring-cloud-starter-alicloud-oss Spring Cloud Starter Alibaba Cloud OSS From f0d91140e43f4fbfd95ba25856a7c0d819662ffa Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Tue, 23 Oct 2018 16:16:44 -0400 Subject: [PATCH 12/63] Adds circleci support --- .circleci/config.yml | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 .circleci/config.yml diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 000000000..f1ba4f613 --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,40 @@ +version: 2 +jobs: + build: + docker: + - image: springcloud/pipeline-base + user: appuser + environment: + _JAVA_OPTIONS: "-Xms1024m -Xmx2048m" + TERM: dumb + branches: + ignore: + - gh-pages # list of branches to ignore + resource_class: large + steps: + - checkout + - restore_cache: + key: sc-alibaba-{{ .Branch }} + - run: + name: "Download dependencies" + command: ./mvnw -s .settings.xml -U --fail-never dependency:go-offline || true + - save_cache: + key: sc-alibaba-{{ .Branch }} + paths: + - ~/.m2 + - run: + name: "Running build" + command: ./mvnw -s .settings.xml clean install -U -nsu --batch-mode -Dmaven.test.redirectTestOutputToFile=true -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn + - run: + name: "Aggregate test results" + when: always + command: | + mkdir -p ~/junit/ + find . -type f -regex ".*/target/.*-reports/.*" -exec cp {} ~/junit/ \; + bash <(curl -s https://codecov.io/bash) + - store_artifacts: + path: ~/junit/ + destination: artifacts + - store_test_results: + path: ~/junit/ + destination: testartifacts \ No newline at end of file From a443d7045abcfca5b246d30427277532f6003c96 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Tue, 23 Oct 2018 16:21:15 -0400 Subject: [PATCH 13/63] Adds spring profile to circleci --- .circleci/config.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.circleci/config.yml b/.circleci/config.yml index f1ba4f613..44439fd8d 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -17,14 +17,14 @@ jobs: key: sc-alibaba-{{ .Branch }} - run: name: "Download dependencies" - command: ./mvnw -s .settings.xml -U --fail-never dependency:go-offline || true + command: ./mvnw -Pspring -U --fail-never dependency:go-offline || true - save_cache: key: sc-alibaba-{{ .Branch }} paths: - ~/.m2 - run: name: "Running build" - command: ./mvnw -s .settings.xml clean install -U -nsu --batch-mode -Dmaven.test.redirectTestOutputToFile=true -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn + command: ./mvnw -Pspring clean install -U -nsu --batch-mode -Dmaven.test.redirectTestOutputToFile=true -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=warn - run: name: "Aggregate test results" when: always From d7dcae59d2f6ab2913f5d4988a687c63edf6950b Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Wed, 24 Oct 2018 10:38:23 +0800 Subject: [PATCH 14/63] Polish #51. Add a new module spring-cloud-alibaba-sentinel-datasource separate from spring-cloud-alibaba-sentinel. --- pom.xml | 2 +- spring-cloud-alibaba-dependencies/pom.xml | 5 + .../sentinel-core-example/readme-zh.md | 2 +- .../sentinel-core-example/readme.md | 3 +- .../pom.xml | 69 +++++ .../sentinel/datasource/DataSourceLoader.java | 152 ++++++++++ .../SentinelDataSourceConstants.java | 33 +++ .../SentinelDataSourcePostProcessor.java | 258 +++++++++++++++++ .../SentinelDataSourceRegistry.java | 7 +- .../annotation/SentinelDataSource.java | 4 +- .../ApolloDataSourceFactoryBean.java | 63 +++++ .../FileRefreshableDataSourceFactoryBean.java | 76 +++++ .../NacosDataSourceFactoryBean.java | 62 ++++ .../ZookeeperDataSourceFactoryBean.java | 81 ++++++ .../util/PropertySourcesUtils.java | 12 +- .../META-INF/sentinel-datasource.properties | 0 spring-cloud-alibaba-sentinel/pom.xml | 26 +- .../alibaba/sentinel/SentinelConstants.java | 7 - .../sentinel/datasource/DataSourceLoader.java | 151 ---------- .../SentinelDataSourcePostProcessor.java | 264 ------------------ .../ApolloDataSourceFactoryBean.java | 61 ---- .../FileRefreshableDataSourceFactoryBean.java | 74 ----- .../NacosDataSourceFactoryBean.java | 60 ---- .../ZookeeperDataSourceFactoryBean.java | 80 ------ .../pom.xml | 12 - 25 files changed, 818 insertions(+), 746 deletions(-) create mode 100644 spring-cloud-alibaba-sentinel-datasource/pom.xml create mode 100644 spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/DataSourceLoader.java create mode 100644 spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourceConstants.java create mode 100644 spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java rename {spring-cloud-alibaba-sentinel => spring-cloud-alibaba-sentinel-datasource}/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourceRegistry.java (91%) rename {spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel => spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource}/annotation/SentinelDataSource.java (91%) create mode 100644 spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ApolloDataSourceFactoryBean.java create mode 100644 spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/FileRefreshableDataSourceFactoryBean.java create mode 100644 spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/NacosDataSourceFactoryBean.java create mode 100644 spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ZookeeperDataSourceFactoryBean.java rename {spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel => spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource}/util/PropertySourcesUtils.java (96%) rename {spring-cloud-alibaba-sentinel => spring-cloud-alibaba-sentinel-datasource}/src/main/resources/META-INF/sentinel-datasource.properties (100%) delete mode 100644 spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/DataSourceLoader.java delete mode 100644 spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java delete mode 100644 spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ApolloDataSourceFactoryBean.java delete mode 100644 spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/FileRefreshableDataSourceFactoryBean.java delete mode 100644 spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/NacosDataSourceFactoryBean.java delete mode 100644 spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ZookeeperDataSourceFactoryBean.java diff --git a/pom.xml b/pom.xml index 1353ad3e7..5b742e3dd 100644 --- a/pom.xml +++ b/pom.xml @@ -74,7 +74,7 @@ spring-cloud-alibaba-dependencies spring-cloud-alibaba-sentinel - + spring-cloud-alibaba-sentinel-datasource spring-cloud-alibaba-nacos-config spring-cloud-alibaba-nacos-discovery spring-cloud-alibaba-examples diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index 167f71e9d..b253dd10a 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -95,6 +95,11 @@ spring-cloud-alibaba-sentinel ${project.version} + + org.springframework.cloud + spring-cloud-alibaba-sentinel-datasource + ${project.version} + org.springframework.cloud spring-cloud-alicloud-oss diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md index 4c0b2aeba..c07c20e37 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme-zh.md @@ -222,7 +222,7 @@ Sentinel starter 整合了目前存在的几类 DataSource。只需要在配置 `spring.cloud.sentinel.datasource.converter`代表 `Converter` 在 Spring 容器里的 name。如果没找到,会抛出异常。 -type目前支持file, nacos, zk, apollo。 +type目前支持file, nacos, zk, apollo。其中nacos,zk,apollo的使用需要加上对应的依赖`sentinel-datasource-nacos`, `sentinel-datasource-zookeeper`, `sentinel-datasource-apollo` ### 自定义DataSource diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme.md b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme.md index a51ed38ad..dcdd77fc3 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme.md +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/readme.md @@ -193,7 +193,8 @@ spring.cloud.sentinel.datasource.recommendRefreshMs means the recommendRefreshMs spring.cloud.sentinel.datasource.converter means the name of spring bean that type is Converter. If the bean is not exists, will throw exception. -Now datasource type support 4 categories: file, nacos, zk, apollo. +Now datasource type support 4 categories: file, nacos, zk, apollo. If you want to using nacos, zk or apollo, you should add `sentinel-datasource-nacos`, `sentinel-datasource-zookeeper` or `sentinel-datasource-apollo` dependency. + ### User-defined DataSource diff --git a/spring-cloud-alibaba-sentinel-datasource/pom.xml b/spring-cloud-alibaba-sentinel-datasource/pom.xml new file mode 100644 index 000000000..077e52615 --- /dev/null +++ b/spring-cloud-alibaba-sentinel-datasource/pom.xml @@ -0,0 +1,69 @@ + + + + + org.springframework.cloud + spring-cloud-alibaba + 0.2.0 + + 4.0.0 + + org.springframework.cloud + spring-cloud-alibaba-sentinel-datasource + Spring Cloud Alibaba Sentinel DataSource + + + + + com.alibaba.csp + sentinel-datasource-extension + + + + com.alibaba.csp + sentinel-datasource-nacos + provided + true + + + + com.alibaba.csp + sentinel-datasource-zookeeper + provided + true + + + + com.alibaba.csp + sentinel-datasource-apollo + provided + true + + + + + + org.springframework.boot + spring-boot + provided + true + + + + org.springframework.boot + spring-boot-autoconfigure + provided + true + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + diff --git a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/DataSourceLoader.java b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/DataSourceLoader.java new file mode 100644 index 000000000..40f17197d --- /dev/null +++ b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/DataSourceLoader.java @@ -0,0 +1,152 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.sentinel.datasource; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.Properties; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; + +import com.alibaba.csp.sentinel.datasource.ReadableDataSource; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.core.io.Resource; +import org.springframework.core.io.support.PathMatchingResourcePatternResolver; +import org.springframework.core.io.support.PropertiesLoaderUtils; +import org.springframework.core.io.support.ResourcePatternResolver; +import org.springframework.util.Assert; +import org.springframework.util.ClassUtils; + +import static org.springframework.core.io.support.ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX; + +/** + * {@link ReadableDataSource} Loader + * + * @author Jim + */ +public class DataSourceLoader { + + private static final Logger logger = LoggerFactory.getLogger(DataSourceLoader.class); + + private final static String PROPERTIES_RESOURCE_LOCATION = "META-INF/sentinel-datasource.properties"; + + private final static String ALL_PROPERTIES_RESOURCES_LOCATION = CLASSPATH_ALL_URL_PREFIX + + PROPERTIES_RESOURCE_LOCATION; + + private final static ConcurrentMap> dataSourceClassesCache + = new ConcurrentHashMap>( + 4); + + static void loadAllDataSourceClassesCache() { + Map> dataSourceClassesMap = loadAllDataSourceClassesCache( + ALL_PROPERTIES_RESOURCES_LOCATION); + + dataSourceClassesCache.putAll(dataSourceClassesMap); + } + + static Map> loadAllDataSourceClassesCache( + String resourcesLocation) { + + Map> dataSourcesMap + = new HashMap>( + 4); + + ClassLoader classLoader = DataSourceLoader.class.getClassLoader(); + + ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); + + try { + + Resource[] resources = resolver.getResources(resourcesLocation); + + for (Resource resource : resources) { + if (resource.exists()) { + Properties properties = PropertiesLoaderUtils + .loadProperties(resource); + for (Map.Entry entry : properties.entrySet()) { + + String type = (String)entry.getKey(); + String className = (String)entry.getValue(); + + if (!ClassUtils.isPresent(className, classLoader)) { + if (logger.isDebugEnabled()) { + logger.debug( + "Sentinel DataSource implementation [ type : " + + type + ": , class : " + className + + " , url : " + resource.getURL() + + "] was not present in current classpath , " + + "thus loading will be ignored , please add dependency if required !"); + } + continue; + } + + Assert.isTrue(!dataSourcesMap.containsKey(type), + "The duplicated type[" + type + + "] of SentinelDataSource were found in " + + "resource [" + resource.getURL() + "]"); + + Class dataSourceClass = ClassUtils.resolveClassName(className, + classLoader); + Assert.isAssignable(ReadableDataSource.class, dataSourceClass); + + dataSourcesMap.put(type, + (Class)dataSourceClass); + + if (logger.isDebugEnabled()) { + logger.debug("Sentinel DataSource implementation [ type : " + + type + ": , class : " + className + + "] was loaded."); + } + } + } + } + + } catch (IOException e) { + if (logger.isErrorEnabled()) { + logger.error(e.getMessage(), e); + } + } + + return dataSourcesMap; + } + + public static Class loadClass(String type) + throws IllegalArgumentException { + + Class dataSourceClass = dataSourceClassesCache.get(type); + + if (dataSourceClass == null) { + if (dataSourceClassesCache.isEmpty()) { + loadAllDataSourceClassesCache(); + dataSourceClass = dataSourceClassesCache.get(type); + } + } + + if (dataSourceClass == null) { + throw new IllegalArgumentException( + "Sentinel DataSource implementation [ type : " + type + + " ] can't be found!"); + } + + return dataSourceClass; + + } + +} diff --git a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourceConstants.java b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourceConstants.java new file mode 100644 index 000000000..49549399f --- /dev/null +++ b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourceConstants.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.sentinel.datasource; + +/** + * @author Jim + */ +public interface SentinelDataSourceConstants { + + String PROPERTY_PREFIX = "spring.cloud.sentinel"; + + String PROPERTY_ITEM_SEPARATOR = "."; + + String PROPERTY_DATASOURCE_NAME = "datasource"; + + String PROPERTY_DATASOURCE_PREFIX = PROPERTY_PREFIX + PROPERTY_ITEM_SEPARATOR + + PROPERTY_DATASOURCE_NAME; + +} diff --git a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java new file mode 100644 index 000000000..0a83db7a5 --- /dev/null +++ b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java @@ -0,0 +1,258 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.sentinel.datasource; + +import java.beans.PropertyDescriptor; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import com.alibaba.csp.sentinel.datasource.Converter; +import com.alibaba.csp.sentinel.datasource.ReadableDataSource; +import com.alibaba.csp.sentinel.property.SentinelProperty; +import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule; +import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager; +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager; +import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; +import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; +import com.alibaba.csp.sentinel.slots.system.SystemRule; +import com.alibaba.csp.sentinel.slots.system.SystemRuleManager; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.PropertyValues; +import org.springframework.beans.factory.BeanCreationException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; +import org.springframework.beans.factory.support.BeanDefinitionBuilder; +import org.springframework.beans.factory.support.DefaultListableBeanFactory; +import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor; +import org.springframework.beans.factory.support.RootBeanDefinition; +import org.springframework.boot.context.event.ApplicationStartedEvent; +import org.springframework.cloud.alibaba.sentinel.datasource.annotation.SentinelDataSource; +import org.springframework.cloud.alibaba.sentinel.datasource.util.PropertySourcesUtils; +import org.springframework.context.ApplicationContext; +import org.springframework.context.event.EventListener; +import org.springframework.core.env.ConfigurableEnvironment; +import org.springframework.util.ReflectionUtils; +import org.springframework.util.StringUtils; + +import static org.springframework.core.annotation.AnnotationUtils.getAnnotation; + +/** + * {@link SentinelDataSource @SentinelDataSource} Post Processor + * + * @author Jim + * @see ReadableDataSource + * @see SentinelDataSource + */ +public class SentinelDataSourcePostProcessor + extends InstantiationAwareBeanPostProcessorAdapter + implements MergedBeanDefinitionPostProcessor { + + private static final Logger logger = LoggerFactory + .getLogger(SentinelDataSourcePostProcessor.class); + + @Autowired + private ApplicationContext applicationContext; + + @Autowired + private ConfigurableEnvironment environment; + + private final Map> dataSourceFieldCache = new ConcurrentHashMap<>( + 64); + + @Override + public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, + Class beanType, String beanName) { + // find all fields using by @SentinelDataSource annotation + ReflectionUtils.doWithFields(beanType, new ReflectionUtils.FieldCallback() { + @Override + public void doWith(Field field) + throws IllegalArgumentException, IllegalAccessException { + SentinelDataSource annotation = getAnnotation(field, + SentinelDataSource.class); + if (annotation != null) { + if (Modifier.isStatic(field.getModifiers())) { + if (logger.isWarnEnabled()) { + logger.warn( + "@SentinelDataSource annotation is not supported on static fields: " + + field); + } + return; + } + if (dataSourceFieldCache.containsKey(beanName)) { + dataSourceFieldCache.get(beanName) + .add(new SentinelDataSourceField(annotation, field)); + } else { + List list = new ArrayList<>(); + list.add(new SentinelDataSourceField(annotation, field)); + dataSourceFieldCache.put(beanName, list); + } + } + } + }); + } + + @Override + public PropertyValues postProcessPropertyValues(PropertyValues pvs, + PropertyDescriptor[] pds, Object bean, String beanName) + throws BeanCreationException { + if (dataSourceFieldCache.containsKey(beanName)) { + List sentinelDataSourceFields = dataSourceFieldCache + .get(beanName); + sentinelDataSourceFields.forEach(sentinelDataSourceField -> { + try { + // construct DataSource field annotated by @SentinelDataSource + Field field = sentinelDataSourceField.getField(); + ReflectionUtils.makeAccessible(field); + String dataSourceBeanName = constructDataSource( + sentinelDataSourceField.getSentinelDataSource()); + field.set(bean, applicationContext.getBean(dataSourceBeanName)); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (Exception e) { + e.printStackTrace(); + } + }); + } + return pvs; + } + + private String constructDataSource(SentinelDataSource annotation) { + String prefix = annotation.value(); + if (StringUtils.isEmpty(prefix)) { + prefix = SentinelDataSourceConstants.PROPERTY_DATASOURCE_PREFIX; + } + Map propertyMap = PropertySourcesUtils + .getSubProperties(environment.getPropertySources(), prefix); + String alias = propertyMap.get("type").toString(); + Class dataSourceClass = DataSourceLoader.loadClass(alias); + + String beanName = StringUtils.isEmpty(annotation.name()) + ? StringUtils.uncapitalize(dataSourceClass.getSimpleName()) + "_" + prefix + : annotation.name(); + if (applicationContext.containsBean(beanName)) { + return beanName; + } + + Class targetClass = null; + // if alias exists in SentinelDataSourceRegistry, wired properties into + // FactoryBean + if (SentinelDataSourceRegistry.checkFactoryBean(alias)) { + targetClass = SentinelDataSourceRegistry.getFactoryBean(alias); + } else { + // if alias not exists in SentinelDataSourceRegistry, wired properties into + // raw class + targetClass = dataSourceClass; + } + + registerDataSource(beanName, targetClass, propertyMap); + + return beanName; + } + + private void registerDataSource(String beanName, Class targetClass, + Map propertyMap) { + BeanDefinitionBuilder builder = BeanDefinitionBuilder + .genericBeanDefinition(targetClass); + for (String propertyName : propertyMap.keySet()) { + Field field = ReflectionUtils.findField(targetClass, propertyName); + if (field != null) { + if (field.getType().isAssignableFrom(Converter.class)) { + // Converter get from ApplicationContext + builder.addPropertyReference(propertyName, + propertyMap.get(propertyName).toString()); + } else { + // wired properties + builder.addPropertyValue(propertyName, propertyMap.get(propertyName)); + } + } + } + + DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory)applicationContext + .getAutowireCapableBeanFactory(); + beanFactory.registerBeanDefinition(beanName, builder.getBeanDefinition()); + } + + @EventListener(classes = ApplicationStartedEvent.class) + public void appStartedListener(ApplicationStartedEvent event) throws Exception { + Map dataSourceMap = event.getApplicationContext().getBeansOfType( + ReadableDataSource.class); + if (dataSourceMap.size() == 1) { + ReadableDataSource dataSource = dataSourceMap.values().iterator().next(); + Object ruleConfig = dataSource.loadConfig(); + SentinelProperty sentinelProperty = dataSource.getProperty(); + if (checkRuleType(ruleConfig, FlowRule.class)) { + FlowRuleManager.register2Property(sentinelProperty); + } + if (checkRuleType(ruleConfig, DegradeRule.class)) { + DegradeRuleManager.register2Property(sentinelProperty); + } + if (checkRuleType(ruleConfig, SystemRule.class)) { + SystemRuleManager.register2Property(sentinelProperty); + } + if (checkRuleType(ruleConfig, AuthorityRule.class)) { + AuthorityRuleManager.register2Property(sentinelProperty); + } + } + } + + private boolean checkRuleType(Object ruleConfig, Class type) { + if (ruleConfig.getClass() == type) { + return true; + } else if (ruleConfig instanceof List) { + List ruleList = (List)ruleConfig; + if (ruleList.stream().filter(rule -> rule.getClass() == type).toArray().length == ruleList.size()) { + return true; + } + } + return false; + } + + class SentinelDataSourceField { + private SentinelDataSource sentinelDataSource; + private Field field; + + public SentinelDataSourceField(SentinelDataSource sentinelDataSource, + Field field) { + this.sentinelDataSource = sentinelDataSource; + this.field = field; + } + + public SentinelDataSource getSentinelDataSource() { + return sentinelDataSource; + } + + public void setSentinelDataSource(SentinelDataSource sentinelDataSource) { + this.sentinelDataSource = sentinelDataSource; + } + + public Field getField() { + return field; + } + + public void setField(Field field) { + this.field = field; + } + } + +} diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourceRegistry.java b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourceRegistry.java similarity index 91% rename from spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourceRegistry.java rename to spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourceRegistry.java index ceb90ee1f..5108196b5 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourceRegistry.java +++ b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourceRegistry.java @@ -16,7 +16,7 @@ package org.springframework.cloud.alibaba.sentinel.datasource; -import java.util.concurrent.ConcurrentHashMap; +import java.util.HashMap; import com.alibaba.csp.sentinel.datasource.ReadableDataSource; @@ -29,7 +29,7 @@ import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.Zookeep /** * Registry to save DataSource FactoryBean * - * @author fangjian + * @author Jim * @see ReadableDataSource * @see FileRefreshableDataSourceFactoryBean * @see ZookeeperDataSourceFactoryBean @@ -38,7 +38,7 @@ import org.springframework.cloud.alibaba.sentinel.datasource.factorybean.Zookeep */ public class SentinelDataSourceRegistry { - private static ConcurrentHashMap> cache = new ConcurrentHashMap<>( + private static HashMap> cache = new HashMap<>( 32); static { @@ -54,7 +54,6 @@ public class SentinelDataSourceRegistry { public static synchronized void registerFactoryBean(String alias, Class clazz) { - cache.putIfAbsent(alias, clazz); cache.put(alias, clazz); } diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/annotation/SentinelDataSource.java b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/annotation/SentinelDataSource.java similarity index 91% rename from spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/annotation/SentinelDataSource.java rename to spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/annotation/SentinelDataSource.java index 93ef1bdb9..b6b33226b 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/annotation/SentinelDataSource.java +++ b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/annotation/SentinelDataSource.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.cloud.alibaba.sentinel.annotation; +package org.springframework.cloud.alibaba.sentinel.datasource.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; @@ -31,7 +31,7 @@ import org.springframework.core.annotation.AliasFor; * into a Spring Bean. The Properties of DataSource bean get from config files with * specific prefix. * - * @author fangjian + * Jim * @see ReadableDataSource */ @Target({ElementType.FIELD}) diff --git a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ApolloDataSourceFactoryBean.java b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ApolloDataSourceFactoryBean.java new file mode 100644 index 000000000..e0df905b1 --- /dev/null +++ b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ApolloDataSourceFactoryBean.java @@ -0,0 +1,63 @@ +package org.springframework.cloud.alibaba.sentinel.datasource.factorybean; + +import com.alibaba.csp.sentinel.datasource.Converter; +import com.alibaba.csp.sentinel.datasource.apollo.ApolloDataSource; + +import org.springframework.beans.factory.FactoryBean; + +/** + * A {@link FactoryBean} for creating {@link ApolloDataSource} instance. + * + * @author Jim + * @see ApolloDataSource + */ +public class ApolloDataSourceFactoryBean implements FactoryBean { + + private String namespaceName; + private String flowRulesKey; + private String defaultFlowRuleValue; + private Converter converter; + + @Override + public ApolloDataSource getObject() throws Exception { + return new ApolloDataSource(namespaceName, flowRulesKey, defaultFlowRuleValue, + converter); + } + + @Override + public Class getObjectType() { + return ApolloDataSource.class; + } + + public String getNamespaceName() { + return namespaceName; + } + + public void setNamespaceName(String namespaceName) { + this.namespaceName = namespaceName; + } + + public String getFlowRulesKey() { + return flowRulesKey; + } + + public void setFlowRulesKey(String flowRulesKey) { + this.flowRulesKey = flowRulesKey; + } + + public String getDefaultFlowRuleValue() { + return defaultFlowRuleValue; + } + + public void setDefaultFlowRuleValue(String defaultFlowRuleValue) { + this.defaultFlowRuleValue = defaultFlowRuleValue; + } + + public Converter getConverter() { + return converter; + } + + public void setConverter(Converter Converter) { + this.converter = Converter; + } +} diff --git a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/FileRefreshableDataSourceFactoryBean.java b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/FileRefreshableDataSourceFactoryBean.java new file mode 100644 index 000000000..c7b4771f3 --- /dev/null +++ b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/FileRefreshableDataSourceFactoryBean.java @@ -0,0 +1,76 @@ +package org.springframework.cloud.alibaba.sentinel.datasource.factorybean; + +import java.io.File; +import java.nio.charset.Charset; + +import com.alibaba.csp.sentinel.datasource.Converter; +import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource; + +import org.springframework.beans.factory.FactoryBean; + +/** + * A {@link FactoryBean} for creating {@link FileRefreshableDataSource} instance. + * + * @author Jim + * @see FileRefreshableDataSource + */ +public class FileRefreshableDataSourceFactoryBean + implements FactoryBean { + + private String file; + private String charset; + private long recommendRefreshMs; + private int bufSize; + private Converter converter; + + @Override + public FileRefreshableDataSource getObject() throws Exception { + return new FileRefreshableDataSource(new File(file), converter, + recommendRefreshMs, bufSize, Charset.forName(charset)); + } + + @Override + public Class getObjectType() { + return FileRefreshableDataSource.class; + } + + public String getFile() { + return file; + } + + public void setFile(String file) { + this.file = file; + } + + public String getCharset() { + return charset; + } + + public void setCharset(String charset) { + this.charset = charset; + } + + public long getRecommendRefreshMs() { + return recommendRefreshMs; + } + + public void setRecommendRefreshMs(long recommendRefreshMs) { + this.recommendRefreshMs = recommendRefreshMs; + } + + public int getBufSize() { + return bufSize; + } + + public void setBufSize(int bufSize) { + this.bufSize = bufSize; + } + + public Converter getConverter() { + return converter; + } + + public void setConverter(Converter Converter) { + this.converter = Converter; + } +} diff --git a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/NacosDataSourceFactoryBean.java b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/NacosDataSourceFactoryBean.java new file mode 100644 index 000000000..12dbd138f --- /dev/null +++ b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/NacosDataSourceFactoryBean.java @@ -0,0 +1,62 @@ +package org.springframework.cloud.alibaba.sentinel.datasource.factorybean; + +import com.alibaba.csp.sentinel.datasource.Converter; +import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource; + +import org.springframework.beans.factory.FactoryBean; + +/** + * A {@link FactoryBean} for creating {@link NacosDataSource} instance. + * + * @author Jim + * @see NacosDataSource + */ +public class NacosDataSourceFactoryBean implements FactoryBean { + + private String serverAddr; + private String groupId; + private String dataId; + private Converter converter; + + @Override + public NacosDataSource getObject() throws Exception { + return new NacosDataSource(serverAddr, groupId, dataId, converter); + } + + @Override + public Class getObjectType() { + return NacosDataSource.class; + } + + public String getServerAddr() { + return serverAddr; + } + + public void setServerAddr(String serverAddr) { + this.serverAddr = serverAddr; + } + + public String getGroupId() { + return groupId; + } + + public void setGroupId(String groupId) { + this.groupId = groupId; + } + + public String getDataId() { + return dataId; + } + + public void setDataId(String dataId) { + this.dataId = dataId; + } + + public Converter getConverter() { + return converter; + } + + public void setConverter(Converter Converter) { + this.converter = Converter; + } +} diff --git a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ZookeeperDataSourceFactoryBean.java b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ZookeeperDataSourceFactoryBean.java new file mode 100644 index 000000000..8b4ed177e --- /dev/null +++ b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ZookeeperDataSourceFactoryBean.java @@ -0,0 +1,81 @@ +package org.springframework.cloud.alibaba.sentinel.datasource.factorybean; + +import com.alibaba.csp.sentinel.datasource.Converter; +import com.alibaba.csp.sentinel.datasource.zookeeper.ZookeeperDataSource; + +import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.factory.FactoryBean; + +/** + * A {@link FactoryBean} for creating {@link ZookeeperDataSource} instance. + * + * @author Jim + * @see ZookeeperDataSource + */ +public class ZookeeperDataSourceFactoryBean implements FactoryBean { + + private String serverAddr; + + private String path; + + private String groupId; + private String dataId; + + private Converter converter; + + @Override + public ZookeeperDataSource getObject() throws Exception { + if (StringUtils.isNotEmpty(groupId) && StringUtils.isNotEmpty(dataId)) { + // the path will be /{groupId}/{dataId} + return new ZookeeperDataSource(serverAddr, groupId, dataId, converter); + } else { + // using path directly + return new ZookeeperDataSource(serverAddr, path, converter); + } + } + + @Override + public Class getObjectType() { + return ZookeeperDataSource.class; + } + + public String getServerAddr() { + return serverAddr; + } + + public void setServerAddr(String serverAddr) { + this.serverAddr = serverAddr; + } + + public String getPath() { + return path; + } + + public void setPath(String path) { + this.path = path; + } + + public String getGroupId() { + return groupId; + } + + public void setGroupId(String groupId) { + this.groupId = groupId; + } + + public String getDataId() { + return dataId; + } + + public void setDataId(String dataId) { + this.dataId = dataId; + } + + public Converter getConverter() { + return converter; + } + + public void setConverter(Converter Converter) { + this.converter = Converter; + } +} diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/util/PropertySourcesUtils.java b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/util/PropertySourcesUtils.java similarity index 96% rename from spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/util/PropertySourcesUtils.java rename to spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/util/PropertySourcesUtils.java index 6eafbd63b..fde8fc44f 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/util/PropertySourcesUtils.java +++ b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/util/PropertySourcesUtils.java @@ -14,16 +14,16 @@ * limitations under the License. */ -package org.springframework.cloud.alibaba.sentinel.util; - -import org.springframework.core.env.EnumerablePropertySource; -import org.springframework.core.env.PropertySource; -import org.springframework.core.env.PropertySources; +package org.springframework.cloud.alibaba.sentinel.datasource.util; import java.util.LinkedHashMap; import java.util.Map; import java.util.Properties; +import org.springframework.core.env.EnumerablePropertySource; +import org.springframework.core.env.PropertySource; +import org.springframework.core.env.PropertySources; + /** * {@link PropertySources} Utilities * @@ -47,7 +47,7 @@ public abstract class PropertySourcesUtils { for (PropertySource source : propertySources) { if (source instanceof EnumerablePropertySource) { - for (String name : ((EnumerablePropertySource) source).getPropertyNames()) { + for (String name : ((EnumerablePropertySource)source).getPropertyNames()) { if (!subProperties.containsKey(name) && name.startsWith(normalizedPrefix)) { String subName = name.substring(normalizedPrefix.length()); if (!subProperties.containsKey(subName)) { // take first one diff --git a/spring-cloud-alibaba-sentinel/src/main/resources/META-INF/sentinel-datasource.properties b/spring-cloud-alibaba-sentinel-datasource/src/main/resources/META-INF/sentinel-datasource.properties similarity index 100% rename from spring-cloud-alibaba-sentinel/src/main/resources/META-INF/sentinel-datasource.properties rename to spring-cloud-alibaba-sentinel-datasource/src/main/resources/META-INF/sentinel-datasource.properties diff --git a/spring-cloud-alibaba-sentinel/pom.xml b/spring-cloud-alibaba-sentinel/pom.xml index 74b58421b..87f56fdba 100644 --- a/spring-cloud-alibaba-sentinel/pom.xml +++ b/spring-cloud-alibaba-sentinel/pom.xml @@ -27,35 +27,17 @@ com.alibaba.csp - sentinel-datasource-extension - - - - com.alibaba.csp - sentinel-datasource-nacos - true - - - - com.alibaba.csp - sentinel-datasource-zookeeper - true - - - - com.alibaba.csp - sentinel-datasource-apollo - true + sentinel-annotation-aspectj com.alibaba.csp - sentinel-annotation-aspectj + sentinel-dubbo-adapter - com.alibaba.csp - sentinel-dubbo-adapter + org.springframework.cloud + spring-cloud-alibaba-sentinel-datasource diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelConstants.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelConstants.java index 747044a50..620445a4a 100644 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelConstants.java +++ b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/SentinelConstants.java @@ -23,11 +23,4 @@ public interface SentinelConstants { String PROPERTY_PREFIX = "spring.cloud.sentinel"; - String PROPERTY_ITEM_SEPARATOR = "."; - - String PROPERTY_DATASOURCE_NAME = "datasource"; - - String PROPERTY_DATASOURCE_PREFIX = PROPERTY_PREFIX + PROPERTY_ITEM_SEPARATOR - + PROPERTY_DATASOURCE_NAME; - } diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/DataSourceLoader.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/DataSourceLoader.java deleted file mode 100644 index 068717660..000000000 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/DataSourceLoader.java +++ /dev/null @@ -1,151 +0,0 @@ -/* - * Copyright (C) 2018 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.alibaba.sentinel.datasource; - -import static org.springframework.core.io.support.ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX; - -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; -import java.util.Properties; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.core.io.Resource; -import org.springframework.core.io.support.PathMatchingResourcePatternResolver; -import org.springframework.core.io.support.PropertiesLoaderUtils; -import org.springframework.core.io.support.ResourcePatternResolver; -import org.springframework.util.Assert; -import org.springframework.util.ClassUtils; - -import com.alibaba.csp.sentinel.datasource.ReadableDataSource; - -/** - * {@link ReadableDataSource} Loader - * - * @author fangjian - */ -public class DataSourceLoader { - - private static final Logger logger = LoggerFactory.getLogger(DataSourceLoader.class); - - private final static String PROPERTIES_RESOURCE_LOCATION = "META-INF/sentinel-datasource.properties"; - - private final static String ALL_PROPERTIES_RESOURCES_LOCATION = CLASSPATH_ALL_URL_PREFIX - + PROPERTIES_RESOURCE_LOCATION; - - private final static ConcurrentMap> dataSourceClassesCache = new ConcurrentHashMap>( - 4); - - static void loadAllDataSourceClassesCache() { - Map> dataSourceClassesMap = loadAllDataSourceClassesCache( - ALL_PROPERTIES_RESOURCES_LOCATION); - - dataSourceClassesCache.putAll(dataSourceClassesMap); - } - - static Map> loadAllDataSourceClassesCache( - String resourcesLocation) { - - Map> dataSourcesMap = new HashMap>( - 4); - - ClassLoader classLoader = DataSourceLoader.class.getClassLoader(); - - ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); - - try { - - Resource[] resources = resolver.getResources(resourcesLocation); - - for (Resource resource : resources) { - if (resource.exists()) { - Properties properties = PropertiesLoaderUtils - .loadProperties(resource); - for (Map.Entry entry : properties.entrySet()) { - - String type = (String) entry.getKey(); - String className = (String) entry.getValue(); - - if (!ClassUtils.isPresent(className, classLoader)) { - if (logger.isDebugEnabled()) { - logger.debug( - "Sentinel DataSource implementation [ type : " - + type + ": , class : " + className - + " , url : " + resource.getURL() - + "] was not present in current classpath , " - + "thus loading will be ignored , please add dependency if required !"); - } - continue; - } - - Assert.isTrue(!dataSourcesMap.containsKey(type), - "The duplicated type[" + type - + "] of SentinelDataSource were found in " - + "resource [" + resource.getURL() + "]"); - - Class dataSourceClass = ClassUtils.resolveClassName(className, - classLoader); - Assert.isAssignable(ReadableDataSource.class, dataSourceClass); - - dataSourcesMap.put(type, - (Class) dataSourceClass); - - if (logger.isDebugEnabled()) { - logger.debug("Sentinel DataSource implementation [ type : " - + type + ": , class : " + className - + "] was loaded."); - } - } - } - } - - } - catch (IOException e) { - if (logger.isErrorEnabled()) { - logger.error(e.getMessage(), e); - } - } - - return dataSourcesMap; - } - - public static Class loadClass(String type) - throws IllegalArgumentException { - - Class dataSourceClass = dataSourceClassesCache.get(type); - - if (dataSourceClass == null) { - if (dataSourceClassesCache.isEmpty()) { - loadAllDataSourceClassesCache(); - dataSourceClass = dataSourceClassesCache.get(type); - } - } - - if (dataSourceClass == null) { - throw new IllegalArgumentException( - "Sentinel DataSource implementation [ type : " + type - + " ] can't be found!"); - } - - return dataSourceClass; - - } - -} diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java deleted file mode 100644 index 627c5be03..000000000 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java +++ /dev/null @@ -1,264 +0,0 @@ -/* - * Copyright (C) 2018 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.alibaba.sentinel.datasource; - -import static org.springframework.core.annotation.AnnotationUtils.getAnnotation; - -import java.beans.PropertyDescriptor; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.PropertyValues; -import org.springframework.beans.factory.BeanCreationException; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessorAdapter; -import org.springframework.beans.factory.support.BeanDefinitionBuilder; -import org.springframework.beans.factory.support.DefaultListableBeanFactory; -import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor; -import org.springframework.beans.factory.support.RootBeanDefinition; -import org.springframework.boot.context.event.ApplicationStartedEvent; -import org.springframework.cloud.alibaba.sentinel.SentinelConstants; -import org.springframework.cloud.alibaba.sentinel.annotation.SentinelDataSource; -import org.springframework.cloud.alibaba.sentinel.util.PropertySourcesUtils; -import org.springframework.context.ApplicationContext; -import org.springframework.context.event.EventListener; -import org.springframework.core.env.ConfigurableEnvironment; -import org.springframework.util.ReflectionUtils; -import org.springframework.util.StringUtils; - -import com.alibaba.csp.sentinel.datasource.Converter; -import com.alibaba.csp.sentinel.datasource.ReadableDataSource; -import com.alibaba.csp.sentinel.property.SentinelProperty; -import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRule; -import com.alibaba.csp.sentinel.slots.block.authority.AuthorityRuleManager; -import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRule; -import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager; -import com.alibaba.csp.sentinel.slots.block.flow.FlowRule; -import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; -import com.alibaba.csp.sentinel.slots.system.SystemRule; -import com.alibaba.csp.sentinel.slots.system.SystemRuleManager; - -/** - * {@link SentinelDataSource @SentinelDataSource} Post Processor - * - * @author fangjian - * @see com.alibaba.csp.sentinel.datasource.ReadableDataSource - * @see SentinelDataSource - */ -public class SentinelDataSourcePostProcessor - extends InstantiationAwareBeanPostProcessorAdapter - implements MergedBeanDefinitionPostProcessor { - - private static final Logger logger = LoggerFactory - .getLogger(SentinelDataSourcePostProcessor.class); - - @Autowired - private ApplicationContext applicationContext; - - @Autowired - private ConfigurableEnvironment environment; - - private final Map> dataSourceFieldCache = new ConcurrentHashMap<>( - 64); - - @Override - public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, - Class beanType, String beanName) { - // find all fields using by @SentinelDataSource annotation - ReflectionUtils.doWithFields(beanType, new ReflectionUtils.FieldCallback() { - @Override - public void doWith(Field field) - throws IllegalArgumentException, IllegalAccessException { - SentinelDataSource annotation = getAnnotation(field, - SentinelDataSource.class); - if (annotation != null) { - if (Modifier.isStatic(field.getModifiers())) { - if (logger.isWarnEnabled()) { - logger.warn( - "@SentinelDataSource annotation is not supported on static fields: " - + field); - } - return; - } - if (dataSourceFieldCache.containsKey(beanName)) { - dataSourceFieldCache.get(beanName) - .add(new SentinelDataSourceField(annotation, field)); - } - else { - List list = new ArrayList<>(); - list.add(new SentinelDataSourceField(annotation, field)); - dataSourceFieldCache.put(beanName, list); - } - } - } - }); - } - - @Override - public PropertyValues postProcessPropertyValues(PropertyValues pvs, - PropertyDescriptor[] pds, Object bean, String beanName) - throws BeanCreationException { - if (dataSourceFieldCache.containsKey(beanName)) { - List sentinelDataSourceFields = dataSourceFieldCache - .get(beanName); - sentinelDataSourceFields.forEach(sentinelDataSourceField -> { - try { - // construct DataSource field annotated by @SentinelDataSource - Field field = sentinelDataSourceField.getField(); - ReflectionUtils.makeAccessible(field); - String dataSourceBeanName = constructDataSource( - sentinelDataSourceField.getSentinelDataSource()); - field.set(bean, applicationContext.getBean(dataSourceBeanName)); - } - catch (IllegalAccessException e) { - e.printStackTrace(); - } - catch (Exception e) { - e.printStackTrace(); - } - }); - } - return pvs; - } - - private String constructDataSource(SentinelDataSource annotation) { - String prefix = annotation.value(); - if (StringUtils.isEmpty(prefix)) { - prefix = SentinelConstants.PROPERTY_DATASOURCE_PREFIX; - } - Map propertyMap = PropertySourcesUtils - .getSubProperties(environment.getPropertySources(), prefix); - String alias = propertyMap.get("type").toString(); - Class dataSourceClass = DataSourceLoader.loadClass(alias); - - String beanName = StringUtils.isEmpty(annotation.name()) - ? StringUtils.uncapitalize(dataSourceClass.getSimpleName()) + "_" + prefix - : annotation.name(); - if (applicationContext.containsBean(beanName)) { - return beanName; - } - - Class targetClass = null; - // if alias exists in SentinelDataSourceRegistry, wired properties into - // FactoryBean - if (SentinelDataSourceRegistry.checkFactoryBean(alias)) { - targetClass = SentinelDataSourceRegistry.getFactoryBean(alias); - } - else { - // if alias not exists in SentinelDataSourceRegistry, wired properties into - // raw class - targetClass = dataSourceClass; - } - - registerDataSource(beanName, targetClass, propertyMap); - - return beanName; - } - - private void registerDataSource(String beanName, Class targetClass, - Map propertyMap) { - BeanDefinitionBuilder builder = BeanDefinitionBuilder - .genericBeanDefinition(targetClass); - for (String propertyName : propertyMap.keySet()) { - Field field = ReflectionUtils.findField(targetClass, propertyName); - if (field != null) { - if (field.getType().isAssignableFrom(Converter.class)) { - // Converter get from ApplicationContext - builder.addPropertyReference(propertyName, - propertyMap.get(propertyName).toString()); - } - else { - // wired properties - builder.addPropertyValue(propertyName, propertyMap.get(propertyName)); - } - } - } - - DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory) applicationContext - .getAutowireCapableBeanFactory(); - beanFactory.registerBeanDefinition(beanName, builder.getBeanDefinition()); - } - - - @EventListener(classes = ApplicationStartedEvent.class) - public void appStartedListener(ApplicationStartedEvent event) throws Exception { - Map dataSourceMap = event.getApplicationContext().getBeansOfType(ReadableDataSource.class); - if(dataSourceMap.size() == 1) { - ReadableDataSource dataSource = dataSourceMap.values().iterator().next(); - Object ruleConfig = dataSource.loadConfig(); - SentinelProperty sentinelProperty = dataSource.getProperty(); - if(checkRuleType(ruleConfig, FlowRule.class)) { - FlowRuleManager.register2Property(sentinelProperty); - } - if(checkRuleType(ruleConfig, DegradeRule.class)) { - DegradeRuleManager.register2Property(sentinelProperty); - } - if(checkRuleType(ruleConfig, SystemRule.class)) { - SystemRuleManager.register2Property(sentinelProperty); - } - if(checkRuleType(ruleConfig, AuthorityRule.class)) { - AuthorityRuleManager.register2Property(sentinelProperty); - } - } - } - - private boolean checkRuleType(Object ruleConfig, Class type) { - if(ruleConfig.getClass() == type) { - return true; - } else if(ruleConfig instanceof List) { - List ruleList = (List)ruleConfig; - if(ruleList.stream().filter(rule -> rule.getClass() == type).toArray().length == ruleList.size()) { - return true; - } - } - return false; - } - - class SentinelDataSourceField { - private SentinelDataSource sentinelDataSource; - private Field field; - - public SentinelDataSourceField(SentinelDataSource sentinelDataSource, - Field field) { - this.sentinelDataSource = sentinelDataSource; - this.field = field; - } - - public SentinelDataSource getSentinelDataSource() { - return sentinelDataSource; - } - - public void setSentinelDataSource(SentinelDataSource sentinelDataSource) { - this.sentinelDataSource = sentinelDataSource; - } - - public Field getField() { - return field; - } - - public void setField(Field field) { - this.field = field; - } - } - -} diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ApolloDataSourceFactoryBean.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ApolloDataSourceFactoryBean.java deleted file mode 100644 index 09b39bb44..000000000 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ApolloDataSourceFactoryBean.java +++ /dev/null @@ -1,61 +0,0 @@ -package org.springframework.cloud.alibaba.sentinel.datasource.factorybean; - -import org.springframework.beans.factory.FactoryBean; - -import com.alibaba.csp.sentinel.datasource.Converter; -import com.alibaba.csp.sentinel.datasource.apollo.ApolloDataSource; - -/** - * @author fangjian - * @see ApolloDataSource - */ -public class ApolloDataSourceFactoryBean implements FactoryBean { - - private String namespaceName; - private String flowRulesKey; - private String defaultFlowRuleValue; - private Converter converter; - - @Override - public ApolloDataSource getObject() throws Exception { - return new ApolloDataSource(namespaceName, flowRulesKey, defaultFlowRuleValue, - converter); - } - - @Override - public Class getObjectType() { - return ApolloDataSource.class; - } - - public String getNamespaceName() { - return namespaceName; - } - - public void setNamespaceName(String namespaceName) { - this.namespaceName = namespaceName; - } - - public String getFlowRulesKey() { - return flowRulesKey; - } - - public void setFlowRulesKey(String flowRulesKey) { - this.flowRulesKey = flowRulesKey; - } - - public String getDefaultFlowRuleValue() { - return defaultFlowRuleValue; - } - - public void setDefaultFlowRuleValue(String defaultFlowRuleValue) { - this.defaultFlowRuleValue = defaultFlowRuleValue; - } - - public Converter getConverter() { - return converter; - } - - public void setConverter(Converter Converter) { - this.converter = Converter; - } -} diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/FileRefreshableDataSourceFactoryBean.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/FileRefreshableDataSourceFactoryBean.java deleted file mode 100644 index 759827647..000000000 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/FileRefreshableDataSourceFactoryBean.java +++ /dev/null @@ -1,74 +0,0 @@ -package org.springframework.cloud.alibaba.sentinel.datasource.factorybean; - -import java.io.File; -import java.nio.charset.Charset; - -import org.springframework.beans.factory.FactoryBean; - -import com.alibaba.csp.sentinel.datasource.Converter; -import com.alibaba.csp.sentinel.datasource.FileRefreshableDataSource; - -/** - * @author fangjian - * @see FileRefreshableDataSource - */ -public class FileRefreshableDataSourceFactoryBean - implements FactoryBean { - - private String file; - private String charset; - private long recommendRefreshMs; - private int bufSize; - private Converter converter; - - @Override - public FileRefreshableDataSource getObject() throws Exception { - return new FileRefreshableDataSource(new File(file), converter, - recommendRefreshMs, bufSize, Charset.forName(charset)); - } - - @Override - public Class getObjectType() { - return FileRefreshableDataSource.class; - } - - public String getFile() { - return file; - } - - public void setFile(String file) { - this.file = file; - } - - public String getCharset() { - return charset; - } - - public void setCharset(String charset) { - this.charset = charset; - } - - public long getRecommendRefreshMs() { - return recommendRefreshMs; - } - - public void setRecommendRefreshMs(long recommendRefreshMs) { - this.recommendRefreshMs = recommendRefreshMs; - } - - public int getBufSize() { - return bufSize; - } - - public void setBufSize(int bufSize) { - this.bufSize = bufSize; - } - - public Converter getConverter() { - return converter; - } - - public void setConverter(Converter Converter) { - this.converter = Converter; - } -} diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/NacosDataSourceFactoryBean.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/NacosDataSourceFactoryBean.java deleted file mode 100644 index 3de75fad4..000000000 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/NacosDataSourceFactoryBean.java +++ /dev/null @@ -1,60 +0,0 @@ -package org.springframework.cloud.alibaba.sentinel.datasource.factorybean; - -import org.springframework.beans.factory.FactoryBean; - -import com.alibaba.csp.sentinel.datasource.Converter; -import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource; - -/** - * @author fangjian - * @see NacosDataSource - */ -public class NacosDataSourceFactoryBean implements FactoryBean { - - private String serverAddr; - private String groupId; - private String dataId; - private Converter converter; - - @Override - public NacosDataSource getObject() throws Exception { - return new NacosDataSource(serverAddr, groupId, dataId, converter); - } - - @Override - public Class getObjectType() { - return NacosDataSource.class; - } - - public String getServerAddr() { - return serverAddr; - } - - public void setServerAddr(String serverAddr) { - this.serverAddr = serverAddr; - } - - public String getGroupId() { - return groupId; - } - - public void setGroupId(String groupId) { - this.groupId = groupId; - } - - public String getDataId() { - return dataId; - } - - public void setDataId(String dataId) { - this.dataId = dataId; - } - - public Converter getConverter() { - return converter; - } - - public void setConverter(Converter Converter) { - this.converter = Converter; - } -} diff --git a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ZookeeperDataSourceFactoryBean.java b/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ZookeeperDataSourceFactoryBean.java deleted file mode 100644 index aa10270a7..000000000 --- a/spring-cloud-alibaba-sentinel/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/factorybean/ZookeeperDataSourceFactoryBean.java +++ /dev/null @@ -1,80 +0,0 @@ -package org.springframework.cloud.alibaba.sentinel.datasource.factorybean; - -import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.FactoryBean; - -import com.alibaba.csp.sentinel.datasource.Converter; -import com.alibaba.csp.sentinel.datasource.zookeeper.ZookeeperDataSource; - -/** - * @author fangjian - * @see ZookeeperDataSource - */ -public class ZookeeperDataSourceFactoryBean implements FactoryBean { - - private String serverAddr; - - private String path; - - private String groupId; - private String dataId; - - private Converter converter; - - @Override - public ZookeeperDataSource getObject() throws Exception { - if (StringUtils.isNotEmpty(groupId) && StringUtils.isNotEmpty(dataId)) { - // the path will be /{groupId}/{dataId} - return new ZookeeperDataSource(serverAddr, groupId, dataId, converter); - } - else { - // using path directly - return new ZookeeperDataSource(serverAddr, path, converter); - } - } - - @Override - public Class getObjectType() { - return ZookeeperDataSource.class; - } - - public String getServerAddr() { - return serverAddr; - } - - public void setServerAddr(String serverAddr) { - this.serverAddr = serverAddr; - } - - public String getPath() { - return path; - } - - public void setPath(String path) { - this.path = path; - } - - public String getGroupId() { - return groupId; - } - - public void setGroupId(String groupId) { - this.groupId = groupId; - } - - public String getDataId() { - return dataId; - } - - public void setDataId(String dataId) { - this.dataId = dataId; - } - - public Converter getConverter() { - return converter; - } - - public void setConverter(Converter Converter) { - this.converter = Converter; - } -} diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml index 32aab630f..a2adef837 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml @@ -15,18 +15,6 @@ org.springframework.cloud spring-cloud-alibaba-sentinel - - com.alibaba.csp - sentinel-datasource-nacos - - - com.alibaba.csp - sentinel-datasource-zookeeper - - - com.alibaba.csp - sentinel-datasource-apollo - From ca41dc5c66ab105fc2baec9f79511d53208550e3 Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Wed, 24 Oct 2018 10:51:34 +0800 Subject: [PATCH 15/63] Updates to snapshot versions --- spring-cloud-alibaba-sentinel-datasource/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud-alibaba-sentinel-datasource/pom.xml b/spring-cloud-alibaba-sentinel-datasource/pom.xml index 077e52615..23048b77b 100644 --- a/spring-cloud-alibaba-sentinel-datasource/pom.xml +++ b/spring-cloud-alibaba-sentinel-datasource/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0 + 0.2.0.BUILD-SNAPSHOT 4.0.0 From c9c9c775726095ba9fad981129dd9742e1c336bb Mon Sep 17 00:00:00 2001 From: flystar32 Date: Wed, 24 Oct 2018 11:39:54 +0800 Subject: [PATCH 16/63] update version of spring-cloud-commons, spring-cloud-build and spring-cloud-dependencied-parent --- pom.xml | 4 ++-- spring-cloud-alibaba-dependencies/pom.xml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index 604de7541..b114a3034 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ org.springframework.cloud spring-cloud-build - 2.0.0.RELEASE + 2.0.3.RELEASE @@ -55,7 +55,7 @@ - 2.0.0.RELEASE + 2.0.1.RELEASE 2.0.0.RELEASE 2.0.0.RELEASE diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index 425a23358..ff8c5c583 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -6,7 +6,7 @@ spring-cloud-dependencies-parent org.springframework.cloud - 2.0.0.RELEASE + 2.0.3.RELEASE spring-cloud-alibaba-dependencies From 71d9987a39e554f9f836993903875f1fab181d51 Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Thu, 25 Oct 2018 10:43:01 +0800 Subject: [PATCH 17/63] sentinel add logs to query datasource process. --- .../SentinelDataSourcePostProcessor.java | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java index 0a83db7a5..d54dd0299 100644 --- a/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java +++ b/spring-cloud-alibaba-sentinel-datasource/src/main/java/org/springframework/cloud/alibaba/sentinel/datasource/SentinelDataSourcePostProcessor.java @@ -195,37 +195,51 @@ public class SentinelDataSourcePostProcessor @EventListener(classes = ApplicationStartedEvent.class) public void appStartedListener(ApplicationStartedEvent event) throws Exception { + logger.info("[Sentinel Starter] Start to find ReadableDataSource"); Map dataSourceMap = event.getApplicationContext().getBeansOfType( ReadableDataSource.class); if (dataSourceMap.size() == 1) { + logger.info("[Sentinel Starter] There exists only one ReadableDataSource named {}, start to load rules", + dataSourceMap.keySet().iterator().next()); ReadableDataSource dataSource = dataSourceMap.values().iterator().next(); Object ruleConfig = dataSource.loadConfig(); SentinelProperty sentinelProperty = dataSource.getProperty(); - if (checkRuleType(ruleConfig, FlowRule.class)) { + Integer rulesNum; + if ((rulesNum = checkRuleType(ruleConfig, FlowRule.class)) > 0) { FlowRuleManager.register2Property(sentinelProperty); + logger.info("[Sentinel Starter] load {} flow rules", rulesNum); } - if (checkRuleType(ruleConfig, DegradeRule.class)) { + if ((rulesNum = checkRuleType(ruleConfig, DegradeRule.class)) > 0) { DegradeRuleManager.register2Property(sentinelProperty); + logger.info("[Sentinel Starter] load {} degrade rules", rulesNum); } - if (checkRuleType(ruleConfig, SystemRule.class)) { + if ((rulesNum = checkRuleType(ruleConfig, SystemRule.class)) > 0) { SystemRuleManager.register2Property(sentinelProperty); + logger.info("[Sentinel Starter] load {} system rules", rulesNum); } - if (checkRuleType(ruleConfig, AuthorityRule.class)) { + if ((rulesNum = checkRuleType(ruleConfig, AuthorityRule.class)) > 0) { AuthorityRuleManager.register2Property(sentinelProperty); + logger.info("[Sentinel Starter] load {} authority rules", rulesNum); } + } else if (dataSourceMap.size() > 1) { + logger.warn( + "[Sentinel Starter] There exists more than one ReadableDataSource, can not choose which one to load"); + } else { + logger.warn( + "[Sentinel Starter] No ReadableDataSource exists"); } } - private boolean checkRuleType(Object ruleConfig, Class type) { + private Integer checkRuleType(Object ruleConfig, Class type) { if (ruleConfig.getClass() == type) { - return true; + return 1; } else if (ruleConfig instanceof List) { List ruleList = (List)ruleConfig; if (ruleList.stream().filter(rule -> rule.getClass() == type).toArray().length == ruleList.size()) { - return true; + return ruleList.size(); } } - return false; + return -1; } class SentinelDataSourceField { From e636e260b83fd4a03e62d3535216d7e476300534 Mon Sep 17 00:00:00 2001 From: flystar32 Date: Fri, 26 Oct 2018 11:21:23 +0800 Subject: [PATCH 18/63] update readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index c0941db39..c69092a13 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ # Spring Cloud Alibaba +A project maintained by Alibaba. + See the [中文文档](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/README-zh.md) for Chinese readme. Spring Cloud Alibaba provides a one-stop solution for distributed application development. It contains all the components required to develop distributed applications, making it easy for you to develop your applications using Spring Cloud. From b70d7bb3007292cb769af4818df723afb183e9af Mon Sep 17 00:00:00 2001 From: "chenzhu.zxl" Date: Fri, 26 Oct 2018 19:08:12 +0800 Subject: [PATCH 19/63] closes #63, closes #64, closes #65, closes #66. --- pom.xml | 7 + spring-cloud-alibaba-dependencies/pom.xml | 65 +++- .../acm-example/acm-local-example/pom.xml | 27 ++ .../cloud/examples/AcmApplication.java | 34 ++ .../cloud/examples/EchoController.java | 41 +++ .../src/main/resources/bootstrap.properties | 6 + .../ans-consumer-feign-example/pom.xml | 23 ++ .../cloud/examples/ConsumerApplication.java | 34 ++ .../alibaba/cloud/examples/EchoService.java | 32 ++ .../cloud/examples/HomeController.java | 46 +++ .../src/main/resources/application.properties | 4 + .../ans-consumer-ribbon-example/pom.xml | 19 + .../cloud/examples/ConsumerApplication.java | 43 +++ .../cloud/examples/HomeController.java | 47 +++ .../src/main/resources/application.properties | 4 + .../ans-example/ans-provider-example/pom.xml | 19 + .../cloud/examples/EchoController.java | 42 +++ .../cloud/examples/ProviderApplication.java | 34 ++ .../src/main/resources/application.properties | 2 + .../cloud/examples/OSSApplication.java | 64 ++-- .../alibaba/cloud/examples/OSSController.java | 89 +++-- .../src/main/resources/application.properties | 10 +- .../src/main/resources/oss-test.json | 5 +- spring-cloud-alibaba-examples/pom.xml | 4 + spring-cloud-alicloud-acm/pom.xml | 71 ++++ .../alicloud/acm/AcmAutoConfiguration.java | 73 ++++ .../acm/AcmPropertySourceRepository.java | 68 ++++ .../acm/bootstrap/AcmPropertySource.java | 55 +++ .../bootstrap/AcmPropertySourceBuilder.java | 96 +++++ .../bootstrap/AcmPropertySourceLocator.java | 142 ++++++++ .../DiamondConnectionFailureAnalyzer.java | 40 +++ .../DiamondConnectionFailureException.java | 52 +++ .../alicloud/acm/endpoint/AcmEndpoint.java | 77 ++++ .../AcmEndpointAutoConfiguration.java | 52 +++ .../acm/endpoint/AcmHealthIndicator.java | 71 ++++ .../acm/refresh/AcmContextRefresher.java | 125 +++++++ .../acm/refresh/AcmRefreshHistory.java | 72 ++++ ...itional-spring-configuration-metadata.json | 9 + .../main/resources/META-INF/spring.factories | 9 + spring-cloud-alicloud-ans/pom.xml | 96 +++++ .../alicloud/ans/AnsAutoConfiguration.java | 66 ++++ .../alicloud/ans/AnsDiscoveryClient.java | 87 +++++ .../AnsDiscoveryClientAutoConfiguration.java | 41 +++ .../alicloud/ans/AnsServiceInstance.java | 90 +++++ .../alicloud/ans/ConditionalOnAnsEnabled.java | 33 ++ .../alicloud/ans/endpoint/AnsEndpoint.java | 72 ++++ .../AnsEndpointAutoConfiguration.java | 36 ++ .../registry/AnsAutoServiceRegistration.java | 103 ++++++ .../ans/registry/AnsRegistration.java | 131 +++++++ .../ans/registry/AnsServiceRegistry.java | 114 ++++++ .../ribbon/AnsRibbonClientConfiguration.java | 39 ++ .../cloud/alicloud/ans/ribbon/AnsServer.java | 74 ++++ .../alicloud/ans/ribbon/AnsServerList.java | 78 ++++ .../ans/ribbon/ConditionalOnRibbonAns.java | 34 ++ .../ribbon/RibbonAnsAutoConfiguration.java | 39 ++ ...itional-spring-configuration-metadata.json | 28 ++ .../main/resources/META-INF/spring.factories | 6 + .../ans/ribbon/AnsServiceListTests.java | 78 ++++ spring-cloud-alicloud-context/pom.xml | 99 +++++ .../AliCloudContextAutoConfiguration.java | 29 ++ .../alicloud/context/AliCloudProperties.java | 57 +++ .../acm/AcmContextBootstrapConfiguration.java | 56 +++ .../alicloud/context/acm/AcmProperties.java | 158 ++++++++ .../ans/AnsContextApplicationListener.java | 54 +++ .../ans/AnsContextAutoConfiguration.java | 44 +++ .../alicloud/context/ans/AnsProperties.java | 337 ++++++++++++++++++ .../edas/EdasContextAutoConfiguration.java | 48 +++ .../alicloud/context/edas/EdasProperties.java | 87 +++++ .../AbstractOnceApplicationListener.java | 86 +++++ .../oss/OssContextAutoConfiguration.java | 80 +++++ .../alicloud/context/oss/OssProperties.java | 109 ++++++ ...itional-spring-configuration-metadata.json | 34 ++ .../main/resources/META-INF/spring.factories | 9 + .../alicloud/acm/AcmAutoConfiguration.java | 23 ++ .../alicloud/ans/AnsAutoConfiguration.java | 23 ++ .../context/AliCloudPropertiesTests.java | 53 +++ .../AliCloudSpringApplicationTests.java | 50 +++ .../context/acm/AnsPropertiesTests.java | 78 ++++ .../context/ans/AnsPropertiesTests.java | 101 ++++++ .../context/edas/EdasPropertiesTests.java | 68 ++++ .../oss/OssAutoConfigurationTests.java | 33 +- .../alicloud/oss/OssAutoConfiguration.java | 23 ++ spring-cloud-alicloud-oss/pom.xml | 6 +- .../cloud/alibaba/oss/OSSProperties.java | 118 ------ .../alibaba/oss/endpoint/OSSEndpoint.java | 72 ---- .../oss/OssApplicationListener.java} | 6 +- .../oss/OssAutoConfiguration.java} | 40 +-- .../oss/OssConstants.java} | 4 +- .../alicloud/oss/endpoint/OssEndpoint.java | 71 ++++ .../OssEndpointAutoConfiguration.java} | 8 +- .../resource/OssStorageProtocolResolver.java} | 8 +- .../oss/resource/OssStorageResource.java} | 12 +- .../main/resources/META-INF/spring.factories | 6 +- .../OSSMultiClientAutoConfigurationTests.java | 116 ------ spring-cloud-starter-alicloud/pom.xml | 2 + .../spring-cloud-starter-alicloud-acm/pom.xml | 20 ++ .../spring-cloud-starter-alicloud-ans/pom.xml | 21 ++ 97 files changed, 4738 insertions(+), 469 deletions(-) create mode 100644 spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml create mode 100644 spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/AcmApplication.java create mode 100644 spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/EchoController.java create mode 100644 spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/resources/bootstrap.properties create mode 100644 spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml create mode 100644 spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ConsumerApplication.java create mode 100644 spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/EchoService.java create mode 100644 spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/HomeController.java create mode 100644 spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/resources/application.properties create mode 100644 spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml create mode 100644 spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ConsumerApplication.java create mode 100644 spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/HomeController.java create mode 100644 spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/resources/application.properties create mode 100644 spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml create mode 100644 spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/EchoController.java create mode 100644 spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ProviderApplication.java create mode 100644 spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/resources/application.properties create mode 100644 spring-cloud-alicloud-acm/pom.xml create mode 100644 spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/AcmAutoConfiguration.java create mode 100644 spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/AcmPropertySourceRepository.java create mode 100644 spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySource.java create mode 100644 spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceBuilder.java create mode 100644 spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceLocator.java create mode 100644 spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/diagnostics/analyzer/DiamondConnectionFailureAnalyzer.java create mode 100644 spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/diagnostics/analyzer/DiamondConnectionFailureException.java create mode 100644 spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmEndpoint.java create mode 100644 spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmEndpointAutoConfiguration.java create mode 100644 spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmHealthIndicator.java create mode 100644 spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/refresh/AcmContextRefresher.java create mode 100644 spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/refresh/AcmRefreshHistory.java create mode 100644 spring-cloud-alicloud-acm/src/main/resources/META-INF/additional-spring-configuration-metadata.json create mode 100644 spring-cloud-alicloud-acm/src/main/resources/META-INF/spring.factories create mode 100644 spring-cloud-alicloud-ans/pom.xml create mode 100644 spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsAutoConfiguration.java create mode 100644 spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsDiscoveryClient.java create mode 100644 spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsDiscoveryClientAutoConfiguration.java create mode 100644 spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsServiceInstance.java create mode 100644 spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ConditionalOnAnsEnabled.java create mode 100644 spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/endpoint/AnsEndpoint.java create mode 100644 spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/endpoint/AnsEndpointAutoConfiguration.java create mode 100644 spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/registry/AnsAutoServiceRegistration.java create mode 100644 spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/registry/AnsRegistration.java create mode 100644 spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/registry/AnsServiceRegistry.java create mode 100644 spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/AnsRibbonClientConfiguration.java create mode 100644 spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/AnsServer.java create mode 100644 spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/AnsServerList.java create mode 100644 spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/ConditionalOnRibbonAns.java create mode 100644 spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/RibbonAnsAutoConfiguration.java create mode 100644 spring-cloud-alicloud-ans/src/main/resources/META-INF/additional-spring-configuration-metadata.json create mode 100644 spring-cloud-alicloud-ans/src/main/resources/META-INF/spring.factories create mode 100644 spring-cloud-alicloud-ans/src/test/java/org/springframework/cloud/alibaba/ans/ribbon/AnsServiceListTests.java create mode 100644 spring-cloud-alicloud-context/pom.xml create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/AliCloudContextAutoConfiguration.java create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/AliCloudProperties.java create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmContextBootstrapConfiguration.java create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmProperties.java create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsContextApplicationListener.java create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsContextAutoConfiguration.java create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsProperties.java create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/edas/EdasContextAutoConfiguration.java create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/edas/EdasProperties.java create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/listener/AbstractOnceApplicationListener.java create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/oss/OssContextAutoConfiguration.java create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/oss/OssProperties.java create mode 100644 spring-cloud-alicloud-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json create mode 100644 spring-cloud-alicloud-context/src/main/resources/META-INF/spring.factories create mode 100644 spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/acm/AcmAutoConfiguration.java create mode 100644 spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/ans/AnsAutoConfiguration.java create mode 100644 spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudPropertiesTests.java create mode 100644 spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudSpringApplicationTests.java create mode 100644 spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/acm/AnsPropertiesTests.java create mode 100644 spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/ans/AnsPropertiesTests.java create mode 100644 spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/edas/EdasPropertiesTests.java rename spring-cloud-alicloud-oss/src/test/java/org/springframework/cloud/alibaba/oss/test/OSSAutoConfigurationTests.java => spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/oss/OssAutoConfigurationTests.java (66%) create mode 100644 spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/oss/OssAutoConfiguration.java delete mode 100644 spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/OSSProperties.java delete mode 100644 spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/endpoint/OSSEndpoint.java rename spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/{alibaba/oss/OSSApplicationListener.java => alicloud/oss/OssApplicationListener.java} (90%) rename spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/{alibaba/oss/OSSAutoConfiguration.java => alicloud/oss/OssAutoConfiguration.java} (52%) rename spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/{alibaba/oss/OSSConstants.java => alicloud/oss/OssConstants.java} (90%) create mode 100644 spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alicloud/oss/endpoint/OssEndpoint.java rename spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/{alibaba/oss/endpoint/OSSEndpointAutoConfiguration.java => alicloud/oss/endpoint/OssEndpointAutoConfiguration.java} (88%) rename spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/{alibaba/oss/resource/OSSStorageProtocolResolver.java => alicloud/oss/resource/OssStorageProtocolResolver.java} (92%) rename spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/{alibaba/oss/resource/OSSStorageResource.java => alicloud/oss/resource/OssStorageResource.java} (93%) delete mode 100644 spring-cloud-alicloud-oss/src/test/java/org/springframework/cloud/alibaba/oss/test/OSSMultiClientAutoConfigurationTests.java create mode 100644 spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-acm/pom.xml create mode 100644 spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml diff --git a/pom.xml b/pom.xml index 94a523859..1bf958f4a 100644 --- a/pom.xml +++ b/pom.xml @@ -47,6 +47,10 @@ fangjian fangjian0423@gmail.com + + xiaolongzuo + 150349407@qq.com + hengyunabc hengyunabc@gmail.com @@ -82,6 +86,9 @@ spring-cloud-starter-alibaba spring-cloud-starter-alicloud spring-cloud-alicloud-oss + spring-cloud-alicloud-context + spring-cloud-alicloud-acm + spring-cloud-alicloud-ans diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index 4680ca710..e08a9dc28 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -19,11 +19,47 @@ 0.2.0 3.1.0 0.3.0-RC1 + 1.0.8 + 0.1.1 + 4.0.1 + 1.0.0 + 2.16.0 - + + + com.alibaba.cloud + alicloud-context + ${alicloud.context.version} + + + com.aliyun + aliyun-java-sdk-edas + ${aliyun.sdk.edas.version} + + + com.aliyun + aliyun-java-sdk-core + + + + + com.aliyun + aliyun-java-sdk-core + ${aliyun.sdk.version} + + + com.alibaba.ans + ans-sdk + ${ans.version} + + + com.alibaba.edas.acm + acm-sdk + ${acm.version} + com.alibaba.nacos nacos-client @@ -115,6 +151,21 @@ spring-cloud-alibaba-nacos-config ${project.version} + + org.springframework.cloud + spring-cloud-alicloud-acm + ${project.version} + + + org.springframework.cloud + spring-cloud-alicloud-ans + ${project.version} + + + org.springframework.cloud + spring-cloud-alicloud-context + ${project.version} + @@ -140,9 +191,17 @@ ${project.version} + + org.springframework.cloud + spring-cloud-starter-alicloud-ans + ${project.version} + - - + + org.springframework.cloud + spring-cloud-starter-alicloud-acm + ${project.version} + diff --git a/spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml b/spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml new file mode 100644 index 000000000..f83e5da34 --- /dev/null +++ b/spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml @@ -0,0 +1,27 @@ + + + + spring-cloud-alibaba-examples + org.springframework.cloud + 0.2.0.BUILD-SNAPSHOT + + 4.0.0 + acm-local-example + + + + org.springframework.cloud + spring-cloud-starter-alicloud-acm + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-actuator + + + \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/AcmApplication.java b/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/AcmApplication.java new file mode 100644 index 000000000..5d48c381d --- /dev/null +++ b/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/AcmApplication.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.cloud.examples; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +/** + * @author xiaolongzuo + */ +@SpringBootApplication +@EnableDiscoveryClient +public class AcmApplication { + + public static void main(String[] args) { + SpringApplication.run(AcmApplication.class, args); + } + +} diff --git a/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/EchoController.java b/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/EchoController.java new file mode 100644 index 000000000..b7c874610 --- /dev/null +++ b/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/EchoController.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.cloud.examples; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author xiaolongzuo + */ +@RestController +public class EchoController { + + private static final Logger LOGGER = LoggerFactory.getLogger(EchoController.class); + + @Value("${user.id}") + private String userId; + + @RequestMapping(value = "/") + public String echo() { + LOGGER.info("User id is " + userId); + return userId; + } +} diff --git a/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/resources/bootstrap.properties b/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/resources/bootstrap.properties new file mode 100644 index 000000000..dceeba95a --- /dev/null +++ b/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/resources/bootstrap.properties @@ -0,0 +1,6 @@ +spring.application.group=com.alibaba.acm +spring.application.name=acm-local +server.port=18089 +spring.cloud.alicloud.acm.server-list=127.0.0.1 +spring.cloud.alicloud.acm.server-port=8080 + diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml new file mode 100644 index 000000000..e0966d4f7 --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml @@ -0,0 +1,23 @@ + + + + spring-cloud-alibaba-examples + org.springframework.cloud + 0.2.0.BUILD-SNAPSHOT + + 4.0.0 + ans-consumer-feign-example + + + + org.springframework.cloud + spring-cloud-starter-alicloud-ans + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ConsumerApplication.java b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ConsumerApplication.java new file mode 100644 index 000000000..e95b2e9cf --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ConsumerApplication.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.cloud.examples; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.openfeign.EnableFeignClients; + +/** + * @author xiaolongzuo + */ +@SpringBootApplication +@EnableFeignClients(basePackages = {"org.springframework.cloud.alibaba.cloud.examples"}) +public class ConsumerApplication { + + public static void main(String[] args) { + SpringApplication.run(ConsumerApplication.class, args); + } + +} diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/EchoService.java b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/EchoService.java new file mode 100644 index 000000000..78b3c486a --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/EchoService.java @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.cloud.examples; + +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; + +/** + * @author xiaolongzuo + */ +@FeignClient(value = "ans-provider") +public interface EchoService { + + @RequestMapping(path = "echo/{str}") + String echo(@RequestParam("str") String param); + +} diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/HomeController.java b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/HomeController.java new file mode 100644 index 000000000..8f2a3e8c2 --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/HomeController.java @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.cloud.examples; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author xiaolongzuo + */ +@RestController +public class HomeController { + + private static final Logger LOGGER = LoggerFactory.getLogger(HomeController.class); + + @Autowired + private EchoService echoService; + + @RequestMapping(value = "/", method = RequestMethod.GET, produces = "application/json") + public String home() { + LOGGER.info("-----------------consumer调用开始-----------------"); + String param = "Nice to meet you."; + LOGGER.info("消费者传递参数:" + param); + String result = echoService.echo(param); + LOGGER.info("收到提供者响应:" + result); + return param + "
" + result; + } +} diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/resources/application.properties new file mode 100644 index 000000000..1b25ea1e7 --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/resources/application.properties @@ -0,0 +1,4 @@ +server.port=18083 +# The following configuration can be omitted. +spring.cloud.ans.server.list=127.0.0.1 +spring.cloud.ans.server.port=8080 \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml new file mode 100644 index 000000000..238ea7f4c --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml @@ -0,0 +1,19 @@ + + + + spring-cloud-alibaba-examples + org.springframework.cloud + 0.2.0.BUILD-SNAPSHOT + + 4.0.0 + ans-consumer-ribbon-example + + + + org.springframework.cloud + spring-cloud-starter-alicloud-ans + + + \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ConsumerApplication.java b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ConsumerApplication.java new file mode 100644 index 000000000..223a2d891 --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ConsumerApplication.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.cloud.examples; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; +import org.springframework.cloud.client.loadbalancer.LoadBalanced; +import org.springframework.context.annotation.Bean; +import org.springframework.web.client.RestTemplate; + +/** + * @author xiaolongzuo + */ +@SpringBootApplication +@EnableDiscoveryClient +public class ConsumerApplication { + + @Bean + @LoadBalanced + public RestTemplate restTemplate() { + return new RestTemplate(); + } + + public static void main(String[] args) { + SpringApplication.run(ConsumerApplication.class, args); + } + +} diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/HomeController.java b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/HomeController.java new file mode 100644 index 000000000..e3853fdd3 --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/HomeController.java @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.cloud.examples; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; + +/** + * @author xiaolongzuo + */ +@RestController +public class HomeController { + + private static final Logger LOGGER = LoggerFactory.getLogger(HomeController.class); + + @Autowired + private RestTemplate restTemplate; + + @RequestMapping(value = "/", method = RequestMethod.GET, produces = "application/json") + public String home() { + LOGGER.info("-----------------consumer调用开始-----------------"); + String param = "Nice to meet you."; + LOGGER.info("消费者传递参数:" + param); + String result = restTemplate.getForObject("http://ans-provider/echo/" + param, String.class); + LOGGER.info("收到提供者响应:" + result); + return param + "
" + result; + } +} diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/resources/application.properties new file mode 100644 index 000000000..c7bf8206f --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/resources/application.properties @@ -0,0 +1,4 @@ +server.port=18082 +# The following configuration can be omitted. +spring.cloud.ans.server.list=127.0.0.1 +spring.cloud.ans.server.port=8080 \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml new file mode 100644 index 000000000..d79e46f14 --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml @@ -0,0 +1,19 @@ + + + + spring-cloud-alibaba-examples + org.springframework.cloud + 0.2.0.BUILD-SNAPSHOT + + 4.0.0 + ans-provider-example + + + + org.springframework.cloud + spring-cloud-starter-alicloud-ans + + + \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/EchoController.java b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/EchoController.java new file mode 100644 index 000000000..a9c031ead --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/EchoController.java @@ -0,0 +1,42 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.cloud.examples; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +/** + * @author xiaolongzuo + */ +@RestController +public class EchoController { + + private static final Logger LOGGER = LoggerFactory.getLogger(EchoController.class); + + @RequestMapping(value = "/echo/{str}", method = RequestMethod.GET, produces = "application/json") + public String echo(@PathVariable String str) { + LOGGER.info("-----------收到消费者请求-----------"); + LOGGER.info("收到消费者传递的参数:" + str); + String result = "Nice to meet you, too."; + LOGGER.info("提供者返回结果:" + result); + return result; + } +} diff --git a/spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ProviderApplication.java b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ProviderApplication.java new file mode 100644 index 000000000..03436dcb4 --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/ProviderApplication.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.cloud.examples; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.client.discovery.EnableDiscoveryClient; + +/** + * @author xiaolongzuo + */ +@SpringBootApplication +@EnableDiscoveryClient +public class ProviderApplication { + + public static void main(String[] args) { + SpringApplication.run(ProviderApplication.class, args); + } + +} diff --git a/spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/resources/application.properties new file mode 100644 index 000000000..76862f6b7 --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/resources/application.properties @@ -0,0 +1,2 @@ +spring.application.name=ans-provider +server.port=18081 \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OSSApplication.java b/spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OSSApplication.java index 0e4546fac..f9265800d 100644 --- a/spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OSSApplication.java +++ b/spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OSSApplication.java @@ -1,7 +1,6 @@ package org.springframework.cloud.alibaba.cloud.examples; -import java.net.URISyntaxException; - +import com.aliyun.oss.OSS; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; @@ -9,7 +8,7 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; -import com.aliyun.oss.OSS; +import java.net.URISyntaxException; /** * OSS Application @@ -17,35 +16,34 @@ import com.aliyun.oss.OSS; * @author Jim */ @SpringBootApplication -public class OSSApplication { - - public static final String BUCKET_NAME = "spring-cloud-alibaba"; - - public static void main(String[] args) throws URISyntaxException { - SpringApplication.run(OSSApplication.class, args); - } - - @Bean - public AppRunner appRunner() { - return new AppRunner(); - } - - class AppRunner implements ApplicationRunner { - @Autowired - private OSS ossClient; - - @Override - public void run(ApplicationArguments args) throws Exception { - try { - if (!ossClient.doesBucketExist(BUCKET_NAME)) { - ossClient.createBucket(BUCKET_NAME); - } - } - catch (Exception e) { - System.err.println("oss handle bucket error: " + e.getMessage()); - System.exit(-1); - } - } - } +public class OssApplication { + + public static final String BUCKET_NAME = "spring-cloud-alibaba-test"; + + public static void main(String[] args) throws URISyntaxException { + SpringApplication.run(OssApplication.class, args); + } + + @Bean + public AppRunner appRunner() { + return new AppRunner(); + } + + class AppRunner implements ApplicationRunner { + @Autowired + private OSS ossClient; + + @Override + public void run(ApplicationArguments args) throws Exception { + try { + if (!ossClient.doesBucketExist(BUCKET_NAME)) { + ossClient.createBucket(BUCKET_NAME); + } + } catch (Exception e) { + System.err.println("oss handle bucket error: " + e.getMessage()); + System.exit(-1); + } + } + } } diff --git a/spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OSSController.java b/spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OSSController.java index 1f3aac896..7c6e427a9 100644 --- a/spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OSSController.java +++ b/spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OSSController.java @@ -1,7 +1,8 @@ package org.springframework.cloud.alibaba.cloud.examples; -import java.nio.charset.Charset; - +import com.aliyun.oss.OSS; +import com.aliyun.oss.common.utils.IOUtils; +import com.aliyun.oss.model.OSSObject; import org.apache.commons.codec.CharEncoding; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; @@ -10,9 +11,7 @@ import org.springframework.util.StreamUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; -import com.aliyun.oss.OSS; -import com.aliyun.oss.common.utils.IOUtils; -import com.aliyun.oss.model.OSSObject; +import java.nio.charset.Charset; /** * OSS Controller @@ -20,53 +19,47 @@ import com.aliyun.oss.model.OSSObject; * @author Jim */ @RestController -public class OSSController { - - @Autowired - private OSS ossClient; +public class OssController { - @Value("oss://" + OSSApplication.BUCKET_NAME + "/oss-test") - private Resource file; + @Autowired + private OSS ossClient; - private String dir = "custom-dir/"; + @Value("oss://" + OssApplication.BUCKET_NAME + "/oss-test.json") + private Resource file; - @GetMapping("/upload") - public String upload() { - try { - ossClient.putObject(OSSApplication.BUCKET_NAME, dir + "oss-test", this - .getClass().getClassLoader().getResourceAsStream("oss-test.json")); - } - catch (Exception e) { - e.printStackTrace(); - return "upload fail: " + e.getMessage(); - } - return "upload success"; - } + @GetMapping("/upload") + public String upload() { + try { + ossClient.putObject(OssApplication.BUCKET_NAME, "oss-test.json", this + .getClass().getClassLoader().getResourceAsStream("oss-test.json")); + } catch (Exception e) { + e.printStackTrace(); + return "upload fail: " + e.getMessage(); + } + return "upload success"; + } - @GetMapping("/file-resource") - public String fileResource() { - try { - return "get file resource success. content: " + StreamUtils.copyToString( - file.getInputStream(), Charset.forName(CharEncoding.UTF_8)); - } - catch (Exception e) { - e.printStackTrace(); - return "get resource fail: " + e.getMessage(); - } - } + @GetMapping("/file-resource") + public String fileResource() { + try { + return "get file resource success. content: " + StreamUtils.copyToString( + file.getInputStream(), Charset.forName(CharEncoding.UTF_8)); + } catch (Exception e) { + e.printStackTrace(); + return "get resource fail: " + e.getMessage(); + } + } - @GetMapping("/download") - public String download() { - try { - OSSObject ossObject = ossClient.getObject(OSSApplication.BUCKET_NAME, - dir + "oss-test"); - return "download success, content: " + IOUtils - .readStreamAsString(ossObject.getObjectContent(), CharEncoding.UTF_8); - } - catch (Exception e) { - e.printStackTrace(); - return "download fail: " + e.getMessage(); - } - } + @GetMapping("/download") + public String download() { + try { + OSSObject ossObject = ossClient.getObject(OssApplication.BUCKET_NAME, "oss-test.json"); + return "download success, content: " + IOUtils + .readStreamAsString(ossObject.getObjectContent(), CharEncoding.UTF_8); + } catch (Exception e) { + e.printStackTrace(); + return "download fail: " + e.getMessage(); + } + } } diff --git a/spring-cloud-alibaba-examples/oss-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/oss-example/src/main/resources/application.properties index 4b269483e..59c650d3d 100644 --- a/spring-cloud-alibaba-examples/oss-example/src/main/resources/application.properties +++ b/spring-cloud-alibaba-examples/oss-example/src/main/resources/application.properties @@ -1,8 +1,6 @@ spring.application.name=oss-example server.port=18084 - -spring.cloud.alibaba.oss.accessKeyId=[your-ak] -spring.cloud.alibaba.oss.secretAccessKey=[your-sk] -spring.cloud.alibaba.oss.region=[your-region] - -management.endpoints.web.exposure.include=* \ No newline at end of file +spring.cloud.alicloud.access-key=AK +spring.cloud.alicloud.secret-key=SK +spring.cloud.alicloud.oss.endpoint=***.aliyuncs.com +management.endpoints.web.exposure.include=* diff --git a/spring-cloud-alibaba-examples/oss-example/src/main/resources/oss-test.json b/spring-cloud-alibaba-examples/oss-example/src/main/resources/oss-test.json index eb0fe5f89..f1683e94f 100644 --- a/spring-cloud-alibaba-examples/oss-example/src/main/resources/oss-test.json +++ b/spring-cloud-alibaba-examples/oss-example/src/main/resources/oss-test.json @@ -1,6 +1,3 @@ { - "name": "spring-cloud-alibaba", - "github": "https://github.com/spring-cloud-incubator/spring-cloud-alibaba", - "authors": ["Jim", "flystar32"], - "emails": ["fangjian0423@gmail.com", "flystar32@163.com"] + "name": "chenzhu-test" } \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/pom.xml b/spring-cloud-alibaba-examples/pom.xml index ed4acb014..eef97298b 100644 --- a/spring-cloud-alibaba-examples/pom.xml +++ b/spring-cloud-alibaba-examples/pom.xml @@ -23,6 +23,10 @@ nacos-example/nacos-discovery-example nacos-example/nacos-config-example oss-example + ans-example/ans-consumer-feign-example + ans-example/ans-consumer-ribbon-example + ans-example/ans-provider-example + acm-example/acm-local-example diff --git a/spring-cloud-alicloud-acm/pom.xml b/spring-cloud-alicloud-acm/pom.xml new file mode 100644 index 000000000..971d33827 --- /dev/null +++ b/spring-cloud-alicloud-acm/pom.xml @@ -0,0 +1,71 @@ + + + 4.0.0 + + + + org.springframework.cloud + spring-cloud-alibaba + 0.2.0.BUILD-SNAPSHOT + + + spring-cloud-alicloud-acm + + + + + org.springframework.cloud + spring-cloud-alicloud-context + + + + com.aliyun + aliyun-java-sdk-core + + + + com.aliyun + aliyun-java-sdk-edas + + + + com.alibaba.edas.acm + acm-sdk + + + + org.springframework.boot + spring-boot-autoconfigure + provided + true + + + org.springframework.boot + spring-boot-starter-actuator + true + + + + + org.springframework.cloud + spring-cloud-context + + + org.springframework.cloud + spring-cloud-commons + + + org.springframework.boot + spring-boot-configuration-processor + true + + + org.springframework.boot + spring-boot-starter-test + test + + + + + diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/AcmAutoConfiguration.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/AcmAutoConfiguration.java new file mode 100644 index 000000000..5930568a9 --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/AcmAutoConfiguration.java @@ -0,0 +1,73 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.acm; + +import com.taobao.diamond.client.Diamond; +import org.springframework.beans.BeansException; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.alicloud.acm.endpoint.AcmHealthIndicator; +import org.springframework.cloud.alicloud.acm.refresh.AcmContextRefresher; +import org.springframework.cloud.alicloud.acm.refresh.AcmRefreshHistory; +import org.springframework.cloud.alicloud.context.acm.AcmProperties; +import org.springframework.cloud.context.refresh.ContextRefresher; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Created on 01/10/2017. + * + * @author juven.xuxb + */ +@Configuration +@ConditionalOnClass({Diamond.class}) +@EnableConfigurationProperties(AcmProperties.class) +public class AcmAutoConfiguration implements ApplicationContextAware { + + private ApplicationContext applicationContext; + + @Bean + public AcmPropertySourceRepository acmPropertySourceRepository() { + return new AcmPropertySourceRepository(applicationContext); + } + + @Bean + public AcmHealthIndicator acmHealthIndicator(AcmProperties acmProperties, + AcmPropertySourceRepository acmPropertySourceRepository) { + return new AcmHealthIndicator(acmProperties, acmPropertySourceRepository); + } + + @Bean + public AcmRefreshHistory acmRefreshHistory() { + return new AcmRefreshHistory(); + } + + @Bean + public AcmContextRefresher acmContextRefresher(AcmProperties acmProperties, ContextRefresher contextRefresher, + AcmRefreshHistory refreshHistory, + AcmPropertySourceRepository propertySourceRepository) { + return new AcmContextRefresher(contextRefresher, acmProperties, refreshHistory, propertySourceRepository); + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) + throws BeansException { + this.applicationContext = applicationContext; + } +} diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/AcmPropertySourceRepository.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/AcmPropertySourceRepository.java new file mode 100644 index 000000000..42b84c89f --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/AcmPropertySourceRepository.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.acm; + +import org.springframework.cloud.alicloud.acm.bootstrap.AcmPropertySource; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.core.env.CompositePropertySource; +import org.springframework.core.env.PropertySource; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author juven.xuxb, 5/17/16. + */ +public class AcmPropertySourceRepository { + + private final ApplicationContext applicationContext; + + public AcmPropertySourceRepository(ApplicationContext applicationContext) { + this.applicationContext = applicationContext; + } + + /** + * get all acm properties from application context + * @return + */ + public List getAll() { + List result = new ArrayList<>(); + ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) applicationContext; + for (PropertySource p : ctx.getEnvironment().getPropertySources()) { + if (p instanceof AcmPropertySource) { + result.add((AcmPropertySource) p); + } + else if (p instanceof CompositePropertySource) { + collectAcmPropertySources((CompositePropertySource) p, result); + } + } + return result; + } + + private void collectAcmPropertySources(CompositePropertySource composite, + List result) { + for (PropertySource p : composite.getPropertySources()) { + if (p instanceof AcmPropertySource) { + result.add((AcmPropertySource) p); + } + else if (p instanceof CompositePropertySource) { + collectAcmPropertySources((CompositePropertySource) p, result); + } + } + } +} diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySource.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySource.java new file mode 100644 index 000000000..3ebae2773 --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySource.java @@ -0,0 +1,55 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.acm.bootstrap; + +import org.springframework.core.env.MapPropertySource; + +import java.util.Date; +import java.util.Map; + +/** + * @author juven.xuxb + * @author xiaolongzuo + */ +public class AcmPropertySource extends MapPropertySource { + + private final String dataId; + + private final Date timestamp; + + private final boolean groupLevel; + + AcmPropertySource(String dataId, Map source, Date timestamp, + boolean groupLevel) { + super(dataId, source); + this.dataId = dataId; + this.timestamp = timestamp; + this.groupLevel = groupLevel; + } + + public String getDataId() { + return dataId; + } + + public Date getTimestamp() { + return timestamp; + } + + public boolean isGroupLevel() { + return groupLevel; + } +} diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceBuilder.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceBuilder.java new file mode 100644 index 000000000..2bf9cbba3 --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceBuilder.java @@ -0,0 +1,96 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.acm.bootstrap; + +import com.alibaba.edas.acm.ConfigService; +import com.alibaba.edas.acm.exception.ConfigException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; +import org.springframework.core.io.ByteArrayResource; +import org.springframework.util.StringUtils; + +import java.io.StringReader; +import java.util.*; + +/** + * @author juven.xuxb + * @author xiaolongzuo + */ +class AcmPropertySourceBuilder { + + private Logger logger = LoggerFactory.getLogger(AcmPropertySourceBuilder.class); + + /** + * 传入 ACM 的 DataId 和 groupID,获取到解析后的 AcmProperty 对象 + * + * @param dataId + * @param diamondGroup + * @param groupLevel + * @return + */ + AcmPropertySource build(String dataId, String diamondGroup, boolean groupLevel) { + Properties properties = loadDiamondData(dataId, diamondGroup); + if (properties == null) { + return null; + } + return new AcmPropertySource(dataId, toMap(properties), new Date(), groupLevel); + } + + private Properties loadDiamondData(String dataId, String diamondGroup) { + try { + String data = ConfigService.getConfig(dataId, diamondGroup, 3000L); + if (StringUtils.isEmpty(data)) { + return null; + } + if (dataId.endsWith(".properties")) { + Properties properties = new Properties(); + logger.info(String.format("Loading acm data, dataId: '%s', group: '%s'", + dataId, diamondGroup)); + properties.load(new StringReader(data)); + return properties; + } else if (dataId.endsWith(".yaml") || dataId.endsWith(".yml")) { + YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean(); + yamlFactory.setResources(new ByteArrayResource(data.getBytes())); + return yamlFactory.getObject(); + } + } catch (Exception e) { + if (e instanceof ConfigException) { + logger.error("DIAMOND-100500:" + dataId + ", " + e.toString(), e); + } else { + logger.error("DIAMOND-100500:" + dataId, e); + } + } + return null; + } + + @SuppressWarnings("unchecked") + private Map toMap(Properties properties) { + Map result = new HashMap<>(); + Enumeration keys = (Enumeration)properties.propertyNames(); + while (keys.hasMoreElements()) { + String key = keys.nextElement(); + Object value = properties.getProperty(key); + if (value != null) { + result.put(key, ((String)value).trim()); + } else { + result.put(key, null); + } + } + return result; + } +} diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceLocator.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceLocator.java new file mode 100644 index 000000000..8c08e1baf --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceLocator.java @@ -0,0 +1,142 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.acm.bootstrap; + +import com.taobao.diamond.maintenance.DiamondHealth; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.alicloud.acm.diagnostics.analyzer.DiamondConnectionFailureException; +import org.springframework.cloud.alicloud.context.acm.AcmProperties; +import org.springframework.cloud.bootstrap.config.PropertySourceLocator; +import org.springframework.core.env.CompositePropertySource; +import org.springframework.core.env.Environment; +import org.springframework.core.env.PropertySource; +import org.springframework.util.StringUtils; + +import static com.taobao.diamond.client.impl.ServerHttpAgent.addressPort; +import static com.taobao.diamond.client.impl.ServerHttpAgent.domainName; + +/** + * @author juven.xuxb + * @author xiaolongzuo + */ +public class AcmPropertySourceLocator implements PropertySourceLocator { + + private final Logger logger = LoggerFactory.getLogger(AcmPropertySourceLocator.class); + + private static final String DIAMOND_PROPERTY_SOURCE_NAME = "diamond"; + + private static String defaultDiamondGroup = "DEFAULT_GROUP"; + + private AcmPropertySourceBuilder acmPropertySourceBuilder = new AcmPropertySourceBuilder(); + + @Autowired + private AcmProperties acmProperties; + + @Override + public PropertySource locate(Environment environment) { + checkDiamondHealth(); + + String applicationName = environment.getProperty("spring.application.name"); + logger.info("Initialize spring.application.name '" + applicationName + "'."); + String applicationGroup = environment.getProperty("spring.application.group"); + + if (StringUtils.isEmpty(applicationName)) { + throw new IllegalStateException( + "'spring.application.name' must be configured."); + } + + CompositePropertySource compositePropertySource = new CompositePropertySource( + DIAMOND_PROPERTY_SOURCE_NAME); + + loadGroupConfigurationRecursively(compositePropertySource, applicationGroup); + + loadApplicationConfiguration(compositePropertySource, environment, + applicationGroup, applicationName); + + return compositePropertySource; + } + + private void checkDiamondHealth() { + logger.info("Checking ACM health"); + try { + if (!"UP".equals(DiamondHealth.getHealth())) { + throw new DiamondConnectionFailureException(domainName, addressPort, + DiamondHealth.getHealth()); + } + } + catch (Throwable t) { + throw new DiamondConnectionFailureException(domainName, addressPort, + "ACM Health error", t); + } + } + + private void loadGroupConfigurationRecursively( + CompositePropertySource compositePropertySource, String applicationGroup) { + if (StringUtils.isEmpty(applicationGroup)) { + return; + } + String[] parts = applicationGroup.split("\\."); + for (int i = 1; i < parts.length; i++) { + String subGroup = parts[0]; + for (int j = 1; j <= i; j++) { + subGroup = subGroup + "." + parts[j]; + } + String dataId = subGroup + ":application." + acmProperties.getFileExtension(); + loadDiamondDataIfPresent(compositePropertySource, dataId, defaultDiamondGroup, + true); + } + } + + private void loadApplicationConfiguration( + CompositePropertySource compositePropertySource, Environment environment, + String applicationGroup, String applicationName) { + + if (!StringUtils.isEmpty(applicationGroup)) { + String dataId = applicationGroup + ":" + applicationName + "." + + acmProperties.getFileExtension(); + loadDiamondDataIfPresent(compositePropertySource, dataId, defaultDiamondGroup, + false); + for (String profile : environment.getActiveProfiles()) { + dataId = applicationGroup + ":" + applicationName + "-" + profile + "." + + acmProperties.getFileExtension(); + loadDiamondDataIfPresent(compositePropertySource, dataId, + defaultDiamondGroup, false); + } + + } + String dataId = applicationName + "." + acmProperties.getFileExtension(); + loadDiamondDataIfPresent(compositePropertySource, dataId, defaultDiamondGroup, + false); + for (String profile : environment.getActiveProfiles()) { + dataId = applicationName + "-" + profile + "." + + acmProperties.getFileExtension(); + loadDiamondDataIfPresent(compositePropertySource, dataId, defaultDiamondGroup, + false); + } + } + + private void loadDiamondDataIfPresent(final CompositePropertySource composite, + final String dataId, final String diamondGroup, final boolean groupLevel) { + AcmPropertySource ps = acmPropertySourceBuilder.build(dataId, diamondGroup, + groupLevel); + if (ps != null) { + composite.addFirstPropertySource(ps); + } + } +} diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/diagnostics/analyzer/DiamondConnectionFailureAnalyzer.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/diagnostics/analyzer/DiamondConnectionFailureAnalyzer.java new file mode 100644 index 000000000..64d56be39 --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/diagnostics/analyzer/DiamondConnectionFailureAnalyzer.java @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.acm.diagnostics.analyzer; + +import org.springframework.boot.diagnostics.AbstractFailureAnalyzer; +import org.springframework.boot.diagnostics.FailureAnalysis; + +/** + * A {@code FailureAnalyzer} that performs analysis of failures caused by a + * {@code DiamondConnectionFailureException}. + * + * @author juven.xuxb, 07/11/2016. + */ +public class DiamondConnectionFailureAnalyzer + extends AbstractFailureAnalyzer { + + @Override + protected FailureAnalysis analyze(Throwable rootFailure, + DiamondConnectionFailureException cause) { + return new FailureAnalysis( + "Application failed to connect to Diamond, unable to access http://" + + cause.getDomain() + ":" + cause.getPort() + + "/diamond-server/diamond", + "config the right endpoint", cause); + } +} diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/diagnostics/analyzer/DiamondConnectionFailureException.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/diagnostics/analyzer/DiamondConnectionFailureException.java new file mode 100644 index 000000000..a1b10abf6 --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/diagnostics/analyzer/DiamondConnectionFailureException.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.acm.diagnostics.analyzer; + +/** + * A {@code DiamondConnectionFailureException} is thrown when the application fails to + * connect to Diamond Server. + * + * @author juven.xuxb, 07/11/2016. + */ +public class DiamondConnectionFailureException extends RuntimeException { + + private final String domain; + + private final String port; + + public DiamondConnectionFailureException(String domain, String port, String message) { + super(message); + this.domain = domain; + this.port = port; + } + + public DiamondConnectionFailureException(String domain, String port, String message, + Throwable cause) { + super(message, cause); + this.domain = domain; + this.port = port; + } + + String getDomain() { + return domain; + } + + String getPort() { + return port; + } + +} diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmEndpoint.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmEndpoint.java new file mode 100644 index 000000000..6c5518d76 --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmEndpoint.java @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.acm.endpoint; + +import org.springframework.boot.actuate.endpoint.annotation.Endpoint; +import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; +import org.springframework.cloud.alicloud.acm.AcmPropertySourceRepository; +import org.springframework.cloud.alicloud.acm.bootstrap.AcmPropertySource; +import org.springframework.cloud.alicloud.acm.refresh.AcmRefreshHistory; +import org.springframework.cloud.alicloud.context.acm.AcmProperties; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created on 01/10/2017. + * + * @author juven.xuxb + */ +@Endpoint(id = "acm") +public class AcmEndpoint { + + private final AcmProperties properties; + + private final AcmRefreshHistory refreshHistory; + + private final AcmPropertySourceRepository propertySourceRepository; + + private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + public AcmEndpoint(AcmProperties properties, AcmRefreshHistory refreshHistory, + AcmPropertySourceRepository propertySourceRepository) { + this.properties = properties; + this.refreshHistory = refreshHistory; + this.propertySourceRepository = propertySourceRepository; + } + + @ReadOperation + public Map invoke() { + Map result = new HashMap<>(); + result.put("config", properties); + + Map runtime = new HashMap<>(); + List all = propertySourceRepository.getAll(); + + List> sources = new ArrayList<>(); + for (AcmPropertySource ps : all) { + Map source = new HashMap<>(); + source.put("dataId", ps.getDataId()); + source.put("lastSynced", dateFormat.format(ps.getTimestamp())); + sources.add(source); + } + runtime.put("sources", sources); + runtime.put("refreshHistory", refreshHistory.getRecords()); + + result.put("runtime", runtime); + return result; + } +} diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmEndpointAutoConfiguration.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmEndpointAutoConfiguration.java new file mode 100644 index 000000000..2154c7701 --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmEndpointAutoConfiguration.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.acm.endpoint; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.cloud.alicloud.acm.AcmPropertySourceRepository; +import org.springframework.cloud.alicloud.acm.refresh.AcmRefreshHistory; +import org.springframework.cloud.alicloud.context.acm.AcmProperties; +import org.springframework.context.annotation.Bean; + +/** + * @author xiaojing + */ +@ConditionalOnWebApplication +@ConditionalOnClass(name = "org.springframework.boot.actuate.autoconfigure.web.ManagementContextConfiguration") +public class AcmEndpointAutoConfiguration { + + @Autowired + private AcmProperties acmProperties; + + @Autowired + private AcmRefreshHistory acmRefreshHistory; + + @Autowired + private AcmPropertySourceRepository acmPropertySourceRepository; + + @ConditionalOnMissingBean + @ConditionalOnEnabledEndpoint + @Bean + public AcmEndpoint acmEndpoint() { + return new AcmEndpoint(acmProperties, acmRefreshHistory, + acmPropertySourceRepository); + } +} diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmHealthIndicator.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmHealthIndicator.java new file mode 100644 index 000000000..27569ec1a --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/endpoint/AcmHealthIndicator.java @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.acm.endpoint; + +import com.alibaba.edas.acm.ConfigService; +import org.springframework.boot.actuate.health.AbstractHealthIndicator; +import org.springframework.boot.actuate.health.Health; +import org.springframework.cloud.alicloud.acm.AcmPropertySourceRepository; +import org.springframework.cloud.alicloud.acm.bootstrap.AcmPropertySource; +import org.springframework.cloud.alicloud.context.acm.AcmProperties; +import org.springframework.util.StringUtils; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author leijuan + * @author juven + */ +public class AcmHealthIndicator extends AbstractHealthIndicator { + + private final AcmProperties acmProperties; + + private final AcmPropertySourceRepository acmPropertySourceRepository; + + private final List dataIds; + + public AcmHealthIndicator(AcmProperties acmProperties, + AcmPropertySourceRepository acmPropertySourceRepository) { + this.acmProperties = acmProperties; + this.acmPropertySourceRepository = acmPropertySourceRepository; + + this.dataIds = new ArrayList<>(); + for (AcmPropertySource acmPropertySource : this.acmPropertySourceRepository + .getAll()) { + this.dataIds.add(acmPropertySource.getDataId()); + } + } + + @Override + protected void doHealthCheck(Health.Builder builder) throws Exception { + for (String dataId : dataIds) { + try { + String config = ConfigService.getConfig(dataId, acmProperties.getGroup(), + acmProperties.getTimeout()); + if (StringUtils.isEmpty(config)) { + builder.down().withDetail(String.format("dataId: '%s', group: '%s'", + dataId, acmProperties.getGroup()), "config is empty"); + } + } catch (Exception e) { + builder.down().withDetail(String.format("dataId: '%s', group: '%s'", + dataId, acmProperties.getGroup()), e.getMessage()); + } + } + builder.up().withDetail("dataIds", dataIds); + } +} diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/refresh/AcmContextRefresher.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/refresh/AcmContextRefresher.java new file mode 100644 index 000000000..5ed57a709 --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/refresh/AcmContextRefresher.java @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.acm.refresh; + +import com.alibaba.edas.acm.ConfigService; +import com.alibaba.edas.acm.listener.ConfigChangeListener; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.event.ApplicationReadyEvent; +import org.springframework.cloud.alicloud.acm.AcmPropertySourceRepository; +import org.springframework.cloud.alicloud.acm.bootstrap.AcmPropertySource; +import org.springframework.cloud.alicloud.context.acm.AcmProperties; +import org.springframework.cloud.context.refresh.ContextRefresher; +import org.springframework.context.ApplicationListener; +import org.springframework.core.env.Environment; +import org.springframework.util.StringUtils; + +import java.io.UnsupportedEncodingException; +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * On application start up, AcmContextRefresher add diamond listeners to all application + * level dataIds, when there is a change in the data, listeners will refresh + * configurations. + * + * @author juven.xuxb, 5/13/16. + */ +public class AcmContextRefresher implements ApplicationListener { + + private Logger logger = LoggerFactory.getLogger(AcmContextRefresher.class); + + private final ContextRefresher contextRefresher; + + private final AcmProperties properties; + + private final AcmRefreshHistory refreshHistory; + + private final AcmPropertySourceRepository acmPropertySourceRepository; + + private Map listenerMap = new ConcurrentHashMap<>(16); + + @Autowired + private Environment environment; + + public AcmContextRefresher(ContextRefresher contextRefresher, + AcmProperties properties, AcmRefreshHistory refreshHistory, + AcmPropertySourceRepository acmPropertySourceRepository) { + this.contextRefresher = contextRefresher; + this.properties = properties; + this.refreshHistory = refreshHistory; + this.acmPropertySourceRepository = acmPropertySourceRepository; + } + + @Override + public void onApplicationEvent(ApplicationReadyEvent event) { + this.registerDiamondListenersForApplications(); + } + + private void registerDiamondListenersForApplications() { + if (properties.isRefreshEnabled()) { + for (AcmPropertySource acmPropertySource : acmPropertySourceRepository + .getAll()) { + if (acmPropertySource.isGroupLevel()) { + continue; + } + String dataId = acmPropertySource.getDataId(); + registerDiamondListener(dataId); + } + if (acmPropertySourceRepository.getAll().isEmpty()) { + + String applicationName = environment + .getProperty("spring.application.name"); + String dataId = applicationName + "." + properties.getFileExtension(); + + registerDiamondListener(dataId); + } + } + } + + private void registerDiamondListener(final String dataId) { + + ConfigChangeListener listener = listenerMap.computeIfAbsent(dataId, + i -> new ConfigChangeListener() { + @Override + public void receiveConfigInfo(String configInfo) { + String md5 = ""; + if (!StringUtils.isEmpty(configInfo)) { + try { + MessageDigest md = MessageDigest.getInstance("MD5"); + md5 = new BigInteger(1, + md.digest(configInfo.getBytes("UTF-8"))) + .toString(16); + } + catch (NoSuchAlgorithmException + | UnsupportedEncodingException e) { + logger.warn("unable to get md5 for dataId: " + dataId, e); + } + } + refreshHistory.add(dataId, md5); + contextRefresher.refresh(); + } + }); + ConfigService.addListener(dataId, properties.getGroup(), listener); + } + +} diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/refresh/AcmRefreshHistory.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/refresh/AcmRefreshHistory.java new file mode 100644 index 000000000..439e63604 --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/refresh/AcmRefreshHistory.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.acm.refresh; + +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.LinkedList; + +/** + * @author juven.xuxb, 5/16/16. + */ +public class AcmRefreshHistory { + + private static final int MAX_SIZE = 20; + + private LinkedList records = new LinkedList<>(); + + private DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); + + public void add(String dataId, String md5) { + records.addFirst(new Record(dateFormat.format(new Date()), dataId, md5)); + if (records.size() > MAX_SIZE) { + records.removeLast(); + } + } + + public LinkedList getRecords() { + return records; + } +} + +class Record { + + private final String timestamp; + + private final String dataId; + + private final String md5; + + public Record(String timestamp, String dataId, String md5) { + this.timestamp = timestamp; + this.dataId = dataId; + this.md5 = md5; + } + + public String getTimestamp() { + return timestamp; + } + + public String getDataId() { + return dataId; + } + + public String getMd5() { + return md5; + } +} diff --git a/spring-cloud-alicloud-acm/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-alicloud-acm/src/main/resources/META-INF/additional-spring-configuration-metadata.json new file mode 100644 index 000000000..29f53c76e --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -0,0 +1,9 @@ +{ + "properties": [ + { + "name": "spring.application.group", + "type": "java.lang.String", + "description": "spring application group." + } + ] +} \ No newline at end of file diff --git a/spring-cloud-alicloud-acm/src/main/resources/META-INF/spring.factories b/spring-cloud-alicloud-acm/src/main/resources/META-INF/spring.factories new file mode 100644 index 000000000..4f39095fe --- /dev/null +++ b/spring-cloud-alicloud-acm/src/main/resources/META-INF/spring.factories @@ -0,0 +1,9 @@ +org.springframework.cloud.bootstrap.BootstrapConfiguration=\ +org.springframework.cloud.alicloud.acm.bootstrap.AcmPropertySourceLocator + +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +org.springframework.cloud.alicloud.acm.AcmAutoConfiguration,\ +org.springframework.cloud.alicloud.acm.endpoint.AcmEndpointAutoConfiguration + +org.springframework.boot.diagnostics.FailureAnalyzer=\ +org.springframework.cloud.alicloud.acm.diagnostics.analyzer.DiamondConnectionFailureAnalyzer \ No newline at end of file diff --git a/spring-cloud-alicloud-ans/pom.xml b/spring-cloud-alicloud-ans/pom.xml new file mode 100644 index 000000000..6c33b9ee9 --- /dev/null +++ b/spring-cloud-alicloud-ans/pom.xml @@ -0,0 +1,96 @@ + + + + spring-cloud-alibaba + org.springframework.cloud + 0.2.0.BUILD-SNAPSHOT + + 4.0.0 + + org.springframework.cloud + spring-cloud-alicloud-ans + Spring Cloud Alibaba Cloud ANS + + + + + com.alibaba.ans + ans-sdk + + + + com.aliyun + aliyun-java-sdk-core + + + + com.aliyun + aliyun-java-sdk-edas + + + + org.springframework.cloud + spring-cloud-alicloud-context + + + + org.springframework + spring-context + + + org.springframework.cloud + spring-cloud-commons + + + + org.springframework.cloud + spring-cloud-starter-netflix-ribbon + + + + org.springframework.boot + spring-boot-actuator + true + + + org.springframework.boot + spring-boot-actuator-autoconfigure + provided + true + + + org.springframework.boot + spring-boot-configuration-processor + provided + true + + + org.springframework.boot + spring-boot-autoconfigure + provided + true + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-actuator + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.cloud + spring-cloud-test-support + test + + + + + \ No newline at end of file diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsAutoConfiguration.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsAutoConfiguration.java new file mode 100644 index 000000000..a14c6e12a --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsAutoConfiguration.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.ans; + +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.alicloud.ans.registry.AnsAutoServiceRegistration; +import org.springframework.cloud.alicloud.ans.registry.AnsRegistration; +import org.springframework.cloud.alicloud.ans.registry.AnsServiceRegistry; +import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationAutoConfiguration; +import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author xiaolongzuo + */ +@Configuration +@EnableConfigurationProperties +@ConditionalOnClass(name = "org.springframework.boot.web.context.WebServerInitializedEvent") +@ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true) +@ConditionalOnAnsEnabled +@AutoConfigureBefore({ AutoServiceRegistrationAutoConfiguration.class, + AnsDiscoveryClientAutoConfiguration.class }) +public class AnsAutoConfiguration { + + @Bean + public AnsServiceRegistry ansServiceRegistry() { + return new AnsServiceRegistry(); + } + + @Bean + @ConditionalOnBean(AutoServiceRegistrationProperties.class) + @ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true) + public AnsRegistration ansRegistration() { + return new AnsRegistration(); + } + + @Bean + @ConditionalOnBean(AutoServiceRegistrationProperties.class) + @ConditionalOnProperty(value = "spring.cloud.service-registry.auto-registration.enabled", matchIfMissing = true) + public AnsAutoServiceRegistration ansAutoServiceRegistration( + AnsServiceRegistry registry, + AutoServiceRegistrationProperties autoServiceRegistrationProperties, + AnsRegistration registration) { + return new AnsAutoServiceRegistration(registry, autoServiceRegistrationProperties, + registration); + } +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsDiscoveryClient.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsDiscoveryClient.java new file mode 100644 index 000000000..c95757b22 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsDiscoveryClient.java @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.ans; + +import java.util.*; + +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.DiscoveryClient; + +import com.alibaba.ans.core.NamingService; +import com.alibaba.ans.shaded.com.taobao.vipserver.client.core.Host; + +/** + * @author xiaolongzuo + */ +public class AnsDiscoveryClient implements DiscoveryClient { + + public static final String DESCRIPTION = "Spring Cloud ANS Discovery Client"; + + @Override + public String description() { + return DESCRIPTION; + } + + @Override + public List getInstances(String serviceId) { + try { + List hosts = NamingService.getHosts(serviceId); + return hostToServiceInstanceList(hosts, serviceId); + } + catch (Exception e) { + throw new RuntimeException( + "Can not get hosts from ans server. serviceId: " + serviceId, e); + } + } + + private static ServiceInstance hostToServiceInstance(Host host, String serviceId) { + AnsServiceInstance ansServiceInstance = new AnsServiceInstance(); + ansServiceInstance.setHost(host.getIp()); + ansServiceInstance.setPort(host.getPort()); + ansServiceInstance.setServiceId(serviceId); + Map metadata = new HashMap(5); + metadata.put("appUseType", host.getAppUseType()); + metadata.put("site", host.getSite()); + metadata.put("unit", host.getUnit()); + metadata.put("doubleWeight", "" + host.getDoubleWeight()); + metadata.put("weight", "" + host.getWeight()); + ansServiceInstance.setMetadata(metadata); + + return ansServiceInstance; + } + + private static List hostToServiceInstanceList(List hosts, + String serviceId) { + List result = new ArrayList(hosts.size()); + for (Host host : hosts) { + result.add(hostToServiceInstance(host, serviceId)); + } + return result; + } + + @Override + public List getServices() { + + Set doms = NamingService.getDomsSubscribed(); + List result = new LinkedList<>(); + for (String service : doms) { + result.add(service); + } + return result; + } + +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsDiscoveryClientAutoConfiguration.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsDiscoveryClientAutoConfiguration.java new file mode 100644 index 000000000..7d2869763 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsDiscoveryClientAutoConfiguration.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.ans; + +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.client.discovery.DiscoveryClient; +import org.springframework.cloud.client.discovery.simple.SimpleDiscoveryClientAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author xiaolongzuo + */ +@Configuration +@ConditionalOnMissingBean(DiscoveryClient.class) +@EnableConfigurationProperties +@AutoConfigureBefore(SimpleDiscoveryClientAutoConfiguration.class) +public class AnsDiscoveryClientAutoConfiguration { + + @Bean + public DiscoveryClient ansDiscoveryClient() { + return new AnsDiscoveryClient(); + } + +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsServiceInstance.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsServiceInstance.java new file mode 100644 index 000000000..52f7858c0 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/AnsServiceInstance.java @@ -0,0 +1,90 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.ans; + +import java.net.URI; +import java.util.Map; + +import org.springframework.cloud.client.DefaultServiceInstance; +import org.springframework.cloud.client.ServiceInstance; + +/** + * @author xiaolongzuo + */ +public class AnsServiceInstance implements ServiceInstance { + + private String serviceId; + + private String host; + + private int port; + + private boolean secure; + + private Map metadata; + + @Override + public String getServiceId() { + return serviceId; + } + + @Override + public String getHost() { + return host; + } + + @Override + public int getPort() { + return port; + } + + @Override + public boolean isSecure() { + return secure; + } + + @Override + public URI getUri() { + return DefaultServiceInstance.getUri(this); + } + + @Override + public Map getMetadata() { + return metadata; + } + + public void setServiceId(String serviceId) { + this.serviceId = serviceId; + } + + public void setHost(String host) { + this.host = host; + } + + public void setPort(int port) { + this.port = port; + } + + public void setSecure(boolean secure) { + this.secure = secure; + } + + public void setMetadata(Map metadata) { + this.metadata = metadata; + } + +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ConditionalOnAnsEnabled.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ConditionalOnAnsEnabled.java new file mode 100644 index 000000000..dc12044e6 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ConditionalOnAnsEnabled.java @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.ans; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; + +/** + * @author xiaolongzuo + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE, ElementType.METHOD }) +@ConditionalOnProperty(value = "spring.cloud.ans.enabled", matchIfMissing = true) +public @interface ConditionalOnAnsEnabled { +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/endpoint/AnsEndpoint.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/endpoint/AnsEndpoint.java new file mode 100644 index 000000000..a8bc64c7c --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/endpoint/AnsEndpoint.java @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.ans.endpoint; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.actuate.endpoint.annotation.Endpoint; +import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; +import org.springframework.cloud.alicloud.context.ans.AnsProperties; + +import com.alibaba.ans.core.NamingService; +import com.alibaba.ans.shaded.com.taobao.vipserver.client.core.Host; + +/** + * @author xiaolongzuo + */ +@Endpoint(id = "ans") +public class AnsEndpoint { + + private static final Logger LOGGER = LoggerFactory.getLogger(AnsEndpoint.class); + + private AnsProperties ansProperties; + + public AnsEndpoint(AnsProperties ansProperties) { + this.ansProperties = ansProperties; + } + + /** + * @return ans endpoint + */ + @ReadOperation + public Map invoke() { + Map ansEndpoint = new HashMap<>(); + LOGGER.info("ANS endpoint invoke, ansProperties is {}", ansProperties); + ansEndpoint.put("ansProperties", ansProperties); + + Map subscribes = new HashMap<>(); + Set subscribeServices = NamingService.getDomsSubscribed(); + for (String service : subscribeServices) { + try { + List hosts = NamingService.getHosts(service); + subscribes.put(service, hosts); + } + catch (Exception ignoreException) { + + } + } + ansEndpoint.put("subscribes", subscribes); + LOGGER.info("ANS endpoint invoke, subscribes is {}", subscribes); + return ansEndpoint; + } + +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/endpoint/AnsEndpointAutoConfiguration.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/endpoint/AnsEndpointAutoConfiguration.java new file mode 100644 index 000000000..65f5803c7 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/endpoint/AnsEndpointAutoConfiguration.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.ans.endpoint; + +import org.springframework.boot.actuate.endpoint.annotation.Endpoint; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.cloud.alicloud.context.ans.AnsProperties; +import org.springframework.context.annotation.Bean; + +/** + * @author xiaolongzuo + */ +@ConditionalOnWebApplication +@ConditionalOnClass(Endpoint.class) +public class AnsEndpointAutoConfiguration { + + @Bean + public AnsEndpoint ansEndpoint(AnsProperties ansProperties) { + return new AnsEndpoint(ansProperties); + } +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/registry/AnsAutoServiceRegistration.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/registry/AnsAutoServiceRegistration.java new file mode 100644 index 000000000..0774f1856 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/registry/AnsAutoServiceRegistration.java @@ -0,0 +1,103 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.ans.registry; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration; +import org.springframework.cloud.client.serviceregistry.AutoServiceRegistrationProperties; +import org.springframework.cloud.client.serviceregistry.ServiceRegistry; +import org.springframework.util.Assert; +import org.springframework.util.StringUtils; + +/** + * @author xiaolongzuo + */ +public class AnsAutoServiceRegistration + extends AbstractAutoServiceRegistration { + private static final Logger LOGGER = LoggerFactory + .getLogger(AnsAutoServiceRegistration.class); + + @Autowired + private AnsRegistration registration; + + public AnsAutoServiceRegistration(ServiceRegistry serviceRegistry, + AutoServiceRegistrationProperties autoServiceRegistrationProperties, + AnsRegistration registration) { + super(serviceRegistry, autoServiceRegistrationProperties); + this.registration = registration; + } + + @Deprecated + public void setPort(int port) { + getPort().set(port); + } + + @Override + protected AnsRegistration getRegistration() { + if (this.registration.getPort() < 0 && this.getPort().get() > 0) { + this.registration.setPort(this.getPort().get()); + } + Assert.isTrue(this.registration.getPort() > 0, "service.port has not been set"); + return this.registration; + } + + @Override + protected AnsRegistration getManagementRegistration() { + return null; + } + + @Override + protected void register() { + if (!this.registration.getAnsProperties().isRegisterEnabled()) { + LOGGER.debug("Registration disabled."); + return; + } + if (this.registration.getPort() < 0) { + this.registration.setPort(getPort().get()); + } + super.register(); + } + + @Override + protected void registerManagement() { + if (!this.registration.getAnsProperties().isRegisterEnabled()) { + return; + } + super.registerManagement(); + + } + + @Override + protected Object getConfiguration() { + return this.registration.getAnsProperties(); + } + + @Override + protected boolean isEnabled() { + return this.registration.getAnsProperties().isRegisterEnabled(); + } + + @Override + @SuppressWarnings("deprecation") + protected String getAppName() { + String appName = registration.getServiceId(); + return StringUtils.isEmpty(appName) ? super.getAppName() : appName; + } + +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/registry/AnsRegistration.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/registry/AnsRegistration.java new file mode 100644 index 000000000..1fcb292f3 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/registry/AnsRegistration.java @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.ans.registry; + +import java.net.URI; +import java.util.Map; + +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.alicloud.context.ans.AnsProperties; +import org.springframework.cloud.client.DefaultServiceInstance; +import org.springframework.cloud.client.ServiceInstance; +import org.springframework.cloud.client.discovery.ManagementServerPortUtils; +import org.springframework.cloud.client.serviceregistry.Registration; +import org.springframework.context.ApplicationContext; +import org.springframework.core.env.Environment; +import org.springframework.util.StringUtils; + +/** + * @author xiaolongzuo + */ +public class AnsRegistration implements Registration, ServiceInstance { + + private static final String MANAGEMENT_PORT = "management.port"; + private static final String MANAGEMENT_CONTEXT_PATH = "management.context-path"; + private static final String MANAGEMENT_ADDRESS = "management.address"; + + @Autowired + private AnsProperties ansProperties; + + @Autowired + private ApplicationContext context; + + @PostConstruct + public void init() { + + Environment env = context.getEnvironment(); + Integer managementPort = ManagementServerPortUtils.getPort(context); + if (null != managementPort) { + Map metadata = ansProperties.getClientMetadata(); + metadata.put(MANAGEMENT_PORT, managementPort.toString()); + String contextPath = env + .getProperty("management.server.servlet.context-path"); + String address = env.getProperty("management.server.address"); + if (!StringUtils.isEmpty(contextPath)) { + metadata.put(MANAGEMENT_CONTEXT_PATH, contextPath); + } + if (!StringUtils.isEmpty(address)) { + metadata.put(MANAGEMENT_ADDRESS, address); + } + } + } + + @Override + public String getServiceId() { + return ansProperties.getClientDomains(); + } + + @Override + public String getHost() { + return ansProperties.getClientIp(); + } + + @Override + public int getPort() { + return ansProperties.getClientPort(); + } + + public void setPort(int port) { + // if spring.cloud.ans.port is not set,use the port detected from context + if (ansProperties.getClientPort() < 0) { + this.ansProperties.setClientPort(port); + } + } + + @Override + public boolean isSecure() { + return ansProperties.isSecure(); + } + + @Override + public URI getUri() { + return DefaultServiceInstance.getUri(this); + } + + @Override + public Map getMetadata() { + return ansProperties.getClientMetadata(); + } + + public boolean isRegisterEnabled() { + return ansProperties.isRegisterEnabled(); + } + + public String getCluster() { + return ansProperties.getClientCluster(); + } + + public float getRegisterWeight(String dom) { + if (null != ansProperties.getClientWeights().get(dom) + && ansProperties.getClientWeights().get(dom) > 0) { + return ansProperties.getClientWeights().get(dom); + } + return ansProperties.getClientWeight(); + } + + public AnsProperties getAnsProperties() { + return ansProperties; + } + + @Override + public String toString() { + return "AnsRegistration{" + "ansProperties=" + ansProperties + '}'; + } + +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/registry/AnsServiceRegistry.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/registry/AnsServiceRegistry.java new file mode 100644 index 000000000..ebe65ee39 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/registry/AnsServiceRegistry.java @@ -0,0 +1,114 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.ans.registry; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import org.apache.commons.lang.StringUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.cloud.client.serviceregistry.ServiceRegistry; + +import com.alibaba.ans.core.NamingService; +import com.alibaba.ans.shaded.com.taobao.vipserver.client.ipms.NodeReactor; + +/** + * @author xiaolongzuo + */ +public class AnsServiceRegistry implements ServiceRegistry { + + private static Logger logger = LoggerFactory.getLogger(AnsServiceRegistry.class); + + private static final String SEPARATOR = ","; + + @Override + public void register(AnsRegistration registration) { + + if (!registration.isRegisterEnabled()) { + logger.info("Registration is disabled..."); + return; + } + if (StringUtils.isEmpty(registration.getServiceId())) { + logger.info("No service to register for client..."); + return; + } + + List tags = new ArrayList<>(); + for (Map.Entry entry : registration.getAnsProperties().getTags() + .entrySet()) { + NodeReactor.Tag tag = new NodeReactor.Tag(); + tag.setName(entry.getKey()); + tag.setValue(entry.getValue()); + tags.add(tag); + } + + for (String dom : registration.getServiceId().split(SEPARATOR)) { + try { + NamingService.regDom(dom, registration.getHost(), registration.getPort(), + registration.getRegisterWeight(dom), registration.getCluster(), + tags); + logger.info("INFO_ANS_REGISTER, {} {}:{} register finished", dom, + registration.getAnsProperties().getClientIp(), + registration.getAnsProperties().getClientPort()); + } + catch (Exception e) { + logger.error("ERR_ANS_REGISTER, {} register failed...{},", dom, + registration.toString(), e); + } + } + } + + @Override + public void deregister(AnsRegistration registration) { + + logger.info("De-registering from ANSServer now..."); + + if (StringUtils.isEmpty(registration.getServiceId())) { + logger.info("No dom to de-register for client..."); + return; + } + + try { + NamingService.deRegDom(registration.getServiceId(), registration.getHost(), + registration.getPort(), registration.getCluster()); + } + catch (Exception e) { + logger.error("ERR_ANS_DEREGISTER, de-register failed...{},", + registration.toString(), e); + } + + logger.info("De-registration finished."); + } + + @Override + public void close() { + + } + + @Override + public void setStatus(AnsRegistration registration, String status) { + + } + + @Override + public T getStatus(AnsRegistration registration) { + return null; + } + +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/AnsRibbonClientConfiguration.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/AnsRibbonClientConfiguration.java new file mode 100644 index 000000000..7734e0371 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/AnsRibbonClientConfiguration.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.ans.ribbon; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.netflix.client.config.IClientConfig; +import com.netflix.loadbalancer.ServerList; + +/** + * @author xiaolongzuo + */ +@Configuration +public class AnsRibbonClientConfiguration { + + @Bean + @ConditionalOnMissingBean + public ServerList ribbonServerList(IClientConfig config) { + AnsServerList serverList = new AnsServerList(config.getClientName()); + return serverList; + } + +} \ No newline at end of file diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/AnsServer.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/AnsServer.java new file mode 100644 index 000000000..8438a7f35 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/AnsServer.java @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.ans.ribbon; + +import java.util.Collections; +import java.util.Map; + +import com.alibaba.ans.shaded.com.taobao.vipserver.client.core.Host; +import com.netflix.loadbalancer.Server; + +/** + * @author xiaolongzuo + */ +public class AnsServer extends Server { + + private final MetaInfo metaInfo; + private final Host host; + private final Map metadata; + + public AnsServer(final Host host, final String dom) { + super(host.getIp(), host.getPort()); + this.host = host; + this.metadata = Collections.emptyMap(); + metaInfo = new MetaInfo() { + @Override + public String getAppName() { + return dom; + } + + @Override + public String getServerGroup() { + return getMetadata().get("group"); + } + + @Override + public String getServiceIdForDiscovery() { + return null; + } + + @Override + public String getInstanceId() { + return null; + } + }; + } + + @Override + public MetaInfo getMetaInfo() { + return metaInfo; + } + + public Host getHealthService() { + return this.host; + } + + public Map getMetadata() { + return metadata; + } + +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/AnsServerList.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/AnsServerList.java new file mode 100644 index 000000000..f34f19ffd --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/AnsServerList.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.ans.ribbon; + +import java.util.ArrayList; +import java.util.List; + +import com.alibaba.ans.core.NamingService; +import com.alibaba.ans.shaded.com.taobao.vipserver.client.core.Host; +import com.netflix.client.config.IClientConfig; +import com.netflix.loadbalancer.AbstractServerList; + +/** + * @author xiaolongzuo + */ +public class AnsServerList extends AbstractServerList { + + private String dom; + + public AnsServerList(String dom) { + this.dom = dom; + } + + @Override + public List getInitialListOfServers() { + try { + List hosts = NamingService.getHosts(getDom()); + return hostsToServerList(hosts); + } + catch (Exception e) { + throw new IllegalStateException("Can not get ans hosts, dom=" + getDom(), e); + } + } + + @Override + public List getUpdatedListOfServers() { + return getInitialListOfServers(); + } + + private AnsServer hostToServer(Host host) { + AnsServer server = new AnsServer(host, getDom()); + return server; + } + + private List hostsToServerList(List hosts) { + List result = new ArrayList(hosts.size()); + for (Host host : hosts) { + if (host.isValid()) { + result.add(hostToServer(host)); + } + } + + return result; + } + + public String getDom() { + return dom; + } + + @Override + public void initWithNiwsConfig(IClientConfig iClientConfig) { + this.dom = iClientConfig.getClientName(); + } +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/ConditionalOnRibbonAns.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/ConditionalOnRibbonAns.java new file mode 100644 index 000000000..078e6fdd1 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/ConditionalOnRibbonAns.java @@ -0,0 +1,34 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.ans.ribbon; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; + +/** + * @author xiaolongzuo + */ +@Retention(RetentionPolicy.RUNTIME) +@Target({ ElementType.TYPE, ElementType.METHOD }) +@ConditionalOnProperty(value = "ribbon.ans.enabled", matchIfMissing = true) +public @interface ConditionalOnRibbonAns { + +} diff --git a/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/RibbonAnsAutoConfiguration.java b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/RibbonAnsAutoConfiguration.java new file mode 100644 index 000000000..4333cb534 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/java/org/springframework/cloud/alicloud/ans/ribbon/RibbonAnsAutoConfiguration.java @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.ans.ribbon; + +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.alicloud.ans.ConditionalOnAnsEnabled; +import org.springframework.cloud.netflix.ribbon.RibbonAutoConfiguration; +import org.springframework.cloud.netflix.ribbon.RibbonClients; +import org.springframework.cloud.netflix.ribbon.SpringClientFactory; +import org.springframework.context.annotation.Configuration; + +/** + * @author xiaolongzuo + */ +@Configuration +@EnableConfigurationProperties +@ConditionalOnAnsEnabled +@ConditionalOnBean(SpringClientFactory.class) +@ConditionalOnRibbonAns +@AutoConfigureAfter(RibbonAutoConfiguration.class) +@RibbonClients(defaultConfiguration = AnsRibbonClientConfiguration.class) +public class RibbonAnsAutoConfiguration { +} diff --git a/spring-cloud-alicloud-ans/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-alicloud-ans/src/main/resources/META-INF/additional-spring-configuration-metadata.json new file mode 100644 index 000000000..69ade28d5 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -0,0 +1,28 @@ +{ + "properties": [ + { + "name": "spring.cloud.ans.server.list", + "type": "java.lang.String", + "defaultValue": "127.0.0.1", + "description": "ANS server list." + }, + { + "name": "spring.cloud.ans.server.port", + "type": "java.lang.Integer", + "defaultValue": "80", + "description": "ANS server port." + }, + { + "name": "spring.cloud.ans.client.domains", + "type": "java.lang.String", + "defaultValue": "", + "description": "Service name list, default value is ${spring.application.name}." + }, + { + "name": "spring.cloud.ans.client.env", + "type": "java.lang.String", + "defaultValue": "DEFAULT", + "description": "The env for ans, default value is DEFAULT." + } + ] +} \ No newline at end of file diff --git a/spring-cloud-alicloud-ans/src/main/resources/META-INF/spring.factories b/spring-cloud-alicloud-ans/src/main/resources/META-INF/spring.factories new file mode 100644 index 000000000..63d6cd5c8 --- /dev/null +++ b/spring-cloud-alicloud-ans/src/main/resources/META-INF/spring.factories @@ -0,0 +1,6 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + org.springframework.cloud.alicloud.ans.endpoint.AnsEndpointAutoConfiguration,\ + org.springframework.cloud.alicloud.ans.ribbon.RibbonAnsAutoConfiguration,\ + org.springframework.cloud.alicloud.ans.AnsAutoConfiguration +org.springframework.cloud.client.discovery.EnableDiscoveryClient=\ + org.springframework.cloud.alicloud.ans.AnsDiscoveryClientAutoConfiguration diff --git a/spring-cloud-alicloud-ans/src/test/java/org/springframework/cloud/alibaba/ans/ribbon/AnsServiceListTests.java b/spring-cloud-alicloud-ans/src/test/java/org/springframework/cloud/alibaba/ans/ribbon/AnsServiceListTests.java new file mode 100644 index 000000000..5cb38532a --- /dev/null +++ b/spring-cloud-alicloud-ans/src/test/java/org/springframework/cloud/alibaba/ans/ribbon/AnsServiceListTests.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alibaba.ans.ribbon; + +import static org.junit.Assert.*; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Test; +import org.springframework.cloud.alicloud.ans.ribbon.AnsServer; +import org.springframework.cloud.alicloud.ans.ribbon.AnsServerList; + +import com.alibaba.ans.shaded.com.taobao.vipserver.client.core.Host; +import com.netflix.loadbalancer.Server; + +/** + * @author xiaolongzuo + */ +public class AnsServiceListTests { + + static final String IP_ADDR = "10.0.0.2"; + + static final int PORT = 8080; + + @Test + public void testAnsServer() { + AnsServerList serverList = getAnsServerList(); + List servers = serverList.getInitialListOfServers(); + assertNotNull("servers was null", servers); + assertEquals("servers was not size 1", 1, servers.size()); + Server des = assertAnsServer(servers); + assertEquals("hostPort was wrong", IP_ADDR + ":" + PORT, des.getHostPort()); + } + + protected Server assertAnsServer(List servers) { + Server actualServer = servers.get(0); + assertTrue("server was not a DomainExtractingServer", + actualServer instanceof AnsServer); + AnsServer des = AnsServer.class.cast(actualServer); + assertNotNull("host is null", des.getHealthService()); + assertEquals("unit was wrong", "DEFAULT", des.getHealthService().getUnit()); + return des; + } + + protected AnsServerList getAnsServerList() { + Host host = mock(Host.class); + given(host.getIp()).willReturn(IP_ADDR); + given(host.getDoubleWeight()).willReturn(1.0); + given(host.getPort()).willReturn(PORT); + given(host.getWeight()).willReturn(1); + given(host.getUnit()).willReturn("DEFAULT"); + + AnsServer server = new AnsServer(host, "testDom"); + @SuppressWarnings("unchecked") + AnsServerList originalServerList = mock(AnsServerList.class); + given(originalServerList.getInitialListOfServers()) + .willReturn(Arrays.asList(server)); + return originalServerList; + } + +} diff --git a/spring-cloud-alicloud-context/pom.xml b/spring-cloud-alicloud-context/pom.xml new file mode 100644 index 000000000..0084d0910 --- /dev/null +++ b/spring-cloud-alicloud-context/pom.xml @@ -0,0 +1,99 @@ + + + + + org.springframework.cloud + spring-cloud-alibaba + 0.2.0.BUILD-SNAPSHOT + + 4.0.0 + + org.springframework.cloud + spring-cloud-alicloud-context + Spring Cloud AliCloud Context + + + + + com.aliyun + aliyun-java-sdk-edas + provided + + + com.aliyun + aliyun-java-sdk-core + + + + + + com.alibaba.cloud + alicloud-context + + + + com.aliyun + aliyun-java-sdk-core + provided + + + + com.alibaba.ans + ans-sdk + provided + + + + com.aliyun.oss + aliyun-sdk-oss + provided + + + + com.alibaba.edas.acm + acm-sdk + provided + + + + org.springframework.cloud + spring-cloud-commons + + + + org.springframework.boot + spring-boot-starter-logging + provided + true + + + + org.springframework.boot + spring-boot + provided + true + + + + org.springframework.boot + spring-boot-autoconfigure + provided + true + + + + org.springframework.boot + spring-boot-starter-test + test + + + + org.springframework.boot + spring-boot-starter-web + test + + + + + diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/AliCloudContextAutoConfiguration.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/AliCloudContextAutoConfiguration.java new file mode 100644 index 000000000..f0f2f5722 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/AliCloudContextAutoConfiguration.java @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.context; + +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +/** + * @author xiaolongzuo + */ +@Configuration +@EnableConfigurationProperties(AliCloudProperties.class) +public class AliCloudContextAutoConfiguration { + +} diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/AliCloudProperties.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/AliCloudProperties.java new file mode 100644 index 000000000..095173292 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/AliCloudProperties.java @@ -0,0 +1,57 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.context; + +import org.springframework.boot.context.properties.ConfigurationProperties; + +import com.alibaba.cloud.context.AliCloudConfiguration; + +/** + * @author xiaolongzuo + */ +@ConfigurationProperties("spring.cloud.alicloud") +public class AliCloudProperties implements AliCloudConfiguration { + + /** + * alibaba cloud access key. + */ + private String accessKey; + + /** + * alibaba cloud secret key. + */ + private String secretKey; + + @Override + public String getAccessKey() { + return accessKey; + } + + public void setAccessKey(String accessKey) { + this.accessKey = accessKey; + } + + @Override + public String getSecretKey() { + return secretKey; + } + + public void setSecretKey(String secretKey) { + this.secretKey = secretKey; + } + +} diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmContextBootstrapConfiguration.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmContextBootstrapConfiguration.java new file mode 100644 index 000000000..772724420 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmContextBootstrapConfiguration.java @@ -0,0 +1,56 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.context.acm; + +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.alicloud.context.AliCloudProperties; +import org.springframework.cloud.alicloud.context.edas.EdasContextAutoConfiguration; +import org.springframework.cloud.alicloud.context.edas.EdasProperties; +import org.springframework.context.annotation.Configuration; + +import com.alibaba.cloud.context.acm.AliCloudAcmInitializer; + +/** + * @author xiaolongzuo + */ +@Configuration +@EnableConfigurationProperties(AcmProperties.class) +@ConditionalOnClass(name = "org.springframework.cloud.alicloud.acm.AcmAutoConfiguration") +@ImportAutoConfiguration(EdasContextAutoConfiguration.class) +public class AcmContextBootstrapConfiguration { + + @Autowired + private AcmProperties acmProperties; + + @Autowired + private EdasProperties edasProperties; + + @Autowired + private AliCloudProperties aliCloudProperties; + + @PostConstruct + public void initAcmProperties() { + AliCloudAcmInitializer.initialize(aliCloudProperties, edasProperties, + acmProperties); + } + +} diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmProperties.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmProperties.java new file mode 100644 index 000000000..18b037a39 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmProperties.java @@ -0,0 +1,158 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.context.acm; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.ConfigurationProperties; + +import com.alibaba.cloud.context.AliCloudServerMode; +import com.alibaba.cloud.context.acm.AcmConfiguration; + +/** + * acm properties + * + * @author leijuan + * @author xiaolongzuo + */ +@ConfigurationProperties(prefix = "spring.cloud.alicloud.acm") +public class AcmProperties implements AcmConfiguration { + + @Value("${spring.cloud.alicloud.acm.server-mode:LOCAL}") + private AliCloudServerMode serverMode; + + private String serverList = "127.0.0.1"; + + private String serverPort = "8080"; + + /** + * diamond group + */ + private String group = "DEFAULT_GROUP"; + + /** + * timeout to get configuration + */ + private int timeout = 3000; + + /** + * the AliYun endpoint2 for ACM + */ + private String endpoint; + + /** + * ACM namespace + */ + private String namespace; + + /** + * name of ram role granted to ECS + */ + private String ramRoleName; + + private String fileExtension = "properties"; + + private boolean refreshEnabled = true; + + public String getFileExtension() { + return fileExtension; + } + + public void setFileExtension(String fileExtension) { + this.fileExtension = fileExtension; + } + + @Override + public String getServerList() { + return serverList; + } + + public void setServerList(String serverList) { + this.serverList = serverList; + } + + @Override + public String getServerPort() { + return serverPort; + } + + public void setServerPort(String serverPort) { + this.serverPort = serverPort; + } + + @Override + public boolean isRefreshEnabled() { + return refreshEnabled; + } + + public void setRefreshEnabled(boolean refreshEnabled) { + this.refreshEnabled = refreshEnabled; + } + + @Override + public String getGroup() { + return group; + } + + public void setGroup(String group) { + this.group = group; + } + + @Override + public int getTimeout() { + return timeout; + } + + public void setTimeout(int timeout) { + this.timeout = timeout; + } + + @Override + public String getEndpoint() { + return endpoint; + } + + public void setEndpoint(String endpoint) { + this.endpoint = endpoint; + } + + @Override + public String getNamespace() { + return namespace; + } + + public void setNamespace(String namespace) { + this.namespace = namespace; + } + + @Override + public String getRamRoleName() { + return ramRoleName; + } + + public void setRamRoleName(String ramRoleName) { + this.ramRoleName = ramRoleName; + } + + @Override + public AliCloudServerMode getServerMode() { + return serverMode; + } + + public void setServerMode(AliCloudServerMode serverMode) { + this.serverMode = serverMode; + } +} diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsContextApplicationListener.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsContextApplicationListener.java new file mode 100644 index 000000000..6d9e9ca20 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsContextApplicationListener.java @@ -0,0 +1,54 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.context.ans; + +import org.springframework.cloud.alicloud.context.AliCloudProperties; +import org.springframework.cloud.alicloud.context.edas.EdasProperties; +import org.springframework.cloud.alicloud.context.listener.AbstractOnceApplicationListener; +import org.springframework.context.ApplicationContext; +import org.springframework.context.event.ContextRefreshedEvent; + +import com.alibaba.cloud.context.ans.AliCloudAnsInitializer; +import com.alibaba.cloud.context.edas.AliCloudEdasSdk; + +/** + * Init {@link com.alibaba.ans.core.NamingService} properties. + * + * @author xiaolongzuo + */ +public class AnsContextApplicationListener + extends AbstractOnceApplicationListener { + + @Override + protected String conditionalOnClass() { + return "org.springframework.cloud.alicloud.ans.AnsAutoConfiguration"; + } + + @Override + public void handleEvent(ContextRefreshedEvent event) { + ApplicationContext applicationContext = event.getApplicationContext(); + AliCloudProperties aliCloudProperties = applicationContext + .getBean(AliCloudProperties.class); + EdasProperties edasProperties = applicationContext.getBean(EdasProperties.class); + AnsProperties ansProperties = applicationContext.getBean(AnsProperties.class); + AliCloudEdasSdk aliCloudEdasSdk = applicationContext + .getBean(AliCloudEdasSdk.class); + AliCloudAnsInitializer.initialize(aliCloudProperties, edasProperties, + ansProperties, aliCloudEdasSdk); + } + +} diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsContextAutoConfiguration.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsContextAutoConfiguration.java new file mode 100644 index 000000000..fc58b69e9 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsContextAutoConfiguration.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.context.ans; + +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.alicloud.context.edas.EdasContextAutoConfiguration; +import org.springframework.cloud.commons.util.InetUtils; +import org.springframework.cloud.commons.util.InetUtilsProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @author xiaolongzuo + */ +@Configuration +@ConditionalOnClass(name = "org.springframework.cloud.alicloud.ans.AnsAutoConfiguration") +@EnableConfigurationProperties({ AnsProperties.class, InetUtilsProperties.class }) +@ImportAutoConfiguration(EdasContextAutoConfiguration.class) +public class AnsContextAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public InetUtils inetUtils(InetUtilsProperties inetUtilsProperties) { + return new InetUtils(inetUtilsProperties); + } + +} diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsProperties.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsProperties.java new file mode 100644 index 000000000..2889ae796 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsProperties.java @@ -0,0 +1,337 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.context.ans; + +import java.net.Inet4Address; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; +import java.util.Enumeration; +import java.util.HashMap; +import java.util.Map; + +import javax.annotation.PostConstruct; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.cloud.commons.util.InetUtils; +import org.springframework.util.StringUtils; + +import com.alibaba.cloud.context.AliCloudServerMode; +import com.alibaba.cloud.context.ans.AnsConfiguration; + +/** + * @author xiaolongzuo + */ +@ConfigurationProperties("spring.cloud.alicloud.ans") +public class AnsProperties implements AnsConfiguration { + + /** + * 服务端模式,默认为LOCAL + */ + @Value("${spring.cloud.alicloud.ans.server-mode:LOCAL}") + private AliCloudServerMode serverMode; + + /** + * 服务端列表 + */ + @Value("${spring.cloud.alicloud.ans.server-list:127.0.0.1}") + private String serverList; + + /** + * 服务端列表 + */ + @Value("${spring.cloud.alicloud.ans.server-port:8080}") + private String serverPort; + + /** + * 注册的服务名,默认从 spring.cloud.alicloud.ans.doms 中获取,当没有配置时,使用 spring.application.name + */ + @Value("${spring.cloud.alicloud.ans.client-domains:${spring.application.name:}}") + private String clientDomains; + + /** + * 注册服务的权重,从配置 spring.cloud.alicloud.ans.weight 中获取,默认为 1 + */ + private float clientWeight = 1; + + /** + * 当存在多个doms,需要对应不同的 weight 时,通过 spring.cloud.alicloud.ans.weight.dom1=weight1 的方式配置 + */ + private Map clientWeights = new HashMap(); + + /** + * 注册服务的 token ,从 spring.cloud.alicloud.ans.token 中获取 + */ + private String clientToken; + + /** + * 当存在多个doms,需要对应不同的token时,通过 spring.cloud.alicloud.ans.tokens.dom1=token1 的方式配置 + */ + private Map clientTokens = new HashMap(); + + /** + * 配置注册到哪个集群,从 spring.cloud.alicloud.ans.cluster 中获取,默认为 DEFAULT + */ + private String clientCluster = "DEFAULT"; + + /** + * metadata 实现 serviceInstance 接口所需的字段,但 ans 目前尚不支持此字段,配置了也没用 + */ + private Map clientMetadata = new HashMap<>(); + + /** + * 默认打开注册,可以通过 spring.cloud.alicloud.ans.register-enabled=false 的配置来关闭注册 + */ + private boolean registerEnabled = true; + + /** + * 想要发布的服务的ip,从 spring.cloud.alicloud.ans.client-ip 中获取 + */ + private String clientIp; + + /** + * 想要发布的服务的ip从哪一块网卡中获取 + */ + private String clientInterfaceName; + + /** + * 想要发布的服务的端口,从 spring.cloud.alicloud.ans.port 中获取 + */ + private int clientPort = -1; + + /** + * 租户下的环境隔离配置,相同租户的相同环境下的服务才能互相发现 + */ + @Value("${spring.cloud.alicloud.ans.env:${env.id:DEFAULT}}") + private String env; + + /** + * 是否注册成 https 的形式,通过 spring.cloud.alicloud.ans.secure 来配置,默认为false + */ + private boolean secure = false; + + @Autowired + private InetUtils inetUtils; + + private Map tags = new HashMap<>(); + + @PostConstruct + public void init() throws SocketException { + + // 增加注册类型,标记为 spring cloud 应用 + tags.put("ANS_SERVICE_TYPE", "SPRING_CLOUD"); + + if (StringUtils.isEmpty(clientIp)) { + // 如果没有指定注册的ip对应的网卡名,则通过遍历网卡去获取 + if (StringUtils.isEmpty(clientInterfaceName)) { + clientIp = inetUtils.findFirstNonLoopbackHostInfo().getIpAddress(); + } + else { + NetworkInterface networkInterface = NetworkInterface + .getByName(clientInterfaceName); + if (null == networkInterface) { + throw new RuntimeException( + "no such network interface " + clientInterfaceName); + } + + Enumeration inetAddress = networkInterface + .getInetAddresses(); + while (inetAddress.hasMoreElements()) { + InetAddress currentAddress = inetAddress.nextElement(); + if (currentAddress instanceof Inet4Address + && !currentAddress.isLoopbackAddress()) { + clientIp = currentAddress.getHostAddress(); + break; + } + } + + if (StringUtils.isEmpty(clientIp)) { + throw new RuntimeException( + "cannot find available ip from network interface " + + clientInterfaceName); + } + + } + } + } + + @Override + public String getServerPort() { + return serverPort; + } + + public void setServerPort(String serverPort) { + this.serverPort = serverPort; + } + + @Override + public String getServerList() { + return serverList; + } + + public void setServerList(String serverList) { + this.serverList = serverList; + } + + @Override + public boolean isRegisterEnabled() { + return registerEnabled; + } + + public void setRegisterEnabled(boolean registerEnabled) { + this.registerEnabled = registerEnabled; + } + + @Override + public boolean isSecure() { + return secure; + } + + public void setSecure(boolean secure) { + this.secure = secure; + } + + @Override + public String getEnv() { + return env; + } + + public void setEnv(String env) { + this.env = env; + } + + @Override + public Map getTags() { + return tags; + } + + public void setTags(Map tags) { + this.tags = tags; + } + + @Override + public AliCloudServerMode getServerMode() { + return serverMode; + } + + public void setServerMode(AliCloudServerMode serverMode) { + this.serverMode = serverMode; + } + + @Override + public String getClientDomains() { + return clientDomains; + } + + public void setClientDomains(String clientDomains) { + this.clientDomains = clientDomains; + } + + @Override + public float getClientWeight() { + return clientWeight; + } + + public void setClientWeight(float clientWeight) { + this.clientWeight = clientWeight; + } + + @Override + public Map getClientWeights() { + return clientWeights; + } + + public void setClientWeights(Map clientWeights) { + this.clientWeights = clientWeights; + } + + @Override + public String getClientToken() { + return clientToken; + } + + public void setClientToken(String clientToken) { + this.clientToken = clientToken; + } + + @Override + public Map getClientTokens() { + return clientTokens; + } + + public void setClientTokens(Map clientTokens) { + this.clientTokens = clientTokens; + } + + @Override + public String getClientCluster() { + return clientCluster; + } + + public void setClientCluster(String clientCluster) { + this.clientCluster = clientCluster; + } + + @Override + public Map getClientMetadata() { + return clientMetadata; + } + + public void setClientMetadata(Map clientMetadata) { + this.clientMetadata = clientMetadata; + } + + @Override + public String getClientIp() { + return clientIp; + } + + public void setClientIp(String clientIp) { + this.clientIp = clientIp; + } + + @Override + public String getClientInterfaceName() { + return clientInterfaceName; + } + + public void setClientInterfaceName(String clientInterfaceName) { + this.clientInterfaceName = clientInterfaceName; + } + + @Override + public int getClientPort() { + return clientPort; + } + + public void setClientPort(int clientPort) { + this.clientPort = clientPort; + } + + @Override + public String toString() { + return "AnsProperties{" + "doms='" + clientDomains + '\'' + ", weight=" + + clientWeight + ", weights=" + clientWeights + ", token='" + clientToken + + '\'' + ", tokens=" + clientTokens + ", cluster='" + clientCluster + '\'' + + ", metadata=" + clientMetadata + ", registerEnabled=" + registerEnabled + + ", ip='" + clientIp + '\'' + ", interfaceName='" + clientInterfaceName + + '\'' + ", port=" + clientPort + ", env='" + env + '\'' + ", secure=" + + secure + ", tags=" + tags + '}'; + } +} diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/edas/EdasContextAutoConfiguration.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/edas/EdasContextAutoConfiguration.java new file mode 100644 index 000000000..12945c231 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/edas/EdasContextAutoConfiguration.java @@ -0,0 +1,48 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.context.edas; + +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.alicloud.context.AliCloudContextAutoConfiguration; +import org.springframework.cloud.alicloud.context.AliCloudProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.alibaba.cloud.context.edas.AliCloudEdasSdk; +import com.alibaba.cloud.context.edas.AliCloudEdasSdkFactory; + +/** + * @author xiaolongzuo + */ +@Configuration +@EnableConfigurationProperties(EdasProperties.class) +@ImportAutoConfiguration(AliCloudContextAutoConfiguration.class) +public class EdasContextAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + @ConditionalOnClass(name = "com.aliyuncs.edas.model.v20170801.GetSecureTokenRequest") + public AliCloudEdasSdk aliCloudEdasSdk(AliCloudProperties aliCloudProperties, + EdasProperties edasProperties) { + return AliCloudEdasSdkFactory.getDefaultAliCloudEdasSdk(aliCloudProperties, + edasProperties.getRegionId()); + } + +} diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/edas/EdasProperties.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/edas/EdasProperties.java new file mode 100644 index 000000000..43c9a7cb9 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/edas/EdasProperties.java @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.context.edas; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.ConfigurationProperties; + +import com.alibaba.cloud.context.edas.EdasConfiguration; + +/** + * @author xiaolongzuo + */ +@ConfigurationProperties("spring.cloud.alicloud.edas") +public class EdasProperties implements EdasConfiguration { + + private static final String DEFAULT_APPLICATION_NAME = ""; + + /** + * edas application name. + */ + @Value("${spring.application.name:${spring.cloud.alicloud.edas.application.name:}}") + private String applicationName; + + /** + * edas namespace + */ + private String namespace; + + /** + * whether or not connect edas. + */ + private boolean enabled; + + @Override + public String getRegionId() { + if (namespace == null) { + return null; + } + return namespace.contains(":") ? namespace.split(":")[0] : namespace; + } + + @Override + public boolean isApplicationNameValid() { + return !DEFAULT_APPLICATION_NAME.equals(applicationName); + } + + @Override + public String getApplicationName() { + return applicationName; + } + + public void setApplicationName(String applicationName) { + this.applicationName = applicationName; + } + + @Override + public String getNamespace() { + return namespace; + } + + public void setNamespace(String namespace) { + this.namespace = namespace; + } + + @Override + public boolean isEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } +} diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/listener/AbstractOnceApplicationListener.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/listener/AbstractOnceApplicationListener.java new file mode 100644 index 000000000..d8b822a9b --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/listener/AbstractOnceApplicationListener.java @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.context.listener; + +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationEvent; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ApplicationContextEvent; + +/** + * @author xiaolongzuo + */ +public abstract class AbstractOnceApplicationListener + implements ApplicationListener { + + private static final String BOOTSTRAP_CONFIG_NAME_VALUE = "bootstrap"; + + private static final String BOOTSTRAP_CONFIG_NAME_KEY = "spring.config.name"; + + private static ConcurrentHashMap, AtomicBoolean> lockMap = new ConcurrentHashMap<>(); + + @Override + public void onApplicationEvent(T event) { + if (event instanceof ApplicationContextEvent) { + ApplicationContext applicationContext = ((ApplicationContextEvent) event) + .getApplicationContext(); + // skip bootstrap context or super parent context. + if (applicationContext.getParent() == null + || BOOTSTRAP_CONFIG_NAME_VALUE.equals(applicationContext + .getEnvironment().getProperty(BOOTSTRAP_CONFIG_NAME_KEY))) { + return; + } + } + Class clazz = getClass(); + lockMap.putIfAbsent(clazz, new AtomicBoolean(false)); + AtomicBoolean handled = lockMap.get(clazz); + // only execute once. + if (!handled.compareAndSet(false, true)) { + return; + } + if (conditionalOnClass() != null) { + try { + Class.forName(conditionalOnClass()); + } + catch (ClassNotFoundException e) { + // ignored + return; + } + } + handleEvent(event); + } + + /** + * handle event. + * + * @param event + */ + protected abstract void handleEvent(T event); + + /** + * condition on class. + * + * @return + */ + protected String conditionalOnClass() { + return null; + } + +} diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/oss/OssContextAutoConfiguration.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/oss/OssContextAutoConfiguration.java new file mode 100644 index 000000000..e1993a376 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/oss/OssContextAutoConfiguration.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.context.oss; + +import org.springframework.boot.autoconfigure.ImportAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.cloud.alicloud.context.AliCloudContextAutoConfiguration; +import org.springframework.cloud.alicloud.context.AliCloudProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.util.Assert; +import org.springframework.util.StringUtils; + +import com.alibaba.cloud.context.AliCloudAuthorizationMode; +import com.aliyun.oss.OSS; +import com.aliyun.oss.OSSClientBuilder; + +/** + * OSS Auto {@link Configuration} + * + * @author Jim + * @author xiaolongzuo + */ +@Configuration +@ConditionalOnClass(name = "org.springframework.cloud.alicloud.oss.OssAutoConfiguration") +@EnableConfigurationProperties(OssProperties.class) +@ImportAutoConfiguration(AliCloudContextAutoConfiguration.class) +public class OssContextAutoConfiguration { + + @ConditionalOnMissingBean + @Bean + public OSS ossClient(AliCloudProperties aliCloudProperties, + OssProperties ossProperties) { + if (ossProperties.getAuthorizationMode() == AliCloudAuthorizationMode.AK_SK) { + Assert.isTrue(!StringUtils.isEmpty(ossProperties.getEndpoint()), + "Oss endpoint can't be empty."); + Assert.isTrue(!StringUtils.isEmpty(aliCloudProperties.getAccessKey()), + "Access key can't be empty."); + Assert.isTrue(!StringUtils.isEmpty(aliCloudProperties.getSecretKey()), + "Secret key can't be empty."); + return new OSSClientBuilder().build(ossProperties.getEndpoint(), + aliCloudProperties.getAccessKey(), aliCloudProperties.getSecretKey(), + ossProperties.getConfig()); + } + else if (ossProperties.getAuthorizationMode() == AliCloudAuthorizationMode.STS) { + Assert.isTrue(!StringUtils.isEmpty(ossProperties.getEndpoint()), + "Oss endpoint can't be empty."); + Assert.isTrue(!StringUtils.isEmpty(ossProperties.getSts().getAccessKey()), + "Access key can't be empty."); + Assert.isTrue(!StringUtils.isEmpty(ossProperties.getSts().getSecretKey()), + "Secret key can't be empty."); + Assert.isTrue(!StringUtils.isEmpty(ossProperties.getSts().getSecurityToken()), + "Security Token can't be empty."); + return new OSSClientBuilder().build(ossProperties.getEndpoint(), + ossProperties.getSts().getAccessKey(), + ossProperties.getSts().getSecretKey(), + ossProperties.getSts().getSecurityToken(), ossProperties.getConfig()); + } + else { + throw new IllegalArgumentException("Unknown auth mode."); + } + } + +} diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/oss/OssProperties.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/oss/OssProperties.java new file mode 100644 index 000000000..59f64cc3c --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/oss/OssProperties.java @@ -0,0 +1,109 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.context.oss; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.context.properties.ConfigurationProperties; + +import com.alibaba.cloud.context.AliCloudAuthorizationMode; +import com.aliyun.oss.ClientBuilderConfiguration; + +/** + * {@link ConfigurationProperties} for configuring OSS. + * + * @author Jim + * @author xiaolongzuo + */ +@ConfigurationProperties("spring.cloud.alicloud.oss") +public class OssProperties { + + @Value("${spring.cloud.alicloud.oss.authorization-mode:AK_SK}") + private AliCloudAuthorizationMode authorizationMode; + + private String endpoint; + + private StsToken sts; + + private ClientBuilderConfiguration config; + + public AliCloudAuthorizationMode getAuthorizationMode() { + return authorizationMode; + } + + public void setAuthorizationMode(AliCloudAuthorizationMode authorizationMode) { + this.authorizationMode = authorizationMode; + } + + public ClientBuilderConfiguration getConfig() { + return config; + } + + public void setConfig(ClientBuilderConfiguration config) { + this.config = config; + } + + public String getEndpoint() { + return endpoint; + } + + public void setEndpoint(String endpoint) { + this.endpoint = endpoint; + } + + public StsToken getSts() { + return sts; + } + + public void setSts(StsToken sts) { + this.sts = sts; + } + + public static class StsToken { + + private String accessKey; + + private String secretKey; + + private String securityToken; + + public String getAccessKey() { + return accessKey; + } + + public void setAccessKey(String accessKey) { + this.accessKey = accessKey; + } + + public String getSecretKey() { + return secretKey; + } + + public void setSecretKey(String secretKey) { + this.secretKey = secretKey; + } + + public String getSecurityToken() { + return securityToken; + } + + public void setSecurityToken(String securityToken) { + this.securityToken = securityToken; + } + + } + +} diff --git a/spring-cloud-alicloud-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-alicloud-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json new file mode 100644 index 000000000..d50714101 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -0,0 +1,34 @@ +{ + "properties": [ + { + "name": "spring.cloud.alicloud.ans.server.mode", + "type": "java.lang.String", + "defaultValue": "LOCAL", + "description": "Server mode." + }, + { + "name": "spring.cloud.alicloud.ans.server.list", + "type": "java.lang.String", + "defaultValue": "127.0.0.1", + "description": "ANS server list." + }, + { + "name": "spring.cloud.alicloud.ans.server.port", + "type": "java.lang.Integer", + "defaultValue": "80", + "description": "ANS server port." + }, + { + "name": "spring.cloud.alicloud.ans.client.domains", + "type": "java.lang.String", + "defaultValue": "", + "description": "Service name list, default value is ${spring.application.name}." + }, + { + "name": "spring.cloud.alicloud.ans.client.env", + "type": "java.lang.String", + "defaultValue": "DEFAULT", + "description": "The env for ans, default value is DEFAULT." + } + ] +} \ No newline at end of file diff --git a/spring-cloud-alicloud-context/src/main/resources/META-INF/spring.factories b/spring-cloud-alicloud-context/src/main/resources/META-INF/spring.factories new file mode 100644 index 000000000..e10dcb959 --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/resources/META-INF/spring.factories @@ -0,0 +1,9 @@ +org.springframework.cloud.bootstrap.BootstrapConfiguration=\ + org.springframework.cloud.alicloud.context.acm.AcmContextBootstrapConfiguration +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + org.springframework.cloud.alicloud.context.AliCloudContextAutoConfiguration,\ + org.springframework.cloud.alicloud.context.edas.EdasContextAutoConfiguration,\ + org.springframework.cloud.alicloud.context.ans.AnsContextAutoConfiguration,\ + org.springframework.cloud.alicloud.context.oss.OssContextAutoConfiguration +org.springframework.context.ApplicationListener=\ + org.springframework.cloud.alicloud.context.ans.AnsContextApplicationListener \ No newline at end of file diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/acm/AcmAutoConfiguration.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/acm/AcmAutoConfiguration.java new file mode 100644 index 000000000..1ecff17f8 --- /dev/null +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/acm/AcmAutoConfiguration.java @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.acm; + +/** + * @author xiaolongzuo + */ +public class AcmAutoConfiguration { +} diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/ans/AnsAutoConfiguration.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/ans/AnsAutoConfiguration.java new file mode 100644 index 000000000..1f15f8827 --- /dev/null +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/ans/AnsAutoConfiguration.java @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.ans; + +/** + * @author xiaolongzuo + */ +public class AnsAutoConfiguration { +} diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudPropertiesTests.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudPropertiesTests.java new file mode 100644 index 000000000..3253a756f --- /dev/null +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudPropertiesTests.java @@ -0,0 +1,53 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.context; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +import org.junit.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; + +/** + * @author xiaolongzuo + */ +public class AliCloudPropertiesTests { + + private ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration( + AutoConfigurations.of(AliCloudContextAutoConfiguration.class)); + + @Test + public void testConfigurationValueDefaultsAreAsExpected() { + this.contextRunner.run(context -> { + AliCloudProperties config = context.getBean(AliCloudProperties.class); + assertThat(config.getAccessKey()).isNull(); + assertThat(config.getSecretKey()).isNull(); + }); + } + + @Test + public void testConfigurationValuesAreCorrectlyLoaded() { + this.contextRunner.withPropertyValues("spring.cloud.alicloud.access-key=123", + "spring.cloud.alicloud.secret-key=123456").run(context -> { + AliCloudProperties config = context.getBean(AliCloudProperties.class); + assertThat(config.getAccessKey()).isEqualTo("123"); + assertThat(config.getSecretKey()).isEqualTo("123456"); + }); + } + +} diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudSpringApplicationTests.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudSpringApplicationTests.java new file mode 100644 index 000000000..b40edd268 --- /dev/null +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudSpringApplicationTests.java @@ -0,0 +1,50 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.context; + +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit4.SpringRunner; + +/** + * @author xiaolongzuo + */ +@RunWith(SpringRunner.class) +@SpringBootTest(classes = AliCloudSpringApplicationTests.EurekaClientDisabledApp.class, properties = { + "spring.application.name=myapp", + "spring.cloud.alicloud.edas.application.name=myapp", + "spring.cloud.alicloud.access-key=ak", "spring.cloud.alicloud.secret-key=sk", + "spring.cloud.alicloud.oss.endpoint=test" }, webEnvironment = RANDOM_PORT) +@DirtiesContext +public class AliCloudSpringApplicationTests { + + @Test + public void contextLoads() { + System.out.println("Context load..."); + } + + @SpringBootApplication + public static class EurekaClientDisabledApp { + + } + +} diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/acm/AnsPropertiesTests.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/acm/AnsPropertiesTests.java new file mode 100644 index 000000000..9e278aca9 --- /dev/null +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/acm/AnsPropertiesTests.java @@ -0,0 +1,78 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.context.acm; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; + +import org.junit.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.cloud.alicloud.context.AliCloudContextAutoConfiguration; +import org.springframework.cloud.alicloud.context.edas.EdasContextAutoConfiguration; + +import com.alibaba.cloud.context.AliCloudServerMode; + +/** + * @author xiaolongzuo + */ +public class AnsPropertiesTests { + + private ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration( + AutoConfigurations.of(AcmContextBootstrapConfiguration.class, + EdasContextAutoConfiguration.class, + AliCloudContextAutoConfiguration.class)); + + @Test + public void testConfigurationValueDefaultsAreAsExpected() { + this.contextRunner.withPropertyValues().run(context -> { + AcmProperties config = context.getBean(AcmProperties.class); + assertThat(config.getServerMode()).isEqualTo(AliCloudServerMode.LOCAL); + assertThat(config.getServerList()).isEqualTo("127.0.0.1"); + assertThat(config.getServerPort()).isEqualTo("8080"); + assertThat(config.getEndpoint()).isNull(); + assertThat(config.getFileExtension()).isEqualTo("properties"); + assertThat(config.getGroup()).isEqualTo("DEFAULT_GROUP"); + assertThat(config.getNamespace()).isNull(); + assertThat(config.getRamRoleName()).isNull(); + assertThat(config.getTimeout()).isEqualTo(3000); + }); + } + + @Test + public void testConfigurationValuesAreCorrectlyLoaded() { + this.contextRunner.withPropertyValues("spring.cloud.alicloud.access-key=ak", + "spring.cloud.alicloud.secret-key=sk", + "spring.cloud.alicloud.acm.server-mode=EDAS", + "spring.cloud.alicloud.acm.server-port=11111", + "spring.cloud.alicloud.acm.server-list=10.10.10.10", + "spring.cloud.alicloud.acm.namespace=testNamespace", + "spring.cloud.alicloud.acm.endpoint=testDomain", + "spring.cloud.alicloud.acm.group=testGroup", + "spring.cloud.alicloud.acm.file-extension=yaml").run(context -> { + AcmProperties config = context.getBean(AcmProperties.class); + assertThat(config.getServerMode()).isEqualTo(AliCloudServerMode.EDAS); + assertThat(config.getServerList()).isEqualTo("10.10.10.10"); + assertThat(config.getServerPort()).isEqualTo("11111"); + assertThat(config.getEndpoint()).isEqualTo("testDomain"); + assertThat(config.getGroup()).isEqualTo("testGroup"); + assertThat(config.getFileExtension()).isEqualTo("yaml"); + assertThat(config.getNamespace()).isEqualTo("testNamespace"); + }); + } + +} diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/ans/AnsPropertiesTests.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/ans/AnsPropertiesTests.java new file mode 100644 index 000000000..11762c1b3 --- /dev/null +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/ans/AnsPropertiesTests.java @@ -0,0 +1,101 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.context.ans; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; + +import org.junit.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.cloud.alicloud.context.AliCloudContextAutoConfiguration; +import org.springframework.cloud.alicloud.context.edas.EdasContextAutoConfiguration; + +import com.alibaba.cloud.context.AliCloudServerMode; + +/** + * @author xiaolongzuo + */ +public class AnsPropertiesTests { + + private ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(AnsContextAutoConfiguration.class, + EdasContextAutoConfiguration.class, + AliCloudContextAutoConfiguration.class)); + + @Test + public void testConfigurationValueDefaultsAreAsExpected() + throws ClassNotFoundException { + this.contextRunner.withPropertyValues().run(context -> { + AnsProperties config = context.getBean(AnsProperties.class); + assertThat(config.getServerMode()).isEqualTo(AliCloudServerMode.LOCAL); + assertThat(config.getServerList()).isEqualTo("127.0.0.1"); + assertThat(config.getServerPort()).isEqualTo("8080"); + assertThat(config.getClientDomains()).isEqualTo(""); + assertThat(config.getClientWeight()).isEqualTo(1.0F); + assertThat(config.getClientWeights().size()).isEqualTo(0); + assertThat(config.getClientTokens().size()).isEqualTo(0); + assertThat(config.getClientMetadata().size()).isEqualTo(0); + assertThat(config.getClientToken()).isNull(); + assertThat(config.getClientCluster()).isEqualTo("DEFAULT"); + assertThat(config.isRegisterEnabled()).isTrue(); + assertThat(config.getClientInterfaceName()).isNull(); + assertThat(config.getClientPort()).isEqualTo(-1); + assertThat(config.getEnv()).isEqualTo("DEFAULT"); + assertThat(config.isSecure()).isFalse(); + assertThat(config.getTags().size()).isEqualTo(1); + assertThat(config.getTags().keySet().iterator().next()) + .isEqualTo("ANS_SERVICE_TYPE"); + assertThat(config.getTags().get("ANS_SERVICE_TYPE")) + .isEqualTo("SPRING_CLOUD"); + }); + } + + @Test + public void testConfigurationValuesAreCorrectlyLoaded() { + this.contextRunner + .withPropertyValues("spring.cloud.alicloud.ans.server-mode=EDAS", + "spring.cloud.alicloud.ans.server-port=11111", + "spring.cloud.alicloud.ans.server-list=10.10.10.10", + "spring.cloud.alicloud.ans.client-domains=testDomain", + "spring.cloud.alicloud.ans.client-weight=0.9", + "spring.cloud.alicloud.ans.client-weights.testDomain=0.9") + .run(context -> { + AnsProperties config = context.getBean(AnsProperties.class); + assertThat(config.getServerMode()).isEqualTo(AliCloudServerMode.EDAS); + assertThat(config.getServerList()).isEqualTo("10.10.10.10"); + assertThat(config.getServerPort()).isEqualTo("11111"); + assertThat(config.getClientDomains()).isEqualTo("testDomain"); + assertThat(config.getClientWeight()).isEqualTo(0.9F); + assertThat(config.getClientWeights().size()).isEqualTo(1); + assertThat(config.getClientTokens().size()).isEqualTo(0); + assertThat(config.getClientMetadata().size()).isEqualTo(0); + assertThat(config.getClientToken()).isNull(); + assertThat(config.getClientCluster()).isEqualTo("DEFAULT"); + assertThat(config.isRegisterEnabled()).isTrue(); + assertThat(config.getClientInterfaceName()).isNull(); + assertThat(config.getClientPort()).isEqualTo(-1); + assertThat(config.getEnv()).isEqualTo("DEFAULT"); + assertThat(config.isSecure()).isFalse(); + assertThat(config.getTags().size()).isEqualTo(1); + assertThat(config.getTags().keySet().iterator().next()) + .isEqualTo("ANS_SERVICE_TYPE"); + assertThat(config.getTags().get("ANS_SERVICE_TYPE")) + .isEqualTo("SPRING_CLOUD"); + }); + } + +} diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/edas/EdasPropertiesTests.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/edas/EdasPropertiesTests.java new file mode 100644 index 000000000..5f5f08ae0 --- /dev/null +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/edas/EdasPropertiesTests.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.context.edas; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +import org.junit.Test; +import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.cloud.alicloud.context.AliCloudContextAutoConfiguration; + +/** + * @author xiaolongzuo + */ +public class EdasPropertiesTests { + + private ApplicationContextRunner contextRunner = new ApplicationContextRunner() + .withConfiguration(AutoConfigurations.of(EdasContextAutoConfiguration.class, + AliCloudContextAutoConfiguration.class)); + + @Test + public void testConfigurationValueDefaultsAreAsExpected() { + this.contextRunner.withPropertyValues().run(context -> { + EdasProperties config = context.getBean(EdasProperties.class); + assertThat(config.getNamespace()).isNull(); + assertThat(config.isApplicationNameValid()).isFalse(); + }); + } + + @Test + public void testConfigurationValuesAreCorrectlyLoaded1() { + this.contextRunner + .withPropertyValues("spring.cloud.alicloud.edas.namespace=testns", + "spring.application.name=myapps") + .run(context -> { + EdasProperties config = context.getBean(EdasProperties.class); + assertThat(config.getNamespace()).isEqualTo("testns"); + assertThat(config.getApplicationName()).isEqualTo("myapps"); + }); + } + + @Test + public void testConfigurationValuesAreCorrectlyLoaded2() { + this.contextRunner + .withPropertyValues("spring.cloud.alicloud.edas.namespace=testns", + "spring.cloud.alicloud.edas.application.name=myapps") + .run(context -> { + EdasProperties config = context.getBean(EdasProperties.class); + assertThat(config.getNamespace()).isEqualTo("testns"); + assertThat(config.getApplicationName()).isEqualTo("myapps"); + }); + } + +} diff --git a/spring-cloud-alicloud-oss/src/test/java/org/springframework/cloud/alibaba/oss/test/OSSAutoConfigurationTests.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/oss/OssAutoConfigurationTests.java similarity index 66% rename from spring-cloud-alicloud-oss/src/test/java/org/springframework/cloud/alibaba/oss/test/OSSAutoConfigurationTests.java rename to spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/oss/OssAutoConfigurationTests.java index 96a145535..042ec0873 100644 --- a/spring-cloud-alicloud-oss/src/test/java/org/springframework/cloud/alibaba/oss/test/OSSAutoConfigurationTests.java +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/oss/OssAutoConfigurationTests.java @@ -14,46 +14,45 @@ * limitations under the License. */ -package org.springframework.cloud.alibaba.oss.test; +package org.springframework.cloud.alicloud.context.oss; import static org.assertj.core.api.Assertions.assertThat; import org.junit.Test; import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.test.context.runner.ApplicationContextRunner; -import org.springframework.cloud.alibaba.oss.OSSAutoConfiguration; -import org.springframework.cloud.alibaba.oss.OSSProperties; +import org.springframework.cloud.alicloud.context.AliCloudProperties; import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClient; /** - * {@link OSS} {@link OSSProperties} Test + * {@link OSS} {@link OssProperties} Test * * @author Jim */ -public class OSSAutoConfigurationTests { +public class OssAutoConfigurationTests { private ApplicationContextRunner contextRunner = new ApplicationContextRunner() - .withConfiguration(AutoConfigurations.of(OSSAutoConfiguration.class)) - .withPropertyValues("spring.cloud.alibaba.oss.accessKeyId=your-ak") - .withPropertyValues("spring.cloud.alibaba.oss.secretAccessKey=your-sk") + .withConfiguration(AutoConfigurations.of(OssContextAutoConfiguration.class)) + .withPropertyValues("spring.cloud.alicloud.accessKey=your-ak") + .withPropertyValues("spring.cloud.alicloud.secretKey=your-sk") .withPropertyValues( - "spring.cloud.alibaba.oss.endpoint=http://oss-cn-beijing.aliyuncs.com") - .withPropertyValues( - "spring.cloud.alibaba.oss.configuration.userAgent=alibaba"); + "spring.cloud.alicloud.oss.endpoint=http://oss-cn-beijing.aliyuncs.com") + .withPropertyValues("spring.cloud.alicloud.oss.config.userAgent=alibaba"); @Test public void testOSSProperties() { this.contextRunner.run(context -> { - assertThat(context.getBeansOfType(OSSProperties.class).size() == 1).isTrue(); - OSSProperties ossProperties = context.getBean(OSSProperties.class); - assertThat(ossProperties.getAccessKeyId()).isEqualTo("your-ak"); - assertThat(ossProperties.getSecretAccessKey()).isEqualTo("your-sk"); + assertThat(context.getBeansOfType(OssProperties.class).size() == 1).isTrue(); + AliCloudProperties aliCloudProperties = context + .getBean(AliCloudProperties.class); + OssProperties ossProperties = context.getBean(OssProperties.class); + assertThat(aliCloudProperties.getAccessKey()).isEqualTo("your-ak"); + assertThat(aliCloudProperties.getSecretKey()).isEqualTo("your-sk"); assertThat(ossProperties.getEndpoint()) .isEqualTo("http://oss-cn-beijing.aliyuncs.com"); - assertThat(ossProperties.getConfiguration().getUserAgent()) - .isEqualTo("alibaba"); + assertThat(ossProperties.getConfig().getUserAgent()).isEqualTo("alibaba"); }); } diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/oss/OssAutoConfiguration.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/oss/OssAutoConfiguration.java new file mode 100644 index 000000000..2f6961c6d --- /dev/null +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/oss/OssAutoConfiguration.java @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.oss; + +/** + * @author xiaolongzuo + */ +public class OssAutoConfiguration { +} diff --git a/spring-cloud-alicloud-oss/pom.xml b/spring-cloud-alicloud-oss/pom.xml index d486d2c02..a101f1291 100644 --- a/spring-cloud-alicloud-oss/pom.xml +++ b/spring-cloud-alicloud-oss/pom.xml @@ -15,10 +15,14 @@ + + org.springframework.cloud + spring-cloud-alicloud-context + + com.aliyun.oss aliyun-sdk-oss - true diff --git a/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/OSSProperties.java b/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/OSSProperties.java deleted file mode 100644 index 323c1754a..000000000 --- a/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/OSSProperties.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright (C) 2018 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.alibaba.oss; - -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.boot.context.properties.ConfigurationProperties; - -import com.aliyun.oss.ClientBuilderConfiguration; - -/** - * {@link ConfigurationProperties} for configuring OSS. - * - * @author Jim - */ -@ConfigurationProperties(prefix = OSSConstants.PREFIX) -public class OSSProperties { - - private static final Logger logger = LoggerFactory.getLogger(OSSProperties.class); - - public static final Map endpointMap = new HashMap<>(); - - static { - endpointMap.put("cn-beijing", "http://oss-cn-beijing.aliyuncs.com"); - endpointMap.put("cn-qingdao", "http://oss-cn-qingdao.aliyuncs.com"); - endpointMap.put("cn-hangzhou", "http://oss-cn-hangzhou.aliyuncs.com"); - endpointMap.put("cn-hongkong", "http://oss-cn-hongkong.aliyuncs.com"); - endpointMap.put("cn-shenzhen", "http://oss-cn-shenzhen.aliyuncs.com"); - endpointMap.put("us-west-1", "http://oss-us-west-1.aliyuncs.com"); - endpointMap.put("ap-southeast-1", "http://oss-ap-southeast-1.aliyuncs.com"); - } - - private ClientBuilderConfiguration configuration; - - private String accessKeyId; - - private String secretAccessKey; - - private String region; - - private String endpoint; - - // support ram sts - private String securityToken; - - public ClientBuilderConfiguration getConfiguration() { - return configuration; - } - - public void setConfiguration(ClientBuilderConfiguration configuration) { - this.configuration = configuration; - } - - public String getAccessKeyId() { - return accessKeyId; - } - - public void setAccessKeyId(String accessKeyId) { - this.accessKeyId = accessKeyId; - } - - public String getSecretAccessKey() { - return secretAccessKey; - } - - public void setSecretAccessKey(String secretAccessKey) { - this.secretAccessKey = secretAccessKey; - } - - public String getEndpoint() { - return endpoint; - } - - public void setEndpoint(String endpoint) { - this.endpoint = endpoint; - } - - public String getSecurityToken() { - return securityToken; - } - - public void setSecurityToken(String securityToken) { - this.securityToken = securityToken; - } - - public String getRegion() { - return region; - } - - public void setRegion(String region) { - if (!endpointMap.containsKey(region)) { - String errorStr = "error region: " + region + ", please choose from " - + Arrays.toString(endpointMap.keySet().toArray()); - logger.error(errorStr); - throw new IllegalArgumentException(errorStr); - } - this.region = region; - this.setEndpoint(endpointMap.get(region)); - } -} diff --git a/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/endpoint/OSSEndpoint.java b/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/endpoint/OSSEndpoint.java deleted file mode 100644 index d417a858a..000000000 --- a/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/endpoint/OSSEndpoint.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (C) 2018 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.alibaba.oss.endpoint; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.actuate.endpoint.annotation.Endpoint; -import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; -import org.springframework.context.ApplicationContext; - -import com.aliyun.oss.OSSClient; - -/** - * Actuator {@link Endpoint} to expose OSS Meta Data - * - * @author Jim - */ -@Endpoint(id = "oss") -public class OSSEndpoint { - - @Autowired - private ApplicationContext applicationContext; - - @ReadOperation - public Map invoke() { - Map result = new HashMap<>(); - - Map ossClientMap = applicationContext - .getBeansOfType(OSSClient.class); - - int size = ossClientMap.size(); - - List ossClientList = new ArrayList<>(); - - ossClientMap.keySet().forEach(beanName -> { - Map ossProperties = new HashMap<>(); - OSSClient client = ossClientMap.get(beanName); - ossProperties.put("beanName", beanName); - ossProperties.put("endpoint", client.getEndpoint().toString()); - ossProperties.put("clientConfiguration", client.getClientConfiguration()); - ossProperties.put("credentials", - client.getCredentialsProvider().getCredentials()); - ossProperties.put("bucketList", client.listBuckets().stream() - .map(bucket -> bucket.getName()).toArray()); - ossClientList.add(ossProperties); - }); - - result.put("size", size); - result.put("info", ossClientList); - - return result; - } - -} diff --git a/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/OSSApplicationListener.java b/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alicloud/oss/OssApplicationListener.java similarity index 90% rename from spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/OSSApplicationListener.java rename to spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alicloud/oss/OssApplicationListener.java index d29039b04..23120e388 100644 --- a/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/OSSApplicationListener.java +++ b/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alicloud/oss/OssApplicationListener.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.cloud.alibaba.oss; +package org.springframework.cloud.alicloud.oss; import java.util.Map; @@ -30,10 +30,10 @@ import com.aliyun.oss.OSS; * * @author Jim */ -public class OSSApplicationListener implements ApplicationListener { +public class OssApplicationListener implements ApplicationListener { private static final Logger logger = LoggerFactory - .getLogger(OSSApplicationListener.class); + .getLogger(OssApplicationListener.class); @Override public void onApplicationEvent(ContextClosedEvent event) { diff --git a/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/OSSAutoConfiguration.java b/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alicloud/oss/OssAutoConfiguration.java similarity index 52% rename from spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/OSSAutoConfiguration.java rename to spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alicloud/oss/OssAutoConfiguration.java index 194933c35..5d7b57317 100644 --- a/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/OSSAutoConfiguration.java +++ b/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alicloud/oss/OssAutoConfiguration.java @@ -14,21 +14,16 @@ * limitations under the License. */ -package org.springframework.cloud.alibaba.oss; +package org.springframework.cloud.alicloud.oss; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; +import com.aliyun.oss.OSS; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; -import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.cloud.alibaba.oss.resource.OSSStorageProtocolResolver; +import org.springframework.cloud.alicloud.oss.resource.OssStorageProtocolResolver; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; -import com.aliyun.oss.OSS; -import com.aliyun.oss.OSSClientBuilder; - /** * OSS Auto {@link Configuration} * @@ -36,26 +31,13 @@ import com.aliyun.oss.OSSClientBuilder; */ @Configuration @ConditionalOnClass(OSS.class) -@ConditionalOnProperty(name = OSSConstants.ENABLED, havingValue = "true", matchIfMissing = true) -@EnableConfigurationProperties(OSSProperties.class) -public class OSSAutoConfiguration { - - private static final Logger logger = LoggerFactory - .getLogger(OSSAutoConfiguration.class); - - @ConditionalOnMissingBean - @Bean - public OSS ossClient(OSSProperties ossProperties) { - logger.info("construct OSS because it is missing"); - return new OSSClientBuilder().build(ossProperties.getEndpoint(), - ossProperties.getAccessKeyId(), ossProperties.getSecretAccessKey(), - ossProperties.getSecurityToken(), ossProperties.getConfiguration()); - } - - @ConditionalOnMissingBean - @Bean - public OSSStorageProtocolResolver ossStorageProtocolResolver() { - return new OSSStorageProtocolResolver(); - } +@ConditionalOnProperty(name = OssConstants.ENABLED, havingValue = "true", matchIfMissing = true) +public class OssAutoConfiguration { + + @Bean + @ConditionalOnMissingBean + public OssStorageProtocolResolver ossStorageProtocolResolver() { + return new OssStorageProtocolResolver(); + } } diff --git a/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/OSSConstants.java b/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alicloud/oss/OssConstants.java similarity index 90% rename from spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/OSSConstants.java rename to spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alicloud/oss/OssConstants.java index 4f0587923..e764ebacc 100644 --- a/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/OSSConstants.java +++ b/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alicloud/oss/OssConstants.java @@ -14,14 +14,14 @@ * limitations under the License. */ -package org.springframework.cloud.alibaba.oss; +package org.springframework.cloud.alicloud.oss; /** * OSS constants * * @author Jim */ -public interface OSSConstants { +public interface OssConstants { String PREFIX = "spring.cloud.alibaba.oss"; String ENABLED = PREFIX + ".enabled"; diff --git a/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alicloud/oss/endpoint/OssEndpoint.java b/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alicloud/oss/endpoint/OssEndpoint.java new file mode 100644 index 000000000..a09e53fde --- /dev/null +++ b/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alicloud/oss/endpoint/OssEndpoint.java @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.oss.endpoint; + +import com.aliyun.oss.OSSClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.actuate.endpoint.annotation.Endpoint; +import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; +import org.springframework.context.ApplicationContext; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Actuator {@link Endpoint} to expose OSS Meta Data + * + * @author Jim + */ +@Endpoint(id = "oss") +public class OssEndpoint { + + @Autowired + private ApplicationContext applicationContext; + + @ReadOperation + public Map invoke() { + Map result = new HashMap<>(); + + Map ossClientMap = applicationContext + .getBeansOfType(OSSClient.class); + + int size = ossClientMap.size(); + + List ossClientList = new ArrayList<>(); + + ossClientMap.keySet().forEach(beanName -> { + Map ossProperties = new HashMap<>(); + OSSClient client = ossClientMap.get(beanName); + ossProperties.put("beanName", beanName); + ossProperties.put("endpoint", client.getEndpoint().toString()); + ossProperties.put("clientConfiguration", client.getClientConfiguration()); + ossProperties.put("credentials", + client.getCredentialsProvider().getCredentials()); + ossProperties.put("bucketList", client.listBuckets().stream() + .map(bucket -> bucket.getName()).toArray()); + ossClientList.add(ossProperties); + }); + + result.put("size", size); + result.put("info", ossClientList); + + return result; + } + +} diff --git a/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/endpoint/OSSEndpointAutoConfiguration.java b/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alicloud/oss/endpoint/OssEndpointAutoConfiguration.java similarity index 88% rename from spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/endpoint/OSSEndpointAutoConfiguration.java rename to spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alicloud/oss/endpoint/OssEndpointAutoConfiguration.java index c056d5864..0378dd4f7 100644 --- a/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/endpoint/OSSEndpointAutoConfiguration.java +++ b/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alicloud/oss/endpoint/OssEndpointAutoConfiguration.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.cloud.alibaba.oss.endpoint; +package org.springframework.cloud.alicloud.oss.endpoint; import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; @@ -29,13 +29,13 @@ import org.springframework.context.annotation.Configuration; * @author Jim */ @ConditionalOnClass(Endpoint.class) -public class OSSEndpointAutoConfiguration { +public class OssEndpointAutoConfiguration { @Bean @ConditionalOnMissingBean @ConditionalOnEnabledEndpoint - public OSSEndpoint sentinelEndPoint() { - return new OSSEndpoint(); + public OssEndpoint sentinelEndPoint() { + return new OssEndpoint(); } } diff --git a/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/resource/OSSStorageProtocolResolver.java b/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alicloud/oss/resource/OssStorageProtocolResolver.java similarity index 92% rename from spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/resource/OSSStorageProtocolResolver.java rename to spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alicloud/oss/resource/OssStorageProtocolResolver.java index 159f2c49e..681f3e882 100644 --- a/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/resource/OSSStorageProtocolResolver.java +++ b/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alicloud/oss/resource/OssStorageProtocolResolver.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.cloud.alibaba.oss.resource; +package org.springframework.cloud.alicloud.oss.resource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -34,13 +34,13 @@ import com.aliyun.oss.OSS; * * @author Jim */ -public class OSSStorageProtocolResolver +public class OssStorageProtocolResolver implements ProtocolResolver, BeanFactoryPostProcessor, ResourceLoaderAware { public static final String PROTOCOL = "oss://"; private static final Logger logger = LoggerFactory - .getLogger(OSSStorageProtocolResolver.class); + .getLogger(OssStorageProtocolResolver.class); private ConfigurableListableBeanFactory beanFactory; @@ -62,7 +62,7 @@ public class OSSStorageProtocolResolver if (!location.startsWith(PROTOCOL)) { return null; } - return new OSSStorageResource(getOSS(), location); + return new OssStorageResource(getOSS(), location); } @Override diff --git a/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/resource/OSSStorageResource.java b/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alicloud/oss/resource/OssStorageResource.java similarity index 93% rename from spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/resource/OSSStorageResource.java rename to spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alicloud/oss/resource/OssStorageResource.java index 08ef10aa8..82f38fe50 100644 --- a/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alibaba/oss/resource/OSSStorageResource.java +++ b/spring-cloud-alicloud-oss/src/main/java/org/springframework/cloud/alicloud/oss/resource/OssStorageResource.java @@ -14,7 +14,7 @@ * limitations under the License. */ -package org.springframework.cloud.alibaba.oss.resource; +package org.springframework.cloud.alicloud.oss.resource; import java.io.File; import java.io.FileNotFoundException; @@ -42,17 +42,17 @@ import com.aliyun.oss.model.OSSObject; * @see Bucket * @see OSSObject */ -public class OSSStorageResource implements Resource { +public class OssStorageResource implements Resource { private final OSS oss; private final String bucketName; private final String objectKey; private final URI location; - public OSSStorageResource(OSS oss, String location) { + public OssStorageResource(OSS oss, String location) { Assert.notNull(oss, "Object Storage Service can not be null"); - Assert.isTrue(location.startsWith(OSSStorageProtocolResolver.PROTOCOL), - "Location must start with " + OSSStorageProtocolResolver.PROTOCOL); + Assert.isTrue(location.startsWith(OssStorageProtocolResolver.PROTOCOL), + "Location must start with " + OssStorageProtocolResolver.PROTOCOL); this.oss = oss; try { URI locationUri = new URI(location); @@ -123,7 +123,7 @@ public class OSSStorageResource implements Resource { @Override public Resource createRelative(String relativePath) throws IOException { - return new OSSStorageResource(this.oss, + return new OssStorageResource(this.oss, this.location.resolve(relativePath).toString()); } diff --git a/spring-cloud-alicloud-oss/src/main/resources/META-INF/spring.factories b/spring-cloud-alicloud-oss/src/main/resources/META-INF/spring.factories index 4f71c2da2..598bc2166 100644 --- a/spring-cloud-alicloud-oss/src/main/resources/META-INF/spring.factories +++ b/spring-cloud-alicloud-oss/src/main/resources/META-INF/spring.factories @@ -1,5 +1,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ -org.springframework.cloud.alibaba.oss.OSSAutoConfiguration,\ -org.springframework.cloud.alibaba.oss.endpoint.OSSEndpointAutoConfiguration +org.springframework.cloud.alicloud.oss.OssAutoConfiguration,\ +org.springframework.cloud.alicloud.oss.endpoint.OssEndpointAutoConfiguration org.springframework.context.ApplicationListener=\ -org.springframework.cloud.alibaba.oss.OSSApplicationListener \ No newline at end of file +org.springframework.cloud.alicloud.oss.OssApplicationListener \ No newline at end of file diff --git a/spring-cloud-alicloud-oss/src/test/java/org/springframework/cloud/alibaba/oss/test/OSSMultiClientAutoConfigurationTests.java b/spring-cloud-alicloud-oss/src/test/java/org/springframework/cloud/alibaba/oss/test/OSSMultiClientAutoConfigurationTests.java deleted file mode 100644 index 1af1f2853..000000000 --- a/spring-cloud-alicloud-oss/src/test/java/org/springframework/cloud/alibaba/oss/test/OSSMultiClientAutoConfigurationTests.java +++ /dev/null @@ -1,116 +0,0 @@ -/* - * Copyright (C) 2018 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.cloud.alibaba.oss.test; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.Test; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.boot.autoconfigure.AutoConfigurations; -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.boot.test.context.runner.ApplicationContextRunner; -import org.springframework.cloud.alibaba.oss.OSSAutoConfiguration; -import org.springframework.cloud.alibaba.oss.OSSProperties; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import com.aliyun.oss.OSS; -import com.aliyun.oss.OSSClient; -import com.aliyun.oss.OSSClientBuilder; - -/** - * Multi {@link OSS} {@link OSSProperties} Test - * - * @author Jim - */ -public class OSSMultiClientAutoConfigurationTests { - - private ApplicationContextRunner contextRunner = new ApplicationContextRunner() - .withConfiguration(AutoConfigurations.of(OSSAutoConfiguration.class)) - .withPropertyValues("spring.cloud.alibaba.oss.accessKeyId=your-ak") - .withPropertyValues("spring.cloud.alibaba.oss.secretAccessKey=your-sk") - .withPropertyValues( - "spring.cloud.alibaba.oss.endpoint=http://oss-cn-beijing.aliyuncs.com") - .withPropertyValues( - "spring.cloud.alibaba.oss.configuration.userAgent=alibaba") - .withPropertyValues("spring.cloud.alibaba.oss1.accessKeyId=your-ak1") - .withPropertyValues("spring.cloud.alibaba.oss1.secretAccessKey=your-sk1") - .withPropertyValues( - "spring.cloud.alibaba.oss1.endpoint=http://oss-cn-beijing.aliyuncs.com") - .withPropertyValues( - "spring.cloud.alibaba.oss1.configuration.userAgent=alibaba1"); - - @Test - public void testOSSClient() { - this.contextRunner.withUserConfiguration(MultiClientConfiguration.class) - .run(context -> { - assertThat(context.getBeansOfType(OSS.class).size() == 2).isTrue(); - OSSClient ossClient = (OSSClient) context.getBean("ossClient1", - OSS.class); - assertThat(ossClient.getEndpoint().toString()) - .isEqualTo("http://oss-cn-beijing.aliyuncs.com"); - assertThat(ossClient.getClientConfiguration().getUserAgent()) - .isEqualTo("alibaba"); - assertThat(ossClient.getCredentialsProvider().getCredentials() - .getAccessKeyId()).isEqualTo("your-ak"); - assertThat(ossClient.getCredentialsProvider().getCredentials() - .getSecretAccessKey()).isEqualTo("your-sk"); - OSSClient ossClient1 = (OSSClient) context.getBean("ossClient2", - OSS.class); - assertThat(ossClient1.getEndpoint().toString()) - .isEqualTo("http://oss-cn-beijing.aliyuncs.com"); - assertThat(ossClient1.getClientConfiguration().getUserAgent()) - .isEqualTo("alibaba1"); - assertThat(ossClient1.getCredentialsProvider().getCredentials() - .getAccessKeyId()).isEqualTo("your-ak1"); - assertThat(ossClient1.getCredentialsProvider().getCredentials() - .getSecretAccessKey()).isEqualTo("your-sk1"); - }); - } - - @Configuration - protected static class MultiClientConfiguration { - - @Bean - @ConfigurationProperties(prefix = "spring.cloud.alibaba.oss") - public OSSProperties ossProperties1() { - return new OSSProperties(); - } - - @Bean - public OSS ossClient1(@Qualifier("ossProperties1") OSSProperties ossProperties) { - return new OSSClientBuilder().build(ossProperties.getEndpoint(), - ossProperties.getAccessKeyId(), ossProperties.getSecretAccessKey(), - ossProperties.getSecurityToken(), ossProperties.getConfiguration()); - } - - @Bean - @ConfigurationProperties(prefix = "spring.cloud.alibaba.oss1") - public OSSProperties ossProperties2() { - return new OSSProperties(); - } - - @Bean - public OSS ossClient2(@Qualifier("ossProperties2") OSSProperties ossProperties) { - return new OSSClientBuilder().build(ossProperties.getEndpoint(), - ossProperties.getAccessKeyId(), ossProperties.getSecretAccessKey(), - ossProperties.getSecurityToken(), ossProperties.getConfiguration()); - } - - } - -} diff --git a/spring-cloud-starter-alicloud/pom.xml b/spring-cloud-starter-alicloud/pom.xml index f44393a4c..f53a55d2b 100644 --- a/spring-cloud-starter-alicloud/pom.xml +++ b/spring-cloud-starter-alicloud/pom.xml @@ -12,5 +12,7 @@ Spring Cloud Alibaba Cloud Starters spring-cloud-starter-alicloud-oss + spring-cloud-starter-alicloud-acm + spring-cloud-starter-alicloud-ans \ No newline at end of file diff --git a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-acm/pom.xml b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-acm/pom.xml new file mode 100644 index 000000000..fa71c4101 --- /dev/null +++ b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-acm/pom.xml @@ -0,0 +1,20 @@ + + 4.0.0 + + + org.springframework.cloud + spring-cloud-starter-alicloud + 0.2.0.BUILD-SNAPSHOT + + spring-cloud-starter-alicloud-acm + Spring Cloud Starter Alibaba Cloud ACM + + + + org.springframework.cloud + spring-cloud-alicloud-acm + + + + diff --git a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml new file mode 100644 index 000000000..418614fa2 --- /dev/null +++ b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml @@ -0,0 +1,21 @@ + + + + spring-cloud-starter-alibaba + org.springframework.cloud + 0.2.0.BUILD-SNAPSHOT + + 4.0.0 + spring-cloud-starter-alicloud-ans + Spring Cloud Starter Alibaba Cloud ANS + + + + org.springframework.cloud + spring-cloud-alicloud-ans + + + + \ No newline at end of file From c6627cf2a48ee321194622460d8e76727840cb3c Mon Sep 17 00:00:00 2001 From: "chenzhu.zxl" Date: Mon, 29 Oct 2018 10:04:28 +0800 Subject: [PATCH 20/63] rename OssApplication and OssController. --- .gitignore | 1 + .../cloud/examples/OssApplication.java | 49 ++++++++++++++ .../alibaba/cloud/examples/OssController.java | 65 +++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OssApplication.java create mode 100644 spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OssController.java diff --git a/.gitignore b/.gitignore index e4b662ae1..e02b722f5 100644 --- a/.gitignore +++ b/.gitignore @@ -28,4 +28,5 @@ hs_err_pid* .project .settings target +.DS_Store diff --git a/spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OssApplication.java b/spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OssApplication.java new file mode 100644 index 000000000..f9265800d --- /dev/null +++ b/spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OssApplication.java @@ -0,0 +1,49 @@ +package org.springframework.cloud.alibaba.cloud.examples; + +import com.aliyun.oss.OSS; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; + +import java.net.URISyntaxException; + +/** + * OSS Application + * + * @author Jim + */ +@SpringBootApplication +public class OssApplication { + + public static final String BUCKET_NAME = "spring-cloud-alibaba-test"; + + public static void main(String[] args) throws URISyntaxException { + SpringApplication.run(OssApplication.class, args); + } + + @Bean + public AppRunner appRunner() { + return new AppRunner(); + } + + class AppRunner implements ApplicationRunner { + @Autowired + private OSS ossClient; + + @Override + public void run(ApplicationArguments args) throws Exception { + try { + if (!ossClient.doesBucketExist(BUCKET_NAME)) { + ossClient.createBucket(BUCKET_NAME); + } + } catch (Exception e) { + System.err.println("oss handle bucket error: " + e.getMessage()); + System.exit(-1); + } + } + } + +} diff --git a/spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OssController.java b/spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OssController.java new file mode 100644 index 000000000..7c6e427a9 --- /dev/null +++ b/spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OssController.java @@ -0,0 +1,65 @@ +package org.springframework.cloud.alibaba.cloud.examples; + +import com.aliyun.oss.OSS; +import com.aliyun.oss.common.utils.IOUtils; +import com.aliyun.oss.model.OSSObject; +import org.apache.commons.codec.CharEncoding; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.Resource; +import org.springframework.util.StreamUtils; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.nio.charset.Charset; + +/** + * OSS Controller + * + * @author Jim + */ +@RestController +public class OssController { + + @Autowired + private OSS ossClient; + + @Value("oss://" + OssApplication.BUCKET_NAME + "/oss-test.json") + private Resource file; + + @GetMapping("/upload") + public String upload() { + try { + ossClient.putObject(OssApplication.BUCKET_NAME, "oss-test.json", this + .getClass().getClassLoader().getResourceAsStream("oss-test.json")); + } catch (Exception e) { + e.printStackTrace(); + return "upload fail: " + e.getMessage(); + } + return "upload success"; + } + + @GetMapping("/file-resource") + public String fileResource() { + try { + return "get file resource success. content: " + StreamUtils.copyToString( + file.getInputStream(), Charset.forName(CharEncoding.UTF_8)); + } catch (Exception e) { + e.printStackTrace(); + return "get resource fail: " + e.getMessage(); + } + } + + @GetMapping("/download") + public String download() { + try { + OSSObject ossObject = ossClient.getObject(OssApplication.BUCKET_NAME, "oss-test.json"); + return "download success, content: " + IOUtils + .readStreamAsString(ossObject.getObjectContent(), CharEncoding.UTF_8); + } catch (Exception e) { + e.printStackTrace(); + return "download fail: " + e.getMessage(); + } + } + +} From 08f317bb783e37306cfb5ddc53b201cb151dc962 Mon Sep 17 00:00:00 2001 From: Xiaolong Zuo <150349407@qq.com> Date: Mon, 29 Oct 2018 10:05:34 +0800 Subject: [PATCH 21/63] Delete OSSApplication.java --- .../cloud/examples/OSSApplication.java | 49 ------------------- 1 file changed, 49 deletions(-) delete mode 100644 spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OSSApplication.java diff --git a/spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OSSApplication.java b/spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OSSApplication.java deleted file mode 100644 index f9265800d..000000000 --- a/spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OSSApplication.java +++ /dev/null @@ -1,49 +0,0 @@ -package org.springframework.cloud.alibaba.cloud.examples; - -import com.aliyun.oss.OSS; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.ApplicationArguments; -import org.springframework.boot.ApplicationRunner; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.annotation.Bean; - -import java.net.URISyntaxException; - -/** - * OSS Application - * - * @author Jim - */ -@SpringBootApplication -public class OssApplication { - - public static final String BUCKET_NAME = "spring-cloud-alibaba-test"; - - public static void main(String[] args) throws URISyntaxException { - SpringApplication.run(OssApplication.class, args); - } - - @Bean - public AppRunner appRunner() { - return new AppRunner(); - } - - class AppRunner implements ApplicationRunner { - @Autowired - private OSS ossClient; - - @Override - public void run(ApplicationArguments args) throws Exception { - try { - if (!ossClient.doesBucketExist(BUCKET_NAME)) { - ossClient.createBucket(BUCKET_NAME); - } - } catch (Exception e) { - System.err.println("oss handle bucket error: " + e.getMessage()); - System.exit(-1); - } - } - } - -} From a598c64694948691ce56777e5251773e615acbf2 Mon Sep 17 00:00:00 2001 From: Xiaolong Zuo <150349407@qq.com> Date: Mon, 29 Oct 2018 10:05:48 +0800 Subject: [PATCH 22/63] Delete OSSController.java --- .../alibaba/cloud/examples/OSSController.java | 65 ------------------- 1 file changed, 65 deletions(-) delete mode 100644 spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OSSController.java diff --git a/spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OSSController.java b/spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OSSController.java deleted file mode 100644 index 7c6e427a9..000000000 --- a/spring-cloud-alibaba-examples/oss-example/src/main/java/org/springframework/cloud/alibaba/cloud/examples/OSSController.java +++ /dev/null @@ -1,65 +0,0 @@ -package org.springframework.cloud.alibaba.cloud.examples; - -import com.aliyun.oss.OSS; -import com.aliyun.oss.common.utils.IOUtils; -import com.aliyun.oss.model.OSSObject; -import org.apache.commons.codec.CharEncoding; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.core.io.Resource; -import org.springframework.util.StreamUtils; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; - -import java.nio.charset.Charset; - -/** - * OSS Controller - * - * @author Jim - */ -@RestController -public class OssController { - - @Autowired - private OSS ossClient; - - @Value("oss://" + OssApplication.BUCKET_NAME + "/oss-test.json") - private Resource file; - - @GetMapping("/upload") - public String upload() { - try { - ossClient.putObject(OssApplication.BUCKET_NAME, "oss-test.json", this - .getClass().getClassLoader().getResourceAsStream("oss-test.json")); - } catch (Exception e) { - e.printStackTrace(); - return "upload fail: " + e.getMessage(); - } - return "upload success"; - } - - @GetMapping("/file-resource") - public String fileResource() { - try { - return "get file resource success. content: " + StreamUtils.copyToString( - file.getInputStream(), Charset.forName(CharEncoding.UTF_8)); - } catch (Exception e) { - e.printStackTrace(); - return "get resource fail: " + e.getMessage(); - } - } - - @GetMapping("/download") - public String download() { - try { - OSSObject ossObject = ossClient.getObject(OssApplication.BUCKET_NAME, "oss-test.json"); - return "download success, content: " + IOUtils - .readStreamAsString(ossObject.getObjectContent(), CharEncoding.UTF_8); - } catch (Exception e) { - e.printStackTrace(); - return "download fail: " + e.getMessage(); - } - } - -} From 7889c43ed96a1804ec19766d81a2324c3d007307 Mon Sep 17 00:00:00 2001 From: Xiaolong Zuo <150349407@qq.com> Date: Mon, 29 Oct 2018 11:09:00 +0800 Subject: [PATCH 23/63] Detele diamond health check. --- .../cloud/alicloud/acm/bootstrap/AcmPropertySourceLocator.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceLocator.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceLocator.java index 8c08e1baf..9d5eb472f 100644 --- a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceLocator.java +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceLocator.java @@ -50,8 +50,6 @@ public class AcmPropertySourceLocator implements PropertySourceLocator { @Override public PropertySource locate(Environment environment) { - checkDiamondHealth(); - String applicationName = environment.getProperty("spring.application.name"); logger.info("Initialize spring.application.name '" + applicationName + "'."); String applicationGroup = environment.getProperty("spring.application.group"); From e13ea1bf318d6ace854c7250152669f34248330d Mon Sep 17 00:00:00 2001 From: flystar32 Date: Mon, 29 Oct 2018 15:48:19 +0800 Subject: [PATCH 24/63] do some housekeeping --- .../src/main/resources/bootstrap.properties | 3 +-- .../nacos-example/nacos-config-example/readme-zh.md | 6 +++--- .../nacos-example/nacos-config-example/readme.md | 4 ++-- .../nacos-example/nacos-discovery-example/readme-zh.md | 2 +- .../nacos-example/nacos-discovery-example/readme.md | 2 +- .../alibaba/nacos/client/NacosPropertySourceBuilder.java | 3 +-- spring-cloud-alicloud-context/pom.xml | 7 +++++++ .../alicloud/context/AliCloudSpringApplicationTests.java | 4 ++-- 8 files changed, 18 insertions(+), 13 deletions(-) diff --git a/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/resources/bootstrap.properties b/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/resources/bootstrap.properties index dceeba95a..448a0c7dd 100644 --- a/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/resources/bootstrap.properties +++ b/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/resources/bootstrap.properties @@ -2,5 +2,4 @@ spring.application.group=com.alibaba.acm spring.application.name=acm-local server.port=18089 spring.cloud.alicloud.acm.server-list=127.0.0.1 -spring.cloud.alicloud.acm.server-port=8080 - +spring.cloud.alicloud.acm.server-port=8080 \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md index bef4f89d1..b9ec18f02 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md @@ -118,9 +118,9 @@ Nacos Client 从 Nacos Server 端获取数据时,调用的是此接口 `Config * `spring.active.profile` 即为当前环境对应的 profile,详情可以参考 [Spring Boot文档](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html#boot-features-profiles) - **注意,当 activeprofile 为空时,对应的连接符 `-` 也将不存在,dataId 的拼接格式变成 `${prefix}`.`${context.type}`** + **注意,当 activeprofile 为空时,对应的连接符 `-` 也将不存在,dataId 的拼接格式变成 `${prefix}`.`${file-extension}`** -* `file-extension` 为配置内容的数据格式,可以通过配置项 `spring.cloud.nacos.config.content-type`来配置。 +* `file-extension` 为配置内容的数据格式,可以通过配置项 `spring.cloud.nacos.config.file-extension`来配置。 目前只支持 `properties` 类型。 #### group @@ -168,7 +168,7 @@ Spring Boot 2.x 可以通过访问 http://127.0.0.1:18084/actuator/nacos-config 服务端地址|spring.cloud.nacos.config.server-addr|| DataId前缀|spring.cloud.nacos.config.prefix||spring.application.name Group|spring.cloud.nacos.config.group|DEFAULT_GROUP| -dataID后缀及数据格式|spring.cloud.nacos.config.file-extension|properties|目前只支持 properties +dataID后缀及内容文件格式|spring.cloud.nacos.config.file-extension|properties|dataId的后缀,同时也是配置内容的文件格式,目前只支持 properties 配置内容的编码方式|spring.cloud.nacos.config.encode|UTF-8|配置的编码 获取配置的超时时间|spring.cloud.nacos.config.timeout|3000|单位为 ms 配置的命名空间|spring.cloud.nacos.config.namespace||常用场景之一是不同环境的配置的区分隔离,例如开发测试环境和生产环境的资源隔离等。 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md index 05bda7fc7..a2f8f3e8f 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md @@ -119,9 +119,9 @@ In Nacos Config Starter, the splicing format of dataId is as follows * `spring.active.profile` is the profile corresponding to the current environment. For details, please refer to [Spring Boot Doc](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html#boot-features-profiles) - **Note: when the activeprofile is empty, the corresponding connector `-` will also not exist, and the splicing format of the dataId becomes `${prefix}`.`${context.type}`** + **Note: when the activeprofile is empty, the corresponding connector `-` will also not exist, and the splicing format of the dataId becomes `${prefix}`.`${file-extension}`** -* `file-extension` is the data format of the configuration content, which can be configured by the configuration item `spring.cloud.nacos.config.content-type`. +* `file-extension` is the data format of the configuration content, which can be configured by the configuration item `spring.cloud.nacos.config.file-extension`. Currently only the `properties` type is supported. #### group diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme-zh.md b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme-zh.md index a04451d36..47b431792 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme-zh.md +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme-zh.md @@ -197,7 +197,7 @@ AccessKey|spring.cloud.nacos.discovery.access-key|| SecretKey|spring.cloud.nacos.discovery.secret-key|| Metadata|spring.cloud.nacos.discovery.metadata||使用Map格式配置 日志文件名|spring.cloud.nacos.discovery.log-name|| -接入点|spring.cloud.nacos.discovery.enpoint|UTF-8|地域的某个服务的入口域名,通过此域名可以动态地拿到服务端地址 +接入点|spring.cloud.nacos.discovery.endpoint|UTF-8|地域的某个服务的入口域名,通过此域名可以动态地拿到服务端地址 是否集成Ribbon|ribbon.nacos.enabled|true| diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme.md b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme.md index cd26ef587..da81f49b7 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme.md +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/readme.md @@ -205,7 +205,7 @@ AccessKey|spring.cloud.nacos.discovery.access-key|| SecretKey|spring.cloud.nacos.discovery.secret-key|| Metadata|spring.cloud.nacos.discovery.metadata||Extended data, Configure using Map format log name|spring.cloud.nacos.discovery.log-name|| -endpoint|spring.cloud.nacos.discovery.enpoint||The domain name of a service, through which the server address can be dynamically obtained. +endpoint|spring.cloud.nacos.discovery.endpoint||The domain name of a service, through which the server address can be dynamically obtained. Integration Ribbon|ribbon.nacos.enabled|true| diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java index 4e5d63b67..16bf97d09 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceBuilder.java @@ -83,7 +83,6 @@ public class NacosPropertySourceBuilder { String data = null; try { data = configService.getConfig(dataId, group, timeout); - // todo add content type yaml support if (!StringUtils.isEmpty(data)) { logger.info(String.format("Loading nacos data, dataId: '%s', group: '%s'", dataId, group)); @@ -95,7 +94,7 @@ public class NacosPropertySourceBuilder { return properties; } else if (fileExtension.equalsIgnoreCase("yaml") - || fileExtension.equalsIgnoreCase("yml")) { + || fileExtension.equalsIgnoreCase("yml")) { YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean(); yamlFactory.setResources(new ByteArrayResource(data.getBytes())); return yamlFactory.getObject(); diff --git a/spring-cloud-alicloud-context/pom.xml b/spring-cloud-alicloud-context/pom.xml index 0084d0910..e7cbf206e 100644 --- a/spring-cloud-alicloud-context/pom.xml +++ b/spring-cloud-alicloud-context/pom.xml @@ -82,6 +82,13 @@ true + + org.springframework.boot + spring-boot-configuration-processor + provided + true + + org.springframework.boot spring-boot-starter-test diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudSpringApplicationTests.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudSpringApplicationTests.java index b40edd268..636adf64e 100644 --- a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudSpringApplicationTests.java +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudSpringApplicationTests.java @@ -29,7 +29,7 @@ import org.springframework.test.context.junit4.SpringRunner; * @author xiaolongzuo */ @RunWith(SpringRunner.class) -@SpringBootTest(classes = AliCloudSpringApplicationTests.EurekaClientDisabledApp.class, properties = { +@SpringBootTest(classes = AliCloudSpringApplicationTests.AliCloudDisabledApp.class, properties = { "spring.application.name=myapp", "spring.cloud.alicloud.edas.application.name=myapp", "spring.cloud.alicloud.access-key=ak", "spring.cloud.alicloud.secret-key=sk", @@ -43,7 +43,7 @@ public class AliCloudSpringApplicationTests { } @SpringBootApplication - public static class EurekaClientDisabledApp { + public static class AliCloudDisabledApp { } From 6436d38f824e1d5693c487c6c1975fe6f6bcdfbc Mon Sep 17 00:00:00 2001 From: "chenzhu.zxl" Date: Mon, 29 Oct 2018 18:09:23 +0800 Subject: [PATCH 25/63] fixes #71. --- .../bootstrap/AcmPropertySourceLocator.java | 85 ++--------------- .../acm/AcmContextBootstrapConfiguration.java | 28 ++++++ .../context/acm/AcmIntegrationProperties.java | 91 +++++++++++++++++++ .../context/acm/AnsPropertiesTests.java | 78 +++++++++++++--- 4 files changed, 191 insertions(+), 91 deletions(-) create mode 100644 spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmIntegrationProperties.java diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceLocator.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceLocator.java index 9d5eb472f..a7c167619 100644 --- a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceLocator.java +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/bootstrap/AcmPropertySourceLocator.java @@ -16,20 +16,12 @@ package org.springframework.cloud.alicloud.acm.bootstrap; -import com.taobao.diamond.maintenance.DiamondHealth; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cloud.alicloud.acm.diagnostics.analyzer.DiamondConnectionFailureException; -import org.springframework.cloud.alicloud.context.acm.AcmProperties; +import org.springframework.cloud.alicloud.context.acm.AcmIntegrationProperties; import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.Environment; import org.springframework.core.env.PropertySource; -import org.springframework.util.StringUtils; - -import static com.taobao.diamond.client.impl.ServerHttpAgent.addressPort; -import static com.taobao.diamond.client.impl.ServerHttpAgent.domainName; /** * @author juven.xuxb @@ -37,8 +29,6 @@ import static com.taobao.diamond.client.impl.ServerHttpAgent.domainName; */ public class AcmPropertySourceLocator implements PropertySourceLocator { - private final Logger logger = LoggerFactory.getLogger(AcmPropertySourceLocator.class); - private static final String DIAMOND_PROPERTY_SOURCE_NAME = "diamond"; private static String defaultDiamondGroup = "DEFAULT_GROUP"; @@ -46,87 +36,26 @@ public class AcmPropertySourceLocator implements PropertySourceLocator { private AcmPropertySourceBuilder acmPropertySourceBuilder = new AcmPropertySourceBuilder(); @Autowired - private AcmProperties acmProperties; + private AcmIntegrationProperties acmIntegrationProperties; @Override public PropertySource locate(Environment environment) { - String applicationName = environment.getProperty("spring.application.name"); - logger.info("Initialize spring.application.name '" + applicationName + "'."); - String applicationGroup = environment.getProperty("spring.application.group"); - - if (StringUtils.isEmpty(applicationName)) { - throw new IllegalStateException( - "'spring.application.name' must be configured."); - } CompositePropertySource compositePropertySource = new CompositePropertySource( DIAMOND_PROPERTY_SOURCE_NAME); - loadGroupConfigurationRecursively(compositePropertySource, applicationGroup); - - loadApplicationConfiguration(compositePropertySource, environment, - applicationGroup, applicationName); - - return compositePropertySource; - } - - private void checkDiamondHealth() { - logger.info("Checking ACM health"); - try { - if (!"UP".equals(DiamondHealth.getHealth())) { - throw new DiamondConnectionFailureException(domainName, addressPort, - DiamondHealth.getHealth()); - } - } - catch (Throwable t) { - throw new DiamondConnectionFailureException(domainName, addressPort, - "ACM Health error", t); - } - } - - private void loadGroupConfigurationRecursively( - CompositePropertySource compositePropertySource, String applicationGroup) { - if (StringUtils.isEmpty(applicationGroup)) { - return; - } - String[] parts = applicationGroup.split("\\."); - for (int i = 1; i < parts.length; i++) { - String subGroup = parts[0]; - for (int j = 1; j <= i; j++) { - subGroup = subGroup + "." + parts[j]; - } - String dataId = subGroup + ":application." + acmProperties.getFileExtension(); + for (String dataId : acmIntegrationProperties.getGroupConfigurationDataIds()) { loadDiamondDataIfPresent(compositePropertySource, dataId, defaultDiamondGroup, true); } - } - - private void loadApplicationConfiguration( - CompositePropertySource compositePropertySource, Environment environment, - String applicationGroup, String applicationName) { - if (!StringUtils.isEmpty(applicationGroup)) { - String dataId = applicationGroup + ":" + applicationName + "." - + acmProperties.getFileExtension(); - loadDiamondDataIfPresent(compositePropertySource, dataId, defaultDiamondGroup, - false); - for (String profile : environment.getActiveProfiles()) { - dataId = applicationGroup + ":" + applicationName + "-" + profile + "." - + acmProperties.getFileExtension(); - loadDiamondDataIfPresent(compositePropertySource, dataId, - defaultDiamondGroup, false); - } - - } - String dataId = applicationName + "." + acmProperties.getFileExtension(); - loadDiamondDataIfPresent(compositePropertySource, dataId, defaultDiamondGroup, - false); - for (String profile : environment.getActiveProfiles()) { - dataId = applicationName + "-" + profile + "." - + acmProperties.getFileExtension(); + for (String dataId : acmIntegrationProperties + .getApplicationConfigurationDataIds()) { loadDiamondDataIfPresent(compositePropertySource, dataId, defaultDiamondGroup, false); } + + return compositePropertySource; } private void loadDiamondDataIfPresent(final CompositePropertySource composite, diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmContextBootstrapConfiguration.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmContextBootstrapConfiguration.java index 772724420..32fd47da9 100644 --- a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmContextBootstrapConfiguration.java +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmContextBootstrapConfiguration.java @@ -16,6 +16,8 @@ package org.springframework.cloud.alicloud.context.acm; +import static org.springframework.core.env.AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME; + import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; @@ -25,7 +27,11 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties import org.springframework.cloud.alicloud.context.AliCloudProperties; import org.springframework.cloud.alicloud.context.edas.EdasContextAutoConfiguration; import org.springframework.cloud.alicloud.context.edas.EdasProperties; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.env.Environment; +import org.springframework.util.Assert; +import org.springframework.util.StringUtils; import com.alibaba.cloud.context.acm.AliCloudAcmInitializer; @@ -47,10 +53,32 @@ public class AcmContextBootstrapConfiguration { @Autowired private AliCloudProperties aliCloudProperties; + @Autowired + private Environment environment; + @PostConstruct public void initAcmProperties() { AliCloudAcmInitializer.initialize(aliCloudProperties, edasProperties, acmProperties); } + @Bean + public AcmIntegrationProperties acmIntegrationProperties() { + AcmIntegrationProperties acmIntegrationProperties = new AcmIntegrationProperties(); + String applicationName = environment.getProperty("spring.application.name"); + String applicationGroup = environment.getProperty("spring.application.group"); + String activeProfiles = environment.getProperty(ACTIVE_PROFILES_PROPERTY_NAME); + Assert.isTrue(!StringUtils.isEmpty(applicationName), + "'spring.application.name' must be configured.."); + acmIntegrationProperties.setApplicationName(applicationName); + acmIntegrationProperties.setApplicationGroup(applicationGroup); + if (StringUtils.hasText(activeProfiles)) { + acmIntegrationProperties + .setActiveProfiles(StringUtils.commaDelimitedListToStringArray( + StringUtils.trimAllWhitespace(activeProfiles))); + } + acmIntegrationProperties.setAcmProperties(acmProperties); + return acmIntegrationProperties; + } + } diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmIntegrationProperties.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmIntegrationProperties.java new file mode 100644 index 000000000..0ad6a6b4c --- /dev/null +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmIntegrationProperties.java @@ -0,0 +1,91 @@ +/* + * Copyright (C) 2018 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.cloud.alicloud.context.acm; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.util.StringUtils; + +/** + * @author xiaolongzuo + */ +public class AcmIntegrationProperties { + + private String applicationName; + + private String applicationGroup; + + private String[] activeProfiles = new String[0]; + + private AcmProperties acmProperties; + + public List getGroupConfigurationDataIds() { + List groupConfigurationDataIds = new ArrayList<>(); + if (StringUtils.isEmpty(applicationGroup)) { + return groupConfigurationDataIds; + } + String[] parts = applicationGroup.split("\\."); + for (int i = 1; i < parts.length; i++) { + StringBuilder subGroup = new StringBuilder(parts[0]); + for (int j = 1; j <= i; j++) { + subGroup.append(".").append(parts[j]); + } + groupConfigurationDataIds + .add(subGroup + ":application." + acmProperties.getFileExtension()); + } + return groupConfigurationDataIds; + } + + public List getApplicationConfigurationDataIds() { + List applicationConfigurationDataIds = new ArrayList<>(); + if (!StringUtils.isEmpty(applicationGroup)) { + applicationConfigurationDataIds.add(applicationGroup + ":" + applicationName + + "." + acmProperties.getFileExtension()); + for (String profile : activeProfiles) { + applicationConfigurationDataIds + .add(applicationGroup + ":" + applicationName + "-" + profile + + "." + acmProperties.getFileExtension()); + } + + } + applicationConfigurationDataIds + .add(applicationName + "." + acmProperties.getFileExtension()); + for (String profile : activeProfiles) { + applicationConfigurationDataIds.add(applicationName + "-" + profile + "." + + acmProperties.getFileExtension()); + } + return applicationConfigurationDataIds; + } + + public void setApplicationName(String applicationName) { + this.applicationName = applicationName; + } + + public void setApplicationGroup(String applicationGroup) { + this.applicationGroup = applicationGroup; + } + + public void setActiveProfiles(String[] activeProfiles) { + this.activeProfiles = activeProfiles; + } + + public void setAcmProperties(AcmProperties acmProperties) { + this.acmProperties = acmProperties; + } + +} diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/acm/AnsPropertiesTests.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/acm/AnsPropertiesTests.java index 9e278aca9..8a1f473ab 100644 --- a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/acm/AnsPropertiesTests.java +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/acm/AnsPropertiesTests.java @@ -39,23 +39,26 @@ public class AnsPropertiesTests { @Test public void testConfigurationValueDefaultsAreAsExpected() { - this.contextRunner.withPropertyValues().run(context -> { - AcmProperties config = context.getBean(AcmProperties.class); - assertThat(config.getServerMode()).isEqualTo(AliCloudServerMode.LOCAL); - assertThat(config.getServerList()).isEqualTo("127.0.0.1"); - assertThat(config.getServerPort()).isEqualTo("8080"); - assertThat(config.getEndpoint()).isNull(); - assertThat(config.getFileExtension()).isEqualTo("properties"); - assertThat(config.getGroup()).isEqualTo("DEFAULT_GROUP"); - assertThat(config.getNamespace()).isNull(); - assertThat(config.getRamRoleName()).isNull(); - assertThat(config.getTimeout()).isEqualTo(3000); - }); + this.contextRunner.withPropertyValues("spring.application.name=myapp") + .run(context -> { + AcmProperties config = context.getBean(AcmProperties.class); + assertThat(config.getServerMode()) + .isEqualTo(AliCloudServerMode.LOCAL); + assertThat(config.getServerList()).isEqualTo("127.0.0.1"); + assertThat(config.getServerPort()).isEqualTo("8080"); + assertThat(config.getEndpoint()).isNull(); + assertThat(config.getFileExtension()).isEqualTo("properties"); + assertThat(config.getGroup()).isEqualTo("DEFAULT_GROUP"); + assertThat(config.getNamespace()).isNull(); + assertThat(config.getRamRoleName()).isNull(); + assertThat(config.getTimeout()).isEqualTo(3000); + }); } @Test public void testConfigurationValuesAreCorrectlyLoaded() { - this.contextRunner.withPropertyValues("spring.cloud.alicloud.access-key=ak", + this.contextRunner.withPropertyValues("spring.application.name=myapp", + "spring.cloud.alicloud.access-key=ak", "spring.cloud.alicloud.secret-key=sk", "spring.cloud.alicloud.acm.server-mode=EDAS", "spring.cloud.alicloud.acm.server-port=11111", @@ -75,4 +78,53 @@ public class AnsPropertiesTests { }); } + @Test + public void testAcmIntegrationConfigurationValuesAreCorrectlyLoaded() { + this.contextRunner.withPropertyValues("spring.application.name=myapp", + "spring.application.group=com.alicloud.test", + "spring.cloud.alicloud.access-key=ak", + "spring.cloud.alicloud.secret-key=sk", + "spring.cloud.alicloud.acm.server-mode=EDAS", + "spring.cloud.alicloud.acm.server-port=11111", + "spring.cloud.alicloud.acm.server-list=10.10.10.10", + "spring.cloud.alicloud.acm.namespace=testNamespace", + "spring.cloud.alicloud.acm.endpoint=testDomain", + "spring.cloud.alicloud.acm.group=testGroup", + "spring.cloud.alicloud.acm.file-extension=yaml").run(context -> { + AcmIntegrationProperties acmIntegrationProperties = context + .getBean(AcmIntegrationProperties.class); + assertThat(acmIntegrationProperties.getGroupConfigurationDataIds() + .size()).isEqualTo(2); + assertThat(acmIntegrationProperties + .getApplicationConfigurationDataIds().size()).isEqualTo(2); + System.out.println("-----" + + acmIntegrationProperties.getGroupConfigurationDataIds()); + System.out.println(acmIntegrationProperties + .getApplicationConfigurationDataIds()); + }); + } + + @Test + public void testAcmIntegrationConfigurationValuesAreCorrectlyLoaded2() { + this.contextRunner.withPropertyValues("spring.application.name=myapp", + "spring.application.group=com.alicloud.test", + "spring.profiles.active=profile1,profile2", + "spring.cloud.alicloud.access-key=ak", + "spring.cloud.alicloud.secret-key=sk", + "spring.cloud.alicloud.acm.server-mode=EDAS", + "spring.cloud.alicloud.acm.server-port=11111", + "spring.cloud.alicloud.acm.server-list=10.10.10.10", + "spring.cloud.alicloud.acm.namespace=testNamespace", + "spring.cloud.alicloud.acm.endpoint=testDomain", + "spring.cloud.alicloud.acm.group=testGroup", + "spring.cloud.alicloud.acm.file-extension=yaml").run(context -> { + AcmIntegrationProperties acmIntegrationProperties = context + .getBean(AcmIntegrationProperties.class); + assertThat(acmIntegrationProperties.getGroupConfigurationDataIds() + .size()).isEqualTo(2); + assertThat(acmIntegrationProperties + .getApplicationConfigurationDataIds().size()).isEqualTo(6); + }); + } + } From 45632ed5a293d1bde52220d5660d4e73c703b9da Mon Sep 17 00:00:00 2001 From: flystar32 Date: Mon, 29 Oct 2018 22:16:15 +0800 Subject: [PATCH 26/63] put all nacos config metadata to bootstrap --- .../src/main/resources/application.properties | 1 - .../src/main/resources/bootstrap.properties | 1 + .../nacos/NacosConfigAutoConfiguration.java | 16 +---- .../NacosConfigBootstrapConfiguration.java | 2 + .../alibaba/nacos/NacosConfigProperties.java | 65 ++++++++++++++++--- .../client/NacosPropertySourceLocator.java | 41 +----------- .../NacosConfigEndpointAutoConfiguration.java | 14 +--- .../NacosConfigAutoConfigurationTests.java | 10 +-- ...acosConfigBootstrapConfigurationTests.java | 11 ++-- 9 files changed, 71 insertions(+), 90 deletions(-) diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/application.properties index a0e934e2a..c6e216ad7 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/application.properties +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/application.properties @@ -1,3 +1,2 @@ -spring.application.name=nacos-config-example server.port=18084 management.endpoints.web.exposure.include=* \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/bootstrap.properties b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/bootstrap.properties index 8d94d562d..6adb653ca 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/bootstrap.properties +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/src/main/resources/bootstrap.properties @@ -1 +1,2 @@ +spring.application.name=nacos-config-example spring.cloud.nacos.config.server-addr=127.0.0.1:8848 \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfiguration.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfiguration.java index f08969121..5dbdb50af 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfiguration.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfiguration.java @@ -16,8 +16,6 @@ package org.springframework.cloud.alibaba.nacos; -import com.alibaba.nacos.api.config.ConfigService; - import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cloud.alibaba.nacos.refresh.NacosContextRefresher; @@ -43,14 +41,6 @@ public class NacosConfigAutoConfiguration implements ApplicationContextAware { @Autowired private NacosRefreshProperties nacosRefreshProperties; - @Autowired - private ConfigService configService; - - @Bean - public NacosConfigProperties nacosConfigProperties() { - return new NacosConfigProperties(); - } - @Bean public NacosPropertySourceRepository nacosPropertySourceRepository() { return new NacosPropertySourceRepository(applicationContext); @@ -69,10 +59,10 @@ public class NacosConfigAutoConfiguration implements ApplicationContextAware { @Bean public NacosContextRefresher nacosContextRefresher(ContextRefresher contextRefresher, NacosRefreshHistory refreshHistory, - NacosPropertySourceRepository propertySourceRepository, - ConfigService configService) { + NacosPropertySourceRepository propertySourceRepository) { return new NacosContextRefresher(contextRefresher, nacosConfigProperties, - nacosRefreshProperties, refreshHistory, propertySourceRepository,configService); + nacosRefreshProperties, refreshHistory, propertySourceRepository, + nacosConfigProperties.configServiceInstance()); } @Override diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfiguration.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfiguration.java index d5e3bd3b8..e643d35a8 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfiguration.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfiguration.java @@ -16,6 +16,7 @@ package org.springframework.cloud.alibaba.nacos; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.cloud.alibaba.nacos.client.NacosPropertySourceLocator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -32,6 +33,7 @@ public class NacosConfigBootstrapConfiguration { } @Bean + @ConditionalOnMissingBean public NacosConfigProperties nacosConfigProperties() { return new NacosConfigProperties(); } diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java index 446483d69..28b57b933 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java @@ -16,10 +16,27 @@ package org.springframework.cloud.alibaba.nacos; +import java.util.Properties; + +import com.alibaba.nacos.api.NacosFactory; +import com.alibaba.nacos.api.config.ConfigService; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.core.env.Environment; import org.springframework.util.StringUtils; +import static com.alibaba.nacos.api.PropertyKeyConst.ACCESS_KEY; +import static com.alibaba.nacos.api.PropertyKeyConst.CLUSTER_NAME; +import static com.alibaba.nacos.api.PropertyKeyConst.CONTEXT_PATH; +import static com.alibaba.nacos.api.PropertyKeyConst.ENCODE; +import static com.alibaba.nacos.api.PropertyKeyConst.ENDPOINT; +import static com.alibaba.nacos.api.PropertyKeyConst.NAMESPACE; +import static com.alibaba.nacos.api.PropertyKeyConst.SECRET_KEY; +import static com.alibaba.nacos.api.PropertyKeyConst.SERVER_ADDR; + /** * nacos properties * @@ -29,15 +46,18 @@ import org.springframework.util.StringUtils; @ConfigurationProperties("spring.cloud.nacos.config") public class NacosConfigProperties { + private static final Logger LOGGER = LoggerFactory + .getLogger(NacosConfigProperties.class); + /** * nacos config server address */ - private String serverAddr; + private String serverAddr = ""; /** * encode for nacos config content. */ - private String encode; + private String encode = ""; /** * nacos config group, group is config data meta info. @@ -47,7 +67,7 @@ public class NacosConfigProperties { /** * nacos config dataId prefix */ - private String prefix; + private String prefix = ""; /** * the suffix of nacos config dataId, also the file extension of config content. */ @@ -62,32 +82,34 @@ public class NacosConfigProperties { * endpoint for Nacos, the domain name of a service, through which the server address * can be dynamically obtained. */ - private String endpoint; + private String endpoint = ""; /** * namespace, separation configuration of different environments. */ - private String namespace; + private String namespace = ""; /** * access key for namespace. */ - private String accessKey; + private String accessKey = ""; /** * secret key for namespace. */ - private String secretKey; + private String secretKey = ""; /** * context path for nacos config server. */ - private String contextPath; + private String contextPath = ""; /** * nacos config cluster name */ - private String clusterName; + private String clusterName = ""; + + private ConfigService configService; // todo sts support @@ -237,4 +259,29 @@ public class NacosConfigProperties { env.resolvePlaceholders("${spring.cloud.nacos.config.prefix:}")); } } + + public ConfigService configServiceInstance() { + + if (null != configService) { + return configService; + } + + Properties properties = new Properties(); + properties.put(SERVER_ADDR, this.serverAddr); + properties.put(ENCODE, this.encode); + properties.put(NAMESPACE, this.namespace); + properties.put(ACCESS_KEY, this.accessKey); + properties.put(SECRET_KEY, this.secretKey); + properties.put(CONTEXT_PATH, this.contextPath); + properties.put(CLUSTER_NAME, this.clusterName); + properties.put(ENDPOINT, this.endpoint); + try { + configService = NacosFactory.createConfigService(properties); + return configService; + } + catch (Exception e) { + LOGGER.error("create config service error!properties={},e=,", this, e); + return null; + } + } } diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java index f507f1522..e59d40446 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java @@ -16,12 +16,9 @@ package org.springframework.cloud.alibaba.nacos.client; -import java.util.Properties; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.cloud.alibaba.nacos.NacosConfigProperties; import org.springframework.cloud.bootstrap.config.PropertySourceLocator; import org.springframework.core.annotation.Order; @@ -30,11 +27,7 @@ import org.springframework.core.env.Environment; import org.springframework.core.env.PropertySource; import org.springframework.util.StringUtils; -import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.config.ConfigService; -import com.alibaba.nacos.api.exception.NacosException; - -import static com.alibaba.nacos.api.PropertyKeyConst.*; /** * @author xiaojing @@ -48,47 +41,15 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { private static final String SEP1 = "-"; private static final String DOT = "."; - @Autowired - private ConfigurableListableBeanFactory beanFactory; - @Autowired private NacosConfigProperties nacosConfigProperties; - private ConfigService configService; - private NacosPropertySourceBuilder nacosPropertySourceBuilder; - private Properties getPropertiesFromEnv(Environment env) { - - nacosConfigProperties.overrideFromEnv(env); - - Properties properties = new Properties(); - properties.put(SERVER_ADDR, nacosConfigProperties.getServerAddr()); - properties.put(ENCODE, nacosConfigProperties.getEncode()); - properties.put(NAMESPACE, nacosConfigProperties.getNamespace()); - properties.put(ACCESS_KEY, nacosConfigProperties.getAccessKey()); - properties.put(SECRET_KEY, nacosConfigProperties.getSecretKey()); - properties.put(CONTEXT_PATH, nacosConfigProperties.getContextPath()); - properties.put(CLUSTER_NAME, nacosConfigProperties.getClusterName()); - properties.put(ENDPOINT, nacosConfigProperties.getEndpoint()); - return properties; - } - @Override public PropertySource locate(Environment env) { - Properties properties = getPropertiesFromEnv(env); - - try { - configService = NacosFactory.createConfigService(properties); - } - catch (NacosException e) { - logger.error("create config service error, nacosConfigProperties:{}, ", - properties, e); - return null; - } - - beanFactory.registerSingleton("configService", configService); + ConfigService configService = nacosConfigProperties.configServiceInstance(); if (null == configService) { logger.warn( diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigEndpointAutoConfiguration.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigEndpointAutoConfiguration.java index d6d8464bb..c0d92632f 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigEndpointAutoConfiguration.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/endpoint/NacosConfigEndpointAutoConfiguration.java @@ -16,8 +16,6 @@ package org.springframework.cloud.alibaba.nacos.endpoint; -import com.alibaba.nacos.api.config.ConfigService; - import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint; import org.springframework.boot.actuate.endpoint.annotation.Endpoint; @@ -46,15 +44,6 @@ public class NacosConfigEndpointAutoConfiguration { @Autowired private NacosPropertySourceRepository nacosPropertySourceRepository; - @Autowired - private ConfigService configService; - - @Bean - @ConditionalOnBean - public NacosConfigProperties nacosConfigProperties() { - return new NacosConfigProperties(); - } - @ConditionalOnMissingBean @ConditionalOnEnabledEndpoint @Bean @@ -67,6 +56,7 @@ public class NacosConfigEndpointAutoConfiguration { public NacosConfigHealthIndicator nacosConfigHealthIndicator( NacosPropertySourceRepository nacosPropertySourceRepository) { return new NacosConfigHealthIndicator(nacosConfigProperties, - nacosPropertySourceRepository, configService); + nacosPropertySourceRepository, + nacosConfigProperties.configServiceInstance()); } } diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java index 389e4fd20..7e287c0a8 100644 --- a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java +++ b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java @@ -63,15 +63,7 @@ public class NacosConfigAutoConfigurationTests { @Test public void testNacosConfigProperties() { - NacosPropertySourceLocator nacosPropertySourceLocator = this.context.getBean(NacosPropertySourceLocator.class); - Environment environment = this.context.getEnvironment(); - try{ - nacosPropertySourceLocator.locate(environment); - }catch (Exception e){ - - } - - NacosConfigProperties nacosConfigProperties = this.context.getBean(NacosConfigProperties.class); + NacosConfigProperties nacosConfigProperties = this.context.getParent().getBean(NacosConfigProperties.class); assertThat(nacosConfigProperties.getFileExtension()).isEqualTo("properties"); assertThat(nacosConfigProperties.getPrefix()).isEqualTo("myapp"); diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfigurationTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfigurationTests.java index ae43f03f7..6682e8508 100644 --- a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfigurationTests.java +++ b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfigurationTests.java @@ -68,15 +68,14 @@ public class NacosConfigBootstrapConfigurationTests { } - Field configServiceField = ReflectionUtils - .findField(NacosPropertySourceLocator.class, "configService"); - configServiceField.setAccessible(true); + Field nacosConfigPropertiesField = ReflectionUtils + .findField(NacosPropertySourceLocator.class, "nacosConfigProperties"); + nacosConfigPropertiesField.setAccessible(true); - ConfigService configService = (ConfigService) ReflectionUtils - .getField(configServiceField, locator); + NacosConfigProperties configService = (NacosConfigProperties) ReflectionUtils + .getField(nacosConfigPropertiesField, locator); assertThat(configService).isNotNull(); } - } From e9b99ba547b67e0d07fe2c1bc51a5fcfb2fa3ffb Mon Sep 17 00:00:00 2001 From: flystar32 Date: Mon, 29 Oct 2018 22:20:25 +0800 Subject: [PATCH 27/63] update readme of nacos config example --- .../nacos-example/nacos-config-example/readme-zh.md | 9 +++++---- .../nacos-example/nacos-config-example/readme.md | 9 +++++---- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md index b9ec18f02..e7339058c 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme-zh.md @@ -19,9 +19,10 @@ spring-cloud-starter-alibaba-nacos-config -2. 在应用的 /src/main/resources/bootstrap.properties 配置文件中配置 Nacos Config 地址 +2. 在应用的 /src/main/resources/bootstrap.properties 配置文件中配置 Nacos Config 元数据 - spring.cloud.nacos.config.server-addr=127.0.0.1:8848 + spring.application.name=nacos-config-example + spring.cloud.nacos.config.server-addr=127.0.0.1:8848 3. 完成上述两步后,应用会从 Nacos Config 中获取相应的配置,并添加在 Spring Environment 的 PropertySources 中。这里我们使用 @Value 注解来将对应的配置注入到 SampleController 的 userName 和 age 字段,并添加 @RefreshScope 打开动态刷新功能 @@ -70,8 +71,8 @@ 1. 增加配置,在应用的 /src/main/resources/application.properties 中添加基本配置信息 - spring.application.name=nacos-config-example - server.port=18084 + server.port=18084 + management.endpoints.web.exposure.include=* 2. 启动应用,支持 IDE 直接启动和编译打包后启动。 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md index a2f8f3e8f..58b499aef 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/readme.md @@ -19,9 +19,10 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl spring-cloud-starter-alibaba-nacos-config -2. Add Nacos server address configurations to file /src/main/resources/bootstrap.properties +2. Add Nacos config metadata configurations to file /src/main/resources/bootstrap.properties - spring.cloud.nacos.config.server-addr=127.0.0.1:8848 + spring.application.name=nacos-config-example + spring.cloud.nacos.config.server-addr=127.0.0.1:8848 3. After completing the above two steps, the application will get the externalized configuration from Nacos Server and put it in the Spring Environment's PropertySources.We use the @Value annotation to inject the corresponding configuration into the userName and age fields of the SampleController, and add @RefreshScope to turn on dynamic refresh . @RefreshScope @@ -70,8 +71,8 @@ Before we start the demo, let's learn how to connect Nacos Config to a Spring Cl 1. Add necessary configurations to file /src/main/resources/application.properties - spring.application.name=nacos-config-example - server.port=18084 + server.port=18084 + management.endpoints.web.exposure.include=* 2. Start the application in IDE or by building a fatjar. From d134759b62970dc0400ab98ae550497f65eecf9c Mon Sep 17 00:00:00 2001 From: flystar32 Date: Mon, 29 Oct 2018 22:59:46 +0800 Subject: [PATCH 28/63] put all nacos config metadata to bootstrap --- .../alibaba/nacos/NacosConfigProperties.java | 104 ++++++++---------- .../client/NacosPropertySourceLocator.java | 15 +-- 2 files changed, 51 insertions(+), 68 deletions(-) diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java index 28b57b933..e279d9cbe 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/NacosConfigProperties.java @@ -16,17 +16,21 @@ package org.springframework.cloud.alibaba.nacos; +import java.util.Arrays; +import java.util.Objects; import java.util.Properties; +import javax.annotation.PostConstruct; + import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.config.ConfigService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.core.env.Environment; -import org.springframework.util.StringUtils; import static com.alibaba.nacos.api.PropertyKeyConst.ACCESS_KEY; import static com.alibaba.nacos.api.PropertyKeyConst.CLUSTER_NAME; @@ -52,12 +56,12 @@ public class NacosConfigProperties { /** * nacos config server address */ - private String serverAddr = ""; + private String serverAddr; /** * encode for nacos config content. */ - private String encode = ""; + private String encode; /** * nacos config group, group is config data meta info. @@ -67,7 +71,7 @@ public class NacosConfigProperties { /** * nacos config dataId prefix */ - private String prefix = ""; + private String prefix; /** * the suffix of nacos config dataId, also the file extension of config content. */ @@ -82,35 +86,48 @@ public class NacosConfigProperties { * endpoint for Nacos, the domain name of a service, through which the server address * can be dynamically obtained. */ - private String endpoint = ""; + private String endpoint; /** * namespace, separation configuration of different environments. */ - private String namespace = ""; + private String namespace; /** * access key for namespace. */ - private String accessKey = ""; + private String accessKey; /** * secret key for namespace. */ - private String secretKey = ""; + private String secretKey; /** * context path for nacos config server. */ - private String contextPath = ""; + private String contextPath; /** * nacos config cluster name */ - private String clusterName = ""; + private String clusterName; + + @Value("${spring.application.name}") + private String name; + + private String[] activeProfiles; private ConfigService configService; + @Autowired + private Environment environment; + + @PostConstruct + public void init() { + this.activeProfiles = environment.getActiveProfiles(); + } + // todo sts support public String getServerAddr() { @@ -209,6 +226,14 @@ public class NacosConfigProperties { this.clusterName = clusterName; } + public String getName() { + return name; + } + + public String[] getActiveProfiles() { + return activeProfiles; + } + @Override public String toString() { return "NacosConfigProperties{" + "serverAddr='" + serverAddr + '\'' @@ -217,47 +242,8 @@ public class NacosConfigProperties { + ", timeout=" + timeout + ", endpoint='" + endpoint + '\'' + ", namespace='" + namespace + '\'' + ", accessKey='" + accessKey + '\'' + ", secretKey='" + secretKey + '\'' + ", contextPath='" + contextPath - + '\'' + ", clusterName='" + clusterName + '\'' + '}'; - } - - public void overrideFromEnv(Environment env) { - - if (StringUtils.isEmpty(this.getServerAddr())) { - this.setServerAddr( - env.resolvePlaceholders("${spring.cloud.nacos.config.server-addr:}")); - } - if (StringUtils.isEmpty(this.getEncode())) { - this.setEncode( - env.resolvePlaceholders("${spring.cloud.nacos.config.encode:}")); - } - if (StringUtils.isEmpty(this.getNamespace())) { - this.setNamespace( - env.resolvePlaceholders("${spring.cloud.nacos.config.namespace:}")); - } - if (StringUtils.isEmpty(this.getAccessKey())) { - this.setAccessKey( - env.resolvePlaceholders("${spring.cloud.nacos.config.access-key:}")); - } - if (StringUtils.isEmpty(this.getSecretKey())) { - this.setSecretKey( - env.resolvePlaceholders("${spring.cloud.nacos.config.secret-key:}")); - } - if (StringUtils.isEmpty(this.getContextPath())) { - this.setContextPath(env - .resolvePlaceholders("${spring.cloud.nacos.config.context-path:}")); - } - if (StringUtils.isEmpty(this.getClusterName())) { - this.setClusterName(env - .resolvePlaceholders("${spring.cloud.nacos.config.cluster-name:}")); - } - if (StringUtils.isEmpty(this.getEndpoint())) { - this.setEndpoint( - env.resolvePlaceholders("${spring.cloud.nacos.config.endpoint:}")); - } - if (StringUtils.isEmpty(this.getPrefix())) { - this.setPrefix( - env.resolvePlaceholders("${spring.cloud.nacos.config.prefix:}")); - } + + '\'' + ", clusterName='" + clusterName + '\'' + ", name='" + name + '\'' + + ", activeProfiles=" + Arrays.toString(activeProfiles) + '}'; } public ConfigService configServiceInstance() { @@ -267,14 +253,14 @@ public class NacosConfigProperties { } Properties properties = new Properties(); - properties.put(SERVER_ADDR, this.serverAddr); - properties.put(ENCODE, this.encode); - properties.put(NAMESPACE, this.namespace); - properties.put(ACCESS_KEY, this.accessKey); - properties.put(SECRET_KEY, this.secretKey); - properties.put(CONTEXT_PATH, this.contextPath); - properties.put(CLUSTER_NAME, this.clusterName); - properties.put(ENDPOINT, this.endpoint); + properties.put(SERVER_ADDR, Objects.toString(this.serverAddr, "")); + properties.put(ENCODE, Objects.toString(this.encode, "")); + properties.put(NAMESPACE, Objects.toString(this.namespace, "")); + properties.put(ACCESS_KEY, Objects.toString(this.accessKey, "")); + properties.put(SECRET_KEY, Objects.toString(this.secretKey, "")); + properties.put(CONTEXT_PATH, Objects.toString(this.contextPath, "")); + properties.put(CLUSTER_NAME, Objects.toString(this.clusterName, "")); + properties.put(ENDPOINT, Objects.toString(this.endpoint, "")); try { configService = NacosFactory.createConfigService(properties); return configService; diff --git a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java index e59d40446..304fc5672 100644 --- a/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java +++ b/spring-cloud-alibaba-nacos-config/src/main/java/org/springframework/cloud/alibaba/nacos/client/NacosPropertySourceLocator.java @@ -60,13 +60,12 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { nacosPropertySourceBuilder = new NacosPropertySourceBuilder(configService, timeout); - String applicationName = env.getProperty("spring.application.name"); - logger.info("Initialize spring.application.name '" + applicationName + "'."); + String name = nacosConfigProperties.getName(); String nacosGroup = nacosConfigProperties.getGroup(); String dataIdPrefix = nacosConfigProperties.getPrefix(); if (StringUtils.isEmpty(dataIdPrefix)) { - dataIdPrefix = applicationName; + dataIdPrefix = name; } String fileExtension = nacosConfigProperties.getFileExtension(); @@ -74,23 +73,21 @@ public class NacosPropertySourceLocator implements PropertySourceLocator { CompositePropertySource composite = new CompositePropertySource( NACOS_PROPERTY_SOURCE_NAME); - loadApplicationConfiguration(composite, env, nacosGroup, dataIdPrefix, - fileExtension); + loadApplicationConfiguration(composite, nacosGroup, dataIdPrefix, fileExtension); return composite; } private void loadApplicationConfiguration( - CompositePropertySource compositePropertySource, Environment environment, - String nacosGroup, String dataIdPrefix, String fileExtension) { + CompositePropertySource compositePropertySource, String nacosGroup, + String dataIdPrefix, String fileExtension) { loadNacosDataIfPresent(compositePropertySource, dataIdPrefix + DOT + fileExtension, nacosGroup, fileExtension); - for (String profile : environment.getActiveProfiles()) { + for (String profile : nacosConfigProperties.getActiveProfiles()) { String dataId = dataIdPrefix + SEP1 + profile + DOT + fileExtension; loadNacosDataIfPresent(compositePropertySource, dataId, nacosGroup, fileExtension); } - // todo multi profile active order and priority } private void loadNacosDataIfPresent(final CompositePropertySource composite, From d51dc61a9046dace5eb210210420e276b4c5e9f3 Mon Sep 17 00:00:00 2001 From: flystar32 Date: Mon, 29 Oct 2018 23:17:51 +0800 Subject: [PATCH 29/63] fix get active profiles --- .../context/acm/AcmContextBootstrapConfiguration.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmContextBootstrapConfiguration.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmContextBootstrapConfiguration.java index 32fd47da9..4adbc7e94 100644 --- a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmContextBootstrapConfiguration.java +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmContextBootstrapConfiguration.java @@ -67,16 +67,11 @@ public class AcmContextBootstrapConfiguration { AcmIntegrationProperties acmIntegrationProperties = new AcmIntegrationProperties(); String applicationName = environment.getProperty("spring.application.name"); String applicationGroup = environment.getProperty("spring.application.group"); - String activeProfiles = environment.getProperty(ACTIVE_PROFILES_PROPERTY_NAME); Assert.isTrue(!StringUtils.isEmpty(applicationName), "'spring.application.name' must be configured.."); acmIntegrationProperties.setApplicationName(applicationName); acmIntegrationProperties.setApplicationGroup(applicationGroup); - if (StringUtils.hasText(activeProfiles)) { - acmIntegrationProperties - .setActiveProfiles(StringUtils.commaDelimitedListToStringArray( - StringUtils.trimAllWhitespace(activeProfiles))); - } + acmIntegrationProperties.setActiveProfiles(environment.getActiveProfiles()); acmIntegrationProperties.setAcmProperties(acmProperties); return acmIntegrationProperties; } From 5c9d322b50ba356ef212e07b1d1f8163d6748ec9 Mon Sep 17 00:00:00 2001 From: flystar32 Date: Tue, 30 Oct 2018 10:20:31 +0800 Subject: [PATCH 30/63] fix tests --- .../NacosConfigAutoConfigurationTests.java | 107 +++++++++--------- ...acosConfigBootstrapConfigurationTests.java | 3 +- 2 files changed, 56 insertions(+), 54 deletions(-) diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java index 7e287c0a8..66126759f 100644 --- a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java +++ b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigAutoConfigurationTests.java @@ -39,58 +39,59 @@ import static org.assertj.core.api.Assertions.assertThat; */ public class NacosConfigAutoConfigurationTests { - private ConfigurableApplicationContext context; - - @Before - public void setUp() throws Exception { - this.context = new SpringApplicationBuilder( - NacosConfigBootstrapConfiguration.class, - NacosConfigAutoConfiguration.class, - TestConfiguration.class) - .web(WebApplicationType.NONE).run( - "--spring.cloud.config.enabled=true", - "--spring.cloud.nacos.config.server-addr=127.0.0.1:8080", - "--spring.cloud.nacos.config.prefix=myapp"); - } - - @After - public void tearDown() throws Exception { - if (this.context != null) { - this.context.close(); - } - } - - @Test - public void testNacosConfigProperties() { - - NacosConfigProperties nacosConfigProperties = this.context.getParent().getBean(NacosConfigProperties.class); - assertThat(nacosConfigProperties.getFileExtension()).isEqualTo("properties"); - assertThat(nacosConfigProperties.getPrefix()).isEqualTo("myapp"); - - } - - - @Test - public void testNacosRefreshProperties() { - - NacosRefreshProperties nacosRefreshProperties = this.context.getBean(NacosRefreshProperties.class); - assertThat(nacosRefreshProperties.isEnabled()).isEqualTo(true); - - } - - @Configuration - @AutoConfigureBefore(NacosConfigAutoConfiguration.class) - static class TestConfiguration{ - - - @Autowired - ConfigurableApplicationContext context; - - @Bean - ContextRefresher contextRefresher(){ - return new ContextRefresher(context, new RefreshScope()); - } - - } + private ConfigurableApplicationContext context; + + @Before + public void setUp() throws Exception { + this.context = new SpringApplicationBuilder( + NacosConfigBootstrapConfiguration.class, + NacosConfigAutoConfiguration.class, TestConfiguration.class) + .web(WebApplicationType.NONE) + .run("--spring.application.name=myapp", + "--spring.cloud.config.enabled=true", + "--spring.cloud.nacos.config.server-addr=127.0.0.1:8080", + "--spring.cloud.nacos.config.prefix=test"); + } + + @After + public void tearDown() throws Exception { + if (this.context != null) { + this.context.close(); + } + } + + @Test + public void testNacosConfigProperties() { + + NacosConfigProperties nacosConfigProperties = this.context.getParent() + .getBean(NacosConfigProperties.class); + assertThat(nacosConfigProperties.getFileExtension()).isEqualTo("properties"); + assertThat(nacosConfigProperties.getPrefix()).isEqualTo("test"); + assertThat(nacosConfigProperties.getName()).isEqualTo("myapp"); + + } + + @Test + public void testNacosRefreshProperties() { + + NacosRefreshProperties nacosRefreshProperties = this.context + .getBean(NacosRefreshProperties.class); + assertThat(nacosRefreshProperties.isEnabled()).isEqualTo(true); + + } + + @Configuration + @AutoConfigureBefore(NacosConfigAutoConfiguration.class) + static class TestConfiguration { + + @Autowired + ConfigurableApplicationContext context; + + @Bean + ContextRefresher contextRefresher() { + return new ContextRefresher(context, new RefreshScope()); + } + + } } diff --git a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfigurationTests.java b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfigurationTests.java index 6682e8508..73cc98803 100644 --- a/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfigurationTests.java +++ b/spring-cloud-alibaba-nacos-config/src/test/java/org/springframework/cloud/alibaba/nacos/NacosConfigBootstrapConfigurationTests.java @@ -43,9 +43,10 @@ public class NacosConfigBootstrapConfigurationTests { public void setUp() throws Exception { this.context = new SpringApplicationBuilder( NacosConfigBootstrapConfiguration.class).web(WebApplicationType.NONE).run( + "--spring.application.name=myapp", "--spring.cloud.config.enabled=true", "--spring.cloud.nacos.config.server-addr=127.0.0.1:8080", - "--spring.cloud.nacos.config.prefix=myapp"); + "--spring.cloud.nacos.config.prefix=test"); } @After From bc61e50a26d4cb21a3c1acfd458575059ba3e89d Mon Sep 17 00:00:00 2001 From: xiaolongzuo <150349407@qq.com> Date: Tue, 30 Oct 2018 11:17:18 +0800 Subject: [PATCH 31/63] optimize error message. --- .../context/acm/AcmContextBootstrapConfiguration.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmContextBootstrapConfiguration.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmContextBootstrapConfiguration.java index 4adbc7e94..13f038aa9 100644 --- a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmContextBootstrapConfiguration.java +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmContextBootstrapConfiguration.java @@ -16,8 +16,6 @@ package org.springframework.cloud.alicloud.context.acm; -import static org.springframework.core.env.AbstractEnvironment.ACTIVE_PROFILES_PROPERTY_NAME; - import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; @@ -68,7 +66,7 @@ public class AcmContextBootstrapConfiguration { String applicationName = environment.getProperty("spring.application.name"); String applicationGroup = environment.getProperty("spring.application.group"); Assert.isTrue(!StringUtils.isEmpty(applicationName), - "'spring.application.name' must be configured.."); + "'spring.application.name' must be configured in bootstrap.properties or bootstrap.yml/yaml..."); acmIntegrationProperties.setApplicationName(applicationName); acmIntegrationProperties.setApplicationGroup(applicationGroup); acmIntegrationProperties.setActiveProfiles(environment.getActiveProfiles()); From 11aedf3a059b2d6114fd7ee9a7f43c66c61dfa1d Mon Sep 17 00:00:00 2001 From: flystar32 Date: Tue, 30 Oct 2018 12:50:28 +0800 Subject: [PATCH 32/63] update ans pom.xml --- .../ans-example/ans-consumer-feign-example/pom.xml | 8 ++++++++ .../ans-example/ans-consumer-ribbon-example/pom.xml | 8 ++++++++ .../ans-example/ans-provider-example/pom.xml | 8 ++++++++ spring-cloud-alicloud-ans/pom.xml | 2 ++ .../spring-cloud-starter-alicloud-ans/pom.xml | 8 ++++---- 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml index e0966d4f7..662099e40 100644 --- a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml @@ -19,5 +19,13 @@ org.springframework.cloud spring-cloud-starter-openfeign + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-actuator + \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml index 238ea7f4c..a94cbe074 100644 --- a/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml @@ -15,5 +15,13 @@ org.springframework.cloud spring-cloud-starter-alicloud-ans + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-actuator + \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml index d79e46f14..41f880f9b 100644 --- a/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml +++ b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml @@ -15,5 +15,13 @@ org.springframework.cloud spring-cloud-starter-alicloud-ans + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-actuator + \ No newline at end of file diff --git a/spring-cloud-alicloud-ans/pom.xml b/spring-cloud-alicloud-ans/pom.xml index 6c33b9ee9..ff4882646 100644 --- a/spring-cloud-alicloud-ans/pom.xml +++ b/spring-cloud-alicloud-ans/pom.xml @@ -75,10 +75,12 @@ org.springframework.boot spring-boot-starter-web + test org.springframework.boot spring-boot-starter-actuator + test org.springframework.boot diff --git a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml index 418614fa2..acb713d22 100644 --- a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml +++ b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml @@ -1,13 +1,13 @@ - + 4.0.0 - spring-cloud-starter-alibaba org.springframework.cloud + spring-cloud-starter-alicloud 0.2.0.BUILD-SNAPSHOT - 4.0.0 + spring-cloud-starter-alicloud-ans Spring Cloud Starter Alibaba Cloud ANS From dfd6d2d16e78c0733f63122195cc55a66b1950e5 Mon Sep 17 00:00:00 2001 From: xiaolongzuo <150349407@qq.com> Date: Tue, 30 Oct 2018 14:53:29 +0800 Subject: [PATCH 33/63] optimize spring-cloud-alicloud-context tests. --- .../context/AliCloudPropertiesTests.java | 14 ++-- ...tiesTests.java => AcmPropertiesTests.java} | 23 +++--- .../context/ans/AnsPropertiesTests.java | 77 ++++++++++--------- .../context/edas/EdasPropertiesTests.java | 18 ++--- 4 files changed, 66 insertions(+), 66 deletions(-) rename spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/acm/{AnsPropertiesTests.java => AcmPropertiesTests.java} (86%) diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudPropertiesTests.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudPropertiesTests.java index 3253a756f..1094b928f 100644 --- a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudPropertiesTests.java +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/AliCloudPropertiesTests.java @@ -34,9 +34,10 @@ public class AliCloudPropertiesTests { @Test public void testConfigurationValueDefaultsAreAsExpected() { this.contextRunner.run(context -> { - AliCloudProperties config = context.getBean(AliCloudProperties.class); - assertThat(config.getAccessKey()).isNull(); - assertThat(config.getSecretKey()).isNull(); + AliCloudProperties aliCloudProperties = context + .getBean(AliCloudProperties.class); + assertThat(aliCloudProperties.getAccessKey()).isNull(); + assertThat(aliCloudProperties.getSecretKey()).isNull(); }); } @@ -44,9 +45,10 @@ public class AliCloudPropertiesTests { public void testConfigurationValuesAreCorrectlyLoaded() { this.contextRunner.withPropertyValues("spring.cloud.alicloud.access-key=123", "spring.cloud.alicloud.secret-key=123456").run(context -> { - AliCloudProperties config = context.getBean(AliCloudProperties.class); - assertThat(config.getAccessKey()).isEqualTo("123"); - assertThat(config.getSecretKey()).isEqualTo("123456"); + AliCloudProperties aliCloudProperties = context + .getBean(AliCloudProperties.class); + assertThat(aliCloudProperties.getAccessKey()).isEqualTo("123"); + assertThat(aliCloudProperties.getSecretKey()).isEqualTo("123456"); }); } diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/acm/AnsPropertiesTests.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/acm/AcmPropertiesTests.java similarity index 86% rename from spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/acm/AnsPropertiesTests.java rename to spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/acm/AcmPropertiesTests.java index 8a1f473ab..d129d64e9 100644 --- a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/acm/AnsPropertiesTests.java +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/acm/AcmPropertiesTests.java @@ -29,7 +29,7 @@ import com.alibaba.cloud.context.AliCloudServerMode; /** * @author xiaolongzuo */ -public class AnsPropertiesTests { +public class AcmPropertiesTests { private ApplicationContextRunner contextRunner = new ApplicationContextRunner() .withConfiguration( @@ -67,14 +67,15 @@ public class AnsPropertiesTests { "spring.cloud.alicloud.acm.endpoint=testDomain", "spring.cloud.alicloud.acm.group=testGroup", "spring.cloud.alicloud.acm.file-extension=yaml").run(context -> { - AcmProperties config = context.getBean(AcmProperties.class); - assertThat(config.getServerMode()).isEqualTo(AliCloudServerMode.EDAS); - assertThat(config.getServerList()).isEqualTo("10.10.10.10"); - assertThat(config.getServerPort()).isEqualTo("11111"); - assertThat(config.getEndpoint()).isEqualTo("testDomain"); - assertThat(config.getGroup()).isEqualTo("testGroup"); - assertThat(config.getFileExtension()).isEqualTo("yaml"); - assertThat(config.getNamespace()).isEqualTo("testNamespace"); + AcmProperties acmProperties = context.getBean(AcmProperties.class); + assertThat(acmProperties.getServerMode()) + .isEqualTo(AliCloudServerMode.EDAS); + assertThat(acmProperties.getServerList()).isEqualTo("10.10.10.10"); + assertThat(acmProperties.getServerPort()).isEqualTo("11111"); + assertThat(acmProperties.getEndpoint()).isEqualTo("testDomain"); + assertThat(acmProperties.getGroup()).isEqualTo("testGroup"); + assertThat(acmProperties.getFileExtension()).isEqualTo("yaml"); + assertThat(acmProperties.getNamespace()).isEqualTo("testNamespace"); }); } @@ -97,10 +98,6 @@ public class AnsPropertiesTests { .size()).isEqualTo(2); assertThat(acmIntegrationProperties .getApplicationConfigurationDataIds().size()).isEqualTo(2); - System.out.println("-----" - + acmIntegrationProperties.getGroupConfigurationDataIds()); - System.out.println(acmIntegrationProperties - .getApplicationConfigurationDataIds()); }); } diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/ans/AnsPropertiesTests.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/ans/AnsPropertiesTests.java index 11762c1b3..a81f0ab5b 100644 --- a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/ans/AnsPropertiesTests.java +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/ans/AnsPropertiesTests.java @@ -40,26 +40,26 @@ public class AnsPropertiesTests { public void testConfigurationValueDefaultsAreAsExpected() throws ClassNotFoundException { this.contextRunner.withPropertyValues().run(context -> { - AnsProperties config = context.getBean(AnsProperties.class); - assertThat(config.getServerMode()).isEqualTo(AliCloudServerMode.LOCAL); - assertThat(config.getServerList()).isEqualTo("127.0.0.1"); - assertThat(config.getServerPort()).isEqualTo("8080"); - assertThat(config.getClientDomains()).isEqualTo(""); - assertThat(config.getClientWeight()).isEqualTo(1.0F); - assertThat(config.getClientWeights().size()).isEqualTo(0); - assertThat(config.getClientTokens().size()).isEqualTo(0); - assertThat(config.getClientMetadata().size()).isEqualTo(0); - assertThat(config.getClientToken()).isNull(); - assertThat(config.getClientCluster()).isEqualTo("DEFAULT"); - assertThat(config.isRegisterEnabled()).isTrue(); - assertThat(config.getClientInterfaceName()).isNull(); - assertThat(config.getClientPort()).isEqualTo(-1); - assertThat(config.getEnv()).isEqualTo("DEFAULT"); - assertThat(config.isSecure()).isFalse(); - assertThat(config.getTags().size()).isEqualTo(1); - assertThat(config.getTags().keySet().iterator().next()) + AnsProperties ansProperties = context.getBean(AnsProperties.class); + assertThat(ansProperties.getServerMode()).isEqualTo(AliCloudServerMode.LOCAL); + assertThat(ansProperties.getServerList()).isEqualTo("127.0.0.1"); + assertThat(ansProperties.getServerPort()).isEqualTo("8080"); + assertThat(ansProperties.getClientDomains()).isEqualTo(""); + assertThat(ansProperties.getClientWeight()).isEqualTo(1.0F); + assertThat(ansProperties.getClientWeights().size()).isEqualTo(0); + assertThat(ansProperties.getClientTokens().size()).isEqualTo(0); + assertThat(ansProperties.getClientMetadata().size()).isEqualTo(0); + assertThat(ansProperties.getClientToken()).isNull(); + assertThat(ansProperties.getClientCluster()).isEqualTo("DEFAULT"); + assertThat(ansProperties.isRegisterEnabled()).isTrue(); + assertThat(ansProperties.getClientInterfaceName()).isNull(); + assertThat(ansProperties.getClientPort()).isEqualTo(-1); + assertThat(ansProperties.getEnv()).isEqualTo("DEFAULT"); + assertThat(ansProperties.isSecure()).isFalse(); + assertThat(ansProperties.getTags().size()).isEqualTo(1); + assertThat(ansProperties.getTags().keySet().iterator().next()) .isEqualTo("ANS_SERVICE_TYPE"); - assertThat(config.getTags().get("ANS_SERVICE_TYPE")) + assertThat(ansProperties.getTags().get("ANS_SERVICE_TYPE")) .isEqualTo("SPRING_CLOUD"); }); } @@ -74,26 +74,27 @@ public class AnsPropertiesTests { "spring.cloud.alicloud.ans.client-weight=0.9", "spring.cloud.alicloud.ans.client-weights.testDomain=0.9") .run(context -> { - AnsProperties config = context.getBean(AnsProperties.class); - assertThat(config.getServerMode()).isEqualTo(AliCloudServerMode.EDAS); - assertThat(config.getServerList()).isEqualTo("10.10.10.10"); - assertThat(config.getServerPort()).isEqualTo("11111"); - assertThat(config.getClientDomains()).isEqualTo("testDomain"); - assertThat(config.getClientWeight()).isEqualTo(0.9F); - assertThat(config.getClientWeights().size()).isEqualTo(1); - assertThat(config.getClientTokens().size()).isEqualTo(0); - assertThat(config.getClientMetadata().size()).isEqualTo(0); - assertThat(config.getClientToken()).isNull(); - assertThat(config.getClientCluster()).isEqualTo("DEFAULT"); - assertThat(config.isRegisterEnabled()).isTrue(); - assertThat(config.getClientInterfaceName()).isNull(); - assertThat(config.getClientPort()).isEqualTo(-1); - assertThat(config.getEnv()).isEqualTo("DEFAULT"); - assertThat(config.isSecure()).isFalse(); - assertThat(config.getTags().size()).isEqualTo(1); - assertThat(config.getTags().keySet().iterator().next()) + AnsProperties ansProperties = context.getBean(AnsProperties.class); + assertThat(ansProperties.getServerMode()) + .isEqualTo(AliCloudServerMode.EDAS); + assertThat(ansProperties.getServerList()).isEqualTo("10.10.10.10"); + assertThat(ansProperties.getServerPort()).isEqualTo("11111"); + assertThat(ansProperties.getClientDomains()).isEqualTo("testDomain"); + assertThat(ansProperties.getClientWeight()).isEqualTo(0.9F); + assertThat(ansProperties.getClientWeights().size()).isEqualTo(1); + assertThat(ansProperties.getClientTokens().size()).isEqualTo(0); + assertThat(ansProperties.getClientMetadata().size()).isEqualTo(0); + assertThat(ansProperties.getClientToken()).isNull(); + assertThat(ansProperties.getClientCluster()).isEqualTo("DEFAULT"); + assertThat(ansProperties.isRegisterEnabled()).isTrue(); + assertThat(ansProperties.getClientInterfaceName()).isNull(); + assertThat(ansProperties.getClientPort()).isEqualTo(-1); + assertThat(ansProperties.getEnv()).isEqualTo("DEFAULT"); + assertThat(ansProperties.isSecure()).isFalse(); + assertThat(ansProperties.getTags().size()).isEqualTo(1); + assertThat(ansProperties.getTags().keySet().iterator().next()) .isEqualTo("ANS_SERVICE_TYPE"); - assertThat(config.getTags().get("ANS_SERVICE_TYPE")) + assertThat(ansProperties.getTags().get("ANS_SERVICE_TYPE")) .isEqualTo("SPRING_CLOUD"); }); } diff --git a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/edas/EdasPropertiesTests.java b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/edas/EdasPropertiesTests.java index 5f5f08ae0..ed59a46c2 100644 --- a/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/edas/EdasPropertiesTests.java +++ b/spring-cloud-alicloud-context/src/test/java/org/springframework/cloud/alicloud/context/edas/EdasPropertiesTests.java @@ -35,9 +35,9 @@ public class EdasPropertiesTests { @Test public void testConfigurationValueDefaultsAreAsExpected() { this.contextRunner.withPropertyValues().run(context -> { - EdasProperties config = context.getBean(EdasProperties.class); - assertThat(config.getNamespace()).isNull(); - assertThat(config.isApplicationNameValid()).isFalse(); + EdasProperties edasProperties = context.getBean(EdasProperties.class); + assertThat(edasProperties.getNamespace()).isNull(); + assertThat(edasProperties.isApplicationNameValid()).isFalse(); }); } @@ -47,9 +47,9 @@ public class EdasPropertiesTests { .withPropertyValues("spring.cloud.alicloud.edas.namespace=testns", "spring.application.name=myapps") .run(context -> { - EdasProperties config = context.getBean(EdasProperties.class); - assertThat(config.getNamespace()).isEqualTo("testns"); - assertThat(config.getApplicationName()).isEqualTo("myapps"); + EdasProperties edasProperties = context.getBean(EdasProperties.class); + assertThat(edasProperties.getNamespace()).isEqualTo("testns"); + assertThat(edasProperties.getApplicationName()).isEqualTo("myapps"); }); } @@ -59,9 +59,9 @@ public class EdasPropertiesTests { .withPropertyValues("spring.cloud.alicloud.edas.namespace=testns", "spring.cloud.alicloud.edas.application.name=myapps") .run(context -> { - EdasProperties config = context.getBean(EdasProperties.class); - assertThat(config.getNamespace()).isEqualTo("testns"); - assertThat(config.getApplicationName()).isEqualTo("myapps"); + EdasProperties edasProperties = context.getBean(EdasProperties.class); + assertThat(edasProperties.getNamespace()).isEqualTo("testns"); + assertThat(edasProperties.getApplicationName()).isEqualTo("myapps"); }); } From c323c29dafad96938d4c3e12279bf2dcfe7035c2 Mon Sep 17 00:00:00 2001 From: flystar32 Date: Tue, 30 Oct 2018 15:29:31 +0800 Subject: [PATCH 34/63] update example configuration --- .../src/main/resources/application.properties | 4 +-- .../src/main/resources/application.properties | 4 +-- .../src/main/resources/application.properties | 4 ++- ...itional-spring-configuration-metadata.json | 28 ------------------- 4 files changed, 7 insertions(+), 33 deletions(-) delete mode 100644 spring-cloud-alicloud-ans/src/main/resources/META-INF/additional-spring-configuration-metadata.json diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/resources/application.properties index 1b25ea1e7..542653d94 100644 --- a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/resources/application.properties +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/src/main/resources/application.properties @@ -1,4 +1,4 @@ server.port=18083 # The following configuration can be omitted. -spring.cloud.ans.server.list=127.0.0.1 -spring.cloud.ans.server.port=8080 \ No newline at end of file +spring.cloud.alicloud.ans.server.list=127.0.0.1 +spring.cloud.alicloud.ans.server.port=8080 \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/resources/application.properties index c7bf8206f..315a90331 100644 --- a/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/resources/application.properties +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/src/main/resources/application.properties @@ -1,4 +1,4 @@ server.port=18082 # The following configuration can be omitted. -spring.cloud.ans.server.list=127.0.0.1 -spring.cloud.ans.server.port=8080 \ No newline at end of file +spring.cloud.alicloud.ans.server.list=127.0.0.1 +spring.cloud.alicloud.ans.server.port=8080 \ No newline at end of file diff --git a/spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/resources/application.properties b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/resources/application.properties index 76862f6b7..2fb389e2e 100644 --- a/spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/resources/application.properties +++ b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/src/main/resources/application.properties @@ -1,2 +1,4 @@ spring.application.name=ans-provider -server.port=18081 \ No newline at end of file +server.port=18081 +spring.cloud.alicloud.ans.server.list=127.0.0.1 +spring.cloud.alicloud.ans.server.port=8080 \ No newline at end of file diff --git a/spring-cloud-alicloud-ans/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-alicloud-ans/src/main/resources/META-INF/additional-spring-configuration-metadata.json deleted file mode 100644 index 69ade28d5..000000000 --- a/spring-cloud-alicloud-ans/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ /dev/null @@ -1,28 +0,0 @@ -{ - "properties": [ - { - "name": "spring.cloud.ans.server.list", - "type": "java.lang.String", - "defaultValue": "127.0.0.1", - "description": "ANS server list." - }, - { - "name": "spring.cloud.ans.server.port", - "type": "java.lang.Integer", - "defaultValue": "80", - "description": "ANS server port." - }, - { - "name": "spring.cloud.ans.client.domains", - "type": "java.lang.String", - "defaultValue": "", - "description": "Service name list, default value is ${spring.application.name}." - }, - { - "name": "spring.cloud.ans.client.env", - "type": "java.lang.String", - "defaultValue": "DEFAULT", - "description": "The env for ans, default value is DEFAULT." - } - ] -} \ No newline at end of file From c0fd7d458a47333adf8c8bad2f9803ba864d6dc0 Mon Sep 17 00:00:00 2001 From: flystar32 Date: Tue, 30 Oct 2018 15:43:19 +0800 Subject: [PATCH 35/63] update nacos version to 0.3.0 --- spring-cloud-alibaba-dependencies/pom.xml | 2 +- spring-cloud-alicloud-acm/pom.xml | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index e08a9dc28..cb1f76467 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -18,7 +18,7 @@ 0.2.0 3.1.0 - 0.3.0-RC1 + 0.3.0 1.0.8 0.1.1 4.0.1 diff --git a/spring-cloud-alicloud-acm/pom.xml b/spring-cloud-alicloud-acm/pom.xml index 971d33827..97aa95018 100644 --- a/spring-cloud-alicloud-acm/pom.xml +++ b/spring-cloud-alicloud-acm/pom.xml @@ -11,6 +11,7 @@ spring-cloud-alicloud-acm + Spring Cloud Alibaba Cloud ACM From 9660974f85681a390b6899f76b7922200061915d Mon Sep 17 00:00:00 2001 From: xiaolongzuo <150349407@qq.com> Date: Tue, 30 Oct 2018 15:59:38 +0800 Subject: [PATCH 36/63] Extract acm configuration to bootstrap phase. --- .../alicloud/acm/AcmAutoConfiguration.java | 60 ++++++++++--------- .../acm/refresh/AcmContextRefresher.java | 43 ++++++------- .../context/acm/AcmIntegrationProperties.java | 7 +++ 3 files changed, 57 insertions(+), 53 deletions(-) diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/AcmAutoConfiguration.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/AcmAutoConfiguration.java index 5930568a9..36da2e858 100644 --- a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/AcmAutoConfiguration.java +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/AcmAutoConfiguration.java @@ -16,13 +16,12 @@ package org.springframework.cloud.alicloud.acm; -import com.taobao.diamond.client.Diamond; import org.springframework.beans.BeansException; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; -import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.alicloud.acm.endpoint.AcmHealthIndicator; import org.springframework.cloud.alicloud.acm.refresh.AcmContextRefresher; import org.springframework.cloud.alicloud.acm.refresh.AcmRefreshHistory; +import org.springframework.cloud.alicloud.context.acm.AcmIntegrationProperties; import org.springframework.cloud.alicloud.context.acm.AcmProperties; import org.springframework.cloud.context.refresh.ContextRefresher; import org.springframework.context.ApplicationContext; @@ -30,44 +29,47 @@ import org.springframework.context.ApplicationContextAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import com.taobao.diamond.client.Diamond; + /** * Created on 01/10/2017. * * @author juven.xuxb */ @Configuration -@ConditionalOnClass({Diamond.class}) -@EnableConfigurationProperties(AcmProperties.class) +@ConditionalOnClass({ Diamond.class }) public class AcmAutoConfiguration implements ApplicationContextAware { - private ApplicationContext applicationContext; + private ApplicationContext applicationContext; - @Bean - public AcmPropertySourceRepository acmPropertySourceRepository() { - return new AcmPropertySourceRepository(applicationContext); - } + @Bean + public AcmPropertySourceRepository acmPropertySourceRepository() { + return new AcmPropertySourceRepository(applicationContext); + } - @Bean - public AcmHealthIndicator acmHealthIndicator(AcmProperties acmProperties, - AcmPropertySourceRepository acmPropertySourceRepository) { - return new AcmHealthIndicator(acmProperties, acmPropertySourceRepository); - } + @Bean + public AcmHealthIndicator acmHealthIndicator(AcmProperties acmProperties, + AcmPropertySourceRepository acmPropertySourceRepository) { + return new AcmHealthIndicator(acmProperties, acmPropertySourceRepository); + } - @Bean - public AcmRefreshHistory acmRefreshHistory() { - return new AcmRefreshHistory(); - } + @Bean + public AcmRefreshHistory acmRefreshHistory() { + return new AcmRefreshHistory(); + } - @Bean - public AcmContextRefresher acmContextRefresher(AcmProperties acmProperties, ContextRefresher contextRefresher, - AcmRefreshHistory refreshHistory, - AcmPropertySourceRepository propertySourceRepository) { - return new AcmContextRefresher(contextRefresher, acmProperties, refreshHistory, propertySourceRepository); - } + @Bean + public AcmContextRefresher acmContextRefresher( + AcmIntegrationProperties acmIntegrationProperties, + ContextRefresher contextRefresher, AcmRefreshHistory refreshHistory, + AcmPropertySourceRepository propertySourceRepository) { + return new AcmContextRefresher(contextRefresher, acmIntegrationProperties, + refreshHistory, propertySourceRepository); + } - @Override - public void setApplicationContext(ApplicationContext applicationContext) - throws BeansException { - this.applicationContext = applicationContext; - } + @Override + public void setApplicationContext(ApplicationContext applicationContext) + throws BeansException { + this.applicationContext = applicationContext; + } } diff --git a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/refresh/AcmContextRefresher.java b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/refresh/AcmContextRefresher.java index 5ed57a709..a6115912e 100644 --- a/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/refresh/AcmContextRefresher.java +++ b/spring-cloud-alicloud-acm/src/main/java/org/springframework/cloud/alicloud/acm/refresh/AcmContextRefresher.java @@ -16,26 +16,25 @@ package org.springframework.cloud.alicloud.acm.refresh; -import com.alibaba.edas.acm.ConfigService; -import com.alibaba.edas.acm.listener.ConfigChangeListener; +import java.io.UnsupportedEncodingException; +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.context.event.ApplicationReadyEvent; import org.springframework.cloud.alicloud.acm.AcmPropertySourceRepository; import org.springframework.cloud.alicloud.acm.bootstrap.AcmPropertySource; -import org.springframework.cloud.alicloud.context.acm.AcmProperties; +import org.springframework.cloud.alicloud.context.acm.AcmIntegrationProperties; import org.springframework.cloud.context.refresh.ContextRefresher; import org.springframework.context.ApplicationListener; -import org.springframework.core.env.Environment; import org.springframework.util.StringUtils; -import java.io.UnsupportedEncodingException; -import java.math.BigInteger; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; +import com.alibaba.edas.acm.ConfigService; +import com.alibaba.edas.acm.listener.ConfigChangeListener; /** * On application start up, AcmContextRefresher add diamond listeners to all application @@ -50,7 +49,7 @@ public class AcmContextRefresher implements ApplicationListener listenerMap = new ConcurrentHashMap<>(16); - @Autowired - private Environment environment; - public AcmContextRefresher(ContextRefresher contextRefresher, - AcmProperties properties, AcmRefreshHistory refreshHistory, + AcmIntegrationProperties acmIntegrationProperties, + AcmRefreshHistory refreshHistory, AcmPropertySourceRepository acmPropertySourceRepository) { this.contextRefresher = contextRefresher; - this.properties = properties; + this.acmIntegrationProperties = acmIntegrationProperties; this.refreshHistory = refreshHistory; this.acmPropertySourceRepository = acmPropertySourceRepository; } @@ -76,7 +73,7 @@ public class AcmContextRefresher implements ApplicationListener getGroupConfigurationDataIds() { List groupConfigurationDataIds = new ArrayList<>(); if (StringUtils.isEmpty(applicationGroup)) { @@ -88,4 +92,7 @@ public class AcmIntegrationProperties { this.acmProperties = acmProperties; } + public AcmProperties getAcmProperties() { + return acmProperties; + } } From b01e322e8007753167f21b9cc11c057e1a6a6359 Mon Sep 17 00:00:00 2001 From: flystar32 Date: Tue, 30 Oct 2018 20:41:52 +0800 Subject: [PATCH 37/63] update sentinel version to 1.3.0-GA --- spring-cloud-alibaba-dependencies/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index cb1f76467..fd9887ec4 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -16,7 +16,7 @@ Spring Cloud Alibaba Dependencies - 0.2.0 + 1.3.0-GA 3.1.0 0.3.0 1.0.8 From 95e49bfea910c41ca7bc4d10af2fc3a0459975df Mon Sep 17 00:00:00 2001 From: xiaolongzuo <150349407@qq.com> Date: Tue, 30 Oct 2018 20:51:40 +0800 Subject: [PATCH 38/63] Fix configuration specification. --- .../cloud/alicloud/context/acm/AcmProperties.java | 6 ++---- .../cloud/alicloud/context/ans/AnsProperties.java | 9 +++------ 2 files changed, 5 insertions(+), 10 deletions(-) diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmProperties.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmProperties.java index 18b037a39..2a1993e90 100644 --- a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmProperties.java +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/acm/AcmProperties.java @@ -16,7 +16,6 @@ package org.springframework.cloud.alicloud.context.acm; -import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.context.properties.ConfigurationProperties; import com.alibaba.cloud.context.AliCloudServerMode; @@ -31,8 +30,7 @@ import com.alibaba.cloud.context.acm.AcmConfiguration; @ConfigurationProperties(prefix = "spring.cloud.alicloud.acm") public class AcmProperties implements AcmConfiguration { - @Value("${spring.cloud.alicloud.acm.server-mode:LOCAL}") - private AliCloudServerMode serverMode; + private AliCloudServerMode serverMode = AliCloudServerMode.LOCAL; private String serverList = "127.0.0.1"; @@ -49,7 +47,7 @@ public class AcmProperties implements AcmConfiguration { private int timeout = 3000; /** - * the AliYun endpoint2 for ACM + * the AliYun endpoint for ACM */ private String endpoint; diff --git a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsProperties.java b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsProperties.java index 2889ae796..2852ff573 100644 --- a/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsProperties.java +++ b/spring-cloud-alicloud-context/src/main/java/org/springframework/cloud/alicloud/context/ans/AnsProperties.java @@ -44,20 +44,17 @@ public class AnsProperties implements AnsConfiguration { /** * 服务端模式,默认为LOCAL */ - @Value("${spring.cloud.alicloud.ans.server-mode:LOCAL}") - private AliCloudServerMode serverMode; + private AliCloudServerMode serverMode = AliCloudServerMode.LOCAL; /** * 服务端列表 */ - @Value("${spring.cloud.alicloud.ans.server-list:127.0.0.1}") - private String serverList; + private String serverList = "127.0.0.1"; /** * 服务端列表 */ - @Value("${spring.cloud.alicloud.ans.server-port:8080}") - private String serverPort; + private String serverPort = "8080"; /** * 注册的服务名,默认从 spring.cloud.alicloud.ans.doms 中获取,当没有配置时,使用 spring.application.name From 5e7080f78629588531aca40300f7c8ee657015c7 Mon Sep 17 00:00:00 2001 From: xiaolongzuo <150349407@qq.com> Date: Tue, 30 Oct 2018 20:52:07 +0800 Subject: [PATCH 39/63] Fix configuration specification. --- ...itional-spring-configuration-metadata.json | 22 ++----------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/spring-cloud-alicloud-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-cloud-alicloud-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json index d50714101..2b81aad2a 100644 --- a/spring-cloud-alicloud-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-cloud-alicloud-context/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -1,31 +1,13 @@ { "properties": [ { - "name": "spring.cloud.alicloud.ans.server.mode", - "type": "java.lang.String", - "defaultValue": "LOCAL", - "description": "Server mode." - }, - { - "name": "spring.cloud.alicloud.ans.server.list", - "type": "java.lang.String", - "defaultValue": "127.0.0.1", - "description": "ANS server list." - }, - { - "name": "spring.cloud.alicloud.ans.server.port", - "type": "java.lang.Integer", - "defaultValue": "80", - "description": "ANS server port." - }, - { - "name": "spring.cloud.alicloud.ans.client.domains", + "name": "spring.cloud.alicloud.ans.client-domains", "type": "java.lang.String", "defaultValue": "", "description": "Service name list, default value is ${spring.application.name}." }, { - "name": "spring.cloud.alicloud.ans.client.env", + "name": "spring.cloud.alicloud.ans.env", "type": "java.lang.String", "defaultValue": "DEFAULT", "description": "The env for ans, default value is DEFAULT." From 69eb2b1cb32559f8a706137ffb382b9561b96e32 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Tue, 30 Oct 2018 16:38:41 -0400 Subject: [PATCH 40/63] Updates to latest spring cloud dependencies --- pom.xml | 8 ++++---- spring-cloud-alibaba-dependencies/pom.xml | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pom.xml b/pom.xml index 1bf958f4a..fb5be179c 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ org.springframework.cloud spring-cloud-build - 2.0.3.RELEASE + 2.0.4.RELEASE @@ -59,9 +59,9 @@ - 2.0.1.RELEASE - 2.0.0.RELEASE - 2.0.0.RELEASE + 2.0.2.RELEASE + 2.0.2.RELEASE + 2.0.2.RELEASE 4.12 3.0 diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index fd9887ec4..9a2d10773 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -6,7 +6,7 @@ spring-cloud-dependencies-parent org.springframework.cloud - 2.0.3.RELEASE + 2.0.4.RELEASE spring-cloud-alibaba-dependencies From 1a2cb6e49ffd9dfce0049cd3acae260ed59b2226 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Tue, 30 Oct 2018 17:11:50 -0400 Subject: [PATCH 41/63] Update SNAPSHOT to 0.2.0.RELEASE --- pom.xml | 2 +- spring-cloud-alibaba-dependencies/pom.xml | 2 +- .../acm-example/acm-local-example/pom.xml | 2 +- .../ans-example/ans-consumer-feign-example/pom.xml | 2 +- .../ans-example/ans-consumer-ribbon-example/pom.xml | 2 +- .../ans-example/ans-provider-example/pom.xml | 2 +- .../nacos-example/nacos-config-example/pom.xml | 2 +- .../nacos-discovery-consumer-example/pom.xml | 2 +- .../nacos-discovery-provider-example/pom.xml | 2 +- .../nacos-example/nacos-discovery-example/pom.xml | 2 +- spring-cloud-alibaba-examples/oss-example/pom.xml | 2 +- spring-cloud-alibaba-examples/pom.xml | 2 +- .../sentinel-example/sentinel-core-example/pom.xml | 2 +- .../sentinel-dubbo-example/sentinel-dubbo-api/pom.xml | 2 +- .../sentinel-dubbo-consumer-example/pom.xml | 2 +- .../sentinel-dubbo-provider-example/pom.xml | 2 +- spring-cloud-alibaba-nacos-config/pom.xml | 2 +- spring-cloud-alibaba-nacos-discovery/pom.xml | 2 +- spring-cloud-alibaba-sentinel-datasource/pom.xml | 2 +- spring-cloud-alibaba-sentinel/pom.xml | 2 +- spring-cloud-alibaba-test/core-support/pom.xml | 2 +- spring-cloud-alibaba-test/pom.xml | 2 +- spring-cloud-alibaba-test/sentinel-test-support/pom.xml | 2 +- spring-cloud-alicloud-acm/pom.xml | 2 +- spring-cloud-alicloud-ans/pom.xml | 2 +- spring-cloud-alicloud-context/pom.xml | 2 +- spring-cloud-alicloud-oss/pom.xml | 2 +- spring-cloud-starter-alibaba/pom.xml | 2 +- .../spring-cloud-starter-alibaba-nacos-config/pom.xml | 2 +- .../spring-cloud-starter-alibaba-nacos-discovery/pom.xml | 2 +- .../spring-cloud-starter-alibaba-sentinel/pom.xml | 2 +- spring-cloud-starter-alicloud/pom.xml | 2 +- .../spring-cloud-starter-alicloud-acm/pom.xml | 2 +- .../spring-cloud-starter-alicloud-ans/pom.xml | 2 +- .../spring-cloud-starter-alicloud-oss/pom.xml | 2 +- 35 files changed, 35 insertions(+), 35 deletions(-) diff --git a/pom.xml b/pom.xml index fb5be179c..bbcc2567b 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE pom Spring Cloud Alibaba diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index 9a2d10773..db7f4e032 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -10,7 +10,7 @@ spring-cloud-alibaba-dependencies - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE pom Spring Cloud Alibaba Dependencies Spring Cloud Alibaba Dependencies diff --git a/spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml b/spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml index f83e5da34..37f26114b 100644 --- a/spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml +++ b/spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml @@ -5,7 +5,7 @@ spring-cloud-alibaba-examples org.springframework.cloud - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE 4.0.0 acm-local-example diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml index 662099e40..d99bac225 100644 --- a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml @@ -5,7 +5,7 @@ spring-cloud-alibaba-examples org.springframework.cloud - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE 4.0.0 ans-consumer-feign-example diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml index a94cbe074..55b1d510f 100644 --- a/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml @@ -5,7 +5,7 @@ spring-cloud-alibaba-examples org.springframework.cloud - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE 4.0.0 ans-consumer-ribbon-example diff --git a/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml index 41f880f9b..ffaf620ad 100644 --- a/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml +++ b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml @@ -5,7 +5,7 @@ spring-cloud-alibaba-examples org.springframework.cloud - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE 4.0.0 ans-provider-example diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/pom.xml index f7cedc41a..3db04852c 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/pom.xml index ec343d5c5..40aa487d5 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud nacos-discovery-example - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE 4.0.0 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/pom.xml index 294a44b94..ee61588ff 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud nacos-discovery-example - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE 4.0.0 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml index e56d92d4a..90c3a1180 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/oss-example/pom.xml b/spring-cloud-alibaba-examples/oss-example/pom.xml index cfa08a115..effcbe35c 100644 --- a/spring-cloud-alibaba-examples/oss-example/pom.xml +++ b/spring-cloud-alibaba-examples/oss-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE 4.0.0 diff --git a/spring-cloud-alibaba-examples/pom.xml b/spring-cloud-alibaba-examples/pom.xml index eef97298b..c17bc84c0 100644 --- a/spring-cloud-alibaba-examples/pom.xml +++ b/spring-cloud-alibaba-examples/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml index ec35b244d..4c2a99845 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api/pom.xml index 24cde890d..e7b9c438a 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE ../../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/pom.xml index 890656ea8..0ab3bf2dd 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE ../../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example/pom.xml index 87adbbaf7..8b0976c86 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE ../../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-nacos-config/pom.xml b/spring-cloud-alibaba-nacos-config/pom.xml index 12ff873c1..472ed6993 100644 --- a/spring-cloud-alibaba-nacos-config/pom.xml +++ b/spring-cloud-alibaba-nacos-config/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE 4.0.0 diff --git a/spring-cloud-alibaba-nacos-discovery/pom.xml b/spring-cloud-alibaba-nacos-discovery/pom.xml index 3c6bd31af..46ec49a8e 100644 --- a/spring-cloud-alibaba-nacos-discovery/pom.xml +++ b/spring-cloud-alibaba-nacos-discovery/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE 4.0.0 diff --git a/spring-cloud-alibaba-sentinel-datasource/pom.xml b/spring-cloud-alibaba-sentinel-datasource/pom.xml index 23048b77b..5887128b5 100644 --- a/spring-cloud-alibaba-sentinel-datasource/pom.xml +++ b/spring-cloud-alibaba-sentinel-datasource/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE 4.0.0 diff --git a/spring-cloud-alibaba-sentinel/pom.xml b/spring-cloud-alibaba-sentinel/pom.xml index 1ade981ea..c01e29b80 100644 --- a/spring-cloud-alibaba-sentinel/pom.xml +++ b/spring-cloud-alibaba-sentinel/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE 4.0.0 diff --git a/spring-cloud-alibaba-test/core-support/pom.xml b/spring-cloud-alibaba-test/core-support/pom.xml index 26712634f..1ec371cbd 100644 --- a/spring-cloud-alibaba-test/core-support/pom.xml +++ b/spring-cloud-alibaba-test/core-support/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-test - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE 4.0.0 diff --git a/spring-cloud-alibaba-test/pom.xml b/spring-cloud-alibaba-test/pom.xml index 289cc1bb2..d6e77d28b 100644 --- a/spring-cloud-alibaba-test/pom.xml +++ b/spring-cloud-alibaba-test/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE 4.0.0 diff --git a/spring-cloud-alibaba-test/sentinel-test-support/pom.xml b/spring-cloud-alibaba-test/sentinel-test-support/pom.xml index 3a7b21dc7..cffbd0a91 100644 --- a/spring-cloud-alibaba-test/sentinel-test-support/pom.xml +++ b/spring-cloud-alibaba-test/sentinel-test-support/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-test - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE 4.0.0 diff --git a/spring-cloud-alicloud-acm/pom.xml b/spring-cloud-alicloud-acm/pom.xml index 97aa95018..da9af5585 100644 --- a/spring-cloud-alicloud-acm/pom.xml +++ b/spring-cloud-alicloud-acm/pom.xml @@ -7,7 +7,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE spring-cloud-alicloud-acm diff --git a/spring-cloud-alicloud-ans/pom.xml b/spring-cloud-alicloud-ans/pom.xml index ff4882646..fbde799c4 100644 --- a/spring-cloud-alicloud-ans/pom.xml +++ b/spring-cloud-alicloud-ans/pom.xml @@ -5,7 +5,7 @@ spring-cloud-alibaba org.springframework.cloud - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE 4.0.0 diff --git a/spring-cloud-alicloud-context/pom.xml b/spring-cloud-alicloud-context/pom.xml index e7cbf206e..7383df1de 100644 --- a/spring-cloud-alicloud-context/pom.xml +++ b/spring-cloud-alicloud-context/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE 4.0.0 diff --git a/spring-cloud-alicloud-oss/pom.xml b/spring-cloud-alicloud-oss/pom.xml index a101f1291..651682c7e 100644 --- a/spring-cloud-alicloud-oss/pom.xml +++ b/spring-cloud-alicloud-oss/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE 4.0.0 diff --git a/spring-cloud-starter-alibaba/pom.xml b/spring-cloud-starter-alibaba/pom.xml index 877646ae2..501e38869 100644 --- a/spring-cloud-starter-alibaba/pom.xml +++ b/spring-cloud-starter-alibaba/pom.xml @@ -4,7 +4,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE spring-cloud-starter-alibaba pom diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config/pom.xml index 2befd40c8..067ddc63c 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config/pom.xml +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-starter-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE spring-cloud-starter-alibaba-nacos-config Spring Cloud Starter Alibaba Nacos Config diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-discovery/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-discovery/pom.xml index 2ce41a2fd..06ded7cc1 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-discovery/pom.xml +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-discovery/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-starter-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE spring-cloud-starter-alibaba-nacos-discovery Spring Cloud Starter Alibaba Nacos Discovery diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml index 9998a2b0f..34232dd6b 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-starter-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE spring-cloud-starter-alibaba-sentinel Spring Cloud Starter Alibaba Sentinel diff --git a/spring-cloud-starter-alicloud/pom.xml b/spring-cloud-starter-alicloud/pom.xml index f53a55d2b..d073fcd01 100644 --- a/spring-cloud-starter-alicloud/pom.xml +++ b/spring-cloud-starter-alicloud/pom.xml @@ -4,7 +4,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE spring-cloud-starter-alicloud pom diff --git a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-acm/pom.xml b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-acm/pom.xml index fa71c4101..4c4dc348e 100644 --- a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-acm/pom.xml +++ b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-acm/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-starter-alicloud - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE spring-cloud-starter-alicloud-acm Spring Cloud Starter Alibaba Cloud ACM diff --git a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml index acb713d22..98261869d 100644 --- a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml +++ b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-starter-alicloud - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE spring-cloud-starter-alicloud-ans diff --git a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-oss/pom.xml b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-oss/pom.xml index e52d22728..6751e99a5 100644 --- a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-oss/pom.xml +++ b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-oss/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-starter-alicloud - 0.2.0.BUILD-SNAPSHOT + 0.2.0.RELEASE spring-cloud-starter-alicloud-oss Spring Cloud Starter Alibaba Cloud OSS From 38b63d6b34af9829fba51621fbc4f45ff4f45bde Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Tue, 30 Oct 2018 17:25:19 -0400 Subject: [PATCH 42/63] Update to 0.2.1.BUILD-SNAPSHOT --- pom.xml | 2 +- spring-cloud-alibaba-dependencies/pom.xml | 2 +- .../acm-example/acm-local-example/pom.xml | 2 +- .../ans-example/ans-consumer-feign-example/pom.xml | 2 +- .../ans-example/ans-consumer-ribbon-example/pom.xml | 2 +- .../ans-example/ans-provider-example/pom.xml | 2 +- .../nacos-example/nacos-config-example/pom.xml | 2 +- .../nacos-discovery-consumer-example/pom.xml | 2 +- .../nacos-discovery-provider-example/pom.xml | 2 +- .../nacos-example/nacos-discovery-example/pom.xml | 2 +- spring-cloud-alibaba-examples/oss-example/pom.xml | 2 +- spring-cloud-alibaba-examples/pom.xml | 2 +- .../sentinel-example/sentinel-core-example/pom.xml | 2 +- .../sentinel-dubbo-example/sentinel-dubbo-api/pom.xml | 2 +- .../sentinel-dubbo-consumer-example/pom.xml | 2 +- .../sentinel-dubbo-provider-example/pom.xml | 2 +- spring-cloud-alibaba-nacos-config/pom.xml | 2 +- spring-cloud-alibaba-nacos-discovery/pom.xml | 2 +- spring-cloud-alibaba-sentinel-datasource/pom.xml | 2 +- spring-cloud-alibaba-sentinel/pom.xml | 2 +- spring-cloud-alibaba-test/core-support/pom.xml | 2 +- spring-cloud-alibaba-test/pom.xml | 2 +- spring-cloud-alibaba-test/sentinel-test-support/pom.xml | 2 +- spring-cloud-alicloud-acm/pom.xml | 2 +- spring-cloud-alicloud-ans/pom.xml | 2 +- spring-cloud-alicloud-context/pom.xml | 2 +- spring-cloud-alicloud-oss/pom.xml | 2 +- spring-cloud-starter-alibaba/pom.xml | 2 +- .../spring-cloud-starter-alibaba-nacos-config/pom.xml | 2 +- .../spring-cloud-starter-alibaba-nacos-discovery/pom.xml | 2 +- .../spring-cloud-starter-alibaba-sentinel/pom.xml | 2 +- spring-cloud-starter-alicloud/pom.xml | 2 +- .../spring-cloud-starter-alicloud-acm/pom.xml | 2 +- .../spring-cloud-starter-alicloud-ans/pom.xml | 2 +- .../spring-cloud-starter-alicloud-oss/pom.xml | 2 +- 35 files changed, 35 insertions(+), 35 deletions(-) diff --git a/pom.xml b/pom.xml index bbcc2567b..c10db5423 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT pom Spring Cloud Alibaba diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index db7f4e032..33b5ad6af 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -10,7 +10,7 @@ spring-cloud-alibaba-dependencies - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT pom Spring Cloud Alibaba Dependencies Spring Cloud Alibaba Dependencies diff --git a/spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml b/spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml index 37f26114b..35296f912 100644 --- a/spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml +++ b/spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml @@ -5,7 +5,7 @@ spring-cloud-alibaba-examples org.springframework.cloud - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT 4.0.0 acm-local-example diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml index d99bac225..21ea6aae7 100644 --- a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml @@ -5,7 +5,7 @@ spring-cloud-alibaba-examples org.springframework.cloud - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT 4.0.0 ans-consumer-feign-example diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml index 55b1d510f..775a2739e 100644 --- a/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml @@ -5,7 +5,7 @@ spring-cloud-alibaba-examples org.springframework.cloud - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT 4.0.0 ans-consumer-ribbon-example diff --git a/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml index ffaf620ad..cbe5509ca 100644 --- a/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml +++ b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml @@ -5,7 +5,7 @@ spring-cloud-alibaba-examples org.springframework.cloud - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT 4.0.0 ans-provider-example diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/pom.xml index 3db04852c..7ee827cb9 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-config-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/pom.xml index 40aa487d5..2699f7c35 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-consumer-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud nacos-discovery-example - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/pom.xml index ee61588ff..1266769bf 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/nacos-discovery-provider-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud nacos-discovery-example - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml index 90c3a1180..5ad506a9e 100644 --- a/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml +++ b/spring-cloud-alibaba-examples/nacos-example/nacos-discovery-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/oss-example/pom.xml b/spring-cloud-alibaba-examples/oss-example/pom.xml index effcbe35c..16e7b8974 100644 --- a/spring-cloud-alibaba-examples/oss-example/pom.xml +++ b/spring-cloud-alibaba-examples/oss-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-examples/pom.xml b/spring-cloud-alibaba-examples/pom.xml index c17bc84c0..acdcb6049 100644 --- a/spring-cloud-alibaba-examples/pom.xml +++ b/spring-cloud-alibaba-examples/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml index 4c2a99845..91ae63304 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-core-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT ../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api/pom.xml index e7b9c438a..898734b09 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-api/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT ../../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/pom.xml index 0ab3bf2dd..d69b92ba3 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-consumer-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT ../../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example/pom.xml b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example/pom.xml index 8b0976c86..2c48b6b71 100644 --- a/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example/pom.xml +++ b/spring-cloud-alibaba-examples/sentinel-example/sentinel-dubbo-example/sentinel-dubbo-provider-example/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-examples - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT ../../../pom.xml 4.0.0 diff --git a/spring-cloud-alibaba-nacos-config/pom.xml b/spring-cloud-alibaba-nacos-config/pom.xml index 472ed6993..59663771c 100644 --- a/spring-cloud-alibaba-nacos-config/pom.xml +++ b/spring-cloud-alibaba-nacos-config/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-nacos-discovery/pom.xml b/spring-cloud-alibaba-nacos-discovery/pom.xml index 46ec49a8e..740c23982 100644 --- a/spring-cloud-alibaba-nacos-discovery/pom.xml +++ b/spring-cloud-alibaba-nacos-discovery/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-sentinel-datasource/pom.xml b/spring-cloud-alibaba-sentinel-datasource/pom.xml index 5887128b5..f2878dfe1 100644 --- a/spring-cloud-alibaba-sentinel-datasource/pom.xml +++ b/spring-cloud-alibaba-sentinel-datasource/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-sentinel/pom.xml b/spring-cloud-alibaba-sentinel/pom.xml index c01e29b80..3c3b41ead 100644 --- a/spring-cloud-alibaba-sentinel/pom.xml +++ b/spring-cloud-alibaba-sentinel/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-test/core-support/pom.xml b/spring-cloud-alibaba-test/core-support/pom.xml index 1ec371cbd..93801772e 100644 --- a/spring-cloud-alibaba-test/core-support/pom.xml +++ b/spring-cloud-alibaba-test/core-support/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-test - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-test/pom.xml b/spring-cloud-alibaba-test/pom.xml index d6e77d28b..6946dd9f1 100644 --- a/spring-cloud-alibaba-test/pom.xml +++ b/spring-cloud-alibaba-test/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alibaba-test/sentinel-test-support/pom.xml b/spring-cloud-alibaba-test/sentinel-test-support/pom.xml index cffbd0a91..2cff2a660 100644 --- a/spring-cloud-alibaba-test/sentinel-test-support/pom.xml +++ b/spring-cloud-alibaba-test/sentinel-test-support/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba-test - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alicloud-acm/pom.xml b/spring-cloud-alicloud-acm/pom.xml index da9af5585..e63810876 100644 --- a/spring-cloud-alicloud-acm/pom.xml +++ b/spring-cloud-alicloud-acm/pom.xml @@ -7,7 +7,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT spring-cloud-alicloud-acm diff --git a/spring-cloud-alicloud-ans/pom.xml b/spring-cloud-alicloud-ans/pom.xml index fbde799c4..56fba7fbf 100644 --- a/spring-cloud-alicloud-ans/pom.xml +++ b/spring-cloud-alicloud-ans/pom.xml @@ -5,7 +5,7 @@ spring-cloud-alibaba org.springframework.cloud - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alicloud-context/pom.xml b/spring-cloud-alicloud-context/pom.xml index 7383df1de..5b3a0f1a1 100644 --- a/spring-cloud-alicloud-context/pom.xml +++ b/spring-cloud-alicloud-context/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-alicloud-oss/pom.xml b/spring-cloud-alicloud-oss/pom.xml index 651682c7e..6b67c470d 100644 --- a/spring-cloud-alicloud-oss/pom.xml +++ b/spring-cloud-alicloud-oss/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT 4.0.0 diff --git a/spring-cloud-starter-alibaba/pom.xml b/spring-cloud-starter-alibaba/pom.xml index 501e38869..5223b1d61 100644 --- a/spring-cloud-starter-alibaba/pom.xml +++ b/spring-cloud-starter-alibaba/pom.xml @@ -4,7 +4,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT spring-cloud-starter-alibaba pom diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config/pom.xml index 067ddc63c..6962945c2 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config/pom.xml +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-config/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-starter-alibaba - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT spring-cloud-starter-alibaba-nacos-config Spring Cloud Starter Alibaba Nacos Config diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-discovery/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-discovery/pom.xml index 06ded7cc1..50a538535 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-discovery/pom.xml +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-nacos-discovery/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-starter-alibaba - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT spring-cloud-starter-alibaba-nacos-discovery Spring Cloud Starter Alibaba Nacos Discovery diff --git a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml index 34232dd6b..9d202c543 100644 --- a/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml +++ b/spring-cloud-starter-alibaba/spring-cloud-starter-alibaba-sentinel/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-starter-alibaba - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT spring-cloud-starter-alibaba-sentinel Spring Cloud Starter Alibaba Sentinel diff --git a/spring-cloud-starter-alicloud/pom.xml b/spring-cloud-starter-alicloud/pom.xml index d073fcd01..eb2ae1aa9 100644 --- a/spring-cloud-starter-alicloud/pom.xml +++ b/spring-cloud-starter-alicloud/pom.xml @@ -4,7 +4,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT spring-cloud-starter-alicloud pom diff --git a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-acm/pom.xml b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-acm/pom.xml index 4c4dc348e..4f878e58f 100644 --- a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-acm/pom.xml +++ b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-acm/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-starter-alicloud - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT spring-cloud-starter-alicloud-acm Spring Cloud Starter Alibaba Cloud ACM diff --git a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml index 98261869d..002adbbff 100644 --- a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml +++ b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-ans/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-starter-alicloud - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT spring-cloud-starter-alicloud-ans diff --git a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-oss/pom.xml b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-oss/pom.xml index 6751e99a5..ad465ec2e 100644 --- a/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-oss/pom.xml +++ b/spring-cloud-starter-alicloud/spring-cloud-starter-alicloud-oss/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-starter-alicloud - 0.2.0.RELEASE + 0.2.1.BUILD-SNAPSHOT spring-cloud-starter-alicloud-oss Spring Cloud Starter Alibaba Cloud OSS From c4749f76de6c1c69644206320014538a81a6811a Mon Sep 17 00:00:00 2001 From: xiaojing Date: Fri, 2 Nov 2018 09:50:49 +0800 Subject: [PATCH 43/63] fix typo --- README-zh.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README-zh.md b/README-zh.md index 54a9cfa12..c6afae181 100644 --- a/README-zh.md +++ b/README-zh.md @@ -8,7 +8,7 @@ Spring Cloud Alibaba 致力于提供分布式应用服务开发的一站式解 ## 主要功能 * **服务限流降级**:默认支持为 HTTP 服务的提供限流保护,也支持添加注解实现方法的自定义限流降级,且支持动态修改限流降级规则。 -* **服务注册与发现**:适配 sprig cloud 服务注册与发现标准,默认集成了 Ribbon 的支持。 +* **服务注册与发现**:适配 Spring Cloud 服务注册与发现标准,默认集成了 Ribbon 的支持。 * **分布式配置管理**:支持分布式系统中的外部化配置,配置更改时自动刷新。 * **阿里云对象存储**:阿里云提供的海量、安全、低成本、高可靠的云存储服务。支持在任何应用、任何时间、任何地点存储和访问任意类型的数据。 From d75107d11dee95b4d4d1a3792f61d746a2985065 Mon Sep 17 00:00:00 2001 From: Nepxion <1394997@qq.com> Date: Sat, 3 Nov 2018 19:37:50 +0800 Subject: [PATCH 44/63] fixes #87. --- .gitignore | 2 ++ .../cloud/alibaba/nacos/NacosDiscoveryClient.java | 6 +----- .../cloud/alibaba/nacos/ribbon/NacosServerList.java | 7 ++++--- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index e02b722f5..942b16d37 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,7 @@ # Compiled class file *.class +*.classpath +*.factorypath # Log file *.log diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java index 1d22cd67d..4ad0e2e04 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java @@ -24,12 +24,12 @@ import org.springframework.cloud.client.discovery.DiscoveryClient; import java.util.*; -import com.alibaba.nacos.api.naming.NamingService; import com.alibaba.nacos.api.naming.pojo.Instance; import com.alibaba.nacos.api.naming.pojo.ListView; /** * @author xiaojing + * @author renhaojun */ public class NacosDiscoveryClient implements DiscoveryClient { @@ -98,8 +98,4 @@ public class NacosDiscoveryClient implements DiscoveryClient { return Collections.emptyList(); } } - - public NamingService getNamingService() { - return discoveryProperties.namingServiceInstance(); - } } diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServerList.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServerList.java index 261456622..ac33d6fd3 100644 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServerList.java +++ b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/ribbon/NacosServerList.java @@ -19,7 +19,7 @@ package org.springframework.cloud.alibaba.nacos.ribbon; import com.netflix.client.config.IClientConfig; import com.netflix.loadbalancer.AbstractServerList; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cloud.alibaba.nacos.NacosDiscoveryClient; +import org.springframework.cloud.alibaba.nacos.NacosDiscoveryProperties; import java.util.ArrayList; import java.util.List; @@ -28,11 +28,12 @@ import com.alibaba.nacos.api.naming.pojo.Instance; /** * @author xiaojing + * @author renhaojun */ public class NacosServerList extends AbstractServerList { @Autowired - private NacosDiscoveryClient discoveryClient; + private NacosDiscoveryProperties discoveryProperties; private String serviceId; @@ -55,7 +56,7 @@ public class NacosServerList extends AbstractServerList { private List getServers() { try { - List instances = discoveryClient.getNamingService() + List instances = discoveryProperties.namingServiceInstance() .getAllInstances(serviceId); return instancesToServerList(instances); } From d3cc2ec90dacb929b962ec29b8f2225846d0d23a Mon Sep 17 00:00:00 2001 From: fangjian0423 Date: Tue, 6 Nov 2018 17:04:59 +0800 Subject: [PATCH 45/63] Polish #48 --- pom.xml | 1 + spring-cloud-alibaba-docs/pom.xml | 54 +++++++++++++++++++ .../src/main/asciidoc-zh/acm.adoc | 1 + .../src/main/asciidoc-zh/ans.adoc | 1 + .../asciidoc-zh/dependency-management.adoc | 4 ++ .../src/main/asciidoc-zh/nacos-config.adoc | 1 + .../src/main/asciidoc-zh/nacos-discovery.adoc | 1 + .../src/main/asciidoc-zh/oss.adoc | 1 + .../src/main/asciidoc-zh/rocketmq.adoc | 1 + .../src/main/asciidoc-zh/sentinel.adoc | 1 + .../asciidoc-zh/spring-cloud-alibaba.adoc | 31 +++++++++++ .../src/main/asciidoc/acm.adoc | 1 + .../src/main/asciidoc/ans.adoc | 1 + .../main/asciidoc/dependency-management.adoc | 4 ++ .../src/main/asciidoc/nacos-config.adoc | 1 + .../src/main/asciidoc/nacos-discovery.adoc | 1 + .../src/main/asciidoc/oss.adoc | 1 + .../src/main/asciidoc/rocketmq.adoc | 1 + .../src/main/asciidoc/sentinel.adoc | 1 + .../main/asciidoc/spring-cloud-alibaba.adoc | 31 +++++++++++ 20 files changed, 139 insertions(+) create mode 100644 spring-cloud-alibaba-docs/pom.xml create mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc-zh/acm.adoc create mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc-zh/ans.adoc create mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc-zh/dependency-management.adoc create mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc create mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc create mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc-zh/oss.adoc create mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc-zh/rocketmq.adoc create mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc-zh/sentinel.adoc create mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc-zh/spring-cloud-alibaba.adoc create mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc/acm.adoc create mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc/ans.adoc create mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc/dependency-management.adoc create mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc/nacos-config.adoc create mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc/nacos-discovery.adoc create mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc/oss.adoc create mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc/rocketmq.adoc create mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc/sentinel.adoc create mode 100644 spring-cloud-alibaba-docs/src/main/asciidoc/spring-cloud-alibaba.adoc diff --git a/pom.xml b/pom.xml index c10db5423..dbeaab9d7 100644 --- a/pom.xml +++ b/pom.xml @@ -83,6 +83,7 @@ spring-cloud-alibaba-nacos-discovery spring-cloud-alibaba-examples spring-cloud-alibaba-test + spring-cloud-alibaba-docs spring-cloud-starter-alibaba spring-cloud-starter-alicloud spring-cloud-alicloud-oss diff --git a/spring-cloud-alibaba-docs/pom.xml b/spring-cloud-alibaba-docs/pom.xml new file mode 100644 index 000000000..681a5843c --- /dev/null +++ b/spring-cloud-alibaba-docs/pom.xml @@ -0,0 +1,54 @@ + + + + + org.springframework.cloud + spring-cloud-alibaba + 0.2.1.BUILD-SNAPSHOT + + 4.0.0 + + org.springframework.cloud + spring-cloud-alibaba-docs + Spring Cloud Alibaba Documentation + pom + + + spring-cloud-alibaba + ${basedir}/.. + + + + + docs + + + + org.apache.maven.plugins + maven-dependency-plugin + + + org.asciidoctor + asciidoctor-maven-plugin + false + + ${basedir}/src/main/asciidoc-zh + + + + com.agilejava.docbkx + docbkx-maven-plugin + + + org.codehaus.mojo + build-helper-maven-plugin + false + + + + + + + + diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/acm.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/acm.adoc new file mode 100644 index 000000000..488baab69 --- /dev/null +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/acm.adoc @@ -0,0 +1 @@ +== Spring Cloud AliCloud ACM diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/ans.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/ans.adoc new file mode 100644 index 000000000..b9b34ce24 --- /dev/null +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/ans.adoc @@ -0,0 +1 @@ +== Spring Cloud AliCloud ANS diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/dependency-management.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/dependency-management.adoc new file mode 100644 index 000000000..d3f0d8854 --- /dev/null +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/dependency-management.adoc @@ -0,0 +1,4 @@ +== 依赖管理 + +Spring Cloud Alibaba BOM 包含了它所使用的所有依赖的版本。 + diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc new file mode 100644 index 000000000..e933cff8c --- /dev/null +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc @@ -0,0 +1 @@ +== Spring Cloud Alibaba Nacos Config diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc new file mode 100644 index 000000000..21a1177bf --- /dev/null +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc @@ -0,0 +1 @@ +== Spring Cloud Alibaba Nacos Discovery diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/oss.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/oss.adoc new file mode 100644 index 000000000..20b43f75f --- /dev/null +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/oss.adoc @@ -0,0 +1 @@ +== Spring Cloud AliCloud OSS diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/rocketmq.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/rocketmq.adoc new file mode 100644 index 000000000..238f0b4be --- /dev/null +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/rocketmq.adoc @@ -0,0 +1 @@ +== Spring Cloud Alibaba Rocket Binder diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/sentinel.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/sentinel.adoc new file mode 100644 index 000000000..59eaa589c --- /dev/null +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/sentinel.adoc @@ -0,0 +1 @@ +== Spring Cloud Alibaba Sentinel diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/spring-cloud-alibaba.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/spring-cloud-alibaba.adoc new file mode 100644 index 000000000..6a05ccde7 --- /dev/null +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/spring-cloud-alibaba.adoc @@ -0,0 +1,31 @@ +[[spring-cloud-alibaba-reference]] += Spring Cloud Alibaba 参考文档 +xiaojing; xiaolongzuo; jim fang; bingting peng +:doctype: book +:toc: +:toclevels: 4 +:source-highlighter: prettify +:numbered: + +== 介绍 + +Spring Cloud Alibaba 致力于提供分布式应用服务开发的一站式解决方案。此项目包含开发分布式应用服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。 + +依托 Spring Cloud Alibaba,您只需要添加一些注解和少量配置,就可以将 Spring Cloud 应用接入阿里分布式应用解决方案,通过阿里中间件来迅速搭建分布式应用系统。 + +include::dependency-management.adoc[] + +include::nacos-discovery.adoc[] + +include::nacos-config.adoc[] + +include::sentinel.adoc[] + +include::rocketmq.adoc[] + +include::ans.adoc[] + +include::acm.adoc[] + +include::oss.adoc[] + diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/acm.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/acm.adoc new file mode 100644 index 000000000..488baab69 --- /dev/null +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/acm.adoc @@ -0,0 +1 @@ +== Spring Cloud AliCloud ACM diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/ans.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/ans.adoc new file mode 100644 index 000000000..b9b34ce24 --- /dev/null +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/ans.adoc @@ -0,0 +1 @@ +== Spring Cloud AliCloud ANS diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/dependency-management.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/dependency-management.adoc new file mode 100644 index 000000000..1c1640c1c --- /dev/null +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/dependency-management.adoc @@ -0,0 +1,4 @@ +== Dependency Management + +The Spring Cloud Alibaba Bill of Materials (BOM) contains the versions of all the dependencies it uses. + diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-config.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-config.adoc new file mode 100644 index 000000000..e933cff8c --- /dev/null +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-config.adoc @@ -0,0 +1 @@ +== Spring Cloud Alibaba Nacos Config diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-discovery.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-discovery.adoc new file mode 100644 index 000000000..21a1177bf --- /dev/null +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/nacos-discovery.adoc @@ -0,0 +1 @@ +== Spring Cloud Alibaba Nacos Discovery diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/oss.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/oss.adoc new file mode 100644 index 000000000..20b43f75f --- /dev/null +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/oss.adoc @@ -0,0 +1 @@ +== Spring Cloud AliCloud OSS diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/rocketmq.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/rocketmq.adoc new file mode 100644 index 000000000..238f0b4be --- /dev/null +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/rocketmq.adoc @@ -0,0 +1 @@ +== Spring Cloud Alibaba Rocket Binder diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/sentinel.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/sentinel.adoc new file mode 100644 index 000000000..59eaa589c --- /dev/null +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/sentinel.adoc @@ -0,0 +1 @@ +== Spring Cloud Alibaba Sentinel diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc/spring-cloud-alibaba.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc/spring-cloud-alibaba.adoc new file mode 100644 index 000000000..a8c52ae28 --- /dev/null +++ b/spring-cloud-alibaba-docs/src/main/asciidoc/spring-cloud-alibaba.adoc @@ -0,0 +1,31 @@ +[[spring-cloud-alibaba-reference]] += Spring Cloud Alibaba Reference Documentation +xiaojing; xiaolongzuo; jim fang; bingting peng +:doctype: book +:toc: +:toclevels: 4 +:source-highlighter: prettify +:numbered: + +== Introduction + +Spring Cloud Alibaba provides a one-stop solution for distributed application development. It contains all the components required to develop distributed applications, making it easy for you to develop your applications using Spring Cloud. + +With Spring Cloud Alibaba, you only need to add some annotations and a small amount of configurations to connect Spring Cloud applications to the distributed solutions of Alibaba, and build a distributed application system with Alibaba middleware. + +include::dependency-management.adoc[] + +include::nacos-discovery.adoc[] + +include::nacos-config.adoc[] + +include::sentinel.adoc[] + +include::rocketmq.adoc[] + +include::ans.adoc[] + +include::acm.adoc[] + +include::oss.adoc[] + From 4428c87d1216dc4eca011441f9b5235235bc04ed Mon Sep 17 00:00:00 2001 From: xiaolongzuo <150349407@qq.com> Date: Tue, 6 Nov 2018 17:27:09 +0800 Subject: [PATCH 46/63] =?UTF-8?q?Fix=20build=20error=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../acm-example/acm-local-example/pom.xml | 1 + .../ans-example/ans-consumer-feign-example/pom.xml | 1 + .../ans-example/ans-consumer-ribbon-example/pom.xml | 1 + .../ans-example/ans-provider-example/pom.xml | 1 + 4 files changed, 4 insertions(+) diff --git a/spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml b/spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml index 35296f912..504f19ea7 100644 --- a/spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml +++ b/spring-cloud-alibaba-examples/acm-example/acm-local-example/pom.xml @@ -6,6 +6,7 @@ spring-cloud-alibaba-examples org.springframework.cloud 0.2.1.BUILD-SNAPSHOT + ../../pom.xml 4.0.0 acm-local-example diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml index 21ea6aae7..89db55bce 100644 --- a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/pom.xml @@ -6,6 +6,7 @@ spring-cloud-alibaba-examples org.springframework.cloud 0.2.1.BUILD-SNAPSHOT + ../../pom.xml 4.0.0 ans-consumer-feign-example diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml index 775a2739e..1873bf70e 100644 --- a/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/pom.xml @@ -6,6 +6,7 @@ spring-cloud-alibaba-examples org.springframework.cloud 0.2.1.BUILD-SNAPSHOT + ../../pom.xml 4.0.0 ans-consumer-ribbon-example diff --git a/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml index cbe5509ca..1d188a68c 100644 --- a/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml +++ b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/pom.xml @@ -6,6 +6,7 @@ spring-cloud-alibaba-examples org.springframework.cloud 0.2.1.BUILD-SNAPSHOT + ../../pom.xml 4.0.0 ans-provider-example From 88d52bdfed06b21a0d844bcd0fce5629ab9001ca Mon Sep 17 00:00:00 2001 From: flystar32 Date: Tue, 6 Nov 2018 22:11:07 +0800 Subject: [PATCH 47/63] update readme --- README-zh.md | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/README-zh.md b/README-zh.md index c6afae181..72a20fc65 100644 --- a/README-zh.md +++ b/README-zh.md @@ -38,6 +38,46 @@ Spring Cloud 使用 Maven 来构建,最快的使用方式是将本项目clone ## 如何使用 +### 如何引入依赖 +项目已经发布了第一个版本,版本 0.2.0.RELEASE 对应的是 Spring Boot 2.x 版本,版本 0.1.0.RELEASE 对应的是 Spring Boot 1.x 版本。 + +如果需要使用已发布的版本,在 `dependencyManagement` 中添加如下配置。 + + + + + org.springframework.cloud + spring-cloud-alibaba-dependencies + 0.2.0.REALEASE + pom + import + + + + +然后再 `dependencies` 中添加自己所需使用的依赖即可使用。 + +如果您想体验最新的 BUILD-SNAPSHOT 的新功能,则可以将版本换成最新的版本,但是需要在 pom.xml 中配置 Spring BUILDSNAPSHOT 仓库,**注意: SNAPSHOT 版本随时可能更新** + + + + spring-snapshot + Spring Snapshot Repository + https://repo.spring.io/snapshot + + true + + + + + +### Reference Doc + +[文档地址](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/tree/master/spring-cloud-alibaba-docs/src/main/asciidoc-zh) + + +## 演示 Demo + 为了演示如何使用,Spring Cloud Alibaba 项目包含了一个子模块`spring-cloud-alibaba-examples`。此模块中提供了演示用的 example ,您可以阅读对应的 example 工程下的 readme 文档,根据里面的步骤来体验。 Example 列表: From 4af17fb6aa60161a57a322d9d198946967cd7d94 Mon Sep 17 00:00:00 2001 From: pengbingting Date: Wed, 7 Nov 2018 10:36:25 +0800 Subject: [PATCH 48/63] =?UTF-8?q?=E6=B7=BB=E5=8A=A0acm=20reference?= =?UTF-8?q?=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/asciidoc-zh/acm.adoc | 282 +++++++++++++++++- 1 file changed, 281 insertions(+), 1 deletion(-) diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/acm.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/acm.adoc index 488baab69..143e764a5 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/acm.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/acm.adoc @@ -1 +1,281 @@ -== Spring Cloud AliCloud ACM += Spring Cloud AliCloud ACM + +Spring Cloud Alibaba Config 提供了和阿里云上的ACM的集成。使得在项目中如果需要上云的用户可以非常方便无缝的对接阿里云,来享受云端提供的稳定服务。 + +=== 快速开始 + +为了能够在本地开发时可以使用和云上具有相同功能的ACM服务,我们提供了用于本地开发测试的轻量版配置中心。轻量版配置中心你可以理解为用于方便的本地开发测试的Saas平台。 + +==== 基于properties文件扩展名的配置 + +===== 轻量版配置中心服务端初始化 + +1、轻量版配置中心的下载和启动方式可参考 https://help.aliyun.com/document_detail/44163.html?spm=a2c4g.11186623.6.677.5f206b82Z2mTCF[这里] + +2、启动好轻量版配置中心之后,在控制台中添加如下的配置。注意data id是以 properties为扩展名(默认的文件扩展名方式)。 + +[source,subs="normal"] +---- +Group: DEFAULT_GROOUP + +DataId: acm-config.properties + +Content: user.name=alibaba + user.age=56 +---- + +===== 客户端使用方式 + +为了能够在应用程序中使用ACM作为Sping Cloud Config的后端存储服务,在您构建Spring Boot 应用的同时添加一个Spring Boot Starter org.springframework.cloud:spring-cloud-starter-alicloud-acm。以下是一个基础的maven 依赖配置: + +[source,xml] +---- + + org.springframework.boot + spring-boot-starter-parent + 2.0.5.RELEASE + + + + + + + org.springframework.cloud + spring-cloud-dependencies + Finchley.SR1 + pom + import + + + org.springframework.cloud + spring-cloud-alibaba-dependencies + 0.2.0.RELEASE + pom + import + + + + + + + org.springframework.cloud + spring-cloud-starter-alicloud-acm + + + org.springframework.boot + spring-boot-starter + + +---- + +现在你就可以创建一个标准的Spring Boot的应用。 + +[source,java] +---- +@SpringBootApplication +public class ProviderApplication { + + public static void main(String[] args) { + ConfigurableApplicationContext applicationContext = SpringApplication.run(ProviderApplication.class, args); + String userName = applicationContext.getEnvironment().getProperty("user.name"); + String userAge = applicationContext.getEnvironment().getProperty("user.age"); + System.err.println("user name :"+userName+"; age: "+userAge); + } +} +---- + +spring-cloud-starter-alicloud-acm 对于ACM服务端的基础配置有默认值(默认配置绑定到的ip和端口为127.0.0.1和8080),因此在运行此Example 之前, 你只需在 bootstrap.properties 配置文件配置spring.application.name即可,例如: + +[source,properties] +---- +spring.application.name=acm-config #注意,spring.application.name 必须要放在bootstrap.properties中 +---- + +启动这个Example,可以在控制台看到打印出的值正是我们在轻量版配置中心上预先配置的值。 + +[source,subs="normal"] +---- +2018-11-05 11:41:50.661 INFO 1541 --- [main] com.alibaba.demo.normal.ProviderApp : Started ProviderApp in 15.809 seconds (JVM running for 16.513) +user name :alibaba; age: 56 +2018-11-05 11:41:50.710 INFO 1541 --- [gPullingdefault] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@1f854e9e: startup date [Mon Nov 05 11:41:50 CST 2018]; root of context hierarchy +---- + +==== 基于yaml文件扩展名的配置 + +spring-cloud-starter-alicloud-acm 默认对文件扩展名为properties的支持,如果你习惯使用yaml格式来作为您的基础配置,也是可以支持的。这个时候你只需要在bootstrap.properties配置文件中显示的来声明使用的文件扩展名,如下所示 + +[source,properties] +---- +spring.application.name=acm-config +spring.cloud.nacos.config.file-extension=yaml #显示的声明使用的文件扩展名 +---- + +同时还需在轻量版配置中心的控制台新增一个dataid为yaml为扩展名的配置,如下所示: + +[source,subs="normal"] +---- +Group: DEFAULT_GROUP + +DataId: acm-config.yaml + +Content: user.name: yaml-alibaba + user.age: 102 +---- + +完成之后,重启测试程序,可以在控制台看到输出的结果的值。 + +[source,subs="normal"] +---- +2018-11-05 11:50:13.829 INFO 1674 --- [main] com.alibaba.demo.normal.ProviderApp : Started ProviderApp in 15.091 seconds (JVM running for 15.626) +user name :yaml-alibaba; age: 102 +2018-11-05 11:50:13.868 INFO 1674 --- [gPullingdefault] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@7f64acf5: startup date [Mon Nov 05 11:50:13 CST 2018]; root of context hierarchy +---- + +==== 支持配置的动态更新 + +spring-cloud-starter-alicloud-acm 也支持配置的动态更新,如下所示,当变更user.name时,应用程序中能够获取到最新的值: + +[source,subs="normal"] +---- +user name :yaml-alibaba; age: 102 +user name :yaml-alibaba; age: 102 +2018-11-05 11:52:15.192 INFO 1707 --- [gPullingdefault] o.s.boot.SpringApplication : Started application in 0.162 seconds (JVM running for 26.869) +2018-11-05 11:52:15.193 INFO 1707 --- [gPullingdefault] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@36b222ff: startup date [Mon Nov 05 11:52:15 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@5cfd2308 +2018-11-05 11:52:15.193 INFO 1707 --- [gPullingdefault] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@5cfd2308: startup date [Mon Nov 05 11:52:15 CST 2018]; root of context hierarchy +//感知到配置发生变更 +user name :yaml-alibaba-update; age: 1020 +---- + +==== 可支持profile粒度的配置 + +spring-cloud-starter-alicloud 在加载配置的时候,不仅仅加载了以dataid为${spring.application.name}.${file-extension:properties}为前缀的基础配置,还加载了dataid为${spring.application.name}-${profile}.${file-extension:properties}的基础配置。在我们日常开发中如果遇到多套环境下的不同配置,我们可以打开Spring自带的配置功能,放置在application.properties 配置文件中。如下所示: + +[sources,properties] +---- +spring.profiles.active=${deploy.env} +---- + +其中 ${deploy.env}变量的值可以在启动应用时通过-Ddeploy.env=*****来动态指定。比如现在在轻量版配置中心上新增了一个dataid为:acm-config-develop.yaml的基础配置,如下所示: + +[source,subs="normal"] +---- +Group: DEFAULT_GROUP + +DataId: acm-config-develop.yaml + +Content: deploy.env: develop +---- + +同时启动应用的时候通过-Ddeploy.env=develop 来指定当前spring.profiles.active的值。 + +启动 Spring Boot 应用测试的代码如下: + +[source,java] +---- +@SpringBootApplication +public class ProviderApplication { + + public static void main(String[] args) { + ConfigurableApplicationContext applicationContext = SpringApplication.run(ProviderApp.class, args); + + while (true) { + String userName = applicationContext.getEnvironment().getProperty("user.name"); + String userAge = applicationContext.getEnvironment().getProperty("user.age"); + //获取当前的部署环境 + String deployEnv = applicationContext.getEnvironment().getProperty("deploy.env"); + System.err.println("Deploy Env:"+deployEnv+"\n \t user name :" + userName + "; age: " + userAge); + TimeUnit.SECONDS.sleep(1); + } + } +} +---- + +启动后,可见控制台的输出结果: + +[source,subs="normal"] +---- +2018-11-05 14:06:25.898 INFO 1852 --- [main] com.alibaba.demo.normal.ProviderApp : Started ProviderApp in 14.947 seconds (JVM running for 15.471) +Deploy Env:develop + user name :yaml-alibaba-update; age: 1020 +2018-11-05 14:06:25.939 INFO 1852 --- [gPullingdefault] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@75d62d18: startup date [Mon Nov 05 14:06:25 CST 2018]; root of context hierarchy +2018-11-05 14:06:25.967 INFO 1852 --- [gPullingdefault] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring +---- + +如果我们要切换到生产环境,那么只需要更改启动的-Ddeploy.env=product 参数即可。前提是生产环境 模式下轻量版配置中心已经添加了该环境的基础配置。例如dataid为:acm-config-product.yaml的配置: + +[source,subs="normal"] +---- +Group: DEFAULT_GROUP + +DataId: acm-config-product.yaml + +Content: deploy.env: Product +---- + +以-Ddeploy.env=product 启动测试程序,输出结果如下: + +[source,subs="normal"] +---- +2018-11-05 14:10:05.481 INFO 1857 --- [gPullingdefault] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@455d34c2: startup date [Mon Nov 05 14:10:05 CST 2018]; root of context hierarchy +Deploy Env:Product + user name :yaml-alibaba-update; age: 1020 +2018-11-05 14:10:06.798 INFO 1857 --- [ Thread-20] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@3899782c: startup date [Mon Nov 05 14:10:01 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@245b4bdc +---- + +==== 基于组级别配置的高级功能 + +当你习惯使用application.properties 或者 application.yaml 来作为你的配置时,为了能够区分其他应用的配置,这个时候可以开启 spring-cloud-starter-alicloud-acm 提供的基于组级别的配置。即只需在bootstrap.properties配置文件中添加spring.application.group配置来标识应用所属的分组即可。如下所示: + +[source,properties] +---- +spring.application.group=com.infrastructure.alibaba +---- + +这个时候注意在轻量版配置中心填写的dataid的格式为: + + ${spring.application.group}:application.${file-extension} + +如下所示: + +[source,subs="normal"] +---- +Group: DEFAULT_GROUP + +DataId: com.infrastructure.alibaba:application.yaml + +Content: owner.group: infrastructure +---- + +启动 Spring Boot 应用测试的代码如下: + +[source,java] +---- +@SpringBootApplication +public class ProviderApp { + + public static void main(String[] args) throws Exception{ + ConfigurableApplicationContext applicationContext = SpringApplication.run(ProviderApp.class, args); + + while (true) { + String userName = applicationContext.getEnvironment().getProperty("user.name"); + String userAge = applicationContext.getEnvironment().getProperty("user.age"); + String deployEnv = applicationContext.getEnvironment().getProperty("deploy.env"); + //获取当前应用所属的组 + String ownerGroup = applicationContext.getEnvironment().getProperty("owner.group"); + System.err.println("Deploy Env:"+deployEnv+"; in group :"+ownerGroup+"\n \tuser name :" + userName + "; age: " + userAge); + TimeUnit.SECONDS.sleep(1); + } + + } +} +---- + +测试输出的结果如下: + +[source,subs="normal"] +---- +2018-11-05 14:34:28.059 INFO 1902 --- [main] com.alibaba.demo.normal.ProviderApp : Started ProviderApp in 15.431 seconds (JVM running for 16.008) +Deploy Env:product; in group :Infrastructure + user name :yaml-alibaba-update; age: 1020 +2018-11-05 14:34:28.109 INFO 1902 --- [gPullingdefault] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@4846b0aa: startup date [Mon Nov 05 14:34:28 CST 2018]; root of context hierarchy +---- From dc0c8f08066ac8195b4c19c087558787bdd9c246 Mon Sep 17 00:00:00 2001 From: pengbingting Date: Wed, 7 Nov 2018 11:08:01 +0800 Subject: [PATCH 49/63] =?UTF-8?q?=E6=B7=BB=E5=8A=A0Nacos=E7=9A=84config?= =?UTF-8?q?=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/asciidoc-zh/nacos-config.adoc | 236 +++++++++++++++++- 1 file changed, 235 insertions(+), 1 deletion(-) diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc index e933cff8c..be46404b7 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc @@ -1 +1,235 @@ -== Spring Cloud Alibaba Nacos Config += Spring Cloud Alibaba Nacos Config + +== 快速开始 + +=== 基于properties的文件扩展名的配置方式 + +==== Nacos 服务端初始化 + +1、启动Nacos Server。启动方式可见 https://nacos.io/zh-cn/docs/quick-start.html[Naocs 官网] + +2、启动好Nacos之后,在Nacos添加如下的配置。注意data id是以 properties为扩展名,默认的文件扩展名方式。如下所示: + +[source,subs="normal"] +---- +Data ID: nacos-config.properties + +Group : DEFAULT_GROUP + +配置格式: TEXT + +配置内容: user.name: nacos-config-properties + user.age: 90 +---- + +==== 客户端使用方式 + +为了能够在应用程序中使用Nacos作为Sping Cloud Config的后端存储服务,在您构建Spring Boot 应用的同时添加一个Spring Boot Starter org.springframework.cloud:spring-cloud-starter-alibaba-nacos-config。以下是一个基础的maven 依赖配置: + +[source,xml] +---- + + org.springframework.boot + spring-boot-starter-parent + 2.0.5.RELEASE + + + + + + + org.springframework.cloud + spring-cloud-dependencies + Finchley.SR1 + pom + import + + + org.springframework.cloud + spring-cloud-alibaba-dependencies + 0.2.0.RELEASE + pom + import + + + + + + + org.springframework.cloud + spring-cloud-starter-alibaba-nacos-config + + + org.springframework.boot + spring-boot-starter + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + +---- + +现在就可以创建一个标准的Spring Boot的应用。 + +[source,java] +---- +@SpringBootApplication +public class ProviderApplication { + + public static void main(String[] args) { + ConfigurableApplicationContext applicationContext = SpringApplication.run(ProviderApplication.class, args); + String userName = applicationContext.getEnvironment().getProperty("user.name"); + String userAge = applicationContext.getEnvironment().getProperty("user.age"); + System.err.println("user name :"+userName+"; age: "+userAge); + } +} +---- + +spring-cloud-starter-alibaba-nacos-config 对于Nacos服务端的基础配置没有默认值,因此在运行此Example 之前, 必须使用 bootstrap.properties 配置文件来配置Nacos Server地址,例如: + +[source,properties] +---- +spring.application.name=nacos-config #注意,spring.application.name 必须要放在bootstrap.properties中 +spring.cloud.nacos.config.server-addr=127.0.0.1:8848 +---- + +启动这个Example,可以在控制台看到打印出的值正是在Nacos上预先配置好的值。 + +[source,subs="normal"] +---- +2018-11-02 14:24:51.638 INFO 32700 --- [main] c.a.demo.provider.ProviderApplication : Started ProviderApplication in 14.645 seconds (JVM running for 15.139) +user name :nacos-config-properties; age: 90 +2018-11-02 14:24:51.688 INFO 32700 --- [-127.0.0.1:8848] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@a8c5e74: startup date [Fri Nov 02 14:24:51 CST 2018]; root of context hierarchy +2018-11 +---- + +=== 基于yaml的文件扩展名的配置方式 + +spring-cloud-starter-alibaba-nacos-config 默认对文件扩展名为properties的支持,如果习惯使用yaml格式来作为应用中的基础配置,也是可以支持的。这个时候只需要完成以下两步: + +1、在bootstrap.properties配置文件中显示的来声明使用的文件扩展名。如下所示 +[source,properties] +---- +spring.application.name=nacos-config +spring.cloud.nacos.config.server-addr=127.0.0.1:8848 +spring.cloud.nacos.config.file-extension=yaml #显示的声明使用的文件扩展名 +---- + +2、在Nacos的控制台新增一个dataid为yaml为扩展名的配置,如下所示: + +[source,subs="normal"] +---- +Data ID: nacos-config.yaml + +Group : DEFAULT_GROUP + +配置格式: YAML + +配置内容: user.name: nacos-config-yaml + user.age: 68 +---- + +这两步完成后,重启测试程序,可以在控制台看到输出是以dataid为opensource-service-provider3.yaml配置的值。 + +[source,subs="normal"] +---- +2018-11-02 14:59:00.484 INFO 32928 --- [main] c.a.demo.provider.ProviderApplication:Started ProviderApplication in 14.183 seconds (JVM running for 14.671) +user name :nacos-config-yaml; age: 68 +2018-11-02 14:59:00.529 INFO 32928 --- [-127.0.0.1:8848] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@265a478e: startup date [Fri Nov 02 14:59:00 CST 2018]; root of context hierarchy +---- + +=== 支持配置的动态更新 + +spring-cloud-starter-alibaba-nacos-config 也支持配置的动态更新,如下所示,当变更user.name时,应用程序中能够获取到最新的值: + +[source,subs="normal"] +---- +user name :nacos-config-yaml; age: 68 +user name :nacos-config-yaml; age: 68 +user name :nacos-config-yaml; age: 68 +2018-11-02 15:04:25.069 INFO 32957 --- [-127.0.0.1:8848] o.s.boot.SpringApplication : Started application in 0.144 seconds (JVM running for 71.752) +2018-11-02 15:04:25.070 INFO 32957 --- [-127.0.0.1:8848] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@10c89124: startup date [Fri Nov 02 15:04:25 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@6520af7 +2018-11-02 15:04:25.071 INFO 32957 --- [-127.0.0.1:8848] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6520af7: startup date [Fri Nov 02 15:04:24 CST 2018]; root of context hierarchy +user name :nacos-config-yaml-update; age: 68 +user name :nacos-config-yaml-update; age: 68 +---- + +=== 可支持profile粒度的配置 + +spring-cloud-starter-alibaba-nacos-config 在加载配置的时候,不仅仅加载了以dataid为${spring.application.name}.${file-extension:properties}为前缀的基础配置,还加载了dataid为${spring.application.name}-${profile}.${file-extension:properties}的基础配置。在日常开发中如果遇到多套环境下的不同配置,可以打开Spring自带的配置功能。 + +[source,properties] +---- +spring.profiles.active=${deploy.env} +---- + +其中 ${deploy.env}变量的值可以在启动应用时通过-Ddeploy.env=*****来动态指定。比如现在在Nacos上新增了一个dataid为:nacos-config-develop.yaml的基础配置,如下所示: + +[source,subs="normal"] +---- +Data ID: nacos-config-develop.yaml + +Group : DEFAULT_GROUP + +配置格式: YAML + +配置内容: current.env: develop-env +---- + +同时启动应用的时候通过-Ddeploy.env=develop 来指定当前spring.profiles.active的值。 +启动 Spring Boot 应用测试的代码如下: + +[source,java] +---- +@SpringBootApplication +public class ProviderApplication { + + public static void main(String[] args) { + ConfigurableApplicationContext applicationContext = SpringApplication.run(ProviderApplication.class, args); + while(true) { + String userName = applicationContext.getEnvironment().getProperty("user.name"); + String userAge = applicationContext.getEnvironment().getProperty("user.age"); + //获取当前部署的环境 + String currentEnv = applicationContext.getEnvironment().getProperty("current.env"); + System.err.println("in "+currentEnv+" enviroment; "+"user name :" + userName + "; age: " + userAge); + TimeUnit.SECONDS.sleep(1); + } + } +} +---- +启动后,可见控制台的输出结果: + +[source,subs="normal"] +---- +in develop-evn enviroment; user name :nacos-config-yaml-update; age: 68 +2018-11-02 15:34:25.013 INFO 33014 --- [ Thread-11] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6f1c29b7: startup date [Fri Nov 02 15:33:57 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@63355449 +---- + +如果需要切换到生产环境,那么只需要更改启动的-Ddeploy.env=product 参数即可。前提是生产环境上Nacos已经添加了该环境的基础配置。例如,现在在生成环境下的Naocs添加了dataid为:nacos-config-product.yaml的配置: + +[source,subs="normal"] +---- +Data ID: nacos-config-product.yaml + +Group : DEFAULT_GROUP + +配置格式: YAML + +配置内容: current.env: product-env +---- + +以-Ddeploy.env=product 启动测试程序,输出结果如下: + +[source,subs="normal"] +---- +in product-env enviroment; user name :nacos-config-yaml-update; age: 68 +2018-11-02 15:42:14.628 INFO 33024 --- [Thread-11] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6aa8e115: startup date [Fri Nov 02 15:42:03 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@19bb07ed +---- + + From aa8adf5c044645834e9feeb7a1b01cac562a685a Mon Sep 17 00:00:00 2001 From: flystar32 Date: Wed, 7 Nov 2018 20:37:40 +0800 Subject: [PATCH 50/63] update readme --- README-zh.md | 6 +++++- README.md | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/README-zh.md b/README-zh.md index 72a20fc65..06732dba4 100644 --- a/README-zh.md +++ b/README-zh.md @@ -73,7 +73,11 @@ Spring Cloud 使用 Maven 来构建,最快的使用方式是将本项目clone ### Reference Doc -[文档地址](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/tree/master/spring-cloud-alibaba-docs/src/main/asciidoc-zh) +[目录](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-docs/src/main/asciidoc-zh/spring-cloud-alibaba.adoc) + +[Nacos Config](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc) + +[ACM](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-docs/src/main/asciidoc-zh/acm.adoc) ## 演示 Demo diff --git a/README.md b/README.md index c69092a13..c70bd1f18 100644 --- a/README.md +++ b/README.md @@ -37,7 +37,51 @@ Spring Cloud uses Maven for most build-related activities, and you should be abl ./mvnw install -## How to use +## How to Use + +### Add maven dependency +Version 0.2.0.RELEASE is compatible with the Spring Boot 2.0.x line. Version 0.1.0.RELEASE is compatible with the Spring Boot 1.x line. + +These artifacts are available from Maven Central and Spring Release repository via BOM: + + + + + org.springframework.cloud + spring-cloud-alibaba-dependencies + 0.2.0.REALEASE + pom + import + + + + +add the module in `dependencies`. + +If you want to use the latest BUILD-SNAPSHOT version, add `Spring Snapshot Repository` in pom.xml , **Attention: BUILD-SNAPSHOT may be updated in any time** + + + + spring-snapshot + Spring Snapshot Repository + https://repo.spring.io/snapshot + + true + + + + + +### Reference Doc + +[Contents](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-docs/src/main/asciidoc-zh/spring-cloud-alibaba.adoc) + +[Nacos Config](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc) + +[ACM](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-docs/src/main/asciidoc-zh/acm.adoc) + + +## Examples A `spring-cloud-alibaba-examples` module is included in our project for you to get started with Spring Cloud Alibaba quickly. It contains an example, and you can refer to the readme file in the example project for a quick walkthrough. From 9fbe1aa3c502a5dc4461f5a44629b9ac66d4f86f Mon Sep 17 00:00:00 2001 From: xiaolongzuo <150349407@qq.com> Date: Thu, 8 Nov 2018 14:59:05 +0800 Subject: [PATCH 51/63] Add ANS and ACM readme doc. --- .../acm-local-example/readme-zh.md | 46 +++++++++++++++++ .../src/main/resources/bootstrap.properties | 1 - .../ans-consumer-feign-example/readme-zh.md | 25 ++++++++++ .../ans-consumer-ribbon-example/readme-zh.md | 25 ++++++++++ .../ans-provider-example/readme-zh.md | 35 +++++++++++++ .../oss-example/readme-zh.md | 48 ++++++------------ .../oss-example/readme.md | 50 ++++++------------- .../src/main/resources/oss-test.json | 2 +- 8 files changed, 162 insertions(+), 70 deletions(-) create mode 100644 spring-cloud-alibaba-examples/acm-example/acm-local-example/readme-zh.md create mode 100644 spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/readme-zh.md create mode 100644 spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/readme-zh.md create mode 100644 spring-cloud-alibaba-examples/ans-example/ans-provider-example/readme-zh.md diff --git a/spring-cloud-alibaba-examples/acm-example/acm-local-example/readme-zh.md b/spring-cloud-alibaba-examples/acm-example/acm-local-example/readme-zh.md new file mode 100644 index 000000000..f76577aa9 --- /dev/null +++ b/spring-cloud-alibaba-examples/acm-example/acm-local-example/readme-zh.md @@ -0,0 +1,46 @@ +# ACM Local Example + +## 项目说明 + +本项目展示了,在Spring Cloud规范下,如何以最简单且免费的方式,使用ACM产品,将配置统一管理。 + +应用配置管理(Application Configuration Management,简称 ACM),其前身为淘宝内部配置中心 Diamond,是一款应用配置中心产品。基于该应用配置中心产品,您可以在微服务、DevOps、大数据等场景下极大地减轻配置管理的工作量的同时,保证配置的安全合规。更多 ACM 相关的信息,请参考 [ACM官网](https://www.aliyun.com/product/acm)。 + +## 示例 + +### 准备工作 + +ACM支持直接使用免费的轻量版配置中心,进行开发和调试工作。本示例也是基于轻量版配置中心的,因此我们需要首先安装和启动轻量版配置中心。 + +1. [下载轻量版配置中心](https://edas-public.oss-cn-hangzhou.aliyuncs.com/install_package/LCC/2018-11-01/edas-lite-configcenter.tar.gz?file=edas-lite-configcenter.tar.gz) + +2. 解压 edas-lite-configcenter.tar.gz ,然后执行以下命令。 + + cd edas-config-center && sh startup.sh + +出现以下内容说明轻量版配置中心启动成功。 + + Edas-config-center has been started successfully. + You can see more details in logs/config-center.log. + +3. 进入页面 http://127.0.0.1:8080,在左侧"配置列表"页面中,点击"添加"按钮,添加如下配置。 + + Group:DEFAULT_GROUP + DataId:acm-local.properties + Content:user.id=xiaolongzuo + +### 启动应用 + +直接运行main class,即`AcmApplication`。 + +### 查看效果 + +1. 使用`curl`可以看到在配置中心配置的user.id。 + + curl http://127.0.0.1:18089/ + +2. 进入页面 http://127.0.0.1:8080,在左侧"配置列表"页面中,更改user.id的值以后,再次使用`curl`命令,可以看到配置变化。 + + +如果您对 Spring Cloud ACM Starter 有任何建议或想法,欢迎提交 issue 中或者通过其他社区渠道向我们反馈。 + diff --git a/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/resources/bootstrap.properties b/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/resources/bootstrap.properties index 448a0c7dd..40947c21a 100644 --- a/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/resources/bootstrap.properties +++ b/spring-cloud-alibaba-examples/acm-example/acm-local-example/src/main/resources/bootstrap.properties @@ -1,4 +1,3 @@ -spring.application.group=com.alibaba.acm spring.application.name=acm-local server.port=18089 spring.cloud.alicloud.acm.server-list=127.0.0.1 diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/readme-zh.md b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/readme-zh.md new file mode 100644 index 000000000..4b8ad3728 --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-feign-example/readme-zh.md @@ -0,0 +1,25 @@ +# ANS Consumer Feign Example + +## 项目说明 + +本项目展示了,在Spring Cloud规范下,如何以最简单且免费的方式,使用Feign客户端,调用一个服务。 + +## 示例 + +### 准备工作 + +ans-provider-example已经成功启动,并发布服务成功。 + +### 启动应用 + +直接运行main class,即`ConsumerApplication`。 + +### 查看效果 + +使用`curl`命令可以看到应用打印出相应的调用日志,命令如下。 + + curl http://127.0.0.1:18082/ + + +如果您对 Spring Cloud ANS Starter 有任何建议或想法,欢迎提交 issue 中或者通过其他社区渠道向我们反馈。 + diff --git a/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/readme-zh.md b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/readme-zh.md new file mode 100644 index 000000000..b15eb89c6 --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-consumer-ribbon-example/readme-zh.md @@ -0,0 +1,25 @@ +# ANS Consumer Ribbon Example + +## 项目说明 + +本项目展示了,在Spring Cloud规范下,如何以最简单且免费的方式,使用Ribbon客户端,调用一个服务。 + +## 示例 + +### 准备工作 + +ans-provider-example已经成功启动,并发布服务成功。 + +### 启动应用 + +直接运行main class,即`ConsumerApplication`。 + +### 查看效果 + +使用`curl`命令可以看到应用打印出相应的调用日志,命令如下。 + + curl http://127.0.0.1:18082/ + + +如果您对 Spring Cloud ANS Starter 有任何建议或想法,欢迎提交 issue 中或者通过其他社区渠道向我们反馈。 + diff --git a/spring-cloud-alibaba-examples/ans-example/ans-provider-example/readme-zh.md b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/readme-zh.md new file mode 100644 index 000000000..82b158740 --- /dev/null +++ b/spring-cloud-alibaba-examples/ans-example/ans-provider-example/readme-zh.md @@ -0,0 +1,35 @@ +# ANS Provider Example + +## 项目说明 + +本项目展示了,在Spring Cloud规范下,如何以最简单且免费的方式,发布一个服务。 + +## 示例 + +### 准备工作 + +ANS支持直接使用免费的轻量版配置中心,进行开发和调试工作。本示例也是基于轻量版配置中心的,因此我们需要首先安装和启动轻量版配置中心。 + +1. [下载轻量版配置中心](https://edas-public.oss-cn-hangzhou.aliyuncs.com/install_package/LCC/2018-11-01/edas-lite-configcenter.tar.gz?file=edas-lite-configcenter.tar.gz) + +2. 解压 edas-lite-configcenter.tar.gz ,然后执行以下命令。 + + cd edas-config-center && sh startup.sh + +出现以下内容说明轻量版配置中心启动成功。 + + Edas-config-center has been started successfully. + You can see more details in logs/config-center.log. + + +### 启动应用 + +直接运行main class,即`ProviderApplication`。 + +### 查看效果 + +进入页面 http://127.0.0.1:8080,在左侧"服务列表"页面中,可以看到一条名为`ans-provider`的服务。 + + +如果您对 Spring Cloud ANS Starter 有任何建议或想法,欢迎提交 issue 中或者通过其他社区渠道向我们反馈。 + diff --git a/spring-cloud-alibaba-examples/oss-example/readme-zh.md b/spring-cloud-alibaba-examples/oss-example/readme-zh.md index 56ac5cb68..54cc88d28 100644 --- a/spring-cloud-alibaba-examples/oss-example/readme-zh.md +++ b/spring-cloud-alibaba-examples/oss-example/readme-zh.md @@ -11,7 +11,7 @@ ### 接入 OSS 在启动示例进行演示之前,我们先了解一下如何接入 OSS。 -**注意:本节只是为了便于您理解接入方式,本示例代码中已经完成接入工作,您只需修改 accessKeyId、secretAccessKey、region 即可。** +**注意:本节只是为了便于您理解接入方式,本示例代码中已经完成接入工作,您只需修改 accessKey、secretKey、endpoint 即可。** 1. 修改 pom.xml 文件,引入 alicloud-oss starter。 @@ -20,24 +20,24 @@ spring-cloud-starter-alicloud-oss -2. 在配置文件中配置 OSS 服务对应的 accessKeyId、secretAccessKey 和 region。 +2. 在配置文件中配置 OSS 服务对应的 accessKey、secretKey 和 endpoint。 // application.properties - spring.cloud.alibaba.oss.accessKeyId=your-ak - spring.cloud.alibaba.oss.secretAccessKey=your-sk - spring.cloud.alibaba.oss.region=cn-beijing + spring.cloud.alicloud.access-key=your-ak + spring.cloud.alicloud.secret-key=your-sk + spring.cloud.alicloud.oss.endpoint=*** - 以阿里云 accessKeyId、secretAccessKey 为例,获取方式如下。 + 以阿里云 accessKey、secretKey 为例,获取方式如下。 i. 在阿里云控制台界面,单击右上角头像,选择 accesskeys,或者直接登录[用户信息管理界面](https://usercenter.console.aliyun.com/): ![undefined](https://cdn.nlark.com/lark/0/2018/png/64647/1535371973274-3ebec90a-ebde-4eb7-96ed-5372f6b32fe0.png) - ii. 获取 accessKeyId、secretAccessKey: + ii. 获取 accessKey、secretKey: ![undefined](https://cdn.nlark.com/lark/0/2018/png/64647/1535372168883-b94a3d77-3f81-4938-b409-611945a9e21c.png) - **注意:**如果您使用了阿里云 [STS服务](https://help.aliyun.com/document_detail/28756.html) 进行短期访问权限管理,则除了 accessKeyId、secretAccessKey、region 以外,还需配置 securityToken。 + **注意:**如果您使用了阿里云 [STS服务](https://help.aliyun.com/document_detail/28756.html) 进行短期访问权限管理,则除了 accessKey、secretKey、endpoint 以外,还需配置 securityToken。 3. 注入 OSSClient 并进行文件上传下载等操作。 @@ -61,9 +61,9 @@ spring.application.name=oss-example server.port=18084 - spring.cloud.alibaba.oss.accessKeyId=your-ak - spring.cloud.alibaba.oss.secretAccessKey=your-sk - spring.cloud.alibaba.oss.region=cn-beijing + spring.cloud.alicloud.access-key=your-ak + spring.cloud.alicloud.secret-key=your-sk + spring.cloud.alicloud.oss.endpoint=*** 2. 通过 IDE 直接启动或者编译打包后启动应用。 @@ -72,7 +72,7 @@ 1. 执行 `mvn clean package` 将工程编译打包; 2. 执行 `java -jar oss-example.jar`启动应用。 -应用启动后会自动在 OSS 上创建一个名为 `spring-cloud-alibaba` 的 Bucket。 +应用启动后会自动在 OSS 上创建一个名为 `spring-cloud-alibaba-test` 的 Bucket。 ### 上传或下载文件 @@ -96,7 +96,7 @@ 显示结果: // 如果配置正确,则输出 - download success, content: { "name": "spring-cloud-alibaba", "github": "https://github.com/spring-cloud-incubator/spring-cloud-alibaba", "authors": ["Jim", "flystar32"], "emails": ["fangjian0423@gmail.com", "flystar32@163.com"] } + download success, content: { "name": "oss-test" } // 下载的过程中如果发生异常,则会输出download fail: fail reason。比如accessKeyId配置错误,则fail reason内容如下 download fail: The OSS Access Key Id you provided does not exist in our records. [ErrorCode]: InvalidAccessKeyId [RequestId]: RequestId [HostId]: xxx.oss-cn-beijing.aliyuncs.com [ResponseError]: InvalidAccessKeyId The OSS Access Key Id you provided does not exist in our records. RequestId sxxx.oss-cn-beijing.aliyuncs.com xxx-accessKeyId @@ -106,11 +106,11 @@ 完成文件上传或者下载操作后,可以登录 OSS 控制台进行验证。 -1. 登陆[OSS控制台](https://oss.console.aliyun.com/),可以看到左侧 Bucket 列表新增一个名字为`spring-cloud-alibaba`的 Bucket。 +1. 登陆[OSS控制台](https://oss.console.aliyun.com/),可以看到左侧 Bucket 列表新增一个名字为`spring-cloud-alibaba-test`的 Bucket。 ![undefined](https://cdn.nlark.com/lark/0/2018/png/64647/1535369224513-387afdf9-6078-4a42-9f18-d9fe9926a9cd.png) -2. 单击`spring-cloud-alibaba` Bucket,选择 `文件管理` 页签,发现上传的 oss-test 文件在 custom-dir 目录中。上传的 objectName 为`custom-dir/oss-test`。目录和文件以'/'符号分割。 +2. 单击`spring-cloud-alibaba-test` Bucket,选择 `文件管理` 页签,发现上传的 oss-test 文件。上传的 objectName 为`oss-test.json`。目录和文件以'/'符号分割。 ![undefined](https://cdn.nlark.com/lark/0/2018/png/64647/1535615378605-df1381e9-c5ff-4da1-b3b3-ce9acfef313f.png) @@ -134,24 +134,6 @@ Endpoint 内部会显示所有的 OSSClient 配置信息,以及该 OSSClient ![undefined](https://cdn.nlark.com/lark/0/2018/png/64647/1535373658171-20674565-6fe1-4e1e-a596-1dd6f4159ec3.png) - -## 多个 OSSClient 场景 - -如果您需要配置多个 OSSClient,类似多数据源的配置,则可以先构造 `OSSProperties`,再构造 `OSSClient`,并分别为每个 OSSClient 配置相应的 accessKeyId、secretAccessKey 等信息。 - - @Bean - @ConfigurationProperties(prefix = "spring.cloud.alibaba.oss1") - public OSSProperties ossProperties1() { - return new OSSProperties(); - } - - @Bean - public OSS ossClient1(@Qualifier("ossProperties1") OSSProperties ossProperties) { - return new OSSClientBuilder().build(ossProperties.getEndpoint(), - ossProperties.getAccessKeyId(), ossProperties.getSecretAccessKey(), - ossProperties.getSecurityToken(), ossProperties.getConfiguration()); - } -

以 Resource 的形式读取文件

OSS Starter 支持以 Resource 的形式得到文件对象。如果只需读取少量文件,您可以使用这种方式。 diff --git a/spring-cloud-alibaba-examples/oss-example/readme.md b/spring-cloud-alibaba-examples/oss-example/readme.md index 17dfbbdc3..11ea77c82 100644 --- a/spring-cloud-alibaba-examples/oss-example/readme.md +++ b/spring-cloud-alibaba-examples/oss-example/readme.md @@ -11,7 +11,7 @@ If your applications are Spring Cloud applications and you need to use Alibaba C ### Connect to OSS Before we start the demo, let's learn how to connect OSS to a Spring Cloud application. -**Note: This section is to show you how to connect to oss. The actual configurations have been completed in the following example, and you only need to specify your accessKeyId, secretAccessKey and region.** +**Note: This section is to show you how to connect to oss. The actual configurations have been completed in the following example, and you only need to specify your accessKey, secretKey and endpoint.** 1. Add dependency spring-cloud-starter-alicloud-oss in the pom.xml file in your Spring Cloud project. @@ -23,21 +23,21 @@ Before we start the demo, let's learn how to connect OSS to a Spring Cloud appli 2. Configure accessKeyId, secretAccessKey and region in application.properties. // application.properties - spring.cloud.alibaba.oss.accessKeyId=your-ak - spring.cloud.alibaba.oss.secretAccessKey=your-sk - spring.cloud.alibaba.oss.region=cn-beijing + spring.cloud.alicloud.access-key=your-ak + spring.cloud.alicloud.secret-key=your-sk + spring.cloud.alicloud.oss.endpoint=*** - To get accessKeyId, secretAccessKey, follow these steps: + To get accessKey, secretKey, follow these steps: 1. On the Alibaba Cloud console, click your avatar on the upper-right corner and click accesskeys. Or visit [User Management](https://usercenter.console.aliyun.com/) page directly: ![undefined](https://cdn.nlark.com/lark/0/2018/png/64647/1535464041257-5c7ae997-daff-45b3-89d4-02d578da4ac7.png) - 2. Get your accessKeyId、secretAccessKey: + 2. Get your accessKey、secretKey: ![undefined](https://cdn.nlark.com/lark/0/2018/png/64647/1535464098793-517491f6-156b-4a98-a5a4-6113cb3c01a4.png) - **Note:** If you are using [STS](https://www.alibabacloud.com/help/doc-detail/28756.html), you should configure securityToken in addition to accessKeyId, secretAccessKey, and region. + **Note:** If you are using [STS](https://www.alibabacloud.com/help/doc-detail/28756.html), you should configure securityToken in addition to accessKey, secretKey, and endpoint. 3. Inject OSSClient and use it to upload files to the OSS server and download a file from OSS server. @@ -60,9 +60,9 @@ Before we start the demo, let's learn how to connect OSS to a Spring Cloud appli spring.application.name=oss-example server.port=18084 - spring.cloud.alibaba.oss.accessKeyId=your-ak - spring.cloud.alibaba.oss.secretAccessKey=your-sk - spring.cloud.alibaba.oss.region=cn-beijing + spring.cloud.alicloud.access-key=your-ak + spring.cloud.alicloud.secret-key=your-sk + spring.cloud.alicloud.oss.endpoint=*** 2. Start the application in IDE or by building a fatjar. @@ -71,7 +71,7 @@ Before we start the demo, let's learn how to connect OSS to a Spring Cloud appli 1. Execute command `mvn clean package` to build a fatjar. 2. Run command `java -jar oss-example.jar` to start the application. -After startup, a bucket called 'spring-cloud-alibaba' is automatically created in OSS. +After startup, a bucket called 'spring-cloud-alibaba-test' is automatically created in OSS. ### Upload or download files @@ -88,14 +88,14 @@ Results: upload fail: The OSS Access Key Id you provided does not exist in our records. [ErrorCode]: InvalidAccessKeyId [RequestId]: RequestId [HostId]: xxx.oss-cn-beijing.aliyuncs.com [ResponseError]: InvalidAccessKeyId The OSS Access Key Id you provided does not exist in our records. RequestId xxx.oss-cn-beijing.aliyuncs.com xxx-accessKeyId #### Download files -Use `curl` command to download files. It will download the oss-test.json file that you uploaded just now and print in result): +Use `curl` command to download files. It will download the oss-test.json file that you uploaded just now and print in result: curl http://localhost:18084/download Results: // If configurations are correct, the output will be as follows - download success, content: { "name": "spring-cloud-alibaba", "github": "https://github.com/spring-cloud-incubator/spring-cloud-alibaba", "authors": ["Jim", "flystar32"], "emails": ["fangjian0423@gmail.com", "flystar32@163.com"] } + download success, content: { "name": "oss-tes" } // If an error occurs during downloading, the output will be 'download fail: fail reason'. For example, if accessKeyId is wrong,fail reason will be as follows download fail: The OSS Access Key Id you provided does not exist in our records. [ErrorCode]: InvalidAccessKeyId [RequestId]: RequestId [HostId]: xxx.oss-cn-beijing.aliyuncs.com [ResponseError]: InvalidAccessKeyId The OSS Access Key Id you provided does not exist in our records. RequestId sxxx.oss-cn-beijing.aliyuncs.com xxx-accessKeyId @@ -103,11 +103,11 @@ Results: ### Verify results on OSS You can verify results on the OSS console when you finish uploading or downloading files. -1. Log on to the [OSS console](https://oss.console.aliyun.com/),and you will find a bucket named `spring-cloud-alibaba`. +1. Log on to the [OSS console](https://oss.console.aliyun.com/),and you will find a bucket named `spring-cloud-alibaba-test`. ![undefined](https://cdn.nlark.com/lark/0/2018/png/64647/1535464204462-ccebb9e0-7233-499c-8dec-8b8348231b2b.png) -2. Click the `spring-cloud-alibaba` bucket, select the Files tab, and you will find the oss-test file. The file 'oss-test' is located in directory 'custom-dir'. The objectName of the file is 'custom-dir/oss-test'. File directory and file is separated by '/'. +2. Click the `spring-cloud-alibaba-test` bucket, select the Files tab, and you will find the oss-test.json file. The objectName of the file is 'oss-test.json'. File directory and file is separated by '/'. ![undefined](https://cdn.nlark.com/lark/0/2018/png/64647/1535618026281-613a338c-f89c-4c7b-8b04-d404d1320699.png) @@ -133,26 +133,6 @@ Endpoint will show the configurations and the list of buckets of all OSSClients. ![undefined](https://cdn.nlark.com/lark/0/2018/png/64647/1535373658171-20674565-6fe1-4e1e-a596-1dd6f4159ec3.png) -## Multiple OSSClients - -If you need multiple OSSClients,like Multi DataSources, build `OSSProperties` first,and then build `OSSClient`. Specify information such as assessKeyId and secrectAccessKey for each OSSClient. - - @Bean - @ConfigurationProperties(prefix = "spring.cloud.alibaba.oss1") - public OSSProperties ossProperties1() { - return new OSSProperties(); - } - - @Bean - public OSS ossClient1(@Qualifier("ossProperties1") OSSProperties ossProperties) { - return new OSSClientBuilder().build(ossProperties.getEndpoint(), - ossProperties.getAccessKeyId(), ossProperties.getSecretAccessKey(), - ossProperties.getSecurityToken(), ossProperties.getConfiguration()); - } - - -* OSSClient shutdown:You do not need to shutdown OSSClient. It will be done in `OSSApplicationListener`. -

Read file using resource mode

OSS Starter supports getting file objects by `Spring Resource`. Simply configure OSS protocol of the resource: diff --git a/spring-cloud-alibaba-examples/oss-example/src/main/resources/oss-test.json b/spring-cloud-alibaba-examples/oss-example/src/main/resources/oss-test.json index f1683e94f..68682f46e 100644 --- a/spring-cloud-alibaba-examples/oss-example/src/main/resources/oss-test.json +++ b/spring-cloud-alibaba-examples/oss-example/src/main/resources/oss-test.json @@ -1,3 +1,3 @@ { - "name": "chenzhu-test" + "name": "oss-test" } \ No newline at end of file From 5ab8c2385f547c445b8638ffe783d895b5083717 Mon Sep 17 00:00:00 2001 From: xiaolongzuo <150349407@qq.com> Date: Thu, 8 Nov 2018 15:03:28 +0800 Subject: [PATCH 52/63] Add ANS and ACM readme doc. --- README-zh.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README-zh.md b/README-zh.md index 06732dba4..d30367c85 100644 --- a/README-zh.md +++ b/README-zh.md @@ -1,6 +1,6 @@ # Spring Cloud Alibaba -Spring Cloud Alibaba 致力于提供分布式应用服务开发的一站式解决方案。此项目包含开发分布式应用服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。 +Spring Cloud Alibaba 致力于提供基于微服务架构开发分布式应用的一站式解决方案。此项目包含开发分布式应用服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。 依托 Spring Cloud Alibaba,您只需要添加一些注解和少量配置,就可以将 Spring Cloud 应用接入阿里分布式应用解决方案,通过阿里中间件来迅速搭建分布式应用系统。 @@ -94,6 +94,10 @@ Example 列表: [AliCloud OSS Example](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/oss-example/readme-zh.md) +[AliCloud ANS Example](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/ans-example/ans-provider-example/readme-zh.md) + +[AliCloud ACM Example](https://github.com/spring-cloud-incubator/spring-cloud-alibaba/blob/master/spring-cloud-alibaba-examples/acm-example/acm-local-example/readme-zh.md) + ## 版本管理规范 项目的版本号格式为 x.x.x 的形式,其中 x 的数值类型为数字,从0开始取值,且不限于 0~9 这个范围。项目处于孵化器阶段时,第一位版本号固定使用0,即版本号为 0.x.x 的格式。 From 49ca7c6885ba713f75e282acc6552a904ed8eafc Mon Sep 17 00:00:00 2001 From: xiaojing Date: Thu, 8 Nov 2018 15:28:58 +0800 Subject: [PATCH 53/63] update readme --- README-zh.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README-zh.md b/README-zh.md index d30367c85..dd151cf63 100644 --- a/README-zh.md +++ b/README-zh.md @@ -1,13 +1,13 @@ # Spring Cloud Alibaba -Spring Cloud Alibaba 致力于提供基于微服务架构开发分布式应用的一站式解决方案。此项目包含开发分布式应用服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。 +Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案。此项目包含开发分布式应用微服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。 -依托 Spring Cloud Alibaba,您只需要添加一些注解和少量配置,就可以将 Spring Cloud 应用接入阿里分布式应用解决方案,通过阿里中间件来迅速搭建分布式应用系统。 +依托 Spring Cloud Alibaba,您只需要添加一些注解和少量配置,就可以将 Spring Cloud 应用接入阿里微服务解决方案,通过阿里中间件来迅速搭建分布式应用系统。 ## 主要功能 -* **服务限流降级**:默认支持为 HTTP 服务的提供限流保护,也支持添加注解实现方法的自定义限流降级,且支持动态修改限流降级规则。 +* **服务限流降级**:默认支持 Servlet、RestTemplate、Dubbo 和 RocketMQ 限流降级功能的接入,可以在运行时通过控制台实时修改限流降级规则,还支持查看限流降级 Metrics 监控。 * **服务注册与发现**:适配 Spring Cloud 服务注册与发现标准,默认集成了 Ribbon 的支持。 * **分布式配置管理**:支持分布式系统中的外部化配置,配置更改时自动刷新。 * **阿里云对象存储**:阿里云提供的海量、安全、低成本、高可靠的云存储服务。支持在任何应用、任何时间、任何地点存储和访问任意类型的数据。 From 9450826f47c41a217253580ff895e24e674cec14 Mon Sep 17 00:00:00 2001 From: flystar32 Date: Thu, 8 Nov 2018 19:07:17 +0800 Subject: [PATCH 54/63] fix typo --- README-zh.md | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README-zh.md b/README-zh.md index dd151cf63..3c26ba94d 100644 --- a/README-zh.md +++ b/README-zh.md @@ -48,7 +48,7 @@ Spring Cloud 使用 Maven 来构建,最快的使用方式是将本项目clone org.springframework.cloud spring-cloud-alibaba-dependencies - 0.2.0.REALEASE + 0.2.0.RELEASE pom import diff --git a/README.md b/README.md index c70bd1f18..e45c7e2d1 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,7 @@ These artifacts are available from Maven Central and Spring Release repository v org.springframework.cloud spring-cloud-alibaba-dependencies - 0.2.0.REALEASE + 0.2.0.RELEASE pom import From 80368052af2be87ffb9826572d639c32363b8f69 Mon Sep 17 00:00:00 2001 From: xiaolongzuo <150349407@qq.com> Date: Thu, 8 Nov 2018 19:28:33 +0800 Subject: [PATCH 55/63] Update ref doc. --- .../src/main/asciidoc-zh/spring-cloud-alibaba.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/spring-cloud-alibaba.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/spring-cloud-alibaba.adoc index 6a05ccde7..77083a323 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/spring-cloud-alibaba.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/spring-cloud-alibaba.adoc @@ -9,7 +9,7 @@ xiaojing; xiaolongzuo; jim fang; bingting peng == 介绍 -Spring Cloud Alibaba 致力于提供分布式应用服务开发的一站式解决方案。此项目包含开发分布式应用服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。 +Spring Cloud Alibaba 致力于提供微服务开发的一站式解决方案。此项目包含开发分布式应用服务的必需组件,方便开发者通过 Spring Cloud 编程模型轻松使用这些组件来开发分布式应用服务。 依托 Spring Cloud Alibaba,您只需要添加一些注解和少量配置,就可以将 Spring Cloud 应用接入阿里分布式应用解决方案,通过阿里中间件来迅速搭建分布式应用系统。 From 88a870dc5de692277ae8e4a0fea82272419a81f6 Mon Sep 17 00:00:00 2001 From: pengbingting Date: Tue, 13 Nov 2018 09:51:36 +0800 Subject: [PATCH 56/63] =?UTF-8?q?=E6=9B=B4=E6=94=B9Nacos=E7=9A=84=E6=8B=BC?= =?UTF-8?q?=E5=86=99=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/asciidoc-zh/nacos-config.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc index be46404b7..b3f9fb5bd 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-config.adoc @@ -6,7 +6,7 @@ ==== Nacos 服务端初始化 -1、启动Nacos Server。启动方式可见 https://nacos.io/zh-cn/docs/quick-start.html[Naocs 官网] +1、启动Nacos Server。启动方式可见 https://nacos.io/zh-cn/docs/quick-start.html[Nacos 官网] 2、启动好Nacos之后,在Nacos添加如下的配置。注意data id是以 properties为扩展名,默认的文件扩展名方式。如下所示: From daa398dd3281d6467ac2ea2d158bb79dedaad79c Mon Sep 17 00:00:00 2001 From: xiaolongzuo <150349407@qq.com> Date: Tue, 13 Nov 2018 17:47:04 +0800 Subject: [PATCH 57/63] Update ref doc. --- .../src/main/asciidoc-zh/acm.adoc | 2 +- .../src/main/asciidoc-zh/ans.adoc | 110 ++++++++++++ .../src/main/asciidoc-zh/oss.adoc | 156 ++++++++++++++++++ 3 files changed, 267 insertions(+), 1 deletion(-) diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/acm.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/acm.adoc index 143e764a5..16ff206c0 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/acm.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/acm.adoc @@ -1,6 +1,6 @@ = Spring Cloud AliCloud ACM -Spring Cloud Alibaba Config 提供了和阿里云上的ACM的集成。使得在项目中如果需要上云的用户可以非常方便无缝的对接阿里云,来享受云端提供的稳定服务。 +Spring Cloud AliCloud ACM 提供了和阿里云上的ACM的集成。使得在项目中如果需要上云的用户可以非常方便无缝的对接阿里云,来享受云端提供的稳定服务。 === 快速开始 diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/ans.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/ans.adoc index b9b34ce24..63c839763 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/ans.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/ans.adoc @@ -1 +1,111 @@ == Spring Cloud AliCloud ANS + +ANS(Application Naming Service) 是隶属于阿里云EDAS产品的组件, Spring Cloud AliCloud ANS 提供了Spring Cloud规范下商业版的服务注册与发现,可以让用户方便的在本地开发,同时也可以运行在云环境里。 + +=== 如何引入 Spring Cloud AliCloud ANS + +Spring Cloud Alibaba 已经发布了0.2.0版本,需要首先导入依赖管理POM。 + +[source,xml] +---- + + + + org.springframework.cloud + spring-cloud-alibaba-dependencies + 0.2.0.RELEASE + pom + import + + + +---- + +接下来引入 Spring Cloud AliCloud ANS Starter 即可。 + +[source,xml] +---- + + org.springframework.cloud + spring-cloud-starter-alicloud-ans + +---- + +=== 使用ANS进行服务注册 + +当客户端引入了 Spring Cloud AliCloud ANS Starter 以后,服务的元数据会被自动注册到注册中心,比如IP、端口、权重等信息。客户端会与服务端保持心跳,来证明自己可以正常提供服务。 + +以下是一个简单的应用示例。 + +[source,java] +---- +@SpringBootApplication +@EnableDiscoveryClient +@RestController +public class ProviderApplication { + + @RequestMapping("/") + public String home() { + return "Hello world"; + } + + public static void main(String[] args) { + SpringApplication.run(ProviderApplication.class, args); + } + +} +---- + +既然服务会被注册到注册中心,那么肯定需要配置注册中心的地址,在application.properties中,还需要配置上以下地址。 + +[source,properties] +---- +# 应用名会被作为服务名称使用,因此会必选 +spring.application.name=ans-provider +server.port=18081 +# 以下就是注册中心的IP和端口配置,因为默认就是127.0.0.1和8080,因此以下两行配置也可以省略 +spring.cloud.alicloud.ans.server.list=127.0.0.1 +spring.cloud.alicloud.ans.server.port=8080 +---- + +NOTE: 此时没有启动注册中心,启动应用会报错,因此在应用启动之前,应当首先启动注册中心。 + +=== 启动注册中心 + +ANS使用的注册中心有两种,一种是完全免费的轻量版配置中心,主要用于开发和本地调试,一种是云上注册中心,ANS依托于阿里云EDAS产品提供服务注册的功能。通常情况下,可以使用轻量版配置中心作为开发和测试环境,使用云上的EDAS作为灰度和生产环境。 + +==== 启动轻量版配置中心 + +轻量版配置中心的下载和启动方式可参考 https://help.aliyun.com/document_detail/44163.html?spm=a2c4g.11186623.6.677.5f206b82Z2mTCF[这里] + +NOTE: 只需要进行第1步(下载轻量配置中心)和第2步(启动轻量配置中心)即可,第3步(配置hosts)在与 ANS 结合使用时,不需要操作。 + +启动完轻量版配置中心以后,直接启动 ProviderApplication ,即可将服务注册到轻量版配置中心,由于轻量版配置中心的默认端口是8080,因此你可以打开 http://127.0.0.1:8080 ,点击左侧"服务列表",查看注册上来的服务。 + +==== 使用云上注册中心 + +使用云上注册中心,可以省去服务端的维护工作,同时稳定性也会更有保障。当使用云上注册中心时,代码部分和使用轻量配置中心并没有区别,但是配置上会有一些区别。 + +以下是一个简单的使用云上配置中心的配置示例。 + +[source,properties] +---- +# 应用名会被作为服务名称使用,因此是必选 +spring.application.name=ans-provider +# 端口配置自由配置即可 +server.port=18081 +# 以下就是注册中心的IP和端口配置,因为默认就是127.0.0.1和8080,因此以下两行配置也可以省略 +spring.cloud.alicloud.ans.server-mode=EDAS +spring.cloud.alicloud.access-key=你的阿里云AK +spring.cloud.alicloud.secret-key=你的阿里云SK +spring.cloud.alicloud.edas.namespace=cn-xxxxx +---- + +server-mode 的默认值为 LOCAL ,如果要使用云上注册中心,则需要更改为 EDAS 。 + +access-key 和 secret-key 则是阿里云账号的AK/SK,需要首先注册阿里云账号,然后登陆 https://usercenter.console.aliyun.com/#/manage/ak[阿里云AK/SK管理页面] ,即可看到 AccessKey ID 和 Access Key Secret ,如果没有的话,需要点击"创建AccessKey"按钮创建。 + +namespace 是阿里云EDAS产品的概念,用于隔离不同的环境,比如测试环境和生产环境。要获取 namespace 需要 https://common-buy.aliyun.com/?spm=5176.11451019.0.0.6f5965c0Uq5tue&commodityCode=edaspostpay#/buy[开通EDAS服务],按量计费模式下开通是免费的,开通以后进入 https://edas.console.aliyun.com/#/namespaces?regionNo=cn-hangzhou[EDAS控制台],即可看到对应的namespace,比如cn-hangzhou。 + +NOTE: EDAS提供应用托管服务,如果你将应用托管到EDAS,那么EDAS将会自动为你填充所有配置。 + diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/oss.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/oss.adoc index 20b43f75f..778a8560c 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/oss.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/oss.adoc @@ -1 +1,157 @@ == Spring Cloud AliCloud OSS + +OSS(Object Storage Service)是阿里云的一款对象存储服务产品, Spring Cloud AliCloud OSS 提供了Spring Cloud规范下商业版的对象存储服务,提供简单易用的API,并且支持与 Spring 框架中 Resource 的整合。 + +=== 如何引入 Spring Cloud AliCloud OSS + +Spring Cloud Alibaba 已经发布了0.2.0版本,需要首先导入依赖管理POM。 + +[source,xml] +---- + + + + org.springframework.cloud + spring-cloud-alibaba-dependencies + 0.2.0.RELEASE + pom + import + + + +---- + +接下来引入 Spring Cloud AliCloud OSS Starter 即可。 + +[source,xml] +---- + + org.springframework.cloud + spring-cloud-starter-alicloud-oss + +---- + +=== 如何使用 OSS API + +==== 配置 OSS + +使用 Spring Cloud AliCloud OSS 之前,需要在 application.properties 中加入以下配置。 + +[source,properties] +---- +spring.cloud.alicloud.access-key=你的阿里云AK +spring.cloud.alicloud.secret-key=你的阿里云SK +spring.cloud.alicloud.oss.endpoint=***.aliyuncs.com +---- + +access-key 和 secret-key 是阿里云账号的AK/SK,需要首先注册阿里云账号,然后登陆 https://usercenter.console.aliyun.com/#/manage/ak[阿里云AK/SK管理页面] ,即可看到 AccessKey ID 和 Access Key Secret ,如果没有的话,需要点击"创建AccessKey"按钮创建。 + +endpoint可以到 OSS 的 https://help.aliyun.com/document_detail/31837.html?spm=a2c4g.11186623.2.9.7dc72841Z2hGqa#concept-zt4-cvy-5db[官方文档]中查看,根据所在的 region ,填写对应的 endpoint 即可。 + + +==== 引入 OSS API + +Spring Cloud Alicloud OSS 中的 OSS API 基于阿里云官方OSS SDK提供,具备上传、下载、查看等所有对象存储类操作API。 + +一个简单的使用 OSS API 的应用如下。 + +[source,java] +---- +@SpringBootApplication +public class OssApplication { + + @Autowired + private OSS ossClient; + + @RequestMapping("/") + public String home() { + ossClient.putObject("bucketName", "fileName", new FileInputStream("/your/local/file/path")); + return "upload success"; + } + + public static void main(String[] args) throws URISyntaxException { + SpringApplication.run(OssApplication.class, args); + } + +} +---- + +在上传文件之前,首先需要 https://account.aliyun.com/register/register.htm?spm=5176.8142029.388261.26.e9396d3eaYK2sG&oauth_callback=https%3A%2F%2Fwww.aliyun.com%2F[注册阿里云账号] ,如果已经有的话,请 https://common-buy.aliyun.com/?spm=5176.8465980.unusable.dopen.4cdf1450rg8Ujb&commodityCode=oss#/open[开通OSS服务]。 + +进入 https://oss.console.aliyun.com/overview[OSS控制台],点击左侧"新建Bucket",按照提示创建一个Bucket,然后将bucket名称替换掉上面代码中的"bucketName",而"fileName"取任意文件名,"/your/local/file/path"取任意本地文件路径,然后 curl http://127.0.0.1:端口/ 即可上传文件,可以到 https://oss.console.aliyun.com/overview[OSS控制台]查看效果。 + +更多关于 OSS API 的操作,可以参考 https://help.aliyun.com/document_detail/32008.html[OSS官方SDK文档]。 + +=== 与 Spring 框架的 Resource 结合 + +Spring Cloud AliCloud OSS 整合了 Spring 框架的 Resource 规范,可以让用户很方便的引用 OSS 的资源。 + +一个简单的使用 Resource 的例子如下。 + +[source,java] +---- +@SpringBootApplication +public class OssApplication { + + @Value("oss://bucketName/fileName") + private Resource file; + + @GetMapping("/file") + public String fileResource() { + try { + return "get file resource success. content: " + StreamUtils.copyToString( + file.getInputStream(), Charset.forName(CharEncoding.UTF_8)); + } catch (Exception e) { + return "get resource fail: " + e.getMessage(); + } + } + + public static void main(String[] args) throws URISyntaxException { + SpringApplication.run(OssApplication.class, args); + } + +} +---- + +NOTE: 以上示例运行的前提是,在 OSS 上需要有名为"bucketName"的Bucket,同时在该Bucket下,存在名为"fileName"的文件。 + +=== 采用 STS 授权 + +Spring Cloud AliCloud OSS 除了 AccessKey/SecretKey 的授权方式以外,还支持 STS 授权方式。 STS 是临时访问令牌的方式,一般用于授权第三方,临时访问自己的资源。 + +作为第三方,也就是被授权者,只需要配置以下内容,就可以访问临时被授权的资源。 + +[source,properties] +---- +spring.cloud.alicloud.oss.authorization-mode=STS +spring.cloud.alicloud.oss.endpoint=***.aliyuncs.com +spring.cloud.alicloud.oss.sts.access-key=你被授权的AK +spring.cloud.alicloud.oss.sts.secret-key=你被授权的SK +spring.cloud.alicloud.oss.sts.security-token=你被授权的ST +---- + +其中 spring.cloud.alicloud.oss.authorization-mode 是枚举类型,此时填写 STS ,代表采用 STS 的方式授权。 endpoint可以到 OSS 的 https://help.aliyun.com/document_detail/31837.html?spm=a2c4g.11186623.2.9.7dc72841Z2hGqa#concept-zt4-cvy-5db[官方文档]中查看,根据所在的 region ,填写对应的 endpoint 即可。 + +access-key、secret-key和security-token需要由授权方颁发,如果对 STS 不了解的话,可以参考 https://help.aliyun.com/document_detail/31867.html[STS官方文档]。 + +=== 更多客户端配置 + +除了基本的配置项以外, Spring Cloud AliCloud OSS 还支持很多额外的配置,也是在 application.properties 文件中。 + +以下是一些简单的示例。 + +[source,properties] +---- +spring.cloud.alicloud.oss.authorization-mode=STS +spring.cloud.alicloud.oss.endpoint=***.aliyuncs.com +spring.cloud.alicloud.oss.sts.access-key=你被授权的AK +spring.cloud.alicloud.oss.sts.secret-key=你被授权的SK +spring.cloud.alicloud.oss.sts.security-token=你被授权的ST + +spring.cloud.alicloud.oss.config.connection-timeout=3000 +spring.cloud.alicloud.oss.config.max-connections=1000 +---- + +如果想了解更多的配置项,可以参考 https://help.aliyun.com/document_detail/32010.html?spm=a2c4g.11186623.6.703.50b25413nGsYHc[OSSClient配置项] 的末尾表格。 + +NOTE: 通常情况下,都需要将 https://help.aliyun.com/document_detail/32010.html?spm=a2c4g.11186623.6.703.50b25413nGsYHc[OSSClient配置项] 末尾表格中的参数名更换成"-"连接,且所有字母小写。例如 ConnectionTimeout,对应 connection-timeout。 \ No newline at end of file From 3b0cc579a84eed21d220f16039eda33bbcc644da Mon Sep 17 00:00:00 2001 From: pengbingting Date: Wed, 14 Nov 2018 17:38:36 +0800 Subject: [PATCH 58/63] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20Nacos=20Discovery=20?= =?UTF-8?q?=E7=9A=84reference=20=E6=96=87=E6=A1=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/asciidoc-zh/nacos-discovery.adoc | 320 +++++++++++++++++- 1 file changed, 319 insertions(+), 1 deletion(-) diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc index 21a1177bf..d4a9a4c63 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc @@ -1 +1,319 @@ -== Spring Cloud Alibaba Nacos Discovery += Spring Cloud Alibaba Nacos Discovery + +该项目通过自动配置以及其他 Spring 编程模型的习惯用法为Spring Boot 应用程序在服务注册与发现方面提供和Nacos的无缝集成。 +通过一些简单的注解,您可以快速来注册一个服务,并使用经过双十一考验的Nacos组件来作为大规模分布式系统的服务注册中心。 + +== 1. 服务注册发现: Nacos Discovery Starter + +服务发现是微服务架构体系中最关键的组件之一。如果尝试着用手动的方式来给每一个客户端来配置所有服务提供者的服务列表是一件非常困难的事,而且也不利于 +服务的动态扩缩容。Nacos Discovery Starter 可以帮助您将服务自动注册到 Nacos 服务端并且能够动态感知和刷新某个服务实例的服务列表。除此之外,Nacos +Discovery Starter 也将服务实例自身的一些元数据信息-例如 host,port,健康检查URL,主页等-注册到 Nacos 。Nacos 的获取和启动方式可以参考 https://nacos.io/zh-cn/docs/quick-start.html[Nacos 官网]。 + +=== 如何引入 Nacos Discovery Starter + +为了能够在你的工程下引入 Nacos Discovey Starter,使用group ID 为 `org.springframework.cloud` 和 artifact ID 为 `spring-cloud-starter-alibaba-nacos-discovery`。 +pom.xml 示例如下所示: + +[source,xml,indent=0] +---- + + + + + org.springframework.cloud + spring-cloud-alibaba-dependencies + 0.2.0.RELEASE + pom + import + + + + + + + + org.springframework.cloud + spring-cloud-starter-alibaba-nacos-discovery + + +---- + +=== 启动一个 Provider 应用 + +如果您使用的 Spring Cloud 版本是 Finchley.SR1 版本,那么这个时候您的Spring Boot版本的选择可需要额外的小心了,因为版本的不匹对,可能会导致许多意外的效果。 +Spring Cloud 的 Finchley.SR1 版本最佳实践的 Spring Boot 版本是 2.0.6.RELEASE。在启动您的一个 Provider 应用时请检查依赖的 Spring Boot 版本是否是 +1.X.Y.RELEASE 或者 2.1.0.RELEASE 的版本。如果不是,请更正到 2.0.6.RELEASE 版本。 + +以下步骤向您展示了如何将一个服务注册到 Nacos。 + +1. pom.xml的配置。一个完整的 pom.xml 配置如下所示: +[source, xml] +---- + + + 4.0.0 + + open.source.test + nacos-discovery-test + 1.0-SNAPSHOT + nacos-discovery-test + + + org.springframework.boot + spring-boot-starter-parent + 2.0.6.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + + org.springframework.cloud + spring-cloud-dependencies + Finchley.SR1 + pom + import + + + org.springframework.cloud + spring-cloud-alibaba-dependencies + 0.2.0.RELEASE + pom + import + + + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.boot + spring-boot-starter-actuator + + + + org.springframework.cloud + spring-cloud-starter-alibaba-nacos-discovery + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + +---- + +2. application.propertie配置。一些关于 Nacos 基本的配置也必须在 application.properties(也可以是application.yaml)配置,如下所示: + +application.propertoes +[source,properties] +---- +server.port=8081 +spring.application.name=nacos-producer +spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 +management.endpoints.web.exposure.include=* +---- + + +NOTE: 如果不想使用 Nacos 作为您的服务注册与发现,可以将 `spring.cloud.nacos.discovery` 设置为 `false`。 + +3. 启动 Provider 示例。如下所示: +[source,java,indent=0] +---- +@SpringBootApplication +@EnableDiscoveryClient +public class NacosProviderDemoApplication { + + public static void main(String[] args) { + SpringApplication.run(NacosProducerDemoApplication.class, args); + } + + @RestController + public class EchoController { + @GetMapping(value = "/echo/{string}") + public String echo(@PathVariable String string) { + return "Hello Nacos Discovery " + string; + } + } +} +---- + +这个时候你就可以在 Nacos的控制台上看到注册上来的服务信息了。 + +NOTE: 再启动 Provider 应用之前 请先将 Nacos 服务启动。具体启动方式可参考 https://nacos.io/zh-cn/docs/quick-start.html[Naco 官网]。 + +=== 服务的 EndPoint + +spring-cloud-starter-alibaba-nacos-discovery 在实现的时候提供了一个EndPoint,EndPoint的访问地址为`http://ip:port/actuator/nacos-discovery`。 +EndPoint 的信息主要提供了两类: + + 1. subscribe: 显示了当前有哪些服务订阅者 + 2、NacosDiscoveryProperties: 显示了当前服务实例关于 Nacos 的基础配置 + +一个服务实例访问 EndPoint 的信息如下所示: + +[source, json] +---- +{ + "subscribe": [ + { + "jsonFromServer": "", + "name": "nacos-provider", + "clusters": "", + "cacheMillis": 10000, + "hosts": [ + { + "instanceId": "30.5.124.156#8081#DEFAULT#nacos-provider", + "ip": "30.5.124.156", + "port": 8081, + "weight": 1.0, + "healthy": true, + "enabled": true, + "cluster": { + "serviceName": null, + "name": null, + "healthChecker": { + "type": "TCP" + }, + "defaultPort": 80, + "defaultCheckPort": 80, + "useIPPort4Check": true, + "metadata": { + + } + }, + "service": null, + "metadata": { + + } + } + ], + "lastRefTime": 1541755293119, + "checksum": "e5a699c9201f5328241c178e804657e11541755293119", + "allIPs": false, + "key": "nacos-producer", + "valid": true + } + ], + "NacosDiscoveryProperties": { + "serverAddr": "127.0.0.1:8848", + "endpoint": "", + "namespace": "", + "logName": "", + "service": "nacos-provider", + "weight": 1.0, + "clusterName": "DEFAULT", + "metadata": { + + }, + "registerEnabled": true, + "ip": "30.5.124.201", + "networkInterface": "", + "port": 8082, + "secure": false, + "accessKey": "", + "secretKey": "" + } +} +---- + +=== 启动一个 Consumer 应用 + +Consumer 的应用可能还没像启动一个 Provider 应用那么简单。因为在 Consumer 端需要去调用 Provider 端提供的REST 服务。例子中我们使用最原始的一种方式, +即显示的使用 LoadBalanceClient 和 RestTemolate 结合的方式来访问。 +pom.xml 和 application.properties 的配置可以参考 1.2 小结。启动一个 Consumer应用的示例代码如下所示: + +NOTE: 通过带有负载均衡的RestTemplate 和 FeignClient 也是可以访问的。 + +[source, java] +---- +@SpringBootApplication +@EnableDiscoveryClient +public class NacosConsumerApp { + + @RestController + public class NacosController{ + + @Autowired + private LoadBalancerClient loadBalancerClient; + @Autowired + private RestTemplate restTemplate; + + @Value("${spring.application.name}") + private String appName; + + @GetMapping("/echo/app-name") + public String echoAppName(){ + //使用 LoadBalanceClient 和 RestTemolate 结合的方式来访问 + ServiceInstance serviceInstance = loadBalancerClient.choose("nacos-provider"); + String url = String.format("http://%s:%s/echo/%s",serviceInstance.getHost(),serviceInstance.getPort(),appName); + System.out.println("request url:"+url); + return restTemplate.getForObject(url,String.class); + } + + } + + //实例化 RestTemplate 实例 + @Bean + public RestTemplate restTemplate(){ + + return new RestTemplate(); + } + + public static void main(String[] args) { + + SpringApplication.run(NacosConsumerApp.class,args); + } +} +---- + +这个例子中我们注入了一个 LoadBalancerClient 的实例,并且手动的实例化一个 RestTemplate,同时将 `spring.application.name` 的配置值 注入到应用中来, +目的是调用 Provider 提供的服务时,希望将当前配置的应用名给显示出来。 + +NOTE: 再启动 Consumer 应用之前 请先将 Nacos 服务启动。具体启动方式可参考 https://nacos.io/zh-cn/docs/quick-start.html[Naco 官网]。 + +启动后,访问 Consumer 提供出来的 `http://ip:port/echo/app-name` 接口。我这里测试启动的 port是 8082。访问结果如下所示: + + 访问地址:http://127.0.0.1:8082/echo/app-name + 访问结果:Hello Nacos Discovery nacos-consumer + + +== 2. 关于 Nacos Starter 更多的配置项信息 + +更多关于 spring-cloud-starter-alibaba-nacos-discovery starter 的配置项如下所示 + +:frame: topbot +[width="60%",options="header"] +|==== +^|配置项 ^|Key ^|默认值 ^|说明 +|`服务端地址`|`spring.cloud.nacos.discovery.server-addr`|`无`| `Nacos Server 启动监听的ip地址和端口` +|`服务名`|`spring.cloud.nacos.discovery.service`|`${spring.application.name}`|`给当前的服务命名` +|`权重`|`spring.cloud.nacos.discovery.weight`|`1`|`取值范围 1 到 100,数值越大,权重越大` +|`网卡名`|`spring.cloud.nacos.discovery.network-interface`|`无`|`当IP未配置时,注册的IP为此网卡所对应的IP地址,如果此项也未配置,则默认取第一块网卡的地址` +|`注册的IP地址`|`spring.cloud.nacos.discovery.ip`|`无`|`优先级最高` +|`注册的端口`|`spring.cloud.nacos.discovery.port`|`-1`|`默认情况下不用配置,会自动探测` +|`命名空间`|`spring.cloud.nacos.discovery.namespace`|`无`|`常用场景之一是不同环境的注册的区分隔离,例如开发测试环境和生产环境的资源(如配置、服务)隔离等。` +|`AccessKey`|`spring.cloud.nacos.discovery.access-key`|`无`|`当要上阿里云时,阿里云上面的一个云账号名` +|`SecretKey`|`spring.cloud.nacos.discovery.secret-key`|`无`|`当要上阿里云时,阿里云上面的一个云账号密码` +|`Metadata`|`spring.cloud.nacos.discovery.metadata`|`无`|`使用Map格式配置,用户可以根据自己的需要自定义一些和服务相关的元数据信息` +|`日志文件名`|`spring.cloud.nacos.discovery.log-name`|`无`| +|`接入点`|`spring.cloud.nacos.discovery.enpoint`|`UTF-8`|`地域的某个服务的入口域名,通过此域名可以动态地拿到服务端地址` +|`是否集成Ribbon`|`ribbon.nacos.enabled`|`true`|`一般都设置成true即可` +|==== + From a40d6925f5c5848291d40618cbacfaef43742390 Mon Sep 17 00:00:00 2001 From: pengbingting Date: Wed, 14 Nov 2018 17:41:42 +0800 Subject: [PATCH 59/63] =?UTF-8?q?=E8=AE=A2=E6=AD=A3=20properties=E7=9A=84?= =?UTF-8?q?=E6=8B=BC=E5=86=99=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/asciidoc-zh/nacos-discovery.adoc | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc index d4a9a4c63..e6824aef3 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc @@ -1,7 +1,7 @@ = Spring Cloud Alibaba Nacos Discovery -该项目通过自动配置以及其他 Spring 编程模型的习惯用法为Spring Boot 应用程序在服务注册与发现方面提供和Nacos的无缝集成。 -通过一些简单的注解,您可以快速来注册一个服务,并使用经过双十一考验的Nacos组件来作为大规模分布式系统的服务注册中心。 +该项目通过自动配置以及其他 Spring 编程模型的习惯用法为 Spring Boot 应用程序在服务注册与发现方面提供和 Nacos 的无缝集成。 +通过一些简单的注解,您可以快速来注册一个服务,并使用经过双十一考验的 Nacos 组件来作为大规模分布式系统的服务注册中心。 == 1. 服务注册发现: Nacos Discovery Starter @@ -119,8 +119,7 @@ Spring Cloud 的 Finchley.SR1 版本最佳实践的 Spring Boot 版本是 2.0.6. ---- -2. application.propertie配置。一些关于 Nacos 基本的配置也必须在 application.properties(也可以是application.yaml)配置,如下所示: - +2. application.properties 配置。一些关于 Nacos 基本的配置也必须在 application.properties(也可以是application.yaml)配置,如下所示: application.propertoes [source,properties] ---- From 4644c65682706445c602a424a886c6447889035b Mon Sep 17 00:00:00 2001 From: pengbingting Date: Wed, 14 Nov 2018 17:42:57 +0800 Subject: [PATCH 60/63] =?UTF-8?q?=E8=AE=A2=E6=AD=A3=20properties=E7=9A=84?= =?UTF-8?q?=E6=8B=BC=E5=86=99=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/asciidoc-zh/nacos-discovery.adoc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc index e6824aef3..f54b72920 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc @@ -120,7 +120,7 @@ Spring Cloud 的 Finchley.SR1 版本最佳实践的 Spring Boot 版本是 2.0.6. ---- 2. application.properties 配置。一些关于 Nacos 基本的配置也必须在 application.properties(也可以是application.yaml)配置,如下所示: -application.propertoes +application.properties [source,properties] ---- server.port=8081 From 6a204c4a98a25bd61669ef1c28a1661075a4619f Mon Sep 17 00:00:00 2001 From: pengbingting Date: Wed, 14 Nov 2018 17:45:55 +0800 Subject: [PATCH 61/63] =?UTF-8?q?=E8=AE=A2=E6=AD=A3=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/asciidoc-zh/nacos-discovery.adoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc index f54b72920..7d971cad8 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc @@ -159,10 +159,10 @@ NOTE: 再启动 Provider 应用之前 请先将 Nacos 服务启动。具体启 === 服务的 EndPoint -spring-cloud-starter-alibaba-nacos-discovery 在实现的时候提供了一个EndPoint,EndPoint的访问地址为`http://ip:port/actuator/nacos-discovery`。 +spring-cloud-starter-alibaba-nacos-discovery 在实现的时候提供了一个EndPoint,EndPoint的访问地址为 `http://ip:port/actuator/nacos-discovery`。 EndPoint 的信息主要提供了两类: - 1. subscribe: 显示了当前有哪些服务订阅者 + 1、subscribe: 显示了当前有哪些服务订阅者 2、NacosDiscoveryProperties: 显示了当前服务实例关于 Nacos 的基础配置 一个服务实例访问 EndPoint 的信息如下所示: From 231c15bfe1d6c6f2e2a1a5adaf82914e65ad0cf8 Mon Sep 17 00:00:00 2001 From: pengbingting Date: Wed, 14 Nov 2018 17:48:35 +0800 Subject: [PATCH 62/63] =?UTF-8?q?=E8=AE=A2=E6=AD=A3=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/asciidoc-zh/nacos-discovery.adoc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc index 7d971cad8..4aad69351 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc @@ -285,7 +285,7 @@ public class NacosConsumerApp { 这个例子中我们注入了一个 LoadBalancerClient 的实例,并且手动的实例化一个 RestTemplate,同时将 `spring.application.name` 的配置值 注入到应用中来, 目的是调用 Provider 提供的服务时,希望将当前配置的应用名给显示出来。 -NOTE: 再启动 Consumer 应用之前 请先将 Nacos 服务启动。具体启动方式可参考 https://nacos.io/zh-cn/docs/quick-start.html[Naco 官网]。 +NOTE: 在启动 Consumer 应用之前请先将 Nacos 服务启动好。具体启动方式可参考 https://nacos.io/zh-cn/docs/quick-start.html[Nacos 官网]。 启动后,访问 Consumer 提供出来的 `http://ip:port/echo/app-name` 接口。我这里测试启动的 port是 8082。访问结果如下所示: @@ -295,7 +295,7 @@ NOTE: 再启动 Consumer 应用之前 请先将 Nacos 服务启动。具体启 == 2. 关于 Nacos Starter 更多的配置项信息 -更多关于 spring-cloud-starter-alibaba-nacos-discovery starter 的配置项如下所示 +更多关于 spring-cloud-starter-alibaba-nacos-discovery 的 starter 配置项如下所示: :frame: topbot [width="60%",options="header"] From e763b479be118a0fe133ce099e588d2a06f15124 Mon Sep 17 00:00:00 2001 From: pbting <314226532@qq.com> Date: Wed, 14 Nov 2018 18:00:23 +0800 Subject: [PATCH 63/63] =?UTF-8?q?=E5=8E=BB=E6=8E=89=E4=B8=80=E8=A1=8C?= =?UTF-8?q?=E7=A9=BA=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/asciidoc-zh/nacos-discovery.adoc | 1 - 1 file changed, 1 deletion(-) diff --git a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc index 4aad69351..388e68f67 100644 --- a/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc +++ b/spring-cloud-alibaba-docs/src/main/asciidoc-zh/nacos-discovery.adoc @@ -292,7 +292,6 @@ NOTE: 在启动 Consumer 应用之前请先将 Nacos 服务启动好。具体启 访问地址:http://127.0.0.1:8082/echo/app-name 访问结果:Hello Nacos Discovery nacos-consumer - == 2. 关于 Nacos Starter 更多的配置项信息 更多关于 spring-cloud-starter-alibaba-nacos-discovery 的 starter 配置项如下所示: