Springboot 集成nacos
1、版本信息
Springboot 2.2.7.RELEASE
spring-cloud Hoxton.RELEASE
Nacos 1.3.2
Spring Cloud Alibaba Version 2.2.6.RELEASE
2、 主要依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.6</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Hoxton.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
3、启动类用的到注解为:
@EnableDiscoveryClient(import org.springframework.cloud.client.discovery.EnableDiscoveryClient;)
4、修改以前的yml配置为:bootstrap.yml
spring:
profiles:
active: dev
application:
name: api
cloud:
nacos:
config:
# 配置中心配置文件分组,默认为DEFAULT_GROUP
# group: TEST_GROUP
# 配置中心配置文件后缀,默认为properties
file-extension: yml
# 配置中心地址
server-addr: xx.xx.xx.xx:8848
# 配置中心命名空间ID,默认为public
namespace: xxxxx-xxxxx-xxxx-xxxx-xxxxxxxx
# 配置中心配置文件Data Id 的格式是 ${prefix}-${spring.profile.active}.${file-extension},会同时加载配置文件
# ${prefix}-${spring.profile.active}.${file-extension}和${prefix}.${file-extension}的属性,若有相同属性
# 则以${prefix}-${spring.profile.active}.${file-extension}的属性为准
shared-configs:
- data-id: application.yml
refresh: true
- data-id: application-${spring.profiles.active}.yml
refresh: true #是否支持自动刷新
discovery:
# 注册中心的地址
server-addr: xx.xx.xx.xx:8848
# 配置中心命名空间ID,默认为public
namespace: 6ba5609b-xxxx-xxx-xxxx-xxxx
5、nacos配置yml
3、 注意事项
1、检查nacos的配置yml内容&格式是否正取,注册中心是否启动
2、检查各组件的版本是是配套的(springboot不同的版本需要的依赖的其他组件的版本也不同,若需要其他版本请自行研究,欢迎一起探讨)
3、yml格式检查是否正确。
4、…