mirror of
https://gitee.com/mirrors/Spring-Cloud-Alibaba.git
synced 2021-06-26 13:25:11 +08:00
add module nacos-config and nacos-discovery
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
<?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.cloud</groupId>
|
||||
<artifactId>spring-cloud-alibaba-examples</artifactId>
|
||||
<version>0.2.0.BUILD-SNAPSHOT</version>
|
||||
<relativePath>../../pom.xml</relativePath>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
|
||||
<artifactId>nacos-config-example</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
<description>Example demonstrating how to use nacos config</description>
|
||||
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-deploy-plugin</artifactId>
|
||||
<version>${maven-deploy-plugin.version}</version>
|
||||
<configuration>
|
||||
<skip>true</skip>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
@@ -0,0 +1,187 @@
|
||||
# Nacos Config Example
|
||||
|
||||
## 项目说明
|
||||
|
||||
本项目演示如何使用 Nacos Config Starter 完成 Spring Cloud 应用的配置管理。
|
||||
|
||||
[Nacos](https://github.com/alibaba/Nacos) 是阿里巴巴开源的一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。
|
||||
|
||||
## 示例
|
||||
|
||||
### 如何接入
|
||||
在启动示例进行演示之前,我们先了解一下 Spring Cloud 应用如何接入 Nacos Config。
|
||||
**注意 本章节只是为了便于您理解接入方式,本示例代码中已经完成接入工作,您无需再进行修改。**
|
||||
|
||||
1. 首先,修改 pom.xml 文件,引入 Nacos Config Starter。
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
|
||||
2. 在应用的 /src/main/resources/bootstrap.properties 配置文件中配置 Nacos Config 地址
|
||||
|
||||
spring.cloud.nacos.config.server-addr=127.0.0.1:8080
|
||||
|
||||
3. 完成上述两步后,应用会从 Nacos Config 中获取相应的配置,并添加在 Spring Environment 的 PropertySources 中。这里我们使用 @Value 注解来将对应的配置注入到 SampleController 的 userName 和 age 字段,并添加 @RefreshScope 打开动态刷新功能
|
||||
|
||||
@RefreshScope
|
||||
class SampleController {
|
||||
|
||||
@Value("${user.name}")
|
||||
String userName;
|
||||
|
||||
@Value("${user.age}")
|
||||
int age;
|
||||
}
|
||||
|
||||
### 启动 Nacos Server 并添加配置
|
||||
|
||||
1. 首先需要获取 Nacos Server,支持直接下载和源码构建两种方式。**推荐使用最新版本 Nacos Server**
|
||||
|
||||
1. 直接下载:[Nacos Server 下载页](https://github.com/alibaba/nacos/releases)
|
||||
2. 源码构建:进入 Nacos [Github 项目页面](https://github.com/alibaba/nacos),将代码 git clone 到本地自行编译打包,[参考此文档](https://nacos.io/zh-cn/docs/quick-start.html)。
|
||||
|
||||
|
||||
|
||||
2. 启动 Server,进入下载到本地并解压完成后的文件夹(使用源码构建的方式则进入编译打包好的文件夹),再进去其相对文件夹 nacos/bin,并对照操作系统实际情况执行如下命令。[详情参考此文档](https://nacos.io/zh-cn/docs/quick-start.html)。
|
||||
|
||||
1. Linux/Unix/Mac 操作系统,执行命令 `sh startup.sh -m standalone`
|
||||
1. Windows 操作系统,执行命令 `cmd startup.cmd`
|
||||
|
||||
3. 在命令行执行如下命令,向 Nacos Server 中添加一条配置。
|
||||
|
||||
curl -X POST "http://127.0.0.1:8080/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.name=james%0Auser.age=17"
|
||||
|
||||
**注:你也可以使用其他方式添加,遵循 HTTP API 规范即可,若您使用的 Nacos 版本自带控制台,建议直接使用控制台进行配置**
|
||||
|
||||
添加的配置的详情如下
|
||||
|
||||
dataId 为 nacos-config-example.properties
|
||||
group 为 DEFAULT_GROUP
|
||||
|
||||
内容如下
|
||||
|
||||
user.id=1
|
||||
user.name=james
|
||||
user.age=17
|
||||
|
||||
### 应用启动
|
||||
|
||||
1. 增加配置,在应用的 /src/main/resources/application.properties 中添加基本配置信息
|
||||
|
||||
spring.application.name=nacos-config-example
|
||||
server.port=18084
|
||||
|
||||
|
||||
2. 启动应用,支持 IDE 直接启动和编译打包后启动。
|
||||
|
||||
1. IDE直接启动:找到主类 `Application`,执行 main 方法启动应用。
|
||||
2. 打包编译后启动:首先执行 `mvn clean package` 将工程编译打包,然后执行 `java -jar nacos-config-example.jar`启动应用。
|
||||
|
||||
### 验证
|
||||
|
||||
#### 验证自动注入
|
||||
在浏览器地址栏输入 `http://127.0.0.1:18084/user`,并点击调转,可以看到成功从 Nacos Config Server 中获取了数据。
|
||||
|
||||

|
||||
|
||||
#### 验证动态刷新
|
||||
1. 执行如下命令,修改 Nacos Server 端的配置数据
|
||||
|
||||
curl -X POST "http://127.0.0.1:8080/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.name=james%0Auser.age=18"
|
||||
|
||||
2. 在浏览器地址栏输入 `http://127.0.0.1:18084/user`,并点击调转,可以看到应用从 Nacos Server 中获取了最新的数据,age 变成了 18。
|
||||
|
||||

|
||||
|
||||
|
||||
## 原理
|
||||
|
||||
|
||||
### Nacos Config 数据结构
|
||||
|
||||
Nacos Config 主要通过 dataId 和 group 来唯一确定一条配置,我们假定你已经了解此背景。如果不了解,请参考 [Nacos 文档](https://nacos.io/zh-cn/docs/concepts.html)。
|
||||
|
||||
Nacos Client 从 Nacos Server 端获取数据时,调用的是此接口 `ConfigService.getConfig(String dataId, String group, long timeoutMs)`。
|
||||
|
||||
|
||||
### Spring Cloud 应用获取数据
|
||||
|
||||
#### dataID
|
||||
|
||||
在 Nacos Config Starter 中,dataId 的拼接格式如下
|
||||
|
||||
${prefix} - ${spring.active.profile} . ${content-type}
|
||||
|
||||
* `prefix` 默认为 `spring.application.name` 的值,也可以通过配置项 `spring.cloud.nacos.config.prefix`来配置。
|
||||
|
||||
* `spring.active.profile` 即为当前环境对应的 profile,详情可以参考 [Spring Boot文档](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html#boot-features-profiles)
|
||||
|
||||
**注意,当 activeprofile 为空时,对应的连接符 `-` 也将不存在,dataId 的拼接格式变成 `${prefix}`.`${context.type}`**
|
||||
|
||||
* `content-type` 为配置内容的数据格式,可以通过配置项 `spring.cloud.nacos.config.content-type`来配置。
|
||||
目前只支持 `properties` 类型。
|
||||
|
||||
#### group
|
||||
* `group` 默认为 `DEFAULT_GROUP`,可以通过 `spring.cloud.nacos.config.group` 配置。
|
||||
|
||||
|
||||
### 自动注入
|
||||
Nacos Config Starter 实现了 `org.springframework.cloud.bootstrap.config.PropertySourceLocator`接口,并将优先级设置成了最高。
|
||||
|
||||
在 Spring Cloud 应用启动阶段,会主动从 Nacos Server 端获取对应的数据,并将获取到的数据转换成 PropertySource 且注入到 Environment 的 PropertySources 属性中,所以使用 @Value 注解也能直接获取 Nacos Server 端配置的内容。
|
||||
|
||||
### 动态刷新
|
||||
|
||||
Nacos Config Starter 默认为所有获取数据成功的 Nacos 的配置项添加了监听功能,在监听到服务端配置发生变化时会实时触发 `org.springframework.cloud.context.refresh.ContextRefresher` 的 refresh 方法 。
|
||||
|
||||
如果需要对 Bean 进行动态刷新,请参照 Spring 和 Spring Cloud 规范。推荐给类添加 `@RefreshScope` 或 `@ConfigurationProperties ` 注解,
|
||||
|
||||
更多详情请参考 [ContextRefresher Java Doc](http://static.javadoc.io/org.springframework.cloud/spring-cloud-context/2.0.0.RELEASE/org/springframework/cloud/context/refresh/ContextRefresher.html)。
|
||||
|
||||
|
||||
|
||||
|
||||
## Endpoint 信息查看
|
||||
|
||||
Spring Boot 应用支持通过 Endpoint 来暴露相关信息,Nacos Config Starter 也支持这一点。
|
||||
|
||||
在使用之前需要在 maven 中添加 `spring-boot-starter-actuator`依赖,并在配置中允许 Endpoints 的访问。
|
||||
|
||||
* Spring Boot 1.x 中添加配置 management.security.enabled=false
|
||||
* Spring Boot 2.x 中添加配置 management.endpoints.web.exposure.include=*
|
||||
|
||||
Spring Boot 1.x 可以通过访问 http://127.0.0.1:18084/nacos-config 来查看 Nacos Endpoint 的信息。
|
||||
|
||||
Spring Boot 2.x 可以通过访问 http://127.0.0.1:18084/actuator/nacos-config 来访问。
|
||||
|
||||

|
||||
|
||||
如上图所示,Sources 表示此客户端从哪些 Nacos Config 配置项中获取了信息,RefreshHistory 表示动态刷新的历史记录,最多保存20条,NacosConfigProperties 则为 Nacos Config Starter 本身的配置。
|
||||
|
||||
## More
|
||||
|
||||
#### 更多配置项
|
||||
配置项|key|默认值|说明
|
||||
----|----|-----|-----
|
||||
服务端地址|spring.cloud.nacos.config.server-addr||
|
||||
DataId前缀|spring.cloud.nacos.config.prefix||spring.application.name
|
||||
Group|spring.cloud.nacos.config.group|DEFAULT_GROUP|
|
||||
dataID后缀及数据格式|spring.cloud.nacos.config.content-type|properties|目前只支持 properties
|
||||
配置内容的编码方式|spring.cloud.nacos.config.encode|UTF-8|配置的编码
|
||||
获取配置的超时时间|spring.cloud.nacos.config.timeout|3000|单位为 ms
|
||||
配置的命名空间|spring.cloud.nacos.config.namespace||常用场景之一是不同环境的配置的区分隔离,例如开发测试环境和生产环境的资源隔离等。
|
||||
AccessKey|spring.cloud.nacos.config.access-key||
|
||||
SecretKey|spring.cloud.nacos.config.secret-key||
|
||||
相对路径|spring.cloud.nacos.config.context-path||服务端 API 的相对路径
|
||||
接入点|spring.cloud.nacos.config.endpoint|UTF-8|地域的某个服务的入口域名,通过此域名可以动态地拿到服务端地址
|
||||
是否开启监听和自动刷新|spring.cloud.nacos.config.refresh.enabled|true|
|
||||
|
||||
|
||||
|
||||
#### 更多介绍
|
||||
Nacos为用户提供包括动态服务发现,配置管理,服务管理等服务基础设施,帮助用户更灵活,更轻松地构建,交付和管理他们的微服务平台,基于Nacos, 用户可以更快速的构建以“服务”为中心的现代云原生应用。Nacos可以和Spring Cloud、Kubernetes/CNCF、Dubbo 等微服务生态无缝融合,为用户提供更卓越的体验。更多 Nacos 相关的信息,请参考 [Nacos 项目](https://github.com/alibaba/Nacos)。
|
||||
|
||||
如果您对 Spring Cloud Nacos Config Starter 有任何建议或想法,欢迎在 issue 中或者通过其他社区渠道向我们提出。
|
||||
|
@@ -0,0 +1,193 @@
|
||||
# Nacos Config Example
|
||||
|
||||
## Project Instruction
|
||||
|
||||
This example illustrates how to use Nacos Config Starter implement externalized configuration for Spring Cloud applications.
|
||||
|
||||
[Nacos](https://github.com/alibaba/Nacos) an easy-to-use dynamic service discovery, configuration and service management platform for building cloud native applications.
|
||||
|
||||
## Demo
|
||||
|
||||
### Connect to Nacos Config
|
||||
Before we start the demo, let's learn how to connect Nacos Config to a Spring Cloud application. **Note: This section is to show you how to connect to Nacos Config. The configurations have been completed in the following example, so you don't need modify the code any more.**
|
||||
|
||||
|
||||
1. Add dependency spring-cloud-starter-alibaba-nacos-config in the pom.xml file in your Spring Cloud project.
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
|
||||
</dependency>
|
||||
|
||||
2. Add Nacos server address configurations to file /src/main/resources/bootstrap.properties
|
||||
|
||||
spring.cloud.nacos.config.server-addr=127.0.0.1:8080
|
||||
|
||||
3. After completing the above two steps, the application will get the externalized configuration from Nacos Server and put it in the Spring Environment's PropertySources.We use the @Value annotation to inject the corresponding configuration into the userName and age fields of the SampleController, and add @RefreshScope to turn on dynamic refresh .
|
||||
@RefreshScope
|
||||
class SampleController {
|
||||
|
||||
@Value("${user.name}")
|
||||
String userName;
|
||||
|
||||
@Value("${user.age}")
|
||||
int age;
|
||||
}
|
||||
|
||||
### Start Nacos Server
|
||||
|
||||
1. Install Nacos Server by downloading or build from source code.**Recommended latest version Nacos Server**
|
||||
|
||||
1. Download: Download Nacos Server [download page](https://github.com/alibaba/nacos/releases)
|
||||
2. Build from source code: Get source code by git clone git@github.com:alibaba/Nacos.git from Github Nacos and build your code. See [build reference](https://nacos.io/en-us/docs/quick-start.html) for details.
|
||||
|
||||
|
||||
|
||||
2. Unzip the downloaded file and go to the nacos/bin folder(), And according to the actual situation of the operating system, execute the following command。[see reference for more detail](https://nacos.io/en-us/docs/quick-start.html)。
|
||||
|
||||
1. Linux/Unix/Mac , execute `sh startup.sh -m standalone`
|
||||
1. Windows , execute `cmd startup.cmd`
|
||||
|
||||
3. Execute the following command to add a configuration to Nacos Server.
|
||||
|
||||
curl -X POST "http://127.0.0.1:8080/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.name=james%0Auser.age=17"
|
||||
|
||||
**Note: You can also add it in other ways. If you are using the Nacos version with its own console, it is recommended to configure it directly using the console.**
|
||||
|
||||
|
||||
Details of the added configuration are as follows
|
||||
|
||||
dataId is nacos-config-example.properties
|
||||
group is DEFAULT_GROUP
|
||||
|
||||
content is
|
||||
|
||||
user.id=1
|
||||
user.name=james
|
||||
user.age=17
|
||||
|
||||
### Start Application
|
||||
|
||||
1. Add necessary configurations to file /src/main/resources/application.properties
|
||||
|
||||
spring.application.name=nacos-config-example
|
||||
server.port=18084
|
||||
|
||||
|
||||
2. Start the application in IDE or by building a fatjar.
|
||||
|
||||
1. Start in IDE: Find main class `Application`, and execute the main method.
|
||||
2. Build a fatjar:Execute command `mvn clean package` to build a fatjar,and run command `java -jar nacos-config-example.jar` to start the application.
|
||||
|
||||
### Verification
|
||||
|
||||
#### Automatic Injection
|
||||
Enter `http://127.0.0.1:18084/user` in the browser address bar and click Go to, we can see the data successfully obtained from Nacos Config Server.
|
||||
|
||||

|
||||
|
||||
#### Dynamic Refresh
|
||||
1. Run the following command to modify the configuration data on the Nacos Server side.
|
||||
|
||||
curl -X POST "http://127.0.0.1:8080/nacos/v1/cs/configs?dataId=nacos-config-example.properties&group=DEFAULT_GROUP&content=user.id=1%0Auser.name=james%0Auser.age=18"
|
||||
|
||||
2. Enter `http://127.0.0.1:18084/user` in the browser address bar and click Go to,
|
||||
We can see that the app got the latest data from Nacos Server and the age becomes 18.
|
||||
|
||||

|
||||
|
||||
|
||||
## Principle
|
||||
|
||||
|
||||
### Nacos Config Data Structure
|
||||
|
||||
Nacos Config primarily determines a piece of config through dataId and group, and we assume that you already know this background. If you don't understand, please refer to [Nacos Doc](https://nacos.io/en-us/docs/concepts.html)。
|
||||
|
||||
Nacos Client gets data from Nacos Server through this method. `ConfigService.getConfig(String dataId, String group, long timeoutMs)`。
|
||||
|
||||
|
||||
### Spring Cloud Retrieve Data
|
||||
|
||||
#### dataID
|
||||
|
||||
In Nacos Config Starter, the splicing format of dataId is as follows
|
||||
|
||||
${prefix} - ${spring.active.profile} . ${content-type}
|
||||
|
||||
* `prefix` default value is `spring.application.name` value, which can also be configured via the configuration item `spring.cloud.nacos.config.prefix`.
|
||||
|
||||
* `spring.active.profile` is the profile corresponding to the current environment. For details, please refer to [Spring Boot Doc](https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html#boot-features-profiles)
|
||||
|
||||
**Note: when the activeprofile is empty, the corresponding connector `-` will also not exist, and the splicing format of the dataId becomes `${prefix}`.`${context.type}`**
|
||||
|
||||
* `content-type` is the data format of the configuration content, which can be configured by the configuration item `spring.cloud.nacos.config.content-type`.
|
||||
Currently only the `properties` type is supported.
|
||||
|
||||
#### group
|
||||
* `group` defaults to `DEFAULT_GROUP` and can be configured via `spring.cloud.nacos.config.group`.
|
||||
|
||||
|
||||
### Automatic Injection
|
||||
Nacos Config Starter implement `org.springframework.cloud.bootstrap.config.PropertySourceLocator` interface, and set order to 0.
|
||||
|
||||
In the startup phase of the Spring Cloud application, the corresponding data is obtained from the Nacos Server side, and the acquired data is converted into a PropertySource and injected into the PropertySources property of the Spring Environment. so the @Value annotation can also directly obtain the configuration of the Nacos Server side.
|
||||
|
||||
### Dynamic Refresh
|
||||
|
||||
By default, Nacos Config Starter adds a listening function to all Nacos configuration items that have successfully acquired data. It will trigger `org.springframework.cloud.context.refresh.ContextRefresher` 's refresh method in real time when it detects changes in the server configuration.
|
||||
|
||||
If you need to dynamically refresh a bean, please refer to the Spring and Spring Cloud specifications. It is recommended to add `@RefreshScope` or `@ConfigurationProperties ` annotations to the class.
|
||||
|
||||
Please refer to[ContextRefresher Java Doc](http://static.javadoc.io/org.springframework.cloud/spring-cloud-context/2.0.0.RELEASE/org/springframework/cloud/context/refresh/ContextRefresher.html) for more details.
|
||||
|
||||
|
||||
|
||||
|
||||
## Endpoint
|
||||
|
||||
Nacos Config starter also supports the implementation of Spring Boot actuator endpoints.
|
||||
|
||||
**Prerequisite:**
|
||||
|
||||
Add dependency spring-boot-starter-actuator to your pom.xml file, and configure your endpoint security strategy.
|
||||
|
||||
Spring Boot 1.x: Add configuration management.security.enabled=false
|
||||
Spring Boot 2.x: Add configuration management.endpoints.web.exposure.include=*
|
||||
To view the endpoint information, visit the following URLS:
|
||||
|
||||
Spring Boot1.x: Nacos Config Endpoint URL is http://127.0.0.1:18083/nacos-config.
|
||||
Spring Boot2.x: Nacos Config Endpoint URL is http://127.0.0.1:18083/actuator/nacos-config.
|
||||
|
||||

|
||||
|
||||
As shown in the figure above, Sources indicates which Nacos Config configuration items the client has obtained information, RefreshHistory indicates the dynamic refresh history, and up to 20, and NacosConfigProperties is the configuration of Nacos Config Starter itself.
|
||||
|
||||
## More
|
||||
|
||||
#### More configuration items
|
||||
|
||||
Configuration item|key|default value|Description
|
||||
----|----|-----|-----
|
||||
server address|spring.cloud.nacos.config.server-addr||
|
||||
DataId prefix|spring.cloud.nacos.config.prefix||spring.application.name
|
||||
Group|spring.cloud.nacos.config.group|DEFAULT_GROUP|
|
||||
dataID content type|spring.cloud.nacos.config.content-type|properties|currently only support properties
|
||||
encoding |spring.cloud.nacos.config.encode|UTF-8|Content encoding
|
||||
timeout|spring.cloud.nacos.config.timeout|3000|Get the configuration timeout period,unit is ms
|
||||
namespace|spring.cloud.nacos.config.namespace||One of the common scenarios is the separation of the configuration of different environments, such as the development of the test environment and the resource isolation of the production environment.
|
||||
AccessKey|spring.cloud.nacos.config.access-key||
|
||||
SecretKey|spring.cloud.nacos.config.secret-key||
|
||||
context-path|spring.cloud.nacos.config.context-path||Relative path of the server API
|
||||
endpoint|spring.cloud.nacos.config.endpoint|UTF-8|The domain name of a service, through which the server address can be dynamically obtained.
|
||||
refresh|spring.cloud.nacos.config.refresh.enabled|true|enable auto refresh
|
||||
|
||||
|
||||
|
||||
#### More introduction
|
||||
[Nacos](https://github.com/alibaba/Nacos) is committed to help you discover, configure, and manage your microservices. It provides a set of simple and useful features enabling you to realize dynamic service discovery, service configuration, service metadata and traffic management.
|
||||
|
||||
Nacos makes it easier and faster to construct, deliver and manage your microservices platform. It is the infrastructure that supports a service-centered modern application architecture with a microservices or cloud-native approach.
|
||||
|
||||
If you have any ideas or suggestions for Nacos Config starter, please don't hesitate to tell us by submitting github issues.
|
||||
|
@@ -0,0 +1,54 @@
|
||||
package org.springframework.cloud.alibaba.cloud.examples;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.ApplicationArguments;
|
||||
import org.springframework.boot.ApplicationRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* @author xiaojing
|
||||
*/
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
||||
|
||||
@Component
|
||||
class SampleRunner implements ApplicationRunner {
|
||||
|
||||
@Value("${user.name}")
|
||||
String userName;
|
||||
|
||||
@Value("${user.age}")
|
||||
int userAge;
|
||||
|
||||
@Override
|
||||
public void run(ApplicationArguments args) throws Exception {
|
||||
System.out.println(userName);
|
||||
System.out.println(userAge);
|
||||
}
|
||||
}
|
||||
|
||||
@RestController
|
||||
@RefreshScope
|
||||
class SampleController {
|
||||
|
||||
@Value("${user.name}")
|
||||
String userName;
|
||||
|
||||
@Value("${user.age}")
|
||||
int age;
|
||||
|
||||
@RequestMapping("/user")
|
||||
public String simple() {
|
||||
return "Hello Nacos Config!" + "Hello " + userName + " " + age + "!";
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
spring.application.name=nacos-config-example
|
||||
server.port=18084
|
||||
management.endpoints.web.exposure.include=*
|
@@ -0,0 +1 @@
|
||||
spring.cloud.nacos.config.server-addr=127.0.0.1:8080
|
Reference in New Issue
Block a user