由浅入深学习Nacos:从入门到实战部署

由浅入深学习Nacos:从入门到实战部署


一、Nacos是什么?

Nacos(Dynamic Naming and Configuration Service)是阿里巴巴开源的一款集服务发现、配置管理、服务管理于一体的云原生平台。它支持动态服务注册与发现、配置动态更新、服务健康监测等功能,是构建微服务架构的核心基础设施之一。

  • 服务发现:微服务实例自动注册到Nacos,其他服务可通过服务名动态发现实例。
  • 配置管理:集中管理应用配置,支持实时推送更新,避免配置硬编码。
  • 服务管理:提供健康检查、流量管理、动态路由等能力,保障服务高可用。

在这里插入图片描述


二、Nacos功能特性

1. 核心功能

  • 服务注册与发现:支持基于DNS和HTTP的服务注册,提供健康检查机制。
  • 动态配置管理:通过控制台或API管理配置,支持灰度发布和多环境隔离。
  • 服务元数据管理:为服务添加自定义元数据(如版本、权重),支持精细化流量控制。
  • 集群管理:支持多节点集群部署,保障高可用和容灾能力。

2. 适用场景

  • 微服务架构中的服务治理。
  • 分布式系统的配置中心。
  • 混合云环境下的服务统一管理。

三、Nacos部署模式

Nacos支持三种部署模式,适应不同场景需求:

  1. 单机模式:适用于开发测试环境,资源占用低,启动快速。
  2. 集群模式:生产环境推荐,通过多节点保障高可用。
  3. 多集群模式:跨数据中心部署,解决异地容灾问题。

在这里插入图片描述


四、Nacos单机模式部署实战

在这里插入图片描述
访问官网下载地址

1. 版本1.4.8下载及部署(Nacos 1.x 已经停止功能维护,可跳过该版本看2.5.0部署)

步骤1:下载与解压
  • 此处选择nacos-server-1.4.8.zip
  • 解压至非中文路径(如D:\nacos-server-1.4.8)。
步骤2:配置单机模式
  • 修改启动脚本bin/startup.cmd,设置运行模式为单机:
    set MODE="standalone"
    
步骤3:启动与验证
  • 执行bin/startup.cmd启动服务,默认端口8848
  • 访问http://localhost:8848/nacos,使用默认账号nacos/nacos登录。
  • 验证成功标志:控制台显示服务健康状态,无报错日志。
D:\nacos-server-1.4.8\nacos\bin>startup.cmd
"nacos is starting with standalone"

         ,--.
       ,--.'|
   ,--,:  : |                                           Nacos 1.4.8
,`--.'`|  ' :                       ,---.               Running in stand alone mode, All function modules
|   :  :  | |                      '   ,'\   .--.--.    Port: 8848
:   |   \ | :  ,--.--.     ,---.  /   /   | /  /    '   Pid: 14544
|   : '  '; | /       \   /     \.   ; ,. :|  :  /`./   Console: http://192.168.8.36:8848/nacos/index.html
'   ' ;.    ;.--.  .-. | /    / ''   | |: :|  :  ;_
|   | | \   | \__\/: . ..    ' / '   | .; : \  \    `.      https://nacos.io
'   : |  ; .' ," .--.; |'   ; :__|   :    |  `----.   \
|   | '`--'  /  /  ,.  |'   | '.'|\   \  /  /  /`--'  /
'   : |     ;  :   .'   \   :    : `----'  '--'.     /
;   |.'     |  ,     .-./\   \  /            `--'---'
'---'        `--`---'     `----'

2025-02-17 10:08:16,229 INFO Tomcat initialized with port(s): 8848 (http)

2025-02-17 10:08:16,574 INFO Root WebApplicationContext: initialization completed in 2042 ms

2025-02-17 10:08:18,602 INFO Adding welcome page: class path resource [static/index.html]

2025-02-17 10:08:18,911 WARN You are asking Spring Security to ignore Ant [pattern='/**']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead.

2025-02-17 10:08:18,912 INFO Will not secure Ant [pattern='/**']

2025-02-17 10:08:18,933 INFO Will secure any request with [org.springframework.security.web.session.DisableEncodeUrlFilter@3b0c9195, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@366c4480, org.springframework.security.web.context.SecurityContextPersistenceFilter@293a5f75, org.springframework.security.web.header.HeaderWriterFilter@37efd131, org.springframework.security.web.csrf.CsrfFilter@a43ce46, org.springframework.security.web.authentication.logout.LogoutFilter@60975100, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@1dd6d4b7, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@2dd29a59, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@2c7b5824, org.springframework.security.web.session.SessionManagementFilter@7e5d9a50, org.springframework.security.web.access.ExceptionTranslationFilter@3d97a632]

2025-02-17 10:08:18,960 INFO Exposing 1 endpoint(s) beneath base path '/actuator'

2025-02-17 10:08:19,013 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos'

2025-02-17 10:08:19,028 INFO Nacos started successfully in stand alone mode. use embedded storage

在这里插入图片描述
在这里插入图片描述

步骤4:关闭服务

本地使用命令行窗口启动,通过Ctrl+C关闭服务即可。

可选:MySQL持久化

Nacos数据存储类似Redis,默认在内存中存储,也支持数据持久化。
单机模式下支持MySQL,通过以下配置开启持久化。不开启则默认使用内存存储

  • 创建数据库nacos-config,执行conf/mysql-schema.sql初始化表结构。
  • 修改conf/application.properties,配置MySQL连接:
    spring.datasource.platform=mysql
    db.url.0=jdbc:mysql://127.0.0.1:3306/nacos-config?useSSL=false&serverTimezone=UTC
    db.user.0=root
    db.password.0=root
    

启动日志

D:\nacos-server-1.4.8\nacos\bin>startup.cmd
"nacos is starting with standalone"

         ,--.
       ,--.'|
   ,--,:  : |                                           Nacos 1.4.8
,`--.'`|  ' :                       ,---.               Running in stand alone mode, All function modules
|   :  :  | |                      '   ,'\   .--.--.    Port: 8848
:   |   \ | :  ,--.--.     ,---.  /   /   | /  /    '   Pid: 6292
|   : '  '; | /       \   /     \.   ; ,. :|  :  /`./   Console: http://192.168.8.36:8848/nacos/index.html
'   ' ;.    ;.--.  .-. | /    / ''   | |: :|  :  ;_
|   | | \   | \__\/: . ..    ' / '   | .; : \  \    `.      https://nacos.io
'   : |  ; .' ," .--.; |'   ; :__|   :    |  `----.   \
|   | '`--'  /  /  ,.  |'   | '.'|\   \  /  /  /`--'  /
'   : |     ;  :   .'   \   :    : `----'  '--'.     /
;   |.'     |  ,     .-./\   \  /            `--'---'
'---'        `--`---'     `----'

2025-02-17 10:27:28,857 INFO Tomcat initialized with port(s): 8848 (http)

2025-02-17 10:27:29,177 INFO Root WebApplicationContext: initialization completed in 1818 ms

2025-02-17 10:27:30,168 WARN There are no [com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent] publishers for this event, please register

2025-02-17 10:27:30,170 WARN There are no [com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent] publishers for this event, please register

2025-02-17 10:27:30,172 WARN There are no [com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent] publishers for this event, please register

2025-02-17 10:27:30,174 WARN There are no [com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent] publishers for this event, please register

2025-02-17 10:27:30,179 WARN There are no [com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent] publishers for this event, please register

2025-02-17 10:27:30,181 WARN There are no [com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent] publishers for this event, please register

2025-02-17 10:27:30,183 WARN There are no [com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent] publishers for this event, please register

