Nacos配置管理
目录
1. 统一配置管理
1.1 注册中心,引入依赖
# nacos 客户端依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artfactId>spring-cloud-starter-alibaba-nacos-config</artfacrId>
</dependency>
1.2 注册中心,新建bootstrap.yml文件
# nacos 客户端
spring:
application:
name: # 服务名称
profile:
active: # 运行环境
cloud:
nacos:
server-addr: # 注册中心地址
config:
file-extension: yaml # 文件后缀名
1.3 读取配置文件流程
1.4 添加配置
nacos管理页面
配置管理->配置列表->新增
- DataID:服务名称-profile(profile指的是运行环境).yaml
- 并不是所有配置都要配置,只配置需要热更新的配置
2. 配置热更新
- @Value 注解所在的类 加上 @RefrshScope
- @ConfigrationProperties(prefix=“前缀名”)
/**
* 前缀名:
* 变量名:
*/
@Lombok
@Component
@ConfigrationProperties(prefix="前缀名")
public class XXX(){
private String 变量名:
}
3. 配置共享
配置文件优先级(高 --> 低),已读取的配置,不再重复读取
- 指定环境:[spring.application.name]-[spring.profile.active].yaml
- 所有环境:[spring.application.name].yaml
- 本地配置:application.yml
4. 搭建Nacos集群
4.1 数据库集群搭建
4.2 配置Nacos
配置三个 nacos 端口为 8841 8842 8843
- 修改 \nacos\conf\cluster.conf.example 为 cluster.conf
- 修改 cluster.conf 文件内容
- 修改 \nacos\conf\application.properties
- 修改 \nginx\conf\nginx.conf
# cluster.conf
#it is ip
#example
192.168.16.001:8841
192.168.16.002
192.168.16.003
# application.properties
#*************** Spring Boot Related Configurations ***************#
### Default web context path:
server.servlet.contextPath=/nacos
### Default web server port:
server.port=8841
......
#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
spring.datasource.platform=mysql
# mysql数量
### Count of DB:
db.num=1
### Connect URL of DB:
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=nacos
db.password.0=nacos
# nginx.conf
http {
......
upstream nacos-cluster {
server 192.168.16.001:8841
server 192.168.16.002:8842
server 192.168.16.003:8843
}
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location /nacos {
root html;
index index.html index.htm;
}
}
......
4.3 启动
- 启动nacos
- 启动nginx
# 启动nginx
cd /usr/local/nginx/sbin
./nginx
# 查看nginx 的状态
ps -ef | grep nginx
# 启动nacos
cd /usr/local/nacos/bin
./startup.sh -m standalone
最后
以上是学习 黑马程序员《微服务技术全栈教程》的学习笔记