-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Nacos config
Nacos 提供用于存储配置和其他元数据的 key/value 存储,为分布式系统中的外部化配置提供服务器端和客户端支持。使用 Spring Cloud Alibaba Nacos Config,您可以在 Nacos Server 集中管理你 Spring Cloud 应用的外部属性配置。
Spring Cloud Alibaba Nacos Config 是 Config Server 和 Client 的替代方案,客户端和服务器上的概念与 Spring Environment 和 PropertySource 有着一致的抽象,在特殊的 bootstrap 阶段,配置被加载到 Spring 环境中。当应用程序通过部署管道从开发到测试再到生产时,您可以管理这些环境之间的配置,并确保应用程序具有迁移时需要运行的所有内容。
1、启动 Nacos Server。启动方式可见 Nacos 官网
2、启动好 Nacos 之后,在 Nacos 添加 Spring Cloud 应用的外部化配置。
Data ID: nacos-config.properties
Group : DEFAULT_GROUP
配置内容: user.name=nacos-config-properties
user.age=90
Nacos 中的 key,即 DataId 的格式为 `${prefix} - ${spring.active.profile} . ${file-extension}`
Note
|
prefix 默认从配置 spring.application.name 中取值,file-extension 默认的值为 "properties"
|
Note
|
当 profile 未配置时,DataId 的格式为 ${prefix} .${file-extension}
|
使用 Spring Cloud Alibaba Nacos Config 实现 Spring Cloud 应用的外部化配置,需要在构建 Spring Boot 应用的同时添加一个 Spring Cloud Starter。 group 为 org.springframework.cloud
, artifact id 为 spring-cloud-starter-alibaba-nacos-config
。详细的版本对应关系请参考 版本说明。以下是一个基础的 maven 依赖配置:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/>
</parent>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<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</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
现在就可以创建一个标准的 Spring Boot 的应用。
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(ProviderApplication.class, args);
String userName = applicationContext.getEnvironment().getProperty("user.name");
String userAge = applicationContext.getEnvironment().getProperty("user.age");
System.err.println("user name :"+userName+"; age: "+userAge);
}
}
在运行此 Example 之前, 必须使用 bootstrap.properties
配置文件来配置 Nacos Config 的元信息,最基本的信息如下:
spring.application.name=nacos-config
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
Note
|
注意当你使用域名的方式来访问 Nacos 时,spring.cloud.nacos.config.server-addr 配置的方式为 域名:port 。
例如 Nacos 的域名为abc.com.nacos,监听的端口为 80,则 spring.cloud.nacos.config.server-addr=abc.com.nacos:80 。
注意 80 端口不能省略。
|
启动这个Example,可以在控制台看到打印出的值正是在 Nacos 上 DataID 为 nacos-config.properties 对应的内容。
2018-11-02 14:24:51.638 INFO 32700 --- [main] c.a.demo.provider.ProviderApplication : Started ProviderApplication in 14.645 seconds (JVM running for 15.139)
user name :nacos-config-properties; age: 90
2018-11-02 14:24:51.688 INFO 32700 --- [-127.0.0.1:8848] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@a8c5e74: startup date [Fri Nov 02 14:24:51 CST 2018]; root of context hierarchy
2018-11
spring-cloud-starter-alibaba-nacos-config 中 DataId 默认的文件扩展名是 properties。除去 properties 格式之外,也支持 yaml 格式。
这个时候只需要完成以下两步:
1、在应用的 bootstrap.properties 或 bootstrap.yml 配置文件中显示的声明 dataid 文件扩展名。如下所示
spring.cloud.nacos.config.file-extension=yaml
2、在 Nacos 的控制台新增一个 DataId 中以扩展名 .yaml 结尾的配置,如下所示:
Data ID: nacos-config.yaml
Group : DEFAULT_GROUP
配置格式: YAML
配置内容: user.name: nacos-config-yaml
user.age: 68
这两步完成后,重启测试程序,可以在控制台看到输出是以 DataId 为 nacos-config.yaml 配置的值。
2018-11-02 14:59:00.484 INFO 32928 --- [main] c.a.demo.provider.ProviderApplication:Started ProviderApplication in 14.183 seconds (JVM running for 14.671)
user name :nacos-config-yaml; age: 68
2018-11-02 14:59:00.529 INFO 32928 --- [-127.0.0.1:8848] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@265a478e: startup date [Fri Nov 02 14:59:00 CST 2018]; root of context hierarchy
spring-cloud-starter-alibaba-nacos-config 默认已开启配置的动态更新,如下所示,当变更 user.name 时,应用程序中能够获取到最新的值:
user name :nacos-config-yaml; age: 68
user name :nacos-config-yaml; age: 68
user name :nacos-config-yaml; age: 68
2018-11-02 15:04:25.069 INFO 32957 --- [-127.0.0.1:8848] o.s.boot.SpringApplication : Started application in 0.144 seconds (JVM running for 71.752)
2018-11-02 15:04:25.070 INFO 32957 --- [-127.0.0.1:8848] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@10c89124: startup date [Fri Nov 02 15:04:25 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@6520af7
2018-11-02 15:04:25.071 INFO 32957 --- [-127.0.0.1:8848] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@6520af7: startup date [Fri Nov 02 15:04:24 CST 2018]; root of context hierarchy
user name :nacos-config-yaml-update; age: 68
user name :nacos-config-yaml-update; age: 68
Note
|
你可以通过配置 spring.cloud.nacos.config.refresh.enabled=false 来关闭动态刷新 |
spring-cloud-starter-alibaba-nacos-config 在加载配置的时候,首先会尝试去加载 dataid 为${spring.application.name}.${file-extension}的配置,当设置了`spring.profiles.active` 中配置有内容时,还会尝试去加载了dataid 为${spring.application.name}-${profile}.${file-extension}的配置,且后者的优先级高于前者。
spring.profiles.active 属于配置的元数据,所以也必须配置在 bootstrap.properties 或 bootstrap.yaml 中。
spring.profiles.active=develop
Note
|
也可以通过 JVM 参数 -Dspring.profiles.active=develop 或者 --spring.profiles.active=develop 这类优先级更高的方式来配置,遵循 Spring Boot 规范。 |
在Nacos上新增了一个dataid为:nacos-config-develop.yaml的基础配置,如下所示:
Data ID: nacos-config-develop.yaml
Group : DEFAULT_GROUP
配置格式: YAML
配置内容: current.env: develop-env
age: 69
启动 Spring Boot 应用测试的代码如下:
@SpringBootApplication
public class ProviderApplication {
public static void main(String[] args) {
ConfigurableApplicationContext applicationContext = SpringApplication.run(ProviderApplication.class, args);
while(true) {
String userName = applicationContext.getEnvironment().getProperty("user.name");
String userAge = applicationContext.getEnvironment().getProperty("user.age");
//获取当前部署的环境
String currentEnv = applicationContext.getEnvironment().getProperty("current.env");
System.err.println("in "+currentEnv+" enviroment; "+"user name :" + userName + "; age: " + userAge);
TimeUnit.SECONDS.sleep(1);
}
}
}
启动后,可见控制台的输出结果:
in develop-evn enviroment; user name :nacos-config-yaml-update; age: 69
2018-11-02 15:34:25.013 INFO 33014 --- [ Thread-11] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6f1c29b7: startup date [Fri Nov 02 15:33:57 CST 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@63355449
- 文档
- Documents
- Open Source components
- Commercial components
- Example
- awesome spring cloud alibaba