Skip to content

Commit

Permalink
sdk-examples add nacos-spring-boot-config-example #48
Browse files Browse the repository at this point in the history
  • Loading branch information
heqingpan committed Jul 12, 2024
1 parent 1be68c4 commit 2807070
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 0 deletions.
Empty file added sdk-examples/java/README.md
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*.iml
target/
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`;



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>
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);
}
}
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;
}
}
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

0 comments on commit 2807070

Please sign in to comment.