From e3a164a0c55bac6d483dc0099a122c0b763f8119 Mon Sep 17 00:00:00 2001 From: mingmen Date: Mon, 11 Dec 2023 15:27:37 +0800 Subject: [PATCH 1/2] =?UTF-8?q?feat(=E6=B7=BB=E5=8A=A0=20nacos=20sample):?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- samples/README.md | 2 +- .../springboot-samples/config/nacos/README.md | 41 +++++++++ .../config/nacos/base/pom.xml | 72 +++++++++++++++ .../sofa/config/nacos/ConfigController.java | 27 ++++++ .../sofa/config/nacos/NacosApplication.java | 18 ++++ .../src/main/resources/application.properties | 4 + .../config/nacos/NacosApplicationTests.java | 13 +++ .../config/nacos/biz1/conf/ark/rules.txt | 90 +++++++++++++++++++ .../config/nacos/biz1/pom.xml | 84 +++++++++++++++++ .../sofa/config/nacos/ConfigController.java | 27 ++++++ .../sofa/config/nacos/NacosApplication.java | 18 ++++ .../src/main/resources/application.properties | 7 ++ .../config/nacos/ApolloApplicationTests.java | 13 +++ .../config/nacos/biz2/conf/ark/rules.txt | 90 +++++++++++++++++++ .../config/nacos/biz2/pom.xml | 82 +++++++++++++++++ .../sofa/config/nacos/ConfigController.java | 27 ++++++ .../sofa/config/nacos/NacosApplication.java | 18 ++++ .../src/main/resources/application.properties | 7 ++ .../config/nacos/ApolloApplicationTests.java | 13 +++ .../springboot-samples/config/nacos/pom.xml | 39 ++++++++ samples/springboot-samples/pom.xml | 1 + 21 files changed, 692 insertions(+), 1 deletion(-) create mode 100644 samples/springboot-samples/config/nacos/README.md create mode 100644 samples/springboot-samples/config/nacos/base/pom.xml create mode 100644 samples/springboot-samples/config/nacos/base/src/main/java/com/alipay/sofa/config/nacos/ConfigController.java create mode 100644 samples/springboot-samples/config/nacos/base/src/main/java/com/alipay/sofa/config/nacos/NacosApplication.java create mode 100644 samples/springboot-samples/config/nacos/base/src/main/resources/application.properties create mode 100644 samples/springboot-samples/config/nacos/base/src/test/java/com/alipay/sofa/config/nacos/NacosApplicationTests.java create mode 100644 samples/springboot-samples/config/nacos/biz1/conf/ark/rules.txt create mode 100644 samples/springboot-samples/config/nacos/biz1/pom.xml create mode 100644 samples/springboot-samples/config/nacos/biz1/src/main/java/com/alipay/sofa/config/nacos/ConfigController.java create mode 100644 samples/springboot-samples/config/nacos/biz1/src/main/java/com/alipay/sofa/config/nacos/NacosApplication.java create mode 100644 samples/springboot-samples/config/nacos/biz1/src/main/resources/application.properties create mode 100644 samples/springboot-samples/config/nacos/biz1/src/test/java/com/alipay/sofa/config/nacos/ApolloApplicationTests.java create mode 100644 samples/springboot-samples/config/nacos/biz2/conf/ark/rules.txt create mode 100644 samples/springboot-samples/config/nacos/biz2/pom.xml create mode 100644 samples/springboot-samples/config/nacos/biz2/src/main/java/com/alipay/sofa/config/nacos/ConfigController.java create mode 100644 samples/springboot-samples/config/nacos/biz2/src/main/java/com/alipay/sofa/config/nacos/NacosApplication.java create mode 100644 samples/springboot-samples/config/nacos/biz2/src/main/resources/application.properties create mode 100644 samples/springboot-samples/config/nacos/biz2/src/test/java/com/alipay/sofa/config/nacos/ApolloApplicationTests.java create mode 100644 samples/springboot-samples/config/nacos/pom.xml diff --git a/samples/README.md b/samples/README.md index 64c7472d4..4e35da27c 100644 --- a/samples/README.md +++ b/samples/README.md @@ -16,6 +16,6 @@ | dubbo | dubbo + grpc | 模块使用 grpc | [samples/dubbo-samples/rpc/grpc](https://github.com/sofastack/sofa-serverless/tree/master/samples/dubbo-samples/rpc/grpc) | | sofaboot | sofarpc/tomcat | 基座调用模块、中台模式 | [samples/sofaboot-samples/dynamic-stock](https://github.com/sofastack/sofa-serverless/tree/master/samples/sofaboot-samples/dynamic-stock) | | springboot3 | springboot3 | springboot3 | [samples/sofaboot3-samples](https://github.com/sofastack/sofa-serverless/tree/master/samples/sofaboot3-samples) | | -### 注意请在编译器单独导入对应 samples 工程,例如 sofa-serverless/samples/springboot-samples 工程,否则会出现 BizRuntieContext Not found 的报错。 +### 注意请在编译器单独导入对应 samples 工程,例如 sofa-serverless/samples/springboot-samples 工程,否则会出现 BizRuntimeContext Not found 的报错。 ![biz runtime context not found](bizruntimecontext_not_found.png) 原因:samples 工程与 sofa-serverless-runtime 在一个工程目录里,会优先使用本地的 sofa-serverless-runtime,而不是 maven 依赖的 sofa-serverless-runtime,导致找不到 BizRuntimeContext 类。 diff --git a/samples/springboot-samples/config/nacos/README.md b/samples/springboot-samples/config/nacos/README.md new file mode 100644 index 000000000..af9a4d915 --- /dev/null +++ b/samples/springboot-samples/config/nacos/README.md @@ -0,0 +1,41 @@ +# 实验内容 + + +1. 不同模块注册并读取不同的 dataId +2. 不同模块使用相同 dataId + +## 实验任务 + +### 前置准备 +nacos采用标准的 CS 分布式架构, 在实验前需要先将 server 端启动。 +启动步骤参考: [Nacos 快速开始](https://nacos.io/zh-cn/docs/quick-start.html) + +下面我们将启动一个基座 base 和两个模块 biz1 和 biz2, 模块与其对应的 dataId 映射如下 +- base模块: base +- biz1模块: biz +- biz2模块: biz + +> 由于 nacos 依赖 prometheus 做指标采集, 由于其内部机制, io.prometheus:simpleclient 该 GA 不能做包与类下沉基座, 否则模块会启动不了 + + +### 不同模块注册并读取不同的 dataId + +启动 base、biz1、biz2 模块后, 验证 dataId的读取逻辑 +- base: 调用 *curl http://localhost:8090/config/get* 返回false +- biz1: 调用 *curl http://localhost:8090/biz1/config/get* 返回false +- biz2: 调用 *curl http://localhost:8090/biz2/config/get* 返回false + +执行 *curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=base&group=DEFAULT_GROUP&content=useLocalCache=true"* 将 dataId base 的值修改为 true + +再次调用 *curl http://localhost:8090/config/get* 发现值已经范围 true, 说明基座的 dataId已经修改成功且生效。而调用 biz 查询依旧是 false + +### 不同模块使用相同 dataId + +在上述基础上, 由于模块 biz1 和 biz2 都使用 "biz" 作为 dataId, 期望修改该 dataId 的值后, biz1 和 biz2 读取的值同时发生变化。 + +执行 *curl -X POST "http://127.0.0.1:8848/nacos/v1/cs/configs?dataId=biz&group=DEFAULT_GROUP&content=useLocalCache=true"* 将 dataId biz 的值修改为 true + +- biz1: 调用 *curl http://localhost:8090/biz1/config/get* 返回 true +- biz2: 调用 *curl http://localhost:8090/biz2/config/get* 返回 true + +发现, 共享同 dataId 的两个模块都读取到了修改的值。 \ No newline at end of file diff --git a/samples/springboot-samples/config/nacos/base/pom.xml b/samples/springboot-samples/config/nacos/base/pom.xml new file mode 100644 index 000000000..7ced3dcde --- /dev/null +++ b/samples/springboot-samples/config/nacos/base/pom.xml @@ -0,0 +1,72 @@ + + + 4.0.0 + + com.alipay.sofa.config + nacos + 0.0.1-SNAPSHOT + ../pom.xml + + base-nacos + 0.0.1-SNAPSHOT + + + + org.springframework.boot + spring-boot-autoconfigure + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + com.alibaba.boot + nacos-config-spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web + + + + com.alipay.sofa.serverless + sofa-serverless-base-starter + + + com.alipay.sofa + web-ark-plugin + + + + com.google.inject + guice + 5.1.0 + + + + org.projectlombok + lombok + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/samples/springboot-samples/config/nacos/base/src/main/java/com/alipay/sofa/config/nacos/ConfigController.java b/samples/springboot-samples/config/nacos/base/src/main/java/com/alipay/sofa/config/nacos/ConfigController.java new file mode 100644 index 000000000..93a3fa19e --- /dev/null +++ b/samples/springboot-samples/config/nacos/base/src/main/java/com/alipay/sofa/config/nacos/ConfigController.java @@ -0,0 +1,27 @@ +package com.alipay.sofa.config.nacos; + +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; + +import static org.springframework.web.bind.annotation.RequestMethod.GET; + +/** + * @author mingmen + * @date 2023/12/11 + */ +@Controller +@RequestMapping("config") +public class ConfigController { + + @NacosValue(value = "${useLocalCache:false}", autoRefreshed = true) + private boolean useLocalCache; + + @RequestMapping(value = "/get", method = GET) + @ResponseBody + public boolean get() { + return useLocalCache; + } +} diff --git a/samples/springboot-samples/config/nacos/base/src/main/java/com/alipay/sofa/config/nacos/NacosApplication.java b/samples/springboot-samples/config/nacos/base/src/main/java/com/alipay/sofa/config/nacos/NacosApplication.java new file mode 100644 index 000000000..ea16193c4 --- /dev/null +++ b/samples/springboot-samples/config/nacos/base/src/main/java/com/alipay/sofa/config/nacos/NacosApplication.java @@ -0,0 +1,18 @@ +package com.alipay.sofa.config.nacos; + +import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.EnableConfigurationProperties; + +@EnableConfigurationProperties +@NacosPropertySource(dataId = "base", autoRefreshed = true) +@SpringBootApplication +public class NacosApplication { + + public static void main(String[] args) { + + SpringApplication.run(NacosApplication.class, args); + } +} \ No newline at end of file diff --git a/samples/springboot-samples/config/nacos/base/src/main/resources/application.properties b/samples/springboot-samples/config/nacos/base/src/main/resources/application.properties new file mode 100644 index 000000000..f19bcc24f --- /dev/null +++ b/samples/springboot-samples/config/nacos/base/src/main/resources/application.properties @@ -0,0 +1,4 @@ +spring.application.name=base-nacos +server.port=8090 + +nacos.config.server-addr=127.0.0.1:8848 diff --git a/samples/springboot-samples/config/nacos/base/src/test/java/com/alipay/sofa/config/nacos/NacosApplicationTests.java b/samples/springboot-samples/config/nacos/base/src/test/java/com/alipay/sofa/config/nacos/NacosApplicationTests.java new file mode 100644 index 000000000..d8d30f3a6 --- /dev/null +++ b/samples/springboot-samples/config/nacos/base/src/test/java/com/alipay/sofa/config/nacos/NacosApplicationTests.java @@ -0,0 +1,13 @@ +package com.alipay.sofa.config.nacos; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class NacosApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/samples/springboot-samples/config/nacos/biz1/conf/ark/rules.txt b/samples/springboot-samples/config/nacos/biz1/conf/ark/rules.txt new file mode 100644 index 000000000..e62747717 --- /dev/null +++ b/samples/springboot-samples/config/nacos/biz1/conf/ark/rules.txt @@ -0,0 +1,90 @@ +excludeArtifactIds=commons-collections +excludeArtifactIds=commons-httpclient +excludeArtifactIds=commons-io +excludeArtifactIds=commons-lang +excludeGroupIds=aopalliance* +excludeGroupIds=asm* +excludeGroupIds=cglib* +excludeGroupIds=com.alibaba.common.lang* +excludeGroupIds=com.alibaba.common.resourcebundle* +excludeGroupIds=com.alibaba.tbase* +excludeGroupIds=com.alipay* +excludeGroupIds=com.antcloud.antvip* +excludeGroupIds=com.caucho.hessian* +excludeGroupIds=com.ctc.wstx* +excludeGroupIds=com.fasterxml* +excludeGroupIds=com.google.code* +excludeGroupIds=com.google.common* +excludeGroupIds=com.google.gson* +excludeGroupIds=com.google.guava* +excludeGroupIds=com.google.j2objc* +excludeGroupIds=com.google.http-client* +excludeGroupIds=com.google.inject* +excludeGroupIds=com.google.protobuf* +excludeGroupIds=com.ibatis* +excludeGroupIds=com.iwallet.biz* +excludeGroupIds=com.lmax* +excludeGroupIds=com.taobao.config* +excludeGroupIds=com.taobao.hsf* +excludeGroupIds=com.taobao.notify* +excludeGroupIds=com.taobao.remoting* +excludeGroupIds=com.taobao.tair* +excludeGroupIds=groovy* +excludeGroupIds=io.fury* +excludeGroupIds=io.grpc* +excludeGroupIds=io.mosn.layotto* +excludeGroupIds=io.netty* +excludeGroupIds=io.openmessaging* +excludeGroupIds=javax* +excludeGroupIds=javax.el* +excludeGroupIds=javax.script* +excludeGroupIds=javax.servlet* +excludeGroupIds=javax.validation* +excludeGroupIds=loccs-bcprov* +excludeGroupIds=log4j* +excludeGroupIds=mysql* +excludeGroupIds=net.sf.acegisecurity* +excludeGroupIds=net.sf.cglib* +excludeGroupIds=netty* +excludeGroupIds=ognl* +excludeGroupIds=org.aopalliance* +excludeGroupIds=org.apache* +excludeGroupIds=org.aspectj* +excludeGroupIds=org.codehaus* +excludeGroupIds=org.codehaus.groovy* +excludeGroupIds=org.codehaus.xfire* +excludeGroupIds=org.dom4j* +excludeGroupIds=org.hibernate.validator* +excludeGroupIds=org.junit* +excludeGroupIds=org.mvel2* +excludeGroupIds=org.mybatis* +excludeGroupIds=org.mybatis.spring* +excludeGroupIds=org.mybatis.spring.boot.autoconfigure* +excludeGroupIds=org.projectlombok* +excludeGroupIds=org.quartz* +excludeGroupIds=org.reflections* +excludeGroupIds=org.slf4j* +excludeGroupIds=org.springframework* +excludeGroupIds=org.yaml* +excludeGroupIds=xerces* +excludeGroupIds=xml-apis* +excludeGroupIds=xpp3* +excludeGroupIds=jakarta* +excludeGroupIds=org.latencyutils* +excludeGroupIds=org.hdrhistogram* +excludeGroupIds=io.micrometer* +excludeGroupIds=io.micrometer* +excludeGroupIds=ch.qos.logback* +excludeGroupIds=com.squareup.okhttp3* +excludeGroupIds=com.squareup.okhttp* +excludeGroupIds=net.sf.ehcache* +excludeGroupIds=redis.clients* +excludeGroupIds=net.bytebuddy* +excludeGroupIds=com.taobao.text* +excludeGroupIds=net.java.dev.jna* +excludeGroupIds=com.google.errorprone* +excludeGroupIds=com.github.oshi* +excludeGroupIds=org.checkerframework* + + +excludeArtifactIds=fastjson \ No newline at end of file diff --git a/samples/springboot-samples/config/nacos/biz1/pom.xml b/samples/springboot-samples/config/nacos/biz1/pom.xml new file mode 100644 index 000000000..9099c7644 --- /dev/null +++ b/samples/springboot-samples/config/nacos/biz1/pom.xml @@ -0,0 +1,84 @@ + + + 4.0.0 + + com.alipay.sofa.config + nacos + 0.0.1-SNAPSHOT + ../pom.xml + + biz1-nacos + 0.0.1-SNAPSHOT + + + + org.springframework.boot + spring-boot-autoconfigure + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + com.alibaba.boot + nacos-config-spring-boot-starter + + + + + org.springframework.boot + spring-boot-starter-web + + + + + com.google.inject + guice + 5.1.0 + + + + org.projectlombok + lombok + + + + + + + com.alipay.sofa + sofa-ark-maven-plugin + ${sofa.ark.version} + + + default-cli + + repackage + + + + + true + ./target + biz1-nacos + biz1 + true + rules.txt + + + + + + + + + diff --git a/samples/springboot-samples/config/nacos/biz1/src/main/java/com/alipay/sofa/config/nacos/ConfigController.java b/samples/springboot-samples/config/nacos/biz1/src/main/java/com/alipay/sofa/config/nacos/ConfigController.java new file mode 100644 index 000000000..93a3fa19e --- /dev/null +++ b/samples/springboot-samples/config/nacos/biz1/src/main/java/com/alipay/sofa/config/nacos/ConfigController.java @@ -0,0 +1,27 @@ +package com.alipay.sofa.config.nacos; + +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; + +import static org.springframework.web.bind.annotation.RequestMethod.GET; + +/** + * @author mingmen + * @date 2023/12/11 + */ +@Controller +@RequestMapping("config") +public class ConfigController { + + @NacosValue(value = "${useLocalCache:false}", autoRefreshed = true) + private boolean useLocalCache; + + @RequestMapping(value = "/get", method = GET) + @ResponseBody + public boolean get() { + return useLocalCache; + } +} diff --git a/samples/springboot-samples/config/nacos/biz1/src/main/java/com/alipay/sofa/config/nacos/NacosApplication.java b/samples/springboot-samples/config/nacos/biz1/src/main/java/com/alipay/sofa/config/nacos/NacosApplication.java new file mode 100644 index 000000000..302d6d797 --- /dev/null +++ b/samples/springboot-samples/config/nacos/biz1/src/main/java/com/alipay/sofa/config/nacos/NacosApplication.java @@ -0,0 +1,18 @@ +package com.alipay.sofa.config.nacos; + +import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.EnableConfigurationProperties; + +@EnableConfigurationProperties +@NacosPropertySource(dataId = "biz", autoRefreshed = true) +@SpringBootApplication +public class NacosApplication { + + public static void main(String[] args) { + + SpringApplication.run(NacosApplication.class, args); + } +} diff --git a/samples/springboot-samples/config/nacos/biz1/src/main/resources/application.properties b/samples/springboot-samples/config/nacos/biz1/src/main/resources/application.properties new file mode 100644 index 000000000..8456b9006 --- /dev/null +++ b/samples/springboot-samples/config/nacos/biz1/src/main/resources/application.properties @@ -0,0 +1,7 @@ +spring.application.name=biz1-nacos + +server.port=8091 + +spring.jmx.default-domain=${spring.application.name} + +nacos.config.server-addr=127.0.0.1:8848 \ No newline at end of file diff --git a/samples/springboot-samples/config/nacos/biz1/src/test/java/com/alipay/sofa/config/nacos/ApolloApplicationTests.java b/samples/springboot-samples/config/nacos/biz1/src/test/java/com/alipay/sofa/config/nacos/ApolloApplicationTests.java new file mode 100644 index 000000000..82bd5901f --- /dev/null +++ b/samples/springboot-samples/config/nacos/biz1/src/test/java/com/alipay/sofa/config/nacos/ApolloApplicationTests.java @@ -0,0 +1,13 @@ +package com.alipay.sofa.config.nacos; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class ApolloApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/samples/springboot-samples/config/nacos/biz2/conf/ark/rules.txt b/samples/springboot-samples/config/nacos/biz2/conf/ark/rules.txt new file mode 100644 index 000000000..e62747717 --- /dev/null +++ b/samples/springboot-samples/config/nacos/biz2/conf/ark/rules.txt @@ -0,0 +1,90 @@ +excludeArtifactIds=commons-collections +excludeArtifactIds=commons-httpclient +excludeArtifactIds=commons-io +excludeArtifactIds=commons-lang +excludeGroupIds=aopalliance* +excludeGroupIds=asm* +excludeGroupIds=cglib* +excludeGroupIds=com.alibaba.common.lang* +excludeGroupIds=com.alibaba.common.resourcebundle* +excludeGroupIds=com.alibaba.tbase* +excludeGroupIds=com.alipay* +excludeGroupIds=com.antcloud.antvip* +excludeGroupIds=com.caucho.hessian* +excludeGroupIds=com.ctc.wstx* +excludeGroupIds=com.fasterxml* +excludeGroupIds=com.google.code* +excludeGroupIds=com.google.common* +excludeGroupIds=com.google.gson* +excludeGroupIds=com.google.guava* +excludeGroupIds=com.google.j2objc* +excludeGroupIds=com.google.http-client* +excludeGroupIds=com.google.inject* +excludeGroupIds=com.google.protobuf* +excludeGroupIds=com.ibatis* +excludeGroupIds=com.iwallet.biz* +excludeGroupIds=com.lmax* +excludeGroupIds=com.taobao.config* +excludeGroupIds=com.taobao.hsf* +excludeGroupIds=com.taobao.notify* +excludeGroupIds=com.taobao.remoting* +excludeGroupIds=com.taobao.tair* +excludeGroupIds=groovy* +excludeGroupIds=io.fury* +excludeGroupIds=io.grpc* +excludeGroupIds=io.mosn.layotto* +excludeGroupIds=io.netty* +excludeGroupIds=io.openmessaging* +excludeGroupIds=javax* +excludeGroupIds=javax.el* +excludeGroupIds=javax.script* +excludeGroupIds=javax.servlet* +excludeGroupIds=javax.validation* +excludeGroupIds=loccs-bcprov* +excludeGroupIds=log4j* +excludeGroupIds=mysql* +excludeGroupIds=net.sf.acegisecurity* +excludeGroupIds=net.sf.cglib* +excludeGroupIds=netty* +excludeGroupIds=ognl* +excludeGroupIds=org.aopalliance* +excludeGroupIds=org.apache* +excludeGroupIds=org.aspectj* +excludeGroupIds=org.codehaus* +excludeGroupIds=org.codehaus.groovy* +excludeGroupIds=org.codehaus.xfire* +excludeGroupIds=org.dom4j* +excludeGroupIds=org.hibernate.validator* +excludeGroupIds=org.junit* +excludeGroupIds=org.mvel2* +excludeGroupIds=org.mybatis* +excludeGroupIds=org.mybatis.spring* +excludeGroupIds=org.mybatis.spring.boot.autoconfigure* +excludeGroupIds=org.projectlombok* +excludeGroupIds=org.quartz* +excludeGroupIds=org.reflections* +excludeGroupIds=org.slf4j* +excludeGroupIds=org.springframework* +excludeGroupIds=org.yaml* +excludeGroupIds=xerces* +excludeGroupIds=xml-apis* +excludeGroupIds=xpp3* +excludeGroupIds=jakarta* +excludeGroupIds=org.latencyutils* +excludeGroupIds=org.hdrhistogram* +excludeGroupIds=io.micrometer* +excludeGroupIds=io.micrometer* +excludeGroupIds=ch.qos.logback* +excludeGroupIds=com.squareup.okhttp3* +excludeGroupIds=com.squareup.okhttp* +excludeGroupIds=net.sf.ehcache* +excludeGroupIds=redis.clients* +excludeGroupIds=net.bytebuddy* +excludeGroupIds=com.taobao.text* +excludeGroupIds=net.java.dev.jna* +excludeGroupIds=com.google.errorprone* +excludeGroupIds=com.github.oshi* +excludeGroupIds=org.checkerframework* + + +excludeArtifactIds=fastjson \ No newline at end of file diff --git a/samples/springboot-samples/config/nacos/biz2/pom.xml b/samples/springboot-samples/config/nacos/biz2/pom.xml new file mode 100644 index 000000000..9ee77be3d --- /dev/null +++ b/samples/springboot-samples/config/nacos/biz2/pom.xml @@ -0,0 +1,82 @@ + + + 4.0.0 + + com.alipay.sofa.config + nacos + 0.0.1-SNAPSHOT + ../pom.xml + + biz2-nacos + 0.0.1-SNAPSHOT + + + + org.springframework.boot + spring-boot-autoconfigure + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + com.alibaba.boot + nacos-config-spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-web + + + + com.google.inject + guice + 5.1.0 + + + + org.projectlombok + lombok + + + + + + + com.alipay.sofa + sofa-ark-maven-plugin + ${sofa.ark.version} + + + default-cli + + repackage + + + + + true + ./target + biz2-nacos + biz2 + true + rules.txt + + + + + + + + + diff --git a/samples/springboot-samples/config/nacos/biz2/src/main/java/com/alipay/sofa/config/nacos/ConfigController.java b/samples/springboot-samples/config/nacos/biz2/src/main/java/com/alipay/sofa/config/nacos/ConfigController.java new file mode 100644 index 000000000..93a3fa19e --- /dev/null +++ b/samples/springboot-samples/config/nacos/biz2/src/main/java/com/alipay/sofa/config/nacos/ConfigController.java @@ -0,0 +1,27 @@ +package com.alipay.sofa.config.nacos; + +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; + +import static org.springframework.web.bind.annotation.RequestMethod.GET; + +/** + * @author mingmen + * @date 2023/12/11 + */ +@Controller +@RequestMapping("config") +public class ConfigController { + + @NacosValue(value = "${useLocalCache:false}", autoRefreshed = true) + private boolean useLocalCache; + + @RequestMapping(value = "/get", method = GET) + @ResponseBody + public boolean get() { + return useLocalCache; + } +} diff --git a/samples/springboot-samples/config/nacos/biz2/src/main/java/com/alipay/sofa/config/nacos/NacosApplication.java b/samples/springboot-samples/config/nacos/biz2/src/main/java/com/alipay/sofa/config/nacos/NacosApplication.java new file mode 100644 index 000000000..302d6d797 --- /dev/null +++ b/samples/springboot-samples/config/nacos/biz2/src/main/java/com/alipay/sofa/config/nacos/NacosApplication.java @@ -0,0 +1,18 @@ +package com.alipay.sofa.config.nacos; + +import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.context.properties.EnableConfigurationProperties; + +@EnableConfigurationProperties +@NacosPropertySource(dataId = "biz", autoRefreshed = true) +@SpringBootApplication +public class NacosApplication { + + public static void main(String[] args) { + + SpringApplication.run(NacosApplication.class, args); + } +} diff --git a/samples/springboot-samples/config/nacos/biz2/src/main/resources/application.properties b/samples/springboot-samples/config/nacos/biz2/src/main/resources/application.properties new file mode 100644 index 000000000..b9f4d0506 --- /dev/null +++ b/samples/springboot-samples/config/nacos/biz2/src/main/resources/application.properties @@ -0,0 +1,7 @@ +spring.application.name=biz2-nacos + +server.port=8092 + +spring.jmx.default-domain=${spring.application.name} + +nacos.config.server-addr=127.0.0.1:8848 \ No newline at end of file diff --git a/samples/springboot-samples/config/nacos/biz2/src/test/java/com/alipay/sofa/config/nacos/ApolloApplicationTests.java b/samples/springboot-samples/config/nacos/biz2/src/test/java/com/alipay/sofa/config/nacos/ApolloApplicationTests.java new file mode 100644 index 000000000..82bd5901f --- /dev/null +++ b/samples/springboot-samples/config/nacos/biz2/src/test/java/com/alipay/sofa/config/nacos/ApolloApplicationTests.java @@ -0,0 +1,13 @@ +package com.alipay.sofa.config.nacos; + +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class ApolloApplicationTests { + + @Test + void contextLoads() { + } + +} diff --git a/samples/springboot-samples/config/nacos/pom.xml b/samples/springboot-samples/config/nacos/pom.xml new file mode 100644 index 000000000..332b06a1c --- /dev/null +++ b/samples/springboot-samples/config/nacos/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + + com.alipay.sofa + springboot-samples + 0.0.1-SNAPSHOT + ../../pom.xml + + com.alipay.sofa.config + nacos + 0.0.1-SNAPSHOT + nacos + nacos + pom + + + + base + biz1 + biz2 + + + + + + + + + com.alibaba.boot + nacos-config-spring-boot-starter + 0.2.12 + + + + + \ No newline at end of file diff --git a/samples/springboot-samples/pom.xml b/samples/springboot-samples/pom.xml index 4fc4f9db7..d40a8a02e 100644 --- a/samples/springboot-samples/pom.xml +++ b/samples/springboot-samples/pom.xml @@ -55,6 +55,7 @@ db/mongo/biz1 db/mongo/biz2 config/apollo + config/nacos From fcf78c2ace0c19710d3528a676fafc1e6b3a4953 Mon Sep 17 00:00:00 2001 From: mingmen Date: Mon, 11 Dec 2023 17:18:44 +0800 Subject: [PATCH 2/2] add nacos service sample --- .../config/nacos/base/pom.xml | 5 +++ .../config/nacos/DiscoveryController.java | 33 +++++++++++++++++++ .../src/main/resources/application.properties | 1 + .../config/nacos/biz1/pom.xml | 5 +++ .../config/nacos/DiscoveryController.java | 33 +++++++++++++++++++ .../src/main/resources/application.properties | 3 +- .../springboot-samples/config/nacos/pom.xml | 5 +++ 7 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 samples/springboot-samples/config/nacos/base/src/main/java/com/alipay/sofa/config/nacos/DiscoveryController.java create mode 100644 samples/springboot-samples/config/nacos/biz1/src/main/java/com/alipay/sofa/config/nacos/DiscoveryController.java diff --git a/samples/springboot-samples/config/nacos/base/pom.xml b/samples/springboot-samples/config/nacos/base/pom.xml index 7ced3dcde..195b61a50 100644 --- a/samples/springboot-samples/config/nacos/base/pom.xml +++ b/samples/springboot-samples/config/nacos/base/pom.xml @@ -33,6 +33,11 @@ nacos-config-spring-boot-starter + + com.alibaba.boot + nacos-discovery-spring-boot-starter + + org.springframework.boot spring-boot-starter-web diff --git a/samples/springboot-samples/config/nacos/base/src/main/java/com/alipay/sofa/config/nacos/DiscoveryController.java b/samples/springboot-samples/config/nacos/base/src/main/java/com/alipay/sofa/config/nacos/DiscoveryController.java new file mode 100644 index 000000000..840adbcf1 --- /dev/null +++ b/samples/springboot-samples/config/nacos/base/src/main/java/com/alipay/sofa/config/nacos/DiscoveryController.java @@ -0,0 +1,33 @@ +package com.alipay.sofa.config.nacos; + +import java.util.List; + +import com.alibaba.nacos.api.annotation.NacosInjected; +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.naming.NamingService; +import com.alibaba.nacos.api.naming.pojo.Instance; + +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; + +/** + * @author mingmen + * @date 2023/12/11 + */ +@Controller +@RequestMapping("discovery") +public class DiscoveryController { + + @NacosInjected + private NamingService namingService; + + @RequestMapping(value = "/get", method = GET) + @ResponseBody + public List get(@RequestParam String serviceName) throws NacosException { + return namingService.getAllInstances(serviceName); + } +} diff --git a/samples/springboot-samples/config/nacos/base/src/main/resources/application.properties b/samples/springboot-samples/config/nacos/base/src/main/resources/application.properties index f19bcc24f..354eb2bd9 100644 --- a/samples/springboot-samples/config/nacos/base/src/main/resources/application.properties +++ b/samples/springboot-samples/config/nacos/base/src/main/resources/application.properties @@ -2,3 +2,4 @@ spring.application.name=base-nacos server.port=8090 nacos.config.server-addr=127.0.0.1:8848 +nacos.discovery.server-addr=127.0.0.1:8848 diff --git a/samples/springboot-samples/config/nacos/biz1/pom.xml b/samples/springboot-samples/config/nacos/biz1/pom.xml index 9099c7644..70fff4237 100644 --- a/samples/springboot-samples/config/nacos/biz1/pom.xml +++ b/samples/springboot-samples/config/nacos/biz1/pom.xml @@ -33,6 +33,11 @@ nacos-config-spring-boot-starter + + com.alibaba.boot + nacos-discovery-spring-boot-starter + + org.springframework.boot diff --git a/samples/springboot-samples/config/nacos/biz1/src/main/java/com/alipay/sofa/config/nacos/DiscoveryController.java b/samples/springboot-samples/config/nacos/biz1/src/main/java/com/alipay/sofa/config/nacos/DiscoveryController.java new file mode 100644 index 000000000..840adbcf1 --- /dev/null +++ b/samples/springboot-samples/config/nacos/biz1/src/main/java/com/alipay/sofa/config/nacos/DiscoveryController.java @@ -0,0 +1,33 @@ +package com.alipay.sofa.config.nacos; + +import java.util.List; + +import com.alibaba.nacos.api.annotation.NacosInjected; +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.naming.NamingService; +import com.alibaba.nacos.api.naming.pojo.Instance; + +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; + +/** + * @author mingmen + * @date 2023/12/11 + */ +@Controller +@RequestMapping("discovery") +public class DiscoveryController { + + @NacosInjected + private NamingService namingService; + + @RequestMapping(value = "/get", method = GET) + @ResponseBody + public List get(@RequestParam String serviceName) throws NacosException { + return namingService.getAllInstances(serviceName); + } +} diff --git a/samples/springboot-samples/config/nacos/biz1/src/main/resources/application.properties b/samples/springboot-samples/config/nacos/biz1/src/main/resources/application.properties index 8456b9006..ac54a407f 100644 --- a/samples/springboot-samples/config/nacos/biz1/src/main/resources/application.properties +++ b/samples/springboot-samples/config/nacos/biz1/src/main/resources/application.properties @@ -4,4 +4,5 @@ server.port=8091 spring.jmx.default-domain=${spring.application.name} -nacos.config.server-addr=127.0.0.1:8848 \ No newline at end of file +nacos.config.server-addr=127.0.0.1:8848 +nacos.discovery.server-addr=127.0.0.1:8848 diff --git a/samples/springboot-samples/config/nacos/pom.xml b/samples/springboot-samples/config/nacos/pom.xml index 332b06a1c..9759e9b00 100644 --- a/samples/springboot-samples/config/nacos/pom.xml +++ b/samples/springboot-samples/config/nacos/pom.xml @@ -33,6 +33,11 @@ nacos-config-spring-boot-starter 0.2.12 + + com.alibaba.boot + nacos-discovery-spring-boot-starter + 0.2.12 +