4

Nacos 本地单机版部署步骤和使用

 2 years ago
source link: https://segmentfault.com/a/1190000041067477
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

本系列是 Spring Cloud 微服务实战系列教程。之前在 《Spring Cloud Eureka 入门 (一)服务注册中心详解》 聊过 Spring Cloud Eureka。那今天聊聊阿里开源的 Nacos ~

一、Nacos 是什么?

先了解下 Spring Cloud Eureka 是基于 Netflix Eureka (Netflix 是 Java 实现的开源软件)。服务治理(Eureka)包括服务注册、服务发现和服务检测监控等。

那 Nacos 致力于发现、配置和管理微服务。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据及流量管理。

简而言之,Nacos 包含了微服务的配置管理 + 服务的注册、发现等监控。微服务也包括了 Spring Cloud 的微服务实现。

Nacos 其特性重点包含了以下:

  • 服务发现和服务健康监测
  • 动态配置服务
  • 动态 DNS 服务
  • 服务及其元数据管理

官方图如下:

二、如何本地部署和使用 Nacos

可以在 github 上 Nacos Release 版本列表中下载最新的稳定版本。地址:

https://github.com/alibaba/nacos/releases

当前稳定版本:2.0.3

2.1 环境准备

Nacos 依赖 Java 环境,所以如果从源码构建并运行 Nacos 需要配置如下:

  • JDK 1.8+
  • Maven 3.2+
  • 64 bit OS 支持 Linux/Unix/Mac/Windows,推荐选用 Linux/Unix/Mac

如果本机部署的话,不需要从源码构建,则只需要下载编译后的压缩包和 JDK 1.8+ 环境即可

2.2 下载编译后的压缩包

https://github.com/alibaba/na... 地址中,下载 nacos-server-2.0.3.zip 压缩包。然后执行一下解压命令:

unzip nacos-server-2.0.3.zip

这样目录中会出现 Nacos 的项目目录

2.3 启动前配置

目录结构如下:

nacos % ls -l
total 48
-rw-r--r--@  1 qq  staff  16583  3 18  2021 LICENSE
-rw-r--r--@  1 qq  staff   1305  5 14  2020 NOTICE
drwxr-xr-x@  8 qq  staff    256 12  3 14:25 bin
drwxr-xr-x@  9 qq  staff    288  7 27 14:18 conf
drwxr-xr-x   6 qq  staff    192 12  3 14:43 data
drwxr-xr-x  35 qq  staff   1120 12  3 14:25 logs
drwxr-xr-x@  3 qq  staff     96  7 28 19:28 target

进入 conf 目录,目录结构如下:

conf % ls -l
total 176
-rw-r--r--@ 1 qq  staff   1224  6 18 10:39 1.4.0-ipv6_support-update.sql
-rw-r--r--@ 1 qq  staff   9752 12  3 14:41 application.properties
-rw-r--r--@ 1 qq  staff   9506  7 27 14:18 application.properties.example
-rw-r--r--@ 1 qq  staff    670  3 18  2021 cluster.conf.example
-rw-r--r--@ 1 qq  staff  31156  7 15 19:19 nacos-logback.xml
-rw-r--r--@ 1 qq  staff  10660  6 18 10:39 nacos-mysql.sql
-rw-r--r--@ 1 qq  staff   8795  6 18 10:39 schema.sql

这次部署,目录中其中两个文件需要知晓:

  • application.properties 配置文件
  • nacos-mysql.sql 数据库 MySQL 库表信息文件

1/ 创建数据库 nacos 并执行 nacos-mysql.sql

本次测试用 MySQL 作为数据存储,则需要在 MySQL 中新建数据库 nacos,命令如下:

CREATE DATABASE nacos

然后再数据库中执行建表和插入默认数据操作,具体看 nacos-mysql.sql 文件。

2/ 修改 application.properties 配置文件

然后打开 application.properties 配置文件,对应的地方改成如下:

#*************** Config Module Related Configurations ***************#
### If use MySQL as datasource:
spring.datasource.platform=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=admin
db.password.0=123456

2.4 运行并使用 Nacos

以 Mac 本机为例,启动下面命令:

cd nacos/bin

sh startup.sh -m standalone

standalone 代表着单机模式运行,非集群模式

控制台可以看到以下信息:

nacos is starting with standalone
nacos is starting,you can check the /Users/qq/project/nacos/logs/start.out

那如何看 Nacos 日志,从上面可见日志输出在 nacos/logs 目录,查看 start.out 日志文件执行如下命令:

tail -200f /Users/qq/project/nacos/logs/start.out

控制台可以看到以下信息:



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

...

2021-12-04 15:25:18,201 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos'

这说明部署成功,电脑运行 Nacos 很简单,直接打开浏览器,输入地址:

http://localhost:8848/nacos/index.html

账号密码默认都为 nacos,如图

如何关闭呢?

关闭命令:

sh shutdown.sh

控制台会响应:

The nacosServer(4317) is running...
Send shutdown request to nacosServer(4317) OK

三、Nacos 小结

本文主要描述 Nacos 和 Nacos 的使用场景,然后本地部署 Nacos,达到可用状态。如果需要服务器部署,切记不能单机模式。Nacos 主要是微服务的配置中心和服务注册中心。

后续系列文章,会结合 Spring Cloud 微服务实践 Nacos 细节。

作者:泥瓦匠 (公号「程序员泥瓦匠」)出处:https://www.bysocket.com 欢迎转载,也请保留这段声明。谢谢!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK