From 73cda20ae0c45456aa51e4b9c4a60d4cfaae801a Mon Sep 17 00:00:00 2001 From: mercyblitz Date: Thu, 4 Oct 2018 01:04:17 +0800 Subject: [PATCH 1/3] Polish spring-cloud-incubator/spring-cloud-alibaba#34 : Initialize spring-cloud-alibaba-nacos-config-server module --- pom.xml | 1 + spring-cloud-alibaba-dependencies/pom.xml | 13 ++- .../pom.xml | 63 ++++++++++++++ .../NacosConfigServerAutoConfiguration.java | 45 ++++++++++ .../NacosEnvironmentRepository.java | 85 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 3 + .../bootstrap/NacosConfigServerBootstrap.java | 37 ++++++++ .../src/test/resources/application.properties | 2 + 8 files changed, 246 insertions(+), 3 deletions(-) create mode 100644 spring-cloud-alibaba-nacos-config-server/pom.xml create mode 100644 spring-cloud-alibaba-nacos-config-server/src/main/java/org/springframework/cloud/alibaba/nacos/config/server/NacosConfigServerAutoConfiguration.java create mode 100644 spring-cloud-alibaba-nacos-config-server/src/main/java/org/springframework/cloud/alibaba/nacos/config/server/environment/NacosEnvironmentRepository.java create mode 100644 spring-cloud-alibaba-nacos-config-server/src/main/resources/META-INF/spring.factories create mode 100644 spring-cloud-alibaba-nacos-config-server/src/test/java/org/springframework/cloud/alibaba/nacos/config/server/bootstrap/NacosConfigServerBootstrap.java create mode 100644 spring-cloud-alibaba-nacos-config-server/src/test/resources/application.properties diff --git a/pom.xml b/pom.xml index a8388a1ec..8eb156b61 100644 --- a/pom.xml +++ b/pom.xml @@ -82,6 +82,7 @@ spring-cloud-starter-alibaba spring-cloud-starter-alicloud spring-cloud-alicloud-oss + spring-cloud-alibaba-nacos-config-server diff --git a/spring-cloud-alibaba-dependencies/pom.xml b/spring-cloud-alibaba-dependencies/pom.xml index 2ff41fded..dd34a46fb 100644 --- a/spring-cloud-alibaba-dependencies/pom.xml +++ b/spring-cloud-alibaba-dependencies/pom.xml @@ -18,17 +18,26 @@ 0.1.1 3.1.0 - 0.2.1 + 0.2.2-SNAPSHOT + com.alibaba.nacos nacos-client ${nacos.version} + + + com.alibaba.nacos + nacos-config + ${nacos.version} + + + com.alibaba.csp sentinel-core @@ -136,8 +145,6 @@ - - diff --git a/spring-cloud-alibaba-nacos-config-server/pom.xml b/spring-cloud-alibaba-nacos-config-server/pom.xml new file mode 100644 index 000000000..eb5b48f4a --- /dev/null +++ b/spring-cloud-alibaba-nacos-config-server/pom.xml @@ -0,0 +1,63 @@ + + + + org.springframework.cloud + spring-cloud-alibaba + 0.2.0.BUILD-SNAPSHOT + ../pom.xml + + 4.0.0 + + org.springframework.cloud + spring-cloud-alibaba-nacos-config-server + + + + + + com.alibaba.nacos + nacos-config + + + + + org.springframework.cloud + spring-cloud-config-server + + + + + org.springframework.boot + spring-boot-starter + true + + + + org.springframework.boot + spring-boot-starter-actuator + true + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + 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-alibaba-nacos-config-server/src/main/java/org/springframework/cloud/alibaba/nacos/config/server/NacosConfigServerAutoConfiguration.java b/spring-cloud-alibaba-nacos-config-server/src/main/java/org/springframework/cloud/alibaba/nacos/config/server/NacosConfigServerAutoConfiguration.java new file mode 100644 index 000000000..42461f239 --- /dev/null +++ b/spring-cloud-alibaba-nacos-config-server/src/main/java/org/springframework/cloud/alibaba/nacos/config/server/NacosConfigServerAutoConfiguration.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cloud.alibaba.nacos.config.server; + +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.cloud.alibaba.nacos.config.server.environment.NacosEnvironmentRepository; +import org.springframework.cloud.config.server.EnableConfigServer; +import org.springframework.cloud.config.server.config.ConfigServerAutoConfiguration; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; + +/** + * Nacos Config Server Auto-Configuration + * + * @author Mercy + * @since 0.2.0 + */ +@ConditionalOnClass(EnableConfigServer.class) // If class of @EnableConfigServer is present in class-path +@ComponentScan(basePackages = { + "com.alibaba.nacos.config.server", +}) +@AutoConfigureBefore(ConfigServerAutoConfiguration.class) +public class NacosConfigServerAutoConfiguration { + + @Bean + public NacosEnvironmentRepository nacosEnvironmentRepository() { + return new NacosEnvironmentRepository(); + } + +} diff --git a/spring-cloud-alibaba-nacos-config-server/src/main/java/org/springframework/cloud/alibaba/nacos/config/server/environment/NacosEnvironmentRepository.java b/spring-cloud-alibaba-nacos-config-server/src/main/java/org/springframework/cloud/alibaba/nacos/config/server/environment/NacosEnvironmentRepository.java new file mode 100644 index 000000000..50b054aa1 --- /dev/null +++ b/spring-cloud-alibaba-nacos-config-server/src/main/java/org/springframework/cloud/alibaba/nacos/config/server/environment/NacosEnvironmentRepository.java @@ -0,0 +1,85 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cloud.alibaba.nacos.config.server.environment; + +import com.alibaba.nacos.config.server.model.ConfigInfo; +import com.alibaba.nacos.config.server.service.PersistService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cloud.config.environment.Environment; +import org.springframework.cloud.config.environment.PropertySource; +import org.springframework.cloud.config.server.environment.EnvironmentRepository; +import org.springframework.util.StringUtils; + +import java.io.IOException; +import java.io.StringReader; +import java.util.Properties; + +import static com.alibaba.nacos.config.server.constant.Constants.DEFAULT_GROUP; + +/** + * Nacos {@link EnvironmentRepository} + * + * @author Mercy + * @since 0.2.0 + */ +public class NacosEnvironmentRepository implements EnvironmentRepository { + + @Autowired + private PersistService persistService; + + @Override + public Environment findOne(String application, String profile, String label) { + + String dataId = application + "-" + profile + ".properties"; + + ConfigInfo configInfo = persistService.findConfigInfo(dataId, DEFAULT_GROUP, label); + + return createEnvironment(configInfo, application, profile); + } + + private Environment createEnvironment(ConfigInfo configInfo, String application, String profile) { + + Environment environment = new Environment(application, profile); + + Properties properties = createProperties(configInfo); + + String propertySourceName = String.format("Nacos[application : %s , profile : %s]", application, profile); + + PropertySource propertySource = new PropertySource(propertySourceName, properties); + + environment.add(propertySource); + + return environment; + } + + private Properties createProperties(ConfigInfo configInfo) { + Properties properties = new Properties(); + String content = configInfo == null ? null : configInfo.getContent(); + if (StringUtils.hasText(content)) { + try { + properties.load(new StringReader(content)); + } catch (IOException e) { + throw new IllegalStateException("The format of content is a properties"); + } + } + return properties; + } + + private static String[] of(String... values) { + return values; + } +} diff --git a/spring-cloud-alibaba-nacos-config-server/src/main/resources/META-INF/spring.factories b/spring-cloud-alibaba-nacos-config-server/src/main/resources/META-INF/spring.factories new file mode 100644 index 000000000..861f60f02 --- /dev/null +++ b/spring-cloud-alibaba-nacos-config-server/src/main/resources/META-INF/spring.factories @@ -0,0 +1,3 @@ +# Auto-Configuration +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ +org.springframework.cloud.alibaba.nacos.config.server.NacosConfigServerAutoConfiguration \ No newline at end of file diff --git a/spring-cloud-alibaba-nacos-config-server/src/test/java/org/springframework/cloud/alibaba/nacos/config/server/bootstrap/NacosConfigServerBootstrap.java b/spring-cloud-alibaba-nacos-config-server/src/test/java/org/springframework/cloud/alibaba/nacos/config/server/bootstrap/NacosConfigServerBootstrap.java new file mode 100644 index 000000000..68d92e7db --- /dev/null +++ b/spring-cloud-alibaba-nacos-config-server/src/test/java/org/springframework/cloud/alibaba/nacos/config/server/bootstrap/NacosConfigServerBootstrap.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.cloud.alibaba.nacos.config.server.bootstrap; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.cloud.config.server.EnableConfigServer; + +/** + * Nacos Config Server Bootstrap + * + * @author Mercy + * @since 0.2.0 + */ +@EnableAutoConfiguration +@EnableConfigServer +public class NacosConfigServerBootstrap { + + public static void main(String[] args) { + System.setProperty("nacos.standalone", "true"); + SpringApplication.run(NacosConfigServerBootstrap.class); + } +} diff --git a/spring-cloud-alibaba-nacos-config-server/src/test/resources/application.properties b/spring-cloud-alibaba-nacos-config-server/src/test/resources/application.properties new file mode 100644 index 000000000..dcecabe77 --- /dev/null +++ b/spring-cloud-alibaba-nacos-config-server/src/test/resources/application.properties @@ -0,0 +1,2 @@ +spring.application.name=nacos-config-server +management.endpoints.web.exposure.include=* \ No newline at end of file From cf443d7af3000cd6b6d36cfcc670bebd9ee89529 Mon Sep 17 00:00:00 2001 From: xiaojing Date: Sat, 17 Nov 2018 22:18:47 +0800 Subject: [PATCH 2/3] update parent version --- spring-cloud-alibaba-nacos-config-server/pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spring-cloud-alibaba-nacos-config-server/pom.xml b/spring-cloud-alibaba-nacos-config-server/pom.xml index eb5b48f4a..036e7025d 100644 --- a/spring-cloud-alibaba-nacos-config-server/pom.xml +++ b/spring-cloud-alibaba-nacos-config-server/pom.xml @@ -5,7 +5,7 @@ org.springframework.cloud spring-cloud-alibaba - 0.2.0.BUILD-SNAPSHOT + 0.2.1.BUILD-SNAPSHOT ../pom.xml 4.0.0 @@ -60,4 +60,4 @@ - \ No newline at end of file + From a09d79bd2e95f0f7528c04ed9e846d0853003efd Mon Sep 17 00:00:00 2001 From: mercyblitz Date: Mon, 19 Nov 2018 16:18:33 +0800 Subject: [PATCH 3/3] Polish Config Server --- pom.xml | 11 +++++++++-- spring-cloud-alibaba-nacos-config-server/pom.xml | 1 + .../server/NacosConfigServerAutoConfiguration.java | 2 ++ .../server/bootstrap/NacosConfigServerBootstrap.java | 10 ++++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 38ac083ca..1969d6283 100644 --- a/pom.xml +++ b/pom.xml @@ -55,6 +55,13 @@ hengyunabc hengyunabc@gmail.com + + mercyblitz + Mercy Ma + mercyblitz@gmail.com + Alibaba + https://github.com/mercyblitz + @@ -81,14 +88,14 @@ spring-cloud-alibaba-sentinel-datasource spring-cloud-alibaba-nacos-config spring-cloud-alibaba-nacos-discovery + spring-cloud-alibaba-nacos-config-server + spring-cloud-alicloud-context spring-cloud-alibaba-examples spring-cloud-alibaba-test spring-cloud-alibaba-docs spring-cloud-starter-alibaba spring-cloud-starter-alicloud spring-cloud-alicloud-oss - spring-cloud-alibaba-nacos-config-server - spring-cloud-alicloud-context spring-cloud-alicloud-acm spring-cloud-alicloud-ans diff --git a/spring-cloud-alibaba-nacos-config-server/pom.xml b/spring-cloud-alibaba-nacos-config-server/pom.xml index 036e7025d..9287892e8 100644 --- a/spring-cloud-alibaba-nacos-config-server/pom.xml +++ b/spring-cloud-alibaba-nacos-config-server/pom.xml @@ -12,6 +12,7 @@ org.springframework.cloud spring-cloud-alibaba-nacos-config-server + Spring Cloud Alibaba Nacos Config Server diff --git a/spring-cloud-alibaba-nacos-config-server/src/main/java/org/springframework/cloud/alibaba/nacos/config/server/NacosConfigServerAutoConfiguration.java b/spring-cloud-alibaba-nacos-config-server/src/main/java/org/springframework/cloud/alibaba/nacos/config/server/NacosConfigServerAutoConfiguration.java index 42461f239..74a4903dc 100644 --- a/spring-cloud-alibaba-nacos-config-server/src/main/java/org/springframework/cloud/alibaba/nacos/config/server/NacosConfigServerAutoConfiguration.java +++ b/spring-cloud-alibaba-nacos-config-server/src/main/java/org/springframework/cloud/alibaba/nacos/config/server/NacosConfigServerAutoConfiguration.java @@ -23,6 +23,7 @@ import org.springframework.cloud.config.server.EnableConfigServer; import org.springframework.cloud.config.server.config.ConfigServerAutoConfiguration; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; +import org.springframework.context.annotation.Configuration; /** * Nacos Config Server Auto-Configuration @@ -35,6 +36,7 @@ import org.springframework.context.annotation.ComponentScan; "com.alibaba.nacos.config.server", }) @AutoConfigureBefore(ConfigServerAutoConfiguration.class) +@Configuration public class NacosConfigServerAutoConfiguration { @Bean diff --git a/spring-cloud-alibaba-nacos-config-server/src/test/java/org/springframework/cloud/alibaba/nacos/config/server/bootstrap/NacosConfigServerBootstrap.java b/spring-cloud-alibaba-nacos-config-server/src/test/java/org/springframework/cloud/alibaba/nacos/config/server/bootstrap/NacosConfigServerBootstrap.java index 68d92e7db..3e379457e 100644 --- a/spring-cloud-alibaba-nacos-config-server/src/test/java/org/springframework/cloud/alibaba/nacos/config/server/bootstrap/NacosConfigServerBootstrap.java +++ b/spring-cloud-alibaba-nacos-config-server/src/test/java/org/springframework/cloud/alibaba/nacos/config/server/bootstrap/NacosConfigServerBootstrap.java @@ -16,9 +16,11 @@ */ package org.springframework.cloud.alibaba.nacos.config.server.bootstrap; +import org.springframework.boot.ApplicationRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.cloud.config.server.EnableConfigServer; +import org.springframework.context.annotation.Bean; /** * Nacos Config Server Bootstrap @@ -34,4 +36,12 @@ public class NacosConfigServerBootstrap { System.setProperty("nacos.standalone", "true"); SpringApplication.run(NacosConfigServerBootstrap.class); } + + @Bean + public ApplicationRunner applicationRunner() { + + return args -> { + System.out.println("Running..."); + }; + } }