Naocs 配置中心报错问题
Nacos 配置:
Spring Cloud Version | Spring Cloud Alibaba Version | Spring Boot Version |
---|---|---|
Spring Cloud Hoxton.SR8 |
2.2.3.RELEASE |
2.3.2.RELEASE |
Spring Cloud Greenwich.SR6 |
2.1.3.RELEASE |
2.1.13.RELEASE |
Spring Cloud Hoxton.SR8 |
2.2.2.RELEASE |
2.3.2.RELEASE |
Spring Cloud Hoxton.SR3 |
2.2.1.RELEASE |
2.2.5.RELEASE |
Spring Cloud Hoxton.RELEASE |
2.2.0.RELEASE |
2.2.X.RELEASE |
Spring Cloud Greenwich |
2.1.2.RELEASE |
2.1.X.RELEASE |
Spring Cloud Finchley |
2.0.3.RELEASE |
2.0.X.RELEASE |
Spring Cloud Edgware |
1.5.1.RELEASE(停止维护,建议升级) |
1.5.X.RELEASE |
pom.xml文件添加包:
<!--nacos-config--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> <!--nacos-discovery--> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency>
bootstrap.yml优先于application.yml
bootstrap.yml配置:
server: port: 3377 spring: application: name: nacos-config-client cloud: nacos: discovery: server-addr: localhost:8848 # 服务注册中心地址 config: server-addr: localhost:8848 #配置中心地址 file-extension: yaml #指定yaml格式的配置 #group: TEST_GROUP #默认:DEFAULT_GROUP #namespace: 00ab1957-eb3f-4555-96ca-3d20817e854d #命名空间id 默认:public # ${spring.application.name}-${spring.profile.active}.${spring.cloud.nacos.config.file-extension} # nacos-config-client-dev.yaml
application.yml配置:
spring: profiles: active: dev # 表示激活开发环境
类配置:
import org.springframework.beans.factory.annotation.Value; import org.springframework.cloud.context.config.annotation.RefreshScope; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RefreshScope //支持controller的动态刷新 public class ConfigClientController { @Value("${config.info}") private String configInfo; @GetMapping("/config/info") public String getConfigInfo() { return configInfo; } }
启动类:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient @SpringBootApplication public class NacosConfigClientApp { public static void main(String[] args) { SpringApplication.run(NacosConfigClientApp.class,args); } }