Nacos系列:Nacos的三种部署模式

栏目: 数据库 · Mysql · 发布时间: 6年前

内容简介:Nacos支持三种部署模式1、单机模式:可用于测试和单机使用,生产环境切忌使用单机模式(满足不了高可用)2、集群模式:可用于生产环境,确保高可用

三种部署模式

Nacos支持三种部署模式

1、单机模式:可用于测试和单机使用,生产环境切忌使用单机模式(满足不了高可用)

2、集群模式:可用于生产环境,确保高可用

3、多集群模式:可用于多数据中心场景

单机模式

启动 Nacos Server

Linux: sh startup.sh -m standalone
Windows: cmd startup.cmd -m standalone 或 双击 startup.cmd 启动

关闭 Nacos Server

Linux: sh shutdown.sh
Windows: cmd shutdown.cmd 或 双击 shutdown.cmd 启动

0.7 版本之前, Nacos 使用的是嵌入式数据库 Derby ( Apache Derby )来存储数据; 0.7 版本,增加了对 mysql 数据源的支持。

Derby数据源

内嵌的数据库,通过命令直接启动即可,无需额外安装。

startup.cmd -m standalone

MySQL数据源

步骤一:安装 MySQL 数据,版本要求: 5.6.5+

步骤二:初始化数据库

创建数据库

create database if not exists nacos default charset utf8 collate utf8_general_ci;

初始化数据库

在nacos server解压目录conf下,找到 nacos-mysql.sql 文件,直接执行,执行完成后,用Navicat客户端查看

Nacos系列:Nacos的三种部署模式

步骤三:修改 conf/application.properties 文件,添加如下信息

## mysql datasource
spring.datasource.platform=mysql 

db.num=1
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=123456

注意: spring.datasource.platform=mysql 别漏了,要不然还是使用 Derby 数据库。

步骤四:启动 Nacos Server

启动成功后,我们使用上一篇博客: 《Nacos系列:Nacos的Java SDK使用》 中的案例来验证,运行NacosConfig,观察数据表的内容变化,示例知行后, config_info 表和 his_config_info 表都会有和配置相关的数据,如下图所示:

Nacos系列:Nacos的三种部署模式

似乎Nacos的MySQL数据源只存储了配置数据,服务列表和注册的服务实例信息并不会出现在数据表中(通过运行示例中的NacosDiscovery类main()方法,就可以观察到该现象)

集群模式

资源有限,我直接在Windows上模拟部署搭建“伪集群”, 新建一个文件目录NacosCluster,将Nacos Server解压三份到该目录下,分别命名为 nasosSlave0、nasosSlave1、nasosSlave2 ,分配端口: 8845、8846、8847

修改nasosSlave0/conf/application.properties,server.port=8845,并添加

## mysql datasource
spring.datasource.platform=mysql 

db.num=1
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=123456

这里先使用一个MySQL库演示功能,实际生产上至少要使用主备模式,例如:

db.num=2
db.url.0=jdbc:mysql://127.0.0.1:3306/nacos1?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.url.1=jdbc:mysql://127.0.0.1:3306/nacos2?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=root
db.password=123456

拷贝nasosSlave0/conf/cluster.conf.example为 cluster.conf ,修改内容如下:

127.0.0.1:8845
127.0.0.1:8846
127.0.0.1:8847

nasosSlave1、nasosSlave2 和 nasosSlave0 除了端口不同外,其它配置保持一致。

分别启动每台 Nacos Server

startup.cmd -m cluster

注意:在Windows下,这个时候不能再双击 startup.cmd 启动了,如果这样启动仍然是以 单机模式 运行,因为在 bin/startup.cmd 中有下面这段代码:

if not "%2" == "cluster" (
    set "JAVA_OPT=%JAVA_OPT% -Xms512m -Xmx512m -Xmn256m"
    set "JAVA_OPT=%JAVA_OPT% -Dnacos.standalone=true"
 ) else (
    set "JAVA_OPT=%JAVA_OPT% -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m"
    set "JAVA_OPT=%JAVA_OPT% -XX:-OmitStackTraceInFastThrow XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=%BASE_DIR%\logs\java_heapdump.hprof"
    set "JAVA_OPT=%JAVA_OPT% -XX:-UseLargePages"
 )

观察控制台,启动成功后,在控制台可以看到如下信息:

