-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sdk-examples add nacos-spring-boot-config-example #48
- Loading branch information
Showing
7 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
2 changes: 2 additions & 0 deletions
2
sdk-examples/java/spring-boot/nacos-spring-boot-config-example/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
*.iml | ||
target/ |
17 changes: 17 additions & 0 deletions
17
sdk-examples/java/spring-boot/nacos-spring-boot-config-example/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
# nacos-spring-boot-config-example 说明 | ||
|
||
本样例基于 https://github.com/nacos-group/nacos-examples/tree/master/nacos-spring-boot-example/nacos-spring-boot-config-example 修改 | ||
|
||
|
||
|
||
## 使用方式 | ||
|
||
1. 启动r-nacos | ||
2. 在public命名空间下增加一个配置, 配置Id: `example` 配置组: `DEFAULT_GROUP` 配置内容:`useLocalCache=true` | ||
3. 启动spring-boot应用,在本项目目录运行 `mvn spring-boot:run` | ||
4. 访问应用接口 `curl "http://127.0.0.1:8080/config/get",`请求结果为`true`; 或者直接用浏览器打开`http://127.0.0.1:8080/config/get` | ||
5. 验证动态修改配置能力,在r-nacos控制台中修改第2步配置内容为`useLocalCache=false`,在第4步请求的结果变更为`false`; | ||
|
||
|
||
|
54 changes: 54 additions & 0 deletions
54
sdk-examples/java/spring-boot/nacos-spring-boot-config-example/pom.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
|
||
<parent> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-parent</artifactId> | ||
<version>2.7.15</version> | ||
<relativePath/> <!-- lookup parent from repository --> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.nacos.demo</groupId> | ||
<artifactId>nacos-spring-boot-config-example</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>nacos-config</name> | ||
<properties> | ||
<java.version>1.8</java.version> | ||
<nacos.client.version>1.4.6</nacos.client.version> | ||
<nacos-config-spring-boot.version>0.2.12</nacos-config-spring-boot.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-actuator</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.alibaba.boot</groupId> | ||
<artifactId>nacos-config-spring-boot-starter</artifactId> | ||
<version>${nacos-config-spring-boot.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>com.alibaba.boot</groupId> | ||
<artifactId>nacos-config-spring-boot-actuator</artifactId> | ||
<version>${nacos-config-spring-boot.version}</version> | ||
</dependency> | ||
</dependencies> | ||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
</project> |
25 changes: 25 additions & 0 deletions
25
...g-example/src/main/java/com/alibaba/nacos/example/spring/boot/NacosConfigApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.alibaba.nacos.example.spring.boot; | ||
|
||
import com.alibaba.nacos.spring.context.annotation.config.NacosPropertySource; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* Document: https://nacos.io/zh-cn/docs/quick-start-spring-boot.html | ||
* <p> | ||
* Nacos 控制台添加配置: | ||
* <p> | ||
* Data ID:example | ||
* <p> | ||
* Group:DEFAULT_GROUP | ||
* <p> | ||
* 配置内容:useLocalCache=true | ||
*/ | ||
@SpringBootApplication | ||
@NacosPropertySource(dataId = "example", autoRefreshed = true) | ||
public class NacosConfigApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(NacosConfigApplication.class, args); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...mple/src/main/java/com/alibaba/nacos/example/spring/boot/controller/ConfigController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.alibaba.nacos.example.spring.boot.controller; | ||
|
||
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; | ||
|
||
@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; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...va/spring-boot/nacos-spring-boot-config-example/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
nacos.config.server-addr=127.0.0.1:8848 | ||
|
||
# endpoint http://localhost:8080/actuator/nacos-config | ||
# health http://localhost:8080/actuator/health | ||
management.endpoints.web.exposure.include=* | ||
management.endpoint.health.show-details=always |