Skip to content

操作系统

  • 操作系统:推荐 Windows 10 或以上版本。
  • Nacos 版本:推荐使用 Nacos 2.x 版本,支持 JDK 17

环境准备

确保已安装 JDK 17,并配置好环境变量。

  • 检查 JDK 版本:

打开命令提示符,运行 java -version,确认输出为 JDK 17

js
C:\Users>java -version
java version "17.0.9" 2023-10-17 LTS
Java(TM) SE Runtime Environment (build 17.0.9+11-LTS-201)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.9+11-LTS-201, mixed mode, sharing)

温馨提示

如果未安装 JDK 17,请前往 《基础环境搭建>>#安装-JDK17》 查阅详细的安装步骤和流程。

  • Nacos 版本:

推荐使用 Nacos 2.x 版本,支持 JDK 17

  • MySQL 数据库:

Nacos 集群模式需要使用 MySQL 作为外部数据库。

下载 Nacos

访问 NacosGitHub Release 页面:Nacos Releases 下载安装包。

下载最新版本的 Nacos 压缩包(如 nacos-server-2.5.1.zip)。

将下载的压缩包解压到一个目录,例如:E:\nacos-server-2.5.1

配置 Nacos

Nacos 单机模式默认使用嵌入式数据库(Derby),无需额外配置。如果需要使用 MySQL 作为数据库,可以按照以下步骤配置:

  • 修改 application.properties

打开 Nacos 解压目录下的 conf 文件夹,找到 application.properties 文件。

编辑 application.properties,修改以下内容以启用 MySQL 数据库:

js
# 启用 MySQL 数据源
spring.datasource.platform=mysql

# 数据库数量(单机模式为 1
db.num=1

# MySQL 连接配置
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user.0=root
db.password.0=root

# 开启登录认证
nacos.core.auth.enabled=true

### Since 1.4.1, worked when nacos.core.auth.enabled=true and nacos.core.auth.enable.userAgentAuthWhite=false.
### The two properties is the white list for auth and used by identity the request from other server.
nacos.core.auth.server.identity.key=serverIdentity
nacos.core.auth.server.identity.value=security

# 设置默认令牌
nacos.core.auth.plugin.nacos.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789

温馨提示

  • db.url.0 中的 127.0.0.1:3306 替换为你的 MySQL 地址和端口。
  • db.user.0db.password.0 替换为你的 MySQL 用户名和密码。
  • 初始化 MySQL 数据库

MySQL 中创建一个数据库,例如 nacos

执行 Nacos 安装包 E:\nacos-server-2.5.1\conf\nacos-mysql.sql 路径下提供的 mysql-schema.sql 脚本初始化数据库;

执行成功后,nacos 数据库表结构如下:

js
nacos
|- config_info
|- config_info_gray
|- config_tags_relation
|- group_capacity
|- his_config_info
|- permissions
|- roles
|- tenant_capacity
|- tenant_info
|- users

启动 Nacos

打开命令提示符 (CMD),进入 Nacosbin 目录:

js
cd E:\nacos-server-2.5.1\bin

运行以下命令启动 Nacos

js
./startup.cmd -m standalone

单机模式

-m standalone 表示以单机模式启动。

如果启动成功,你会看到类似以下的日志:

PS E:\nacos-server-2.5.1\bin> ./startup.cmd -m standalone
"nacos is starting with standalone"

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

2025-03-18 13:13:32,171 INFO Tomcat initialized with port(s): 8848 (http)

2025-03-18 13:13:32,792 INFO Root WebApplicationContext: initialization completed in 3578 ms

2025-03-18 13:13:37,450 INFO Will secure any request with [org.springframework.security.web.session.DisableEncodeUrlFilter@fe87ddd, org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@4eea94a4, org.springframework.security.web.context.SecurityContextPersistenceFilter@4fb04a72, org.springframework.security.web.header.HeaderWriterFilter@2abbd0d9, org.springframework.security.web.csrf.CsrfFilter@46046c06, org.springframework.security.web.authentication.logout.LogoutFilter@17dad32f, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@343e225a, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@1a07bf6, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@7c281eb8, org.springframework.security.web.session.SessionManagementFilter@6025d790, org.springframework.security.web.access.ExceptionTranslationFilter@32ec9c90]

2025-03-18 13:13:37,910 INFO Adding welcome page: class path resource [static/index.html]

2025-03-18 13:13:38,521 INFO Exposing 1 endpoint(s) beneath base path '/actuator'

2025-03-18 13:13:38,547 WARN You are asking Spring Security to ignore Ant [pattern='/**']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead.

2025-03-18 13:13:38,548 INFO Will not secure Ant [pattern='/**']

2025-03-18 13:13:38,549 WARN You are asking Spring Security to ignore Mvc [pattern='/prometheus']. This is not recommended -- please use permitAll via HttpSecurity#authorizeHttpRequests instead.

2025-03-18 13:13:38,550 INFO Will not secure Mvc [pattern='/prometheus']

2025-03-18 13:13:38,550 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-03-18 13:13:38,550 INFO Will not secure Mvc [pattern='/prometheus/namespaceId/{namespaceId}']

2025-03-18 13:13:38,550 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-03-18 13:13:38,550 INFO Will not secure Mvc [pattern='/prometheus/namespaceId/{namespaceId}/service/{service}']

2025-03-18 13:13:38,641 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos'

2025-03-18 13:13:38,657 INFO No TaskScheduler/ScheduledExecutorService bean found for scheduled processing

2025-03-18 13:13:38,706 INFO Nacos started successfully in stand alone mode. use external storage

访问 Nacos

访问 Nacos 控制台

打开浏览器,访问 Nacos 控制台:

js
http://localhost:8848/nacos

默认用户名和密码均为 nacos

停止 Nacos

如果需要停止 Nacos,可以在 bin 目录下运行以下命令:

js
./shutdown.cmd

常见问题

  1. JDK 17 兼容性问题

如果启动时遇到 JDK 17 的兼容性问题,可以尝试以下解决方案:

  • 确保使用的是 Nacos 2.x 版本,Nacos 2.x 已经支持 JDK 17

  • 如果仍然有问题,可以尝试在 startup.cmd 中显式指定 JDK 17 的路径:

set JAVA_HOME=C:\path\to\jdk-17
  1. 端口冲突

如果端口 8848 被占用,可以修改 Nacos 的端口:

  • 编辑 conf/application.properties,修改以下配置:
js
server.port=8849
  • 重启 Nacos 服务。
  1. MySQL 连接失败

如果 MySQL 连接失败,检查以下内容:

MySQL 服务是否已启动。
MySQL 的用户名和密码是否正确。
MySQL 是否允许远程连接(如果 Nacos 和 MySQL 不在同一台机器)。

总结

通过以上步骤,你可以在 Windows 环境下基于 JDK 17 成功部署 Nacos 单机版本。如果需要进一步扩展为集群模式,可以参考 《Nacos官方文档》进行配置。

小蚂蚁云团队 · 提供技术支持

小蚂蚁云 新品首发
新品首发,限时特惠,抢购从速! 全场95折
赋能开发者,助理企业发展,提供全方位数据中台解决方案。
获取官方授权