1.nacos本地安装版本(版本2.1.0)
安装步骤
- nacos官网
- 下载地址
- 版本选择:版本 0.2.x.RELEASE 对应的是 Spring Boot 2.x 版本,版本 0.1.x.RELEASE 对应的是 Spring Boot 1.x 版本。
- 安装:解压即可
- 启动:
cd nacos/bin 目录下
sh startup.sh -m standalone 启动,standalone代表着单机模式运行,非集群模式 - 关闭:sh shutdown.sh
- 访问:
http://localhost:8848/nacos
用户名/密码 nacos/nacos
2.springboot版本依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
3.nacos版本依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
<version>2.2.0.RELEASE</version>
</dependency>
4.项目配置
-
项目启动类 @EnableDiscoveryClient
-
bootstrap.yml 配置(这里我采用多环境配置,所以只指定了项目名称和环境)
spring:
application:
name: init-teplate
profiles:
active: dev
- bootstrap-dev.yml
- nacos的配置如下
5.动态刷新
- 我们在远程的配置文件,添加如下配置信息
- 代码中添加自动刷新注解(@RefreshScope)+获取属性值注解 (@Value(“${XXX.XXX:默认值}”) )