项目框架为springboot 非cloud
将单项目注册到nacos
starter 搜索网站 https://mvnrepository.com/
使用nacos的配置中心功能
在mvnrepository搜索 spring-cloud-starter-alibaba-nacos-config
引入 pom.xml
<!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-config -->
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId><version>2021.1</version>
</dependency>
加载bootstrap.yml SpringBoot-2.4版本后 加载配置文件机制有变动
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-bootstrap --><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bootstrap</artifactId><version>3.1.1</version></dependency>
使用nacos的注册中心功能
在mvnrepository搜索 spring-cloud-starter-alibaba-nacos-discovery
引入 pom.xml
<!-- https://mvnrepository.com/artifact/com.alibaba.cloud/spring-cloud-starter-alibaba-nacos-discovery -->
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId><version>2021.1</version>
</dependency>
启动类 加注解 @EnableDiscoveryClient
配置文件
配置文件
nacos上命名规则 ${name}-${spring.profiles.active}.${file-extension}
, 此配置文件对应的命名为wuyc-text-dev.yml
实时刷新配置
yml 文件
config:addr: 127.0.0.1:8848 namespace: G20_AGCW_DEVgroup: DEV_GROUPspring:application:name: wuyc-textprofiles:active: devcloud:nacos:discovery:server-addr: ${
config.addr}namespace: ${
config.namespace}group: ${
config.group}service: ${
spring.application.name}-${
spring.profiles.active}-wuycconfig:
# enabled: true
# username: nacos
# password: nacosserver-addr: ${
config.addr}namespace: ${
config.namespace}group: ${
config.group}name: ${
spring.application.name}file-extension: yml