E:\Software\Nacos\nacosCluster\nacosSlave0\bin>startup.cmd -m cluster

         ,--.
       ,--.'|
   ,--,:  : |                                           Nacos 0.8.0
,`--.'`|  ' :                       ,---.               Running in cluster mode
|   :  :  | |                      '   ,'\   .--.--.    Port: 8845
:   |   \ | :  ,--.--.     ,---.  /   /   | /  /    '   Pid: 6568
|   : '  '; | /       \   /     \.   ; ,. :|  :  /`./   Console: http://192.168.1.102:8845/nacos/index.html
'   ' ;.    ;.--.  .-. | /    / ''   | |: :|  :  ;_
|   | | \   | \__\/: . ..    ' / '   | .; : \  \    `.      https://nacos.io
'   : |  ; .' ," .--.; |'   ; :__|   :    |  `----.   \
|   | '`--'  /  /  ,.  |'   | '.'|\   \  /  /  /`--'  /
'   : |     ;  :   .'   \   :    : `----'  '--'.     /
;   |.'     |  ,     .-./\   \  /            `--'---'
'---'        `--`---'     `----'

2019-02-20 23:06:13,059 INFO The server IP list of Nacos is [127.0.0.1:8845, 127.0.0.1:8846, 127.0.0.1:8847]

2019-02-20 23:06:14,185 INFO Nacos is starting...

2019-02-20 23:06:15,409 INFO Nacos is starting...

2019-02-20 23:06:16,512 INFO Nacos is starting...

2019-02-20 23:06:17,605 INFO Nacos is starting...

2019-02-20 23:06:18,736 INFO Nacos is starting...

2019-02-20 23:06:19,860 INFO Nacos is starting...

2019-02-20 23:06:21,021 INFO Nacos is starting...

2019-02-20 23:06:22,230 INFO Nacos is starting...

2019-02-20 23:06:23,390 INFO Nacos is starting...

2019-02-20 23:06:24,605 INFO Nacos is starting...

2019-02-20 23:06:25,991 INFO Nacos is starting...

2019-02-20 23:06:26,993 INFO Nacos is starting...

2019-02-20 23:06:28,197 INFO Nacos is starting...

2019-02-20 23:06:29,264 INFO Nacos is starting...

2019-02-20 23:06:30,515 INFO Nacos is starting...

2019-02-20 23:06:31,810 INFO Nacos is starting...

2019-02-20 23:06:32,934 INFO Nacos is starting...

2019-02-20 23:06:33,976 INFO Nacos is starting...

2019-02-20 23:06:35,044 INFO Nacos is starting...

2019-02-20 23:06:36,153 INFO Nacos is starting...

2019-02-20 23:06:37,290 INFO Nacos is starting...

2019-02-20 23:06:38,616 INFO Nacos is starting...

2019-02-20 23:06:39,736 INFO Nacos is starting...

2019-02-20 23:06:40,824 INFO Nacos is starting...

2019-02-20 23:06:41,757 INFO Nacos Log files: E:\Software\Nacos\nacosCluster\nacosSlave0\/logs/

2019-02-20 23:06:41,768 INFO Nacos Conf files: E:\Software\Nacos\nacosCluster\nacosSlave0\/conf/

2019-02-20 23:06:41,771 INFO Nacos Data files: E:\Software\Nacos\nacosCluster\nacosSlave0\/data/

2019-02-20 23:06:41,774 INFO Nacos started successfully in cluster mode.

在浏览器分别访问如下路径

http://localhost:8845/nacos
http://localhost:8846/nacos

http://localhost:8847/nacos

如果都能访问成功,证明集群模式部署成功。

多集群模式

Nacos支持NameServer路由请求模式,通过它您可以设计一个有用的映射规则来控制请求转发到相应的集群,在映射规则中您可以按命名空间或租户等分片请求(From Nacos官网)

其他说明

Nacos 官方宣称,Nacos v0.8.0 Pre-GA 版本已经可以用于生产环境,在此之前的版本,请勿在生产上使用。

参考资料


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

The Web Designer's Idea Book

The Web Designer's Idea Book

Patrick Mcneil / How / 2008-10-6 / USD 25.00

The Web Designer's Idea Book includes more than 700 websites arranged thematically, so you can find inspiration for layout, color, style and more. Author Patrick McNeil has cataloged more than 5,000 s......一起来看看 《The Web Designer's Idea Book》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

SHA 加密
SHA 加密

SHA 加密工具