Nacos配置中心操作步骤:
一.进入nacos控制台界面,在”配置列表”点击”加号按钮”新建配置
二.在”新建配置”页新建配置
相关概念可参考: https://nacos.io/zh-cn/docs/concepts.html
三.发布配置
四.查看”配置列表”
五.编码阶段
1.pom
<?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>
<artifactId>kr-cloud</artifactId>
<groupId>cn.kr</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>kr-nacos-config</artifactId>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Finchley.SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>0.2.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!--MVC-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--nacos配置中心-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!--Lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.2</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.配置文件( bootstrap.properties)
这里是配置文件必须使用 bootstrap.properties, bootstrap.properties与application.yml的不同,后期有教程会补上。
spring.application.name=nacos-config-client
server.port=8003
spring.cloud.nacos.config.server-addr=127.0.0.1:8848
3.启动主类(NacosConfig8003)
@SpringBootApplication
public class NacosConfig8003 {
public static void main(String[] args) {
SpringApplication.run(NacosConfig8003.class, args);
}
}
4.测试文件(TestConfigController)
@RestController
@RefreshScope//声明刷新域
public class TestConfigController {
@Value("${client.user.name}")
private String name;
@Value("${client.user.phone}")
private String phone;
@GetMapping("/test")
public String test() {
return "name:"+name+" phone:"+phone;
}
}
5.启动
trationDelegate$BeanPostProcessorChecker : Bean 'configurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$f3138e36] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
o.s.c.a.n.c.NacosPropertySourceBuilder : Loading nacos data, dataId: 'nacos-config-client.properties', group: 'DEFAULT_GROUP'
启动日志中有这两句。说明配置成功。
6.测试
浏览器输出: http://localhost:8003/test
7.修改配置数据
修改完,点击发布后,控制台会相应输出更新信息。
再次刷新链接。数据改变了。
nacos共享配置:https://github.com/alibaba/spring-cloud-alibaba/issues/141