[TOC]
打开 nacos 面板新建配置
Data ID: nacos-config-client-dev.yaml
Group: DEV-CLOUD2023
config:
info: config info lalalal 小魔仙~~~~
引入依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
配置文件
bootstrap.yaml
server:
port: 9999
spring:
application:
name: nacos-config-client
cloud:
nacos:
discovery:
server-addr: localhost:7848 #服务注册中心地址
config:
server-addr: localhost:7848 #配置中心地址
file-extension: yaml #指定 yaml 格式的配置
group: DEV-CLOUD2023
application.yaml
spring:
profiles:
active: dev
启动类
@SpringBootApplication
@EnableDiscoveryClient
public class NacosConfigClientMain {public static void main(String[] args) {SpringApplication.run(NacosConfigClientMain.class, args);
}
}
业务类
@RestController
@RefreshScope
public class NacosConfigController {@Value("${config.info}")
private String configInfo;
@GetMapping("/config/info")
public String getConfigInfo() {return configInfo;}
}
访问 : http://localhost:3377/config/info
config info lalalal 小魔仙~~~~
正文完