2025-02-17 10:27:30,186 WARN There are no [com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent] publishers for this event, please register

2025-02-17 10:27:30,187 WARN There are no [com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent] publishers for this event, please register

2025-02-17 10:27:30,188 WARN There are no [com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent] publishers for this event, please register

2025-02-17 10:27:30,600 INFO Adding welcome page: class path resource [static/index.html]

2025-02-17 10:27:31,015 WARN You are asking Spring Security to ignore Ant [pattern='/**']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead.

2025-02-17 10:27:31,017 INFO Will not secure Ant [pattern='/**']

2025-02-17 10:27:31,047 INFO Will secure any request with [org.springframework.security.web.session.DisableEncodeUrlFilter@5b068087, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@6f152006, org.springframework.security.web.context.SecurityContextPersistenceFilter@1d131e1b, org.springframework.security.web.header.HeaderWriterFilter@5e1fa5b1, org.springframework.security.web.csrf.CsrfFilter@35a9782c, org.springframework.security.web.authentication.logout.LogoutFilter@61e94def, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@615091b8, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@4aa83f4f, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@3a393455, org.springframework.security.web.session.SessionManagementFilter@2f17e30d, org.springframework.security.web.access.ExceptionTranslationFilter@158a3b2e]

2025-02-17 10:27:31,078 INFO Exposing 1 endpoint(s) beneath base path '/actuator'

2025-02-17 10:27:31,151 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos'

2025-02-17 10:27:31,173 INFO Nacos started successfully in stand alone mode. use external storage

2025-02-17 10:27:31,373 INFO Initializing Servlet 'dispatcherServlet'

2025-02-17 10:27:31,375 INFO Completed initialization in 2 ms

初始化一些config_info配置数据,登入Nacos可以看到配置列表。
在这里插入图片描述
在这里插入图片描述


2. 版本2.5.0下载及部署

步骤1:下载与解压
  • 下载nacos-server-2.5.0.zip,解压至非中文路径(如D:\nacos-server-2.5.0)。
步骤2:启动与常见问题
  • 问题1:缺少RocksDB依赖库
    现象:启动时报错java.lang.UnsatisfiedLinkError: Can't find dependent libraries
    原因:在Windows本地启用Nacos 2.5.0版本时,遇到java.lang.UnsatisfiedLinkError: C:\librocksdbjni3694313270395220997.dll: Can’t find dependent libraries错误,通常是因为缺少RocksDB所需的依赖库。RocksDB是一个高性能的嵌入式数据库,Nacos使用它来存储数据。
    解决方案

    1. 安装Visual C++ Redistributable 2015-2022
    2. 重启系统后重新启动Nacos。

    在这里插入图片描述

  • 问题2:表结构不兼容
    现象:启动时提示表缺失。
    解决方案
    使用对应版本的mysql-schema.sql初始化数据库,确保表结构与版本匹配。

步骤3:验证启动
  • 启动成功后,访问控制台确认功能正常,日志显示Nacos started successfully
D:\product\unsoft\nacos-server-2.5.0\nacos\bin>startup.cmd
"nacos is starting with standalone"

         ,--.
       ,--.'|
   ,--,:  : |                                           Nacos 2.5.0
,`--.'`|  ' :                       ,---.               Running in stand alone mode, All function modules
|   :  :  | |                      '   ,'\   .--.--.    Port: 8848
:   |   \ | :  ,--.--.     ,---.  /   /   | /  /    '   Pid: 13252
|   : '  '; | /       \   /     \.   ; ,. :|  :  /`./   Console: http://192.168.8.36:8848/nacos/index.html
'   ' ;.    ;.--.  .-. | /    / ''   | |: :|  :  ;_
|   | | \   | \__\/: . ..    ' / '   | .; : \  \    `.      https://nacos.io
'   : |  ; .' ," .--.; |'   ; :__|   :    |  `----.   \
|   | '`--'  /  /  ,.  |'   | '.'|\   \  /  /  /`--'  /
'   : |     ;  :   .'   \   :    : `----'  '--'.     /
;   |.'     |  ,     .-./\   \  /            `--'---'
'---'        `--`---'     `----'

