目录
3 核对nacos中新建的配置文件的名称,我的如下,注意要写全不能缺少什么!!!
核对所有的文件,可以参照我的标准(保准不会报错的的)
1 pom.xml是否有,没有的添加上
<!-- bootstrap引导程序-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bootstrap</artifactId>
<version>3.0.2</version>
</dependency>
2 配置文件是否有bootstrap.yml,没有的加上
bootstrap.yml
server:
port: 8082
spring:
application:
name: nacos-config-client
cloud:
nacos:
discovery:
# Nacos服务注册中心地址
server-addr: localhost:8848
enabled: true
config:
# 指定Nacos配置中心的地址
server-addr: localhost:8848
file-extension: yml # 指定yml格式的配置 默认properties
application.yml
spring:
profiles:
active: dev
3 核对nacos中新建的配置文件的名称,我的如下,注意要写全不能缺少什么!!!
nacos-config-client-dev.yml
最后的文件格式.yml要和这里对应
4 控制器与启动文件
package conf.controller;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/updateConf")
//@RefreshScope用于动态刷新
@RefreshScope
public class CsController {
@Value("${a}")
public String configInfo;
@GetMapping("/info")
public String getConfigInfo() {
return configInfo;
}
}
---------------------------------------------------------
package conf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
@EnableDiscoveryClient
public class NacosClientApplication {
public static void main(String[] args) {
SpringApplication.run(NacosClientApplication.class, args);
}
}
完事