From 97df4a620bb1abdf485616db769821885d263fac Mon Sep 17 00:00:00 2001 From: hxy1991 Date: Wed, 31 Oct 2018 15:10:27 +0800 Subject: [PATCH 01/13] Add the "Nacos connect to ACM Server" example --- .../nacos-spring-boot-config-example/pom.xml | 2 +- .../spring/boot/NacosConfigApplication.java | 4 ++-- .../boot/controller/ConfigController.java | 12 ++++++------ .../src/main/resources/application.properties | 11 ++++++++++- .../src/main/resources/application.properties | 1 - .../cloud/controller/ConfigController.java | 18 +++++++++++++----- .../src/main/resources/bootstrap.properties | 16 ++++++++++++++-- .../example/spring/NacosConfiguration.java | 7 +++++-- .../spring/controller/ConfigController.java | 12 ++++++------ nacos-spring-example/pom.xml | 2 +- 10 files changed, 58 insertions(+), 27 deletions(-) delete mode 100644 nacos-spring-boot-example/nacos-spring-boot-discovery-example/src/main/resources/application.properties diff --git a/nacos-spring-boot-example/nacos-spring-boot-config-example/pom.xml b/nacos-spring-boot-example/nacos-spring-boot-config-example/pom.xml index 79395bb71..d9eb9d7b1 100644 --- a/nacos-spring-boot-example/nacos-spring-boot-config-example/pom.xml +++ b/nacos-spring-boot-example/nacos-spring-boot-config-example/pom.xml @@ -12,7 +12,7 @@ nacos-spring-boot-config-example - 0.2.0-RC1 + 0.2.0 diff --git a/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/NacosConfigApplication.java b/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/NacosConfigApplication.java index e94ebcb2e..3d141d494 100644 --- a/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/NacosConfigApplication.java +++ b/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/NacosConfigApplication.java @@ -5,10 +5,10 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication -@NacosPropertySource(dataId = "example", autoRefreshed = true) +@NacosPropertySource(dataId = "com.alibaba.nacos.example", autoRefreshed = true) public class NacosConfigApplication { public static void main(String[] args) { SpringApplication.run(NacosConfigApplication.class, args); } -} +} \ No newline at end of file diff --git a/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/controller/ConfigController.java b/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/controller/ConfigController.java index f024c77fa..2103e599e 100644 --- a/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/controller/ConfigController.java +++ b/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/controller/ConfigController.java @@ -11,16 +11,16 @@ @RequestMapping("config") public class ConfigController { - @Value("${useLocalCache:false}") - private boolean useLocalCache; + @Value("${connectTimeoutInMills:5000}") + private int connectTimeoutInMills; - public void setUseLocalCache(boolean useLocalCache) { - this.useLocalCache = useLocalCache; + public void setConnectTimeoutInMills(int connectTimeoutInMills) { + this.connectTimeoutInMills = connectTimeoutInMills; } @RequestMapping(value = "/get", method = GET) @ResponseBody - public boolean get() { - return useLocalCache; + public int get() { + return connectTimeoutInMills; } } \ No newline at end of file diff --git a/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/resources/application.properties b/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/resources/application.properties index 25dd747a8..5f675a214 100644 --- a/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/resources/application.properties +++ b/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/resources/application.properties @@ -1 +1,10 @@ -nacos.config.server-addr=127.0.0.1:8848 \ No newline at end of file +# \u5728\u547D\u540D\u7A7A\u95F4\u8BE6\u60C5\u5904\u53EF\u4EE5\u83B7\u53D6\u5230 endpoint \u548C namespace \u7684\u503C +nacos.config.endpoint=${endpoint} +nacos.config.namespace=${namespace} + +# \u63A8\u8350\u4F7F\u7528 RAM \u8D26\u6237\u7684 accessKey \u548C secretKey +nacos.config.access-key=${accessKey} +nacos.config.secret-key=${secretKey} + +# nacos.config.context-path \u56FA\u5B9A\u4E3A \u201Cdiamond-server/nacos\u201D +nacos.config.context-path=diamond-server/nacos \ No newline at end of file diff --git a/nacos-spring-boot-example/nacos-spring-boot-discovery-example/src/main/resources/application.properties b/nacos-spring-boot-example/nacos-spring-boot-discovery-example/src/main/resources/application.properties deleted file mode 100644 index db6453626..000000000 --- a/nacos-spring-boot-example/nacos-spring-boot-discovery-example/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ -nacos.discovery.server-addr=127.0.0.1:8848 \ No newline at end of file diff --git a/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/java/com/alibaba/nacos/example/spring/cloud/controller/ConfigController.java b/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/java/com/alibaba/nacos/example/spring/cloud/controller/ConfigController.java index 8a06680aa..381fd1bfe 100644 --- a/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/java/com/alibaba/nacos/example/spring/cloud/controller/ConfigController.java +++ b/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/java/com/alibaba/nacos/example/spring/cloud/controller/ConfigController.java @@ -3,18 +3,26 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; +import static org.springframework.web.bind.annotation.RequestMethod.GET; + @RestController @RequestMapping("/config") @RefreshScope public class ConfigController { - @Value("${useLocalCache:false}") - private boolean useLocalCache; + @Value("${connectTimeoutInMills:5000}") + private int connectTimeoutInMills; + + public void setConnectTimeoutInMills(int connectTimeoutInMills) { + this.connectTimeoutInMills = connectTimeoutInMills; + } - @RequestMapping("/get") - public boolean get() { - return useLocalCache; + @RequestMapping(value = "/get", method = GET) + @ResponseBody + public int get() { + return connectTimeoutInMills; } } \ No newline at end of file diff --git a/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/resources/bootstrap.properties b/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/resources/bootstrap.properties index 10ade11e6..f1e086f96 100644 --- a/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/resources/bootstrap.properties +++ b/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/resources/bootstrap.properties @@ -1,3 +1,15 @@ -spring.cloud.nacos.config.server-addr=127.0.0.1:8848 +# \u5728\u547D\u540D\u7A7A\u95F4\u8BE6\u60C5\u5904\u53EF\u4EE5\u83B7\u53D6\u5230 endpoint \u548C namespace \u7684\u503C +spring.cloud.nacos.config.endpoint=${endpoint} +spring.cloud.nacos.config.namespace=${namespace} -spring.application.name=example \ No newline at end of file +# \u63A8\u8350\u4F7F\u7528 RAM \u8D26\u6237\u7684 accessKey \u548C secretKey +spring.cloud.nacos.config.access-key=${accessKey} +spring.cloud.nacos.config.secret-key=${secretKey} + +# nacos.config.context-path \u56FA\u5B9A\u4E3A \u201Cdiamond-server/nacos\u201D +spring.cloud.nacos.config.context-path=diamond-server/nacos + +spring.application.name=com.alibaba.nacos.example + +# \u6307\u5B9A\u914D\u7F6E\u7684\u540E\u7F00\uFF0C\u652F\u6301 properties\u3001yaml\u3001yml\uFF0C\u9ED8\u8BA4\u4E3A properties +spring.cloud.nacos.config.file-extension=properties \ No newline at end of file diff --git a/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/NacosConfiguration.java b/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/NacosConfiguration.java index 9be6c1e28..42e66712a 100644 --- a/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/NacosConfiguration.java +++ b/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/NacosConfiguration.java @@ -6,8 +6,11 @@ import org.springframework.context.annotation.Configuration; @Configuration -@EnableNacosConfig(globalProperties = @NacosProperties(serverAddr = "127.0.0.1:8848")) -@NacosPropertySource(dataId = "example", autoRefreshed = true) +// 在命名空间详情处可以获取到 endpoint 和 namespace;accessKey 和 secretKey 推荐使用 RAM 账户的 +@EnableNacosConfig(globalProperties = @NacosProperties(endpoint = "${endpoint}", + namespace = "${namespace}", accessKey = "${accessKey}", + secretKey = "${secretKey}", contextPath = "diamond-server/nacos")) +@NacosPropertySource(dataId = "com.alibaba.nacos.example", autoRefreshed = true) public class NacosConfiguration { } \ No newline at end of file diff --git a/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/controller/ConfigController.java b/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/controller/ConfigController.java index 7103f3a3b..d0844e874 100644 --- a/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/controller/ConfigController.java +++ b/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/controller/ConfigController.java @@ -11,17 +11,17 @@ @RequestMapping("config") public class ConfigController { - @Value("${useLocalCache:false}") - private boolean useLocalCache; + @Value("${connectTimeoutInMills:5000}") + private int connectTimeoutInMills; - public void setUseLocalCache(boolean useLocalCache) { - this.useLocalCache = useLocalCache; + public void setConnectTimeoutInMills(int connectTimeoutInMills) { + this.connectTimeoutInMills = connectTimeoutInMills; } @RequestMapping(value = "/get", method = GET) @ResponseBody - public boolean get() { - return useLocalCache; + public int get() { + return connectTimeoutInMills; } } diff --git a/nacos-spring-example/pom.xml b/nacos-spring-example/pom.xml index db68d22e9..6b16330c4 100644 --- a/nacos-spring-example/pom.xml +++ b/nacos-spring-example/pom.xml @@ -25,7 +25,7 @@ 3.2.18.RELEASE 3.0.1 - 0.2.1-RC1 + 0.1.0-RC1 From ef011a15d3bd0568b8e93a76787839a00c3811dd Mon Sep 17 00:00:00 2001 From: hxy1991 Date: Fri, 2 Nov 2018 10:37:35 +0800 Subject: [PATCH 02/13] remove "context-path" configuration --- .../src/main/resources/application.properties | 5 +---- .../src/main/resources/bootstrap.properties | 3 --- .../com/alibaba/nacos/example/spring/NacosConfiguration.java | 3 +-- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/resources/application.properties b/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/resources/application.properties index 5f675a214..4ad91bd33 100644 --- a/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/resources/application.properties +++ b/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/resources/application.properties @@ -4,7 +4,4 @@ nacos.config.namespace=${namespace} # \u63A8\u8350\u4F7F\u7528 RAM \u8D26\u6237\u7684 accessKey \u548C secretKey nacos.config.access-key=${accessKey} -nacos.config.secret-key=${secretKey} - -# nacos.config.context-path \u56FA\u5B9A\u4E3A \u201Cdiamond-server/nacos\u201D -nacos.config.context-path=diamond-server/nacos \ No newline at end of file +nacos.config.secret-key=${secretKey} \ No newline at end of file diff --git a/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/resources/bootstrap.properties b/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/resources/bootstrap.properties index f1e086f96..30a089af7 100644 --- a/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/resources/bootstrap.properties +++ b/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/resources/bootstrap.properties @@ -6,9 +6,6 @@ spring.cloud.nacos.config.namespace=${namespace} spring.cloud.nacos.config.access-key=${accessKey} spring.cloud.nacos.config.secret-key=${secretKey} -# nacos.config.context-path \u56FA\u5B9A\u4E3A \u201Cdiamond-server/nacos\u201D -spring.cloud.nacos.config.context-path=diamond-server/nacos - spring.application.name=com.alibaba.nacos.example # \u6307\u5B9A\u914D\u7F6E\u7684\u540E\u7F00\uFF0C\u652F\u6301 properties\u3001yaml\u3001yml\uFF0C\u9ED8\u8BA4\u4E3A properties diff --git a/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/NacosConfiguration.java b/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/NacosConfiguration.java index 42e66712a..39623d154 100644 --- a/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/NacosConfiguration.java +++ b/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/NacosConfiguration.java @@ -8,8 +8,7 @@ @Configuration // 在命名空间详情处可以获取到 endpoint 和 namespace;accessKey 和 secretKey 推荐使用 RAM 账户的 @EnableNacosConfig(globalProperties = @NacosProperties(endpoint = "${endpoint}", - namespace = "${namespace}", accessKey = "${accessKey}", - secretKey = "${secretKey}", contextPath = "diamond-server/nacos")) + namespace = "${namespace}", accessKey = "${accessKey}", secretKey = "${secretKey}")) @NacosPropertySource(dataId = "com.alibaba.nacos.example", autoRefreshed = true) public class NacosConfiguration { From f68aaee929a9049fa7dc727d47fcee04973cf130 Mon Sep 17 00:00:00 2001 From: hxy1991 Date: Sat, 1 Dec 2018 16:20:44 +0800 Subject: [PATCH 03/13] Change @Value to @NacosValue --- .../example/spring/boot/controller/ConfigController.java | 4 ++-- .../nacos/example/spring/controller/ConfigController.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/controller/ConfigController.java b/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/controller/ConfigController.java index 2103e599e..136941a6f 100644 --- a/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/controller/ConfigController.java +++ b/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/controller/ConfigController.java @@ -1,6 +1,6 @@ package com.alibaba.nacos.example.spring.boot.controller; -import org.springframework.beans.factory.annotation.Value; +import com.alibaba.nacos.api.config.annotation.NacosValue; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @@ -11,7 +11,7 @@ @RequestMapping("config") public class ConfigController { - @Value("${connectTimeoutInMills:5000}") + @NacosValue("${connectTimeoutInMills:5000}") private int connectTimeoutInMills; public void setConnectTimeoutInMills(int connectTimeoutInMills) { diff --git a/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/controller/ConfigController.java b/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/controller/ConfigController.java index d0844e874..7ab12c473 100644 --- a/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/controller/ConfigController.java +++ b/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/controller/ConfigController.java @@ -1,6 +1,6 @@ package com.alibaba.nacos.example.spring.controller; -import org.springframework.beans.factory.annotation.Value; +import com.alibaba.nacos.api.config.annotation.NacosValue; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @@ -11,7 +11,7 @@ @RequestMapping("config") public class ConfigController { - @Value("${connectTimeoutInMills:5000}") + @NacosValue("${connectTimeoutInMills:5000}") private int connectTimeoutInMills; public void setConnectTimeoutInMills(int connectTimeoutInMills) { From b45a3a37d367690e144353a958ff377e8da4fa4d Mon Sep 17 00:00:00 2001 From: hxy1991 Date: Sat, 1 Dec 2018 16:29:00 +0800 Subject: [PATCH 04/13] Revert @NacosValue to @Value --- .../example/spring/boot/controller/ConfigController.java | 4 ++-- .../nacos/example/spring/controller/ConfigController.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/controller/ConfigController.java b/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/controller/ConfigController.java index 136941a6f..2103e599e 100644 --- a/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/controller/ConfigController.java +++ b/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/controller/ConfigController.java @@ -1,6 +1,6 @@ package com.alibaba.nacos.example.spring.boot.controller; -import com.alibaba.nacos.api.config.annotation.NacosValue; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @@ -11,7 +11,7 @@ @RequestMapping("config") public class ConfigController { - @NacosValue("${connectTimeoutInMills:5000}") + @Value("${connectTimeoutInMills:5000}") private int connectTimeoutInMills; public void setConnectTimeoutInMills(int connectTimeoutInMills) { diff --git a/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/controller/ConfigController.java b/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/controller/ConfigController.java index 7ab12c473..d0844e874 100644 --- a/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/controller/ConfigController.java +++ b/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/controller/ConfigController.java @@ -1,6 +1,6 @@ package com.alibaba.nacos.example.spring.controller; -import com.alibaba.nacos.api.config.annotation.NacosValue; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @@ -11,7 +11,7 @@ @RequestMapping("config") public class ConfigController { - @NacosValue("${connectTimeoutInMills:5000}") + @Value("${connectTimeoutInMills:5000}") private int connectTimeoutInMills; public void setConnectTimeoutInMills(int connectTimeoutInMills) { From 5ea101db1845c32a4bab43658c99ccbe463f52d7 Mon Sep 17 00:00:00 2001 From: hxy1991 Date: Thu, 13 Dec 2018 15:23:50 +0800 Subject: [PATCH 05/13] Upgrade nacos-spring-boot version --- .../nacos/example/spring/controller/ConfigController.java | 8 ++------ nacos-spring-example/pom.xml | 2 +- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/controller/ConfigController.java b/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/controller/ConfigController.java index d0844e874..2e50fba20 100644 --- a/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/controller/ConfigController.java +++ b/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/controller/ConfigController.java @@ -1,6 +1,6 @@ package com.alibaba.nacos.example.spring.controller; -import org.springframework.beans.factory.annotation.Value; +import com.alibaba.nacos.api.config.annotation.NacosValue; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @@ -11,13 +11,9 @@ @RequestMapping("config") public class ConfigController { - @Value("${connectTimeoutInMills:5000}") + @NacosValue(value = "${connectTimeoutInMills:5000}", autoRefreshed = true) private int connectTimeoutInMills; - public void setConnectTimeoutInMills(int connectTimeoutInMills) { - this.connectTimeoutInMills = connectTimeoutInMills; - } - @RequestMapping(value = "/get", method = GET) @ResponseBody public int get() { diff --git a/nacos-spring-example/pom.xml b/nacos-spring-example/pom.xml index 6b16330c4..6211c79b2 100644 --- a/nacos-spring-example/pom.xml +++ b/nacos-spring-example/pom.xml @@ -25,7 +25,7 @@ 3.2.18.RELEASE 3.0.1 - 0.1.0-RC1 + 0.2.2-RC1 From 56b37d746430a08b5686e313e325793ae6cda44a Mon Sep 17 00:00:00 2001 From: hxy1991 Date: Thu, 13 Dec 2018 21:00:17 +0800 Subject: [PATCH 06/13] Upgrade nacos-spring-boot version --- .../nacos-spring-boot-config-example/pom.xml | 2 +- .../example/spring/boot/controller/ConfigController.java | 8 ++------ .../nacos-spring-boot-discovery-example/pom.xml | 2 +- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/nacos-spring-boot-example/nacos-spring-boot-config-example/pom.xml b/nacos-spring-boot-example/nacos-spring-boot-config-example/pom.xml index d9eb9d7b1..fce3efb5c 100644 --- a/nacos-spring-boot-example/nacos-spring-boot-config-example/pom.xml +++ b/nacos-spring-boot-example/nacos-spring-boot-config-example/pom.xml @@ -12,7 +12,7 @@ nacos-spring-boot-config-example - 0.2.0 + 0.2.1 diff --git a/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/controller/ConfigController.java b/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/controller/ConfigController.java index 2103e599e..7edbc7909 100644 --- a/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/controller/ConfigController.java +++ b/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/controller/ConfigController.java @@ -1,6 +1,6 @@ package com.alibaba.nacos.example.spring.boot.controller; -import org.springframework.beans.factory.annotation.Value; +import com.alibaba.nacos.api.config.annotation.NacosValue; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @@ -11,13 +11,9 @@ @RequestMapping("config") public class ConfigController { - @Value("${connectTimeoutInMills:5000}") + @NacosValue(value = "${connectTimeoutInMills:5000}", autoRefreshed = true) private int connectTimeoutInMills; - public void setConnectTimeoutInMills(int connectTimeoutInMills) { - this.connectTimeoutInMills = connectTimeoutInMills; - } - @RequestMapping(value = "/get", method = GET) @ResponseBody public int get() { diff --git a/nacos-spring-boot-example/nacos-spring-boot-discovery-example/pom.xml b/nacos-spring-boot-example/nacos-spring-boot-discovery-example/pom.xml index 60093ffe1..b0062e445 100644 --- a/nacos-spring-boot-example/nacos-spring-boot-discovery-example/pom.xml +++ b/nacos-spring-boot-example/nacos-spring-boot-discovery-example/pom.xml @@ -12,7 +12,7 @@ nacos-spring-boot-discovery-example - 0.2.0-RC1 + 0.2.1 From 72efebba12531898d546c4d50cad65a5cbb9f98d Mon Sep 17 00:00:00 2001 From: hxy1991 Date: Mon, 17 Dec 2018 21:08:42 +0800 Subject: [PATCH 07/13] Add Nacos Spring Config MySQL Example --- .../nacos-spring-config-mysql-example/pom.xml | 74 +++++++++++++++++++ .../spring/controller/UserController.java | 25 +++++++ .../nacos/example/spring/dao/UserMapper.java | 11 +++ .../nacos/example/spring/model/User.java | 24 ++++++ .../example/spring/service/UserService.java | 10 +++ .../spring/service/impl/UserServiceImpl.java | 21 ++++++ .../src/main/resources/user.sql | 9 +++ .../src/main/webapp/WEB-INF/datasource.xml | 23 ++++++ .../webapp/WEB-INF/dispatcher-servlet.xml | 22 ++++++ .../src/main/webapp/WEB-INF/nacos.xml | 15 ++++ .../webapp/WEB-INF/spring-config-mybatis.xml | 16 ++++ .../src/main/webapp/WEB-INF/web.xml | 17 +++++ .../src/main/webapp/index.jsp | 5 ++ nacos-spring-example/pom.xml | 1 + 14 files changed, 273 insertions(+) create mode 100644 nacos-spring-example/nacos-spring-config-mysql-example/pom.xml create mode 100644 nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/controller/UserController.java create mode 100644 nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/dao/UserMapper.java create mode 100644 nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/model/User.java create mode 100644 nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/service/UserService.java create mode 100644 nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/service/impl/UserServiceImpl.java create mode 100644 nacos-spring-example/nacos-spring-config-mysql-example/src/main/resources/user.sql create mode 100644 nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/datasource.xml create mode 100644 nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/dispatcher-servlet.xml create mode 100644 nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/nacos.xml create mode 100644 nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/spring-config-mybatis.xml create mode 100644 nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/web.xml create mode 100644 nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/index.jsp diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/pom.xml b/nacos-spring-example/nacos-spring-config-mysql-example/pom.xml new file mode 100644 index 000000000..30d43f73b --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-mysql-example/pom.xml @@ -0,0 +1,74 @@ + + + + + nacos-spring-example + com.alibaba.nacos + 0.2.0-SNAPSHOT + + + 4.0.0 + + nacos-spring-config-mysql-example + pom + + nacos-spring-config-mysql-example + + http://nacos.io + + + + + javax.servlet + javax.servlet-api + + + + org.springframework + spring-webmvc + + + + org.springframework + spring-jdbc + 3.2.18.RELEASE + + + + org.springframework + spring-tx + 3.2.18.RELEASE + + + + com.alibaba.nacos + nacos-spring-context + + + + mysql + mysql-connector-java + 5.1.40 + + + + com.alibaba + druid + 1.1.12 + + + + org.mybatis + mybatis + 3.4.6 + + + + org.mybatis + mybatis-spring + 1.3.2 + + + + diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/controller/UserController.java b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/controller/UserController.java new file mode 100644 index 000000000..e03d59cc2 --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/controller/UserController.java @@ -0,0 +1,25 @@ +package com.alibaba.nacos.example.spring.controller; + +import com.alibaba.nacos.example.spring.model.User; +import com.alibaba.nacos.example.spring.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +@Controller("user") +public class UserController { + + private final UserService userService; + + @Autowired + public UserController(UserService userService) {this.userService = userService;} + + @RequestMapping + @ResponseBody + public User get(@RequestParam int id) { + return userService.findById(id); + } + +} \ No newline at end of file diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/dao/UserMapper.java b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/dao/UserMapper.java new file mode 100644 index 000000000..8a210712b --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/dao/UserMapper.java @@ -0,0 +1,11 @@ +package com.alibaba.nacos.example.spring.dao; + +import com.alibaba.nacos.example.spring.model.User; +import org.apache.ibatis.annotations.Param; +import org.apache.ibatis.annotations.Select; + +public interface UserMapper { + + @Select(value = "select id, name from user where id = #{id}") + User findById(@Param("id") long id); +} \ No newline at end of file diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/model/User.java b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/model/User.java new file mode 100644 index 000000000..3b2f4da24 --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/model/User.java @@ -0,0 +1,24 @@ +package com.alibaba.nacos.example.spring.model; + +public class User { + + private Integer id; + + private String name; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/service/UserService.java b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/service/UserService.java new file mode 100644 index 000000000..663828a6c --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/service/UserService.java @@ -0,0 +1,10 @@ +package com.alibaba.nacos.example.spring.service; + +import com.alibaba.nacos.example.spring.model.User; +import org.springframework.stereotype.Service; + +@Service +public interface UserService { + + User findById(Integer id); +} \ No newline at end of file diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/service/impl/UserServiceImpl.java b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/service/impl/UserServiceImpl.java new file mode 100644 index 000000000..65eefe0fc --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/service/impl/UserServiceImpl.java @@ -0,0 +1,21 @@ +package com.alibaba.nacos.example.spring.service.impl; + +import com.alibaba.nacos.example.spring.dao.UserMapper; +import com.alibaba.nacos.example.spring.model.User; +import com.alibaba.nacos.example.spring.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class UserServiceImpl implements UserService { + + private final UserMapper userMapper; + + @Autowired + public UserServiceImpl(UserMapper userMapper) {this.userMapper = userMapper;} + + @Override + public User findById(Integer id) { + return userMapper.findById(id); + } +} \ No newline at end of file diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/resources/user.sql b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/resources/user.sql new file mode 100644 index 000000000..f0691cc39 --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/resources/user.sql @@ -0,0 +1,9 @@ +CREATE TABLE `user` ( + `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, + `name` varchar(10) NOT NULL DEFAULT '' COMMENT '名字', + `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', + `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户表'; + +INSERT INTO `user` (`name`, `create_time`, `update_time`) VALUES ('Nacos', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP); \ No newline at end of file diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/datasource.xml b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/datasource.xml new file mode 100644 index 000000000..c13040378 --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/datasource.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + + + + diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/dispatcher-servlet.xml b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/dispatcher-servlet.xml new file mode 100644 index 000000000..5f1d51e0d --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/dispatcher-servlet.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/nacos.xml b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/nacos.xml new file mode 100644 index 000000000..c177861a5 --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/nacos.xml @@ -0,0 +1,15 @@ + + + + + + + + + diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/spring-config-mybatis.xml b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/spring-config-mybatis.xml new file mode 100644 index 000000000..22374a35c --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/spring-config-mybatis.xml @@ -0,0 +1,16 @@ + + + + + + + + + + + + diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/web.xml b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 000000000..5833e24f6 --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,17 @@ + + + + + dispatcher + org.springframework.web.servlet.DispatcherServlet + 1 + + + + dispatcher + / + + + \ No newline at end of file diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/index.jsp b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/index.jsp new file mode 100644 index 000000000..c38169bb9 --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

Hello World!

+ + diff --git a/nacos-spring-example/pom.xml b/nacos-spring-example/pom.xml index 6211c79b2..01faa23b1 100644 --- a/nacos-spring-example/pom.xml +++ b/nacos-spring-example/pom.xml @@ -15,6 +15,7 @@ pom nacos-spring-config-example + nacos-spring-config-mysql-example nacos-spring-discovery-example From f1dc30cf89161a92ca1314dc307922a348f359b6 Mon Sep 17 00:00:00 2001 From: hxy1991 Date: Mon, 17 Dec 2018 21:11:43 +0800 Subject: [PATCH 08/13] Add Nacos Spring Config MySQL Example --- .../src/main/webapp/WEB-INF/nacos.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/nacos.xml b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/nacos.xml index c177861a5..b2e3a607b 100644 --- a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/nacos.xml +++ b/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/nacos.xml @@ -9,7 +9,8 @@ - + + From 424df83054d4b21f3ac16129c36801cb250a6a60 Mon Sep 17 00:00:00 2001 From: hxy1991 Date: Tue, 18 Dec 2018 11:11:17 +0800 Subject: [PATCH 09/13] Change nacos-spring-config-mysql-example to nacos-spring-config-datasource-example --- .gitignore | 1 + .../pom.xml | 4 ++-- .../nacos/example/spring/controller/UserController.java | 0 .../java/com/alibaba/nacos/example/spring/dao/UserMapper.java | 0 .../java/com/alibaba/nacos/example/spring/model/User.java | 0 .../com/alibaba/nacos/example/spring/service/UserService.java | 0 .../nacos/example/spring/service/impl/UserServiceImpl.java | 0 .../src/main/resources/user.sql | 0 .../src/main/webapp/WEB-INF/datasource.xml | 0 .../src/main/webapp/WEB-INF/dispatcher-servlet.xml | 0 .../src/main/webapp/WEB-INF/nacos.xml | 2 +- .../src/main/webapp/WEB-INF/spring-config-mybatis.xml | 0 .../src/main/webapp/WEB-INF/web.xml | 0 .../src/main/webapp/index.jsp | 0 nacos-spring-example/pom.xml | 2 +- 15 files changed, 5 insertions(+), 4 deletions(-) rename nacos-spring-example/{nacos-spring-config-mysql-example => nacos-spring-config-datasource-example}/pom.xml (94%) rename nacos-spring-example/{nacos-spring-config-mysql-example => nacos-spring-config-datasource-example}/src/main/java/com/alibaba/nacos/example/spring/controller/UserController.java (100%) rename nacos-spring-example/{nacos-spring-config-mysql-example => nacos-spring-config-datasource-example}/src/main/java/com/alibaba/nacos/example/spring/dao/UserMapper.java (100%) rename nacos-spring-example/{nacos-spring-config-mysql-example => nacos-spring-config-datasource-example}/src/main/java/com/alibaba/nacos/example/spring/model/User.java (100%) rename nacos-spring-example/{nacos-spring-config-mysql-example => nacos-spring-config-datasource-example}/src/main/java/com/alibaba/nacos/example/spring/service/UserService.java (100%) rename nacos-spring-example/{nacos-spring-config-mysql-example => nacos-spring-config-datasource-example}/src/main/java/com/alibaba/nacos/example/spring/service/impl/UserServiceImpl.java (100%) rename nacos-spring-example/{nacos-spring-config-mysql-example => nacos-spring-config-datasource-example}/src/main/resources/user.sql (100%) rename nacos-spring-example/{nacos-spring-config-mysql-example => nacos-spring-config-datasource-example}/src/main/webapp/WEB-INF/datasource.xml (100%) rename nacos-spring-example/{nacos-spring-config-mysql-example => nacos-spring-config-datasource-example}/src/main/webapp/WEB-INF/dispatcher-servlet.xml (100%) rename nacos-spring-example/{nacos-spring-config-mysql-example => nacos-spring-config-datasource-example}/src/main/webapp/WEB-INF/nacos.xml (77%) rename nacos-spring-example/{nacos-spring-config-mysql-example => nacos-spring-config-datasource-example}/src/main/webapp/WEB-INF/spring-config-mybatis.xml (100%) rename nacos-spring-example/{nacos-spring-config-mysql-example => nacos-spring-config-datasource-example}/src/main/webapp/WEB-INF/web.xml (100%) rename nacos-spring-example/{nacos-spring-config-mysql-example => nacos-spring-config-datasource-example}/src/main/webapp/index.jsp (100%) diff --git a/.gitignore b/.gitignore index f024062f0..8af9b81d5 100644 --- a/.gitignore +++ b/.gitignore @@ -28,3 +28,4 @@ hs_err_pid* .idea/ *.iml out/ +*/target/ diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/pom.xml b/nacos-spring-example/nacos-spring-config-datasource-example/pom.xml similarity index 94% rename from nacos-spring-example/nacos-spring-config-mysql-example/pom.xml rename to nacos-spring-example/nacos-spring-config-datasource-example/pom.xml index 30d43f73b..adee0b59e 100644 --- a/nacos-spring-example/nacos-spring-config-mysql-example/pom.xml +++ b/nacos-spring-example/nacos-spring-config-datasource-example/pom.xml @@ -10,10 +10,10 @@ 4.0.0 - nacos-spring-config-mysql-example + nacos-spring-config-datasource-example pom - nacos-spring-config-mysql-example + nacos-spring-config-datasource-example http://nacos.io diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/controller/UserController.java b/nacos-spring-example/nacos-spring-config-datasource-example/src/main/java/com/alibaba/nacos/example/spring/controller/UserController.java similarity index 100% rename from nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/controller/UserController.java rename to nacos-spring-example/nacos-spring-config-datasource-example/src/main/java/com/alibaba/nacos/example/spring/controller/UserController.java diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/dao/UserMapper.java b/nacos-spring-example/nacos-spring-config-datasource-example/src/main/java/com/alibaba/nacos/example/spring/dao/UserMapper.java similarity index 100% rename from nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/dao/UserMapper.java rename to nacos-spring-example/nacos-spring-config-datasource-example/src/main/java/com/alibaba/nacos/example/spring/dao/UserMapper.java diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/model/User.java b/nacos-spring-example/nacos-spring-config-datasource-example/src/main/java/com/alibaba/nacos/example/spring/model/User.java similarity index 100% rename from nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/model/User.java rename to nacos-spring-example/nacos-spring-config-datasource-example/src/main/java/com/alibaba/nacos/example/spring/model/User.java diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/service/UserService.java b/nacos-spring-example/nacos-spring-config-datasource-example/src/main/java/com/alibaba/nacos/example/spring/service/UserService.java similarity index 100% rename from nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/service/UserService.java rename to nacos-spring-example/nacos-spring-config-datasource-example/src/main/java/com/alibaba/nacos/example/spring/service/UserService.java diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/service/impl/UserServiceImpl.java b/nacos-spring-example/nacos-spring-config-datasource-example/src/main/java/com/alibaba/nacos/example/spring/service/impl/UserServiceImpl.java similarity index 100% rename from nacos-spring-example/nacos-spring-config-mysql-example/src/main/java/com/alibaba/nacos/example/spring/service/impl/UserServiceImpl.java rename to nacos-spring-example/nacos-spring-config-datasource-example/src/main/java/com/alibaba/nacos/example/spring/service/impl/UserServiceImpl.java diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/resources/user.sql b/nacos-spring-example/nacos-spring-config-datasource-example/src/main/resources/user.sql similarity index 100% rename from nacos-spring-example/nacos-spring-config-mysql-example/src/main/resources/user.sql rename to nacos-spring-example/nacos-spring-config-datasource-example/src/main/resources/user.sql diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/datasource.xml b/nacos-spring-example/nacos-spring-config-datasource-example/src/main/webapp/WEB-INF/datasource.xml similarity index 100% rename from nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/datasource.xml rename to nacos-spring-example/nacos-spring-config-datasource-example/src/main/webapp/WEB-INF/datasource.xml diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/dispatcher-servlet.xml b/nacos-spring-example/nacos-spring-config-datasource-example/src/main/webapp/WEB-INF/dispatcher-servlet.xml similarity index 100% rename from nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/dispatcher-servlet.xml rename to nacos-spring-example/nacos-spring-config-datasource-example/src/main/webapp/WEB-INF/dispatcher-servlet.xml diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/nacos.xml b/nacos-spring-example/nacos-spring-config-datasource-example/src/main/webapp/WEB-INF/nacos.xml similarity index 77% rename from nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/nacos.xml rename to nacos-spring-example/nacos-spring-config-datasource-example/src/main/webapp/WEB-INF/nacos.xml index b2e3a607b..2cd111c4f 100644 --- a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/nacos.xml +++ b/nacos-spring-example/nacos-spring-config-datasource-example/src/main/webapp/WEB-INF/nacos.xml @@ -10,7 +10,7 @@ - + diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/spring-config-mybatis.xml b/nacos-spring-example/nacos-spring-config-datasource-example/src/main/webapp/WEB-INF/spring-config-mybatis.xml similarity index 100% rename from nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/spring-config-mybatis.xml rename to nacos-spring-example/nacos-spring-config-datasource-example/src/main/webapp/WEB-INF/spring-config-mybatis.xml diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/web.xml b/nacos-spring-example/nacos-spring-config-datasource-example/src/main/webapp/WEB-INF/web.xml similarity index 100% rename from nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/WEB-INF/web.xml rename to nacos-spring-example/nacos-spring-config-datasource-example/src/main/webapp/WEB-INF/web.xml diff --git a/nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/index.jsp b/nacos-spring-example/nacos-spring-config-datasource-example/src/main/webapp/index.jsp similarity index 100% rename from nacos-spring-example/nacos-spring-config-mysql-example/src/main/webapp/index.jsp rename to nacos-spring-example/nacos-spring-config-datasource-example/src/main/webapp/index.jsp diff --git a/nacos-spring-example/pom.xml b/nacos-spring-example/pom.xml index 01faa23b1..870d8ce19 100644 --- a/nacos-spring-example/pom.xml +++ b/nacos-spring-example/pom.xml @@ -15,7 +15,7 @@ pom nacos-spring-config-example - nacos-spring-config-mysql-example + nacos-spring-config-datasource-example nacos-spring-discovery-example From 4967601f069bfe654c071af7d0e815920150dec1 Mon Sep 17 00:00:00 2001 From: hxy1991 Date: Tue, 18 Dec 2018 11:12:42 +0800 Subject: [PATCH 10/13] Change nacos-spring-config-mysql-example to nacos-spring-config-datasource-example --- .../src/main/webapp/WEB-INF/nacos.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nacos-spring-example/nacos-spring-config-datasource-example/src/main/webapp/WEB-INF/nacos.xml b/nacos-spring-example/nacos-spring-config-datasource-example/src/main/webapp/WEB-INF/nacos.xml index 2cd111c4f..b2e3a607b 100644 --- a/nacos-spring-example/nacos-spring-config-datasource-example/src/main/webapp/WEB-INF/nacos.xml +++ b/nacos-spring-example/nacos-spring-config-datasource-example/src/main/webapp/WEB-INF/nacos.xml @@ -10,7 +10,7 @@ - + From c157182885027426c183c33945327afaeff8784f Mon Sep 17 00:00:00 2001 From: hxy1991 Date: Fri, 18 Jan 2019 12:16:54 +0800 Subject: [PATCH 11/13] Add more examples like @NacosInjected, @NacosConfigListener, @NacosPropertySource, @NacosConfigurationProperties, etc. --- .../nacos-spring-boot-config-example/pom.xml | 11 +++ .../spring/boot/NacosConfigApplication.java | 11 +++ .../src/main/resources/application.properties | 13 ++- nacos-spring-boot-example/pom.xml | 5 +- .../nacos-spring-cloud-config-example/pom.xml | 4 +- .../spring/cloud/NacosConfigApplication.java | 2 +- .../cloud/controller/ConfigController.java | 7 +- .../src/main/resources/bootstrap.properties | 16 ++-- .../pom.xml | 44 ++++++++++ .../example/spring/cloud/UserApplication.java | 30 +++++++ .../cloud/controller/UserController.java | 30 +++++++ .../spring/cloud/dao/UserRepository.java | 8 ++ .../example/spring/cloud/model/User.java | 39 +++++++++ .../spring/cloud/service/UserService.java | 18 ++++ .../cloud/service/impl/UserServiceImpl.java | 65 ++++++++++++++ .../src/main/resources/bootstrap.properties | 45 ++++++++++ nacos-spring-cloud-example/pom.xml | 4 + .../src/main/webapp/WEB-INF/nacos.xml | 17 +++- .../example/spring/NacosConfiguration.java | 20 ++++- .../spring/controller/ConfigController.java | 25 ++++++ .../pom.xml | 37 ++++++++ .../example/spring/AdminConfiguration.java | 17 ++++ .../spring/controller/AdminController.java | 29 +++++++ .../spring/converter/AdminConverter.java | 24 ++++++ .../nacos/example/spring/model/Admin.java | 30 +++++++ .../example/spring/service/AdminService.java | 12 +++ .../spring/service/impl/AdminServiceImpl.java | 56 +++++++++++++ .../WEB-INF/dispatcherServlet-servlet.xml | 21 +++++ .../src/main/webapp/WEB-INF/web.xml | 17 ++++ .../src/main/webapp/index.jsp | 5 ++ .../pom.xml | 67 +++++++++++++++ .../example/spring/NacosConfiguration.java | 58 +++++++++++++ .../spring/controller/UserController.java | 33 ++++++++ .../nacos/example/spring/model/User.java | 33 ++++++++ .../example/spring/service/UserService.java | 13 +++ .../spring/service/impl/UserServiceImpl.java | 84 +++++++++++++++++++ .../src/main/webapp/WEB-INF/cache.xml | 33 ++++++++ .../src/main/webapp/WEB-INF/datasource.xml | 28 +++++++ .../WEB-INF/dispatcherServlet-servlet.xml | 23 +++++ .../src/main/webapp/WEB-INF/web.xml | 17 ++++ .../src/main/webapp/index.jsp | 5 ++ .../nacos-spring-config-pojo-example/pom.xml | 38 +++++++++ .../spring/PromotionConfiguration.java | 24 ++++++ .../controller/PromotionController.java | 25 ++++++ .../nacos/example/spring/model/Promotion.java | 76 +++++++++++++++++ .../WEB-INF/dispatcherServlet-servlet.xml | 23 +++++ .../src/main/webapp/WEB-INF/web.xml | 17 ++++ .../src/main/webapp/index.jsp | 5 ++ nacos-spring-example/pom.xml | 3 + 49 files changed, 1245 insertions(+), 22 deletions(-) create mode 100644 nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/pom.xml create mode 100644 nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/UserApplication.java create mode 100644 nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/controller/UserController.java create mode 100644 nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/dao/UserRepository.java create mode 100644 nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/model/User.java create mode 100644 nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/service/UserService.java create mode 100644 nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/service/impl/UserServiceImpl.java create mode 100644 nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/resources/bootstrap.properties create mode 100644 nacos-spring-example/nacos-spring-config-listener-example/pom.xml create mode 100644 nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/AdminConfiguration.java create mode 100644 nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/controller/AdminController.java create mode 100644 nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/converter/AdminConverter.java create mode 100644 nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/model/Admin.java create mode 100644 nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/service/AdminService.java create mode 100644 nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/service/impl/AdminServiceImpl.java create mode 100644 nacos-spring-example/nacos-spring-config-listener-example/src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml create mode 100644 nacos-spring-example/nacos-spring-config-listener-example/src/main/webapp/WEB-INF/web.xml create mode 100644 nacos-spring-example/nacos-spring-config-listener-example/src/main/webapp/index.jsp create mode 100644 nacos-spring-example/nacos-spring-config-multi-data-ids-example/pom.xml create mode 100644 nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/NacosConfiguration.java create mode 100644 nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/controller/UserController.java create mode 100644 nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/model/User.java create mode 100644 nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/service/UserService.java create mode 100644 nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/service/impl/UserServiceImpl.java create mode 100644 nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/webapp/WEB-INF/cache.xml create mode 100644 nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/webapp/WEB-INF/datasource.xml create mode 100644 nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml create mode 100644 nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/webapp/WEB-INF/web.xml create mode 100644 nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/webapp/index.jsp create mode 100644 nacos-spring-example/nacos-spring-config-pojo-example/pom.xml create mode 100644 nacos-spring-example/nacos-spring-config-pojo-example/src/main/java/com/alibaba/nacos/example/spring/PromotionConfiguration.java create mode 100644 nacos-spring-example/nacos-spring-config-pojo-example/src/main/java/com/alibaba/nacos/example/spring/controller/PromotionController.java create mode 100644 nacos-spring-example/nacos-spring-config-pojo-example/src/main/java/com/alibaba/nacos/example/spring/model/Promotion.java create mode 100644 nacos-spring-example/nacos-spring-config-pojo-example/src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml create mode 100644 nacos-spring-example/nacos-spring-config-pojo-example/src/main/webapp/WEB-INF/web.xml create mode 100644 nacos-spring-example/nacos-spring-config-pojo-example/src/main/webapp/index.jsp diff --git a/nacos-spring-boot-example/nacos-spring-boot-config-example/pom.xml b/nacos-spring-boot-example/nacos-spring-boot-config-example/pom.xml index fce3efb5c..2a69e8af4 100644 --- a/nacos-spring-boot-example/nacos-spring-boot-config-example/pom.xml +++ b/nacos-spring-boot-example/nacos-spring-boot-config-example/pom.xml @@ -21,10 +21,21 @@ spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-actuator + + com.alibaba.boot nacos-config-spring-boot-starter ${nacos-config-spring-boot.version} + + + com.alibaba.boot + nacos-config-spring-boot-actuator + ${nacos-config-spring-boot.version} +
\ No newline at end of file diff --git a/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/NacosConfigApplication.java b/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/NacosConfigApplication.java index 3d141d494..4217efcd8 100644 --- a/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/NacosConfigApplication.java +++ b/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/NacosConfigApplication.java @@ -4,6 +4,17 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +/** + * Document: https://help.aliyun.com/document_detail/94592.html + *

+ * ACM 控制台添加配置: + *

+ * Data ID:com.alibaba.nacos.example + *

+ * Group:DEFAULT_GROUP + *

+ * 配置内容:connectTimeoutInMills=5000 + */ @SpringBootApplication @NacosPropertySource(dataId = "com.alibaba.nacos.example", autoRefreshed = true) public class NacosConfigApplication { diff --git a/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/resources/application.properties b/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/resources/application.properties index 4ad91bd33..3daa51fce 100644 --- a/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/resources/application.properties +++ b/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/resources/application.properties @@ -1,7 +1,12 @@ # \u5728\u547D\u540D\u7A7A\u95F4\u8BE6\u60C5\u5904\u53EF\u4EE5\u83B7\u53D6\u5230 endpoint \u548C namespace \u7684\u503C -nacos.config.endpoint=${endpoint} -nacos.config.namespace=${namespace} +nacos.config.endpoint=xxx +nacos.config.namespace=xxx # \u63A8\u8350\u4F7F\u7528 RAM \u8D26\u6237\u7684 accessKey \u548C secretKey -nacos.config.access-key=${accessKey} -nacos.config.secret-key=${secretKey} \ No newline at end of file +nacos.config.access-key=xxx +nacos.config.secret-key=xxx + +# endpoint http://localhost:8080/actuator/nacos-config +# health http://localhost:8080/actuator/health +management.endpoints.web.exposure.include=* +management.endpoint.health.show-details=always \ No newline at end of file diff --git a/nacos-spring-boot-example/pom.xml b/nacos-spring-boot-example/pom.xml index 9ede32bfd..3a3332e7a 100644 --- a/nacos-spring-boot-example/pom.xml +++ b/nacos-spring-boot-example/pom.xml @@ -26,10 +26,13 @@ + org.springframework.boot - spring-boot-starter-web + spring-boot-dependencies ${spring-boot.version} + pom + import diff --git a/nacos-spring-cloud-example/nacos-spring-cloud-config-example/pom.xml b/nacos-spring-cloud-example/nacos-spring-cloud-config-example/pom.xml index a413b81fb..612622d05 100644 --- a/nacos-spring-cloud-example/nacos-spring-cloud-config-example/pom.xml +++ b/nacos-spring-cloud-example/nacos-spring-cloud-config-example/pom.xml @@ -17,11 +17,11 @@ org.springframework.boot spring-boot-starter-web - + org.springframework.cloud spring-cloud-starter-alibaba-nacos-config - 0.2.0.RELEASE + 0.2.1.RELEASE diff --git a/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/java/com/alibaba/nacos/example/spring/cloud/NacosConfigApplication.java b/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/java/com/alibaba/nacos/example/spring/cloud/NacosConfigApplication.java index d4c2570b9..74105dca6 100644 --- a/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/java/com/alibaba/nacos/example/spring/cloud/NacosConfigApplication.java +++ b/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/java/com/alibaba/nacos/example/spring/cloud/NacosConfigApplication.java @@ -4,7 +4,7 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; /** - * @author xiaojing + * Document: https://help.aliyun.com/document_detail/94708.html */ @SpringBootApplication public class NacosConfigApplication { diff --git a/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/java/com/alibaba/nacos/example/spring/cloud/controller/ConfigController.java b/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/java/com/alibaba/nacos/example/spring/cloud/controller/ConfigController.java index 381fd1bfe..751ce2a3e 100644 --- a/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/java/com/alibaba/nacos/example/spring/cloud/controller/ConfigController.java +++ b/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/java/com/alibaba/nacos/example/spring/cloud/controller/ConfigController.java @@ -16,10 +16,9 @@ public class ConfigController { @Value("${connectTimeoutInMills:5000}") private int connectTimeoutInMills; - public void setConnectTimeoutInMills(int connectTimeoutInMills) { - this.connectTimeoutInMills = connectTimeoutInMills; - } - + /** + * http://localhost:8080/config/get + */ @RequestMapping(value = "/get", method = GET) @ResponseBody public int get() { diff --git a/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/resources/bootstrap.properties b/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/resources/bootstrap.properties index 30a089af7..925de2781 100644 --- a/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/resources/bootstrap.properties +++ b/nacos-spring-cloud-example/nacos-spring-cloud-config-example/src/main/resources/bootstrap.properties @@ -1,12 +1,16 @@ # \u5728\u547D\u540D\u7A7A\u95F4\u8BE6\u60C5\u5904\u53EF\u4EE5\u83B7\u53D6\u5230 endpoint \u548C namespace \u7684\u503C -spring.cloud.nacos.config.endpoint=${endpoint} -spring.cloud.nacos.config.namespace=${namespace} +spring.cloud.nacos.config.endpoint=xxx +spring.cloud.nacos.config.namespace=xxx # \u63A8\u8350\u4F7F\u7528 RAM \u8D26\u6237\u7684 accessKey \u548C secretKey -spring.cloud.nacos.config.access-key=${accessKey} -spring.cloud.nacos.config.secret-key=${secretKey} +spring.cloud.nacos.config.access-key=xxx +spring.cloud.nacos.config.secret-key=xxx +# ACM \u63A7\u5236\u53F0\u6DFB\u52A0\u914D\u7F6E\uFF1A +# Data ID\uFF1Acom.alibaba.nacos.example.properties +# Group\uFF1ADEFAULT_GROUP +# \u914D\u7F6E\u5185\u5BB9\uFF1AconnectTimeoutInMills=5000 spring.application.name=com.alibaba.nacos.example - # \u6307\u5B9A\u914D\u7F6E\u7684\u540E\u7F00\uFF0C\u652F\u6301 properties\u3001yaml\u3001yml\uFF0C\u9ED8\u8BA4\u4E3A properties -spring.cloud.nacos.config.file-extension=properties \ No newline at end of file +spring.cloud.nacos.config.file-extension=properties +#spring.cloud.nacos.config.file-extension=yaml \ No newline at end of file diff --git a/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/pom.xml b/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/pom.xml new file mode 100644 index 000000000..0090628b3 --- /dev/null +++ b/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/pom.xml @@ -0,0 +1,44 @@ + + + + nacos-spring-cloud-example + com.alibaba.nacos + 0.2.0-SNAPSHOT + + 4.0.0 + + nacos-spring-cloud-config-multi-data-ids-example + + + + + org.springframework.boot + spring-boot-starter-web + + + + org.springframework.cloud + spring-cloud-starter-alibaba-nacos-config + 0.2.1.RELEASE + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + + mysql + mysql-connector-java + 5.1.47 + + + + org.springframework.boot + spring-boot-starter-data-redis + + + + \ No newline at end of file diff --git a/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/UserApplication.java b/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/UserApplication.java new file mode 100644 index 000000000..2eab5a6e8 --- /dev/null +++ b/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/UserApplication.java @@ -0,0 +1,30 @@ +package com.alibaba.nacos.example.spring.cloud; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.StringRedisSerializer; + +/** + * @author xiaojing + */ +@SpringBootApplication +public class UserApplication { + + @Bean + public RedisTemplate redisTemplate(RedisConnectionFactory factory) { + RedisTemplate redisTemplate = new RedisTemplate<>(); + redisTemplate.setConnectionFactory(factory); + redisTemplate.setKeySerializer(new StringRedisSerializer()); + redisTemplate.setHashKeySerializer(new StringRedisSerializer()); + return redisTemplate; + } + + public static void main(String[] args) { + SpringApplication.run(UserApplication.class, args); + } +} + + diff --git a/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/controller/UserController.java b/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/controller/UserController.java new file mode 100644 index 000000000..22783f7be --- /dev/null +++ b/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/controller/UserController.java @@ -0,0 +1,30 @@ +package com.alibaba.nacos.example.spring.cloud.controller; + +import com.alibaba.nacos.example.spring.cloud.model.User; +import com.alibaba.nacos.example.spring.cloud.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("user") +public class UserController { + + private final UserService userService; + + @Autowired + public UserController(UserService userService) {this.userService = userService;} + + /** + * http://localhost:8080/user?id=1 + */ + @GetMapping + @ResponseBody + public User get(@RequestParam long id) { + return userService.findById(id); + } + +} \ No newline at end of file diff --git a/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/dao/UserRepository.java b/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/dao/UserRepository.java new file mode 100644 index 000000000..2f8b45a03 --- /dev/null +++ b/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/dao/UserRepository.java @@ -0,0 +1,8 @@ +package com.alibaba.nacos.example.spring.cloud.dao; + +import com.alibaba.nacos.example.spring.cloud.model.User; +import org.springframework.data.repository.CrudRepository; + +public interface UserRepository extends CrudRepository { + +} \ No newline at end of file diff --git a/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/model/User.java b/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/model/User.java new file mode 100644 index 000000000..15a573d95 --- /dev/null +++ b/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/model/User.java @@ -0,0 +1,39 @@ +package com.alibaba.nacos.example.spring.cloud.model; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import java.io.Serializable; + +/** + * CREATE TABLE `user` ( + * `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, + * `name` varchar(10) NOT NULL DEFAULT '', + * PRIMARY KEY (`id`) + * ) ENGINE=InnoDB DEFAULT CHARSET=utf8; + */ +@Entity +public class User implements Serializable { + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + private String name; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/service/UserService.java b/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/service/UserService.java new file mode 100644 index 000000000..f05009581 --- /dev/null +++ b/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/service/UserService.java @@ -0,0 +1,18 @@ +package com.alibaba.nacos.example.spring.cloud.service; + +import com.alibaba.nacos.example.spring.cloud.model.User; +import org.springframework.cloud.context.config.annotation.RefreshScope; +import org.springframework.stereotype.Service; + +/** + * @author hexu.hxy + * @date 2019/1/7 + */ + +@Service +@RefreshScope +public interface UserService { + + User findById(Long id); + +} \ No newline at end of file diff --git a/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/service/impl/UserServiceImpl.java b/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/service/impl/UserServiceImpl.java new file mode 100644 index 000000000..0584fa7a8 --- /dev/null +++ b/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/service/impl/UserServiceImpl.java @@ -0,0 +1,65 @@ +package com.alibaba.nacos.example.spring.cloud.service.impl; + +import com.alibaba.nacos.example.spring.cloud.dao.UserRepository; +import com.alibaba.nacos.example.spring.cloud.model.User; +import com.alibaba.nacos.example.spring.cloud.service.UserService; +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.cloud.context.config.annotation.RefreshScope; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Service; + +/** + * @author hexu.hxy + * @date 2019/1/7 + */ + +@Service +@RefreshScope +public class UserServiceImpl implements UserService { + + private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class); + + private final UserRepository userRepository; + + private final RedisTemplate redisTemplate; + + @Value("${app.user.cache}") + private boolean cache; + + @Autowired + public UserServiceImpl(UserRepository userRepository, RedisTemplate redisTemplate) { + this.userRepository = userRepository; + this.redisTemplate = redisTemplate; + } + + @Override + public User findById(Long id) { + LOGGER.info("cache: {}", cache); + + if (cache) { + Object obj = redisTemplate.opsForValue().get(key(id)); + if (obj != null) { + LOGGER.info("get user from cache, id: {}", id); + return (User)obj; + } + } + + User user = userRepository.findById(id).orElse(null); + if (user != null) { + if (cache) { + LOGGER.info("set cache for user, id: {}", id); + redisTemplate.opsForValue().set(key(id), user); + } + } + + return user; + } + + private String key(Long id) { + return String.format("nacos-spring-cloud-config-multi-data-ids-example:user:%d", id); + } + +} \ No newline at end of file diff --git a/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/resources/bootstrap.properties b/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/resources/bootstrap.properties new file mode 100644 index 000000000..d28e7d6b9 --- /dev/null +++ b/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/resources/bootstrap.properties @@ -0,0 +1,45 @@ +spring.application.name=multi-data-ids-example + +# \u5728\u547D\u540D\u7A7A\u95F4\u8BE6\u60C5\u5904\u53EF\u4EE5\u83B7\u53D6\u5230 endpoint \u548C namespace \u7684\u503C +spring.cloud.nacos.config.endpoint=xxx +spring.cloud.nacos.config.namespace=xxx + +# \u63A8\u8350\u4F7F\u7528 RAM \u8D26\u6237\u7684 accessKey \u548C secretKey +spring.cloud.nacos.config.access-key=xxx +spring.cloud.nacos.config.secret-key=xxx + +# 0.2.1.RELEASE \u4EE5\u4E0A\u652F\u6301\u591A\u4E2A\u914D\u7F6E\uFF0C\u6587\u6863\uFF1Ahttps://github.com/spring-cloud-incubator/spring-cloud-alibaba/wiki/Nacos-config + +# ACM \u63A7\u5236\u53F0\u6DFB\u52A0\u914D\u7F6E\uFF1A +# Data ID\uFF1Aapp.properties +# Group\uFF1Amulti-data-ids +# \u914D\u7F6E\u5185\u5BB9\uFF1Aapp.user.cache=false +spring.cloud.nacos.config.ext-config[0].data-id=app.properties +spring.cloud.nacos.config.ext-config[0].group=multi-data-ids +spring.cloud.nacos.config.ext-config[0].refresh=true + +# ACM \u63A7\u5236\u53F0\u6DFB\u52A0\u914D\u7F6E\uFF1A +# Data ID\uFF1Adatasource.properties +# Group\uFF1Amulti-data-ids +# \u914D\u7F6E\u5185\u5BB9\u793A\u4F8B\uFF1A +# spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false +# spring.datasource.username=root +# spring.datasource.password=root +# spring.datasource.initial-size=10 +# spring.datasource.max-active=20 +spring.cloud.nacos.config.ext-config[1].data-id=datasource.properties +spring.cloud.nacos.config.ext-config[1].group=multi-data-ids + +# ACM \u63A7\u5236\u53F0\u6DFB\u52A0\u914D\u7F6E\uFF1A +# Data ID\uFF1Aredis.properties +# Group\uFF1Amulti-data-ids +# \u914D\u7F6E\u5185\u5BB9\u793A\u4F8B\uFF1A +# spring.redis.host=localhost +# spring.redis.password=20190101 +# spring.redis.timeout=5000 +# spring.redis.max-idle=5 +# spring.redis.max-active=10 +# spring.redis.max-wait=3000 +# spring.redis.test-on-borrow=false +spring.cloud.nacos.config.ext-config[2].data-id=redis.properties +spring.cloud.nacos.config.ext-config[2].group=multi-data-ids \ No newline at end of file diff --git a/nacos-spring-cloud-example/pom.xml b/nacos-spring-cloud-example/pom.xml index 686328031..52200ed01 100644 --- a/nacos-spring-cloud-example/pom.xml +++ b/nacos-spring-cloud-example/pom.xml @@ -12,6 +12,9 @@ 2.0.4.RELEASE Finchley.RELEASE + + 1.8 + 1.8 4.0.0 @@ -21,6 +24,7 @@ pom nacos-spring-cloud-config-example + nacos-spring-cloud-config-multi-data-ids-example nacos-spring-cloud-discovery-example diff --git a/nacos-spring-example/nacos-spring-config-datasource-example/src/main/webapp/WEB-INF/nacos.xml b/nacos-spring-example/nacos-spring-config-datasource-example/src/main/webapp/WEB-INF/nacos.xml index b2e3a607b..0f6739dd6 100644 --- a/nacos-spring-example/nacos-spring-config-datasource-example/src/main/webapp/WEB-INF/nacos.xml +++ b/nacos-spring-example/nacos-spring-config-datasource-example/src/main/webapp/WEB-INF/nacos.xml @@ -9,8 +9,21 @@ - - + + + diff --git a/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/NacosConfiguration.java b/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/NacosConfiguration.java index 39623d154..12c5918a3 100644 --- a/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/NacosConfiguration.java +++ b/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/NacosConfiguration.java @@ -7,8 +7,24 @@ @Configuration // 在命名空间详情处可以获取到 endpoint 和 namespace;accessKey 和 secretKey 推荐使用 RAM 账户的 -@EnableNacosConfig(globalProperties = @NacosProperties(endpoint = "${endpoint}", - namespace = "${namespace}", accessKey = "${accessKey}", secretKey = "${secretKey}")) +@EnableNacosConfig(globalProperties = @NacosProperties( + endpoint = "xxx", + namespace = "xxx", + accessKey = "xxx", + secretKey = "xxx" +)) + +/** + * Document: https://help.aliyun.com/document_detail/94588.html + *

+ * ACM 控制台添加配置: + *

+ * Data ID:com.alibaba.nacos.example + *

+ * Group:DEFAULT_GROUP + *

+ * 配置内容:connectTimeoutInMills=5000 + */ @NacosPropertySource(dataId = "com.alibaba.nacos.example", autoRefreshed = true) public class NacosConfiguration { diff --git a/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/controller/ConfigController.java b/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/controller/ConfigController.java index 2e50fba20..21e78227c 100644 --- a/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/controller/ConfigController.java +++ b/nacos-spring-example/nacos-spring-config-example/src/main/java/com/alibaba/nacos/example/spring/controller/ConfigController.java @@ -1,16 +1,26 @@ package com.alibaba.nacos.example.spring.controller; +import com.alibaba.nacos.api.annotation.NacosInjected; +import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.annotation.NacosValue; +import com.alibaba.nacos.api.exception.NacosException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import static org.springframework.web.bind.annotation.RequestMethod.GET; +import static org.springframework.web.bind.annotation.RequestMethod.POST; @Controller @RequestMapping("config") public class ConfigController { + @NacosInjected + private ConfigService configService; + @NacosValue(value = "${connectTimeoutInMills:5000}", autoRefreshed = true) private int connectTimeoutInMills; @@ -19,5 +29,20 @@ public class ConfigController { public int get() { return connectTimeoutInMills; } + + /** + * curl -X POST 'http://localhost:8080/config?dataId=com.alibaba.nacos.example&content=connectTimeoutInMills=3000' + */ + @RequestMapping(method = POST) + @ResponseBody + public ResponseEntity publish(@RequestParam String dataId, + @RequestParam(defaultValue = "DEFAULT_GROUP") String group, + @RequestParam String content) throws NacosException { + boolean result = configService.publishConfig(dataId, group, content); + if (result) { + return new ResponseEntity("Success", HttpStatus.OK); + } + return new ResponseEntity("Fail", HttpStatus.INTERNAL_SERVER_ERROR); + } } diff --git a/nacos-spring-example/nacos-spring-config-listener-example/pom.xml b/nacos-spring-example/nacos-spring-config-listener-example/pom.xml new file mode 100644 index 000000000..34d4b635f --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-listener-example/pom.xml @@ -0,0 +1,37 @@ + + + + + nacos-spring-example + com.alibaba.nacos + 0.2.0-SNAPSHOT + + + 4.0.0 + + nacos-spring-config-listener-example + pom + + nacos-spring-config-listener-example + + http://nacos.io + + + + + javax.servlet + javax.servlet-api + + + + org.springframework + spring-webmvc + + + + com.alibaba.nacos + nacos-spring-context + + + diff --git a/nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/AdminConfiguration.java b/nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/AdminConfiguration.java new file mode 100644 index 000000000..4fdfffa39 --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/AdminConfiguration.java @@ -0,0 +1,17 @@ +package com.alibaba.nacos.example.spring; + +import com.alibaba.nacos.api.annotation.NacosProperties; +import com.alibaba.nacos.spring.context.annotation.config.EnableNacosConfig; +import org.springframework.context.annotation.Configuration; + +@Configuration +// 在命名空间详情处可以获取到 endpoint 和 namespace;accessKey 和 secretKey 推荐使用 RAM 账户的 +@EnableNacosConfig(globalProperties = @NacosProperties( + endpoint = "xxx", + namespace = "xxx", + accessKey = "xxx", + secretKey = "xxx" +)) +public class AdminConfiguration { + +} \ No newline at end of file diff --git a/nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/controller/AdminController.java b/nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/controller/AdminController.java new file mode 100644 index 000000000..f5eeab613 --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/controller/AdminController.java @@ -0,0 +1,29 @@ +package com.alibaba.nacos.example.spring.controller; + +import com.alibaba.nacos.example.spring.model.Admin; +import com.alibaba.nacos.example.spring.service.AdminService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import static org.springframework.web.bind.annotation.RequestMethod.GET; + +/** + * @author hexu.hxy + */ +@Controller +@RequestMapping("admin") +public class AdminController { + + @Autowired + private AdminService adminService; + + @RequestMapping(method = GET, produces = MediaType.APPLICATION_JSON_VALUE) + @ResponseBody + public Admin get() { + return adminService.getAdmin(); + } +} + diff --git a/nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/converter/AdminConverter.java b/nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/converter/AdminConverter.java new file mode 100644 index 000000000..93b19a2a2 --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/converter/AdminConverter.java @@ -0,0 +1,24 @@ +package com.alibaba.nacos.example.spring.converter; + +import com.alibaba.fastjson.JSON; +import com.alibaba.nacos.api.config.convert.NacosConfigConverter; +import com.alibaba.nacos.example.spring.model.Admin; + +/** + * @author hexu.hxy + */ +public class AdminConverter implements NacosConfigConverter { + + @Override + public boolean canConvert(Class targetType) { + return Admin.class.equals(targetType); + } + + /** + * 可自定义转化格式 + */ + @Override + public Admin convert(String config) { + return JSON.parseObject(config, Admin.class); + } +} \ No newline at end of file diff --git a/nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/model/Admin.java b/nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/model/Admin.java new file mode 100644 index 000000000..febc3a164 --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/model/Admin.java @@ -0,0 +1,30 @@ +package com.alibaba.nacos.example.spring.model; + +public class Admin { + private String username; + private String password; + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + @Override + public String toString() { + return "Admin{" + + "username='" + username + '\'' + + ", password='" + password + '\'' + + '}'; + } +} \ No newline at end of file diff --git a/nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/service/AdminService.java b/nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/service/AdminService.java new file mode 100644 index 000000000..10ee5d65c --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/service/AdminService.java @@ -0,0 +1,12 @@ +package com.alibaba.nacos.example.spring.service; + +import com.alibaba.nacos.example.spring.model.Admin; + +/** + * @author hexu.hxy + * @date 2019/1/6 + */ +public interface AdminService { + + Admin getAdmin(); +} diff --git a/nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/service/impl/AdminServiceImpl.java b/nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/service/impl/AdminServiceImpl.java new file mode 100644 index 000000000..6c1225b66 --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-listener-example/src/main/java/com/alibaba/nacos/example/spring/service/impl/AdminServiceImpl.java @@ -0,0 +1,56 @@ +package com.alibaba.nacos.example.spring.service.impl; + +import com.alibaba.nacos.api.config.annotation.NacosConfigListener; +import com.alibaba.nacos.example.spring.converter.AdminConverter; +import com.alibaba.nacos.example.spring.model.Admin; +import com.alibaba.nacos.example.spring.service.AdminService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Service; + +/** + * @author hexu.hxy + * @date 2019/1/6 + */ +@Service +public class AdminServiceImpl implements AdminService { + + private static final Logger LOGGER = LoggerFactory.getLogger(AdminServiceImpl.class); + + private static final String ADMIN_DATA_ID = "admin.json"; + + private static final String ADMIN_GROUP_ID = "spring-listener"; + + private volatile Admin admin; + + + @NacosConfigListener(dataId = ADMIN_DATA_ID, groupId = ADMIN_GROUP_ID) + public void onReceived(String content) { + LOGGER.info("onReceived(String) : {}", content); + } + + /** + *

+ * ACM 控制台添加配置: + *

+ * Data ID:admin.json + *

+ * Group:spring-listener + *

+ * 配置内容: + * { + * "username": "admin", + * "password": "123456" + * } + */ + @NacosConfigListener(dataId = ADMIN_DATA_ID, groupId = ADMIN_GROUP_ID, converter = AdminConverter.class) + public void onReceived(Admin admin) { + LOGGER.info("onReceived(Admin) : {}", admin); + this.admin = admin; + } + + @Override + public Admin getAdmin() { + return admin; + } +} diff --git a/nacos-spring-example/nacos-spring-config-listener-example/src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml b/nacos-spring-example/nacos-spring-config-listener-example/src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml new file mode 100644 index 000000000..3ec4f2fc6 --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-listener-example/src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + diff --git a/nacos-spring-example/nacos-spring-config-listener-example/src/main/webapp/WEB-INF/web.xml b/nacos-spring-example/nacos-spring-config-listener-example/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 000000000..2e1696d9b --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-listener-example/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,17 @@ + + + + + dispatcherServlet + org.springframework.web.servlet.DispatcherServlet + 1 + + + + dispatcherServlet + / + + + \ No newline at end of file diff --git a/nacos-spring-example/nacos-spring-config-listener-example/src/main/webapp/index.jsp b/nacos-spring-example/nacos-spring-config-listener-example/src/main/webapp/index.jsp new file mode 100644 index 000000000..c38169bb9 --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-listener-example/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

Hello World!

+ + diff --git a/nacos-spring-example/nacos-spring-config-multi-data-ids-example/pom.xml b/nacos-spring-example/nacos-spring-config-multi-data-ids-example/pom.xml new file mode 100644 index 000000000..72e89e406 --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-multi-data-ids-example/pom.xml @@ -0,0 +1,67 @@ + + + + + nacos-spring-example + com.alibaba.nacos + 0.2.0-SNAPSHOT + + + 4.0.0 + + nacos-spring-config-multi-data-ids-example + pom + + nacos-spring-config-multi-data-ids-example + + http://nacos.io + + + + + javax.servlet + javax.servlet-api + + + + org.springframework + spring-webmvc + + + + org.springframework + spring-jdbc + 3.2.18.RELEASE + + + + com.alibaba + druid + 1.1.12 + + + + mysql + mysql-connector-java + 5.1.40 + + + + org.springframework.data + spring-data-redis + 1.3.6.RELEASE + + + + redis.clients + jedis + 2.4.1 + + + + com.alibaba.nacos + nacos-spring-context + + + diff --git a/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/NacosConfiguration.java b/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/NacosConfiguration.java new file mode 100644 index 000000000..9ebdbec3d --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/NacosConfiguration.java @@ -0,0 +1,58 @@ +package com.alibaba.nacos.example.spring; + +import com.alibaba.nacos.api.annotation.NacosProperties; +import com.alibaba.nacos.spring.context.annotation.config.EnableNacosConfig; +import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource; +import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySources; +import org.springframework.context.annotation.Configuration; + +@Configuration + +// 在命名空间详情处可以获取到 endpoint 和 namespace;accessKey 和 secretKey 推荐使用 RAM 账户的 +@EnableNacosConfig(globalProperties = @NacosProperties( + endpoint = "xxx", + namespace = "xxx", + accessKey = "xxx", + secretKey = "xxx" +)) + +@NacosPropertySources({ + + /* + * ACM 控制台添加配置: + * Data ID:app.properties + * Group:multi-data-ids + * 配置内容:app.user.cache=false + */ + @NacosPropertySource(dataId = "app.properties", groupId = "multi-data-ids", autoRefreshed = true), + + /* + * ACM 控制台添加配置: + * Data ID:datasource.properties + * Group:multi-data-ids + * 配置内容示例: + * spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false + * spring.datasource.username=root + * spring.datasource.password=root + * spring.datasource.initial-size=10 + * spring.datasource.max-active=20 + */ + @NacosPropertySource(dataId = "datasource.properties", groupId = "multi-data-ids"), + + /* ACM 控制台添加配置: + * Data ID:redis.properties + * Group:multi-data-ids + * 配置内容示例: + * spring.redis.host=localhost + * spring.redis.password=20190101 + * spring.redis.timeout=5000 + * spring.redis.max-idle=5 + * spring.redis.max-active=10 + * spring.redis.max-wait=3000 + * spring.redis.test-on-borrow=false + */ + @NacosPropertySource(dataId = "redis.properties", groupId = "multi-data-ids") +}) +public class NacosConfiguration { + +} \ No newline at end of file diff --git a/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/controller/UserController.java b/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/controller/UserController.java new file mode 100644 index 000000000..5afe953ab --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/controller/UserController.java @@ -0,0 +1,33 @@ +package com.alibaba.nacos.example.spring.controller; + +import com.alibaba.nacos.example.spring.model.User; +import com.alibaba.nacos.example.spring.service.UserService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +/** + * @author hexu.hxy + */ +@Controller +@RequestMapping("user") +public class UserController { + + private final UserService userService; + + @Autowired + public UserController(UserService userService) {this.userService = userService;} + + /** + * http://localhost:8080/user?id=1 + */ + @RequestMapping(method = RequestMethod.GET) + @ResponseBody + public User get(@RequestParam long id) { + return userService.findById(id); + } + +} \ No newline at end of file diff --git a/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/model/User.java b/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/model/User.java new file mode 100644 index 000000000..f5bdad544 --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/model/User.java @@ -0,0 +1,33 @@ +package com.alibaba.nacos.example.spring.model; + +import java.io.Serializable; + +/** + * CREATE TABLE `user` ( + * `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, + * `name` varchar(10) NOT NULL DEFAULT '', + * PRIMARY KEY (`id`) + * ) ENGINE=InnoDB DEFAULT CHARSET=utf8; + */ +public class User implements Serializable { + + private Long id; + + private String name; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/service/UserService.java b/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/service/UserService.java new file mode 100644 index 000000000..0639a9ece --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/service/UserService.java @@ -0,0 +1,13 @@ +package com.alibaba.nacos.example.spring.service; + +import com.alibaba.nacos.example.spring.model.User; + +/** + * @author hexu.hxy + * @date 2019/1/7 + */ +public interface UserService { + + User findById(Long id); + +} \ No newline at end of file diff --git a/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/service/impl/UserServiceImpl.java b/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/service/impl/UserServiceImpl.java new file mode 100644 index 000000000..3a8ce59dd --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/service/impl/UserServiceImpl.java @@ -0,0 +1,84 @@ +package com.alibaba.nacos.example.spring.service.impl; + +import com.alibaba.nacos.api.config.annotation.NacosValue; +import com.alibaba.nacos.example.spring.model.User; +import com.alibaba.nacos.example.spring.service.UserService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.dao.EmptyResultDataAccessException; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.jdbc.core.JdbcTemplate; +import org.springframework.jdbc.core.RowMapper; +import org.springframework.stereotype.Service; + +import java.sql.ResultSet; +import java.sql.SQLException; + +/** + * @author hexu.hxy + * @date 2019/1/7 + */ +@Service +public class UserServiceImpl implements UserService { + + private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class); + + private final JdbcTemplate jdbcTemplate; + + private final RedisTemplate redisTemplate; + + @NacosValue(value = "${app.user.cache}", autoRefreshed = true) + private boolean cache; + + @Autowired + public UserServiceImpl(JdbcTemplate jdbcTemplate, RedisTemplate redisTemplate) { + this.jdbcTemplate = jdbcTemplate; + this.redisTemplate = redisTemplate; + } + + @Override + public User findById(Long id) { + LOGGER.info("cache: {}", cache); + + if (cache) { + Object obj = redisTemplate.opsForValue().get(key(id)); + if (obj != null) { + LOGGER.info("get user from cache, id: {}", id); + return (User)obj; + } + } + + User user = getUser(id); + if (user != null) { + if (cache) { + LOGGER.info("set cache for user, id: {}", id); + redisTemplate.opsForValue().set(key(id), user); + } + } + + return user; + } + + private User getUser(Long id) { + try { + return jdbcTemplate.queryForObject("SELECT id, name FROM user WHERE id=?", new Object[] {id}, + new RowMapper() { + @Override + public User mapRow(ResultSet rs, int rowNum) throws SQLException { + User user = new User(); + user.setId(rs.getLong(1)); + user.setName(rs.getString(2)); + return user; + } + }); + } catch (EmptyResultDataAccessException e) { + return null; + } + } + + private String key(Long id) { + return String.format("nacos-spring-config-multi-data-ids-example:user:%d", id); + } + +} \ No newline at end of file diff --git a/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/webapp/WEB-INF/cache.xml b/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/webapp/WEB-INF/cache.xml new file mode 100644 index 000000000..8e5ca5d3a --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/webapp/WEB-INF/cache.xml @@ -0,0 +1,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/webapp/WEB-INF/datasource.xml b/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/webapp/WEB-INF/datasource.xml new file mode 100644 index 000000000..1efe1a8d5 --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/webapp/WEB-INF/datasource.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + diff --git a/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml b/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml new file mode 100644 index 000000000..ac868c276 --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + + diff --git a/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/webapp/WEB-INF/web.xml b/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 000000000..2e1696d9b --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,17 @@ + + + + + dispatcherServlet + org.springframework.web.servlet.DispatcherServlet + 1 + + + + dispatcherServlet + / + + + \ No newline at end of file diff --git a/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/webapp/index.jsp b/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/webapp/index.jsp new file mode 100644 index 000000000..c38169bb9 --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-multi-data-ids-example/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

Hello World!

+ + diff --git a/nacos-spring-example/nacos-spring-config-pojo-example/pom.xml b/nacos-spring-example/nacos-spring-config-pojo-example/pom.xml new file mode 100644 index 000000000..cbae9186a --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-pojo-example/pom.xml @@ -0,0 +1,38 @@ + + + + + nacos-spring-example + com.alibaba.nacos + 0.2.0-SNAPSHOT + + + 4.0.0 + + nacos-spring-config-pojo-example + pom + + nacos-spring-config-pojo-example + + http://nacos.io + + + + + javax.servlet + javax.servlet-api + + + + org.springframework + spring-webmvc + + + + com.alibaba.nacos + nacos-spring-context + 0.1.0-RC1 + + + diff --git a/nacos-spring-example/nacos-spring-config-pojo-example/src/main/java/com/alibaba/nacos/example/spring/PromotionConfiguration.java b/nacos-spring-example/nacos-spring-config-pojo-example/src/main/java/com/alibaba/nacos/example/spring/PromotionConfiguration.java new file mode 100644 index 000000000..a746b43fb --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-pojo-example/src/main/java/com/alibaba/nacos/example/spring/PromotionConfiguration.java @@ -0,0 +1,24 @@ +package com.alibaba.nacos.example.spring; + +import com.alibaba.nacos.api.annotation.NacosProperties; +import com.alibaba.nacos.example.spring.model.Promotion; +import com.alibaba.nacos.spring.context.annotation.config.EnableNacosConfig; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +// 在命名空间详情处可以获取到 endpoint 和 namespace;accessKey 和 secretKey 推荐使用 RAM 账户的 +@EnableNacosConfig(globalProperties = @NacosProperties( + endpoint = "xxx", + namespace = "xxx", + accessKey = "xxx", + secretKey = "xxx" +)) +public class PromotionConfiguration { + + @Bean + public Promotion promotion() { + return new Promotion(); + } + +} \ No newline at end of file diff --git a/nacos-spring-example/nacos-spring-config-pojo-example/src/main/java/com/alibaba/nacos/example/spring/controller/PromotionController.java b/nacos-spring-example/nacos-spring-config-pojo-example/src/main/java/com/alibaba/nacos/example/spring/controller/PromotionController.java new file mode 100644 index 000000000..a792b73d8 --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-pojo-example/src/main/java/com/alibaba/nacos/example/spring/controller/PromotionController.java @@ -0,0 +1,25 @@ +package com.alibaba.nacos.example.spring.controller; + +import com.alibaba.nacos.example.spring.model.Promotion; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import static org.springframework.web.bind.annotation.RequestMethod.GET; + +@Controller +@RequestMapping("promotion") +public class PromotionController { + + @Autowired + private Promotion promotion; + + @RequestMapping(method = GET) + @ResponseBody + public Promotion get() { + return promotion; + } + +} + diff --git a/nacos-spring-example/nacos-spring-config-pojo-example/src/main/java/com/alibaba/nacos/example/spring/model/Promotion.java b/nacos-spring-example/nacos-spring-config-pojo-example/src/main/java/com/alibaba/nacos/example/spring/model/Promotion.java new file mode 100644 index 000000000..689f2186f --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-pojo-example/src/main/java/com/alibaba/nacos/example/spring/model/Promotion.java @@ -0,0 +1,76 @@ +package com.alibaba.nacos.example.spring.model; + +import com.alibaba.nacos.api.config.annotation.NacosConfigurationProperties; +import com.alibaba.nacos.api.config.annotation.NacosIgnore; +import com.alibaba.nacos.api.config.annotation.NacosProperty; + +/** + * @author hexu.hxy + * @date 2019/1/6 + * + * ACM 控制台添加配置: + * Data ID:promotion.properties + * Group:spring-example + * 配置内容: + * sku=1 + * price=99.9 + * amount=1000 + * desc=desc + * description=description + * sold=99 + */ +@NacosConfigurationProperties(dataId = "promotion.properties", groupId = "spring-example", autoRefreshed = true) +public class Promotion { + + private long sku; + + private double price; + + private int amount; + + @NacosProperty(value = "desc") + private String description; + + @NacosIgnore + private int sold; + + public long getSku() { + return sku; + } + + public void setSku(long sku) { + this.sku = sku; + } + + public double getPrice() { + return price; + } + + public void setPrice(double price) { + this.price = price; + } + + public int getAmount() { + return amount; + } + + public void setAmount(int amount) { + this.amount = amount; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public int getSold() { + return sold; + } + + public void setSold(int sold) { + this.sold = sold; + } +} diff --git a/nacos-spring-example/nacos-spring-config-pojo-example/src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml b/nacos-spring-example/nacos-spring-config-pojo-example/src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml new file mode 100644 index 000000000..2c1a84ae1 --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-pojo-example/src/main/webapp/WEB-INF/dispatcherServlet-servlet.xml @@ -0,0 +1,23 @@ + + + + + + + + + + + + + diff --git a/nacos-spring-example/nacos-spring-config-pojo-example/src/main/webapp/WEB-INF/web.xml b/nacos-spring-example/nacos-spring-config-pojo-example/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 000000000..2e1696d9b --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-pojo-example/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,17 @@ + + + + + dispatcherServlet + org.springframework.web.servlet.DispatcherServlet + 1 + + + + dispatcherServlet + / + + + \ No newline at end of file diff --git a/nacos-spring-example/nacos-spring-config-pojo-example/src/main/webapp/index.jsp b/nacos-spring-example/nacos-spring-config-pojo-example/src/main/webapp/index.jsp new file mode 100644 index 000000000..c38169bb9 --- /dev/null +++ b/nacos-spring-example/nacos-spring-config-pojo-example/src/main/webapp/index.jsp @@ -0,0 +1,5 @@ + + +

Hello World!

+ + diff --git a/nacos-spring-example/pom.xml b/nacos-spring-example/pom.xml index 870d8ce19..ae37176dd 100644 --- a/nacos-spring-example/pom.xml +++ b/nacos-spring-example/pom.xml @@ -15,6 +15,9 @@ pom nacos-spring-config-example + nacos-spring-config-pojo-example + nacos-spring-config-listener-example + nacos-spring-config-multi-data-ids-example nacos-spring-config-datasource-example nacos-spring-discovery-example From 3295ec09287cad7fc11264f92312e2532f7ffd27 Mon Sep 17 00:00:00 2001 From: hxy1991 Date: Thu, 24 Jan 2019 18:30:27 +0800 Subject: [PATCH 12/13] Remove unnecessary annotation --- .../alibaba/nacos/example/spring/cloud/service/UserService.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/service/UserService.java b/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/service/UserService.java index f05009581..a076829e0 100644 --- a/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/service/UserService.java +++ b/nacos-spring-cloud-example/nacos-spring-cloud-config-multi-data-ids-example/src/main/java/com/alibaba/nacos/example/spring/cloud/service/UserService.java @@ -1,7 +1,6 @@ package com.alibaba.nacos.example.spring.cloud.service; import com.alibaba.nacos.example.spring.cloud.model.User; -import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.stereotype.Service; /** @@ -10,7 +9,6 @@ */ @Service -@RefreshScope public interface UserService { User findById(Long id); From 255ce678911c8aed3ea6b96e08e1443a06b6d563 Mon Sep 17 00:00:00 2001 From: hxy1991 Date: Tue, 30 Jul 2019 14:34:20 +0800 Subject: [PATCH 13/13] Modified dataId --- .../nacos/example/spring/boot/NacosConfigApplication.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/NacosConfigApplication.java b/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/NacosConfigApplication.java index 4217efcd8..73b501f9e 100644 --- a/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/NacosConfigApplication.java +++ b/nacos-spring-boot-example/nacos-spring-boot-config-example/src/main/java/com/alibaba/nacos/example/spring/boot/NacosConfigApplication.java @@ -9,14 +9,14 @@ *

* ACM 控制台添加配置: *

- * Data ID:com.alibaba.nacos.example + * Data ID:com.alibaba.nacos.example.properties *

* Group:DEFAULT_GROUP *

* 配置内容:connectTimeoutInMills=5000 */ @SpringBootApplication -@NacosPropertySource(dataId = "com.alibaba.nacos.example", autoRefreshed = true) +@NacosPropertySource(dataId = "com.alibaba.nacos.example.properties", autoRefreshed = true) public class NacosConfigApplication { public static void main(String[] args) {