2025-02-17 14:17:18,145 INFO Tomcat initialized with port(s): 8848 (http)

2025-02-17 14:17:18,664 INFO Root WebApplicationContext: initialization completed in 3608 ms

2025-02-17 14:17:23,549 INFO Will secure any request with [org.springframework.security.web.session.DisableEncodeUrlFilter@19ae6bb, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@10993713, org.springframework.security.web.context.SecurityContextPersistenceFilter@cb191ca, org.springframework.security.web.header.HeaderWriterFilter@30bcf3c1, org.springframework.security.web.csrf.CsrfFilter@2fc0cc3, org.springframework.security.web.authentication.logout.LogoutFilter@505a9d7c, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@a776e, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@79145d5a, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@58359ebd, org.springframework.security.web.session.SessionManagementFilter@15cafec7, org.springframework.security.web.access.ExceptionTranslationFilter@67ab1c47]

2025-02-17 14:17:23,888 INFO Adding welcome page: class path resource [static/index.html]

2025-02-17 14:17:24,323 INFO Exposing 1 endpoint(s) beneath base path '/actuator'

2025-02-17 14:17:24,345 WARN You are asking Spring Security to ignore Ant [pattern='/**']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead.

2025-02-17 14:17:24,346 INFO Will not secure Ant [pattern='/**']

2025-02-17 14:17:24,347 WARN You are asking Spring Security to ignore Mvc [pattern='/prometheus']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead.

2025-02-17 14:17:24,347 INFO Will not secure Mvc [pattern='/prometheus']

2025-02-17 14:17:24,347 WARN You are asking Spring Security to ignore Mvc [pattern='/prometheus/namespaceId/{namespaceId}']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead.

2025-02-17 14:17:24,347 INFO Will not secure Mvc [pattern='/prometheus/namespaceId/{namespaceId}']

2025-02-17 14:17:24,347 WARN You are asking Spring Security to ignore Mvc [pattern='/prometheus/namespaceId/{namespaceId}/service/{service}']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead.

2025-02-17 14:17:24,347 INFO Will not secure Mvc [pattern='/prometheus/namespaceId/{namespaceId}/service/{service}']

2025-02-17 14:17:24,413 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos'

2025-02-17 14:17:24,430 INFO No TaskScheduler/ScheduledExecutorService bean found for scheduled processing

2025-02-17 14:17:24,462 INFO Nacos started successfully in stand alone mode. use embedded storage

3. 部署中的常见问题及解决方案

问题1:客户端注册失败(版本冲突)
  • 现象:客户端报错nacos registry failed
  • 原因:客户端版本(如2.2.0)高于服务端(如1.4.8)。
  • 解决:升级服务端至2.5.0,确保版本兼容。
问题2:端口占用或网络不通
  • 检查步骤
    1. 使用netstat -ano | findstr 8848确认端口未被占用。
    2. 通过telnet 127.0.0.1 8848测试连通性。
问题3:配置加载失败
  • 排查方向
    1. 检查application.properties中的数据库配置是否正确。
    2. 确保MySQL服务已启动,且用户权限充足。

五、官方FAQ与资源

  • 官方FAQ:参考Nacos用户常见问题
  • 最佳实践
    • 生产环境务必使用集群模式,并配置持久化数据库。
    • 定期备份Nacos配置文件及数据库。

结语

Nacos作为微服务生态的核心组件,其灵活性和易用性为开发者提供了强大支持。通过本文的实战指南,无论是新手还是经验丰富的开发者,都能快速掌握单机部署及问题排查技巧。建议在实际项目中结合业务需求,逐步探索集群部署与高级功能,构建稳定高效的微服务体系。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

niaonao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值