Nacos 学习笔记:安装运行 + 初体验

  • 版本说明
  • 下载地址
  • Windows 版本
  • 安装
  • 单机运行
  • 优化启动方式
  • 使用 mysql 数据库
  • 集群部署
  • Linux 版本
  • 下载
  • 安装
  • 设置数据库使用 MySQL [可选]
  • 运行
  • 添加服务自启动
  • Nacos 管理界面支持的操作
  • 参考资料



学习中使用的是

windows

版本说明

下载地址

站点

链接

Win

Linux

github

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

.zip

.tar.gz

gitee

https://gitee.com/mirrors/Nacos 放在 百度盘提交码rest

zip

.tar.gz

csdn

(v1.3.2)

zip

Windows 版本

安装

不需要安装,解压即可。我解压在 D:\nacos

单机运行

  • 进入D:\nacos\bin在地址栏输入 cmd 打开命令行,输入startup.cmd -m standalone回车启动。
  • 启动完成后访问 http://127.0.0.1:8848/nacos 即可账号密码默认都是 nacos

如何利用Nacos的RefreshEventListener实现功能_微服务

优化启动方式

nacos的启动脚本startup.cmd默认是以cluster 集群方式启动。直接运行肯定报错,官方教程里是启动时添加参数 -m standalone 。但每次都这么手打太不方便,如下两个方案:

  1. 搞一份standalone模式的启动脚本:
    复制startup.cmdstartup-standalone.cmd。修改启动模式 set MODE="cluster" 改为 set MODE="standalone"。这样就可以直接双击 startup-standalone.cmd启动了。
  2. startup.cmd创建快捷方式,在快捷方式中添加参数。然后点击快捷方式启动。
    startup.cmd上右键创建快捷方式。如下添加参数:(注意有空格)

使用 mysql 数据库

nacos默认使用内嵌的h2数据库,但如果有需要也可以设置为使用mysql

  1. 在自己的mysql创建数据库nacos运行脚本 D:\nacos\conf\nacos-mysql.sql 初始化数据库。
  2. 修改配置文件 D:\nacos\conf\application.properties 对应我们的数据库
#*************** Config Module Related Configurations ***************#
### 如果使用 mysql 数据库开启这条配置:
spring.datasource.platform=mysql

### 数据库数量,默认1个:
db.num=1

### 数据库连接参数:
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=数据库账号
db.password=数据库密码
  1. 重新启动nacos即可。官网:Nacos支持三种部署模式中说

再以单机模式启动nacos,nacos所有写嵌入式数据库的数据都写到了mysql

集群部署

  1. 下载nacos复制cluster.conf.example改名为cluster.conf设置ip
#it is ip
#example
127.0.0.1:8840
127.0.0.1:8841
127.0.0.1:8842
  1. 数据库默认用内嵌的不用动。
  2. 把三个nacos的启动端口改为与上对应
  3. 修改nginx配置
upstream nacosnode {
        server 127.0.0.1:8840;
        server 127.0.0.1:8841;
        server 127.0.0.1:8842;
	}

    server {
		listen 80;
        location /nacos/ {
          proxy_pass http://nacosnode/nacos/;
        }
    }

如何利用Nacos的RefreshEventListener实现功能_nacos_02

Linux 版本

下载

  • 下载压缩包到 /usr/local
[root@localhost /]# cd /usr/local
[root@localhost local]# wget https://github.com/alibaba/nacos/releases/download/1.3.2/nacos-server-1.3.2.tar.gz
--2021-01-09 16:23:55--  https://github.com/alibaba/nacos/releases/download/1.3.2/nacos-server-1.3.2.tar.gz
正在解析主机 github.com (github.com)... 13.250.177.223
正在连接 github.com (github.com)|13.250.177.223|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 302 Found
位置:https://github-production-release-asset-2e65be.s3.amazonaws.com/137451403/82686f00-d688-11ea-833a-98d1fdc68fab?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20210109%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20210109T082356Z&X-Amz-Expires=300&X-Amz-Signature=5963031723b804d538a807cb4d20431f7807f33a7b270adc559aa2b8a008e8ea&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=137451403&response-content-disposition=attachment%3B%20filename%3Dnacos-server-1.3.2.tar.gz&response-content-type=application%2Foctet-stream [跟随至新的 URL]
--2021-01-09 16:23:56--  https://github-production-release-asset-2e65be.s3.amazonaws.com/137451403/82686f00-d688-11ea-833a-98d1fdc68fab?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20210109%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20210109T082356Z&X-Amz-Expires=300&X-Amz-Signature=5963031723b804d538a807cb4d20431f7807f33a7b270adc559aa2b8a008e8ea&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=137451403&response-content-disposition=attachment%3B%20filename%3Dnacos-server-1.3.2.tar.gz&response-content-type=application%2Foctet-stream
正在解析主机 github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)... 52.217.64.172
正在连接 github-production-release-asset-2e65be.s3.amazonaws.com (github-production-release-asset-2e65be.s3.amazonaws.com)|52.217.64.172|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:74800894 (71M) [application/octet-stream]
正在保存至: “nacos-server-1.3.2.tar.gz”

100%[==============================================================================>] 74,800,894   783KB/s 用时 95s

2021-01-09 16:25:33 (767 KB/s) - 已保存 “nacos-server-1.3.2.tar.gz” [74800894/74800894])

安装

不需要安装,解压即可使用。解压到 /usr/local/nacos

[root@localhost local]# tar -zxvf nacos-server-1.3.2.tar.gz
nacos/LICENSE
nacos/NOTICE
nacos/target/nacos-server.jar
nacos/conf/
nacos/conf/schema.sql
nacos/conf/nacos-mysql.sql
nacos/conf/application.properties.example
nacos/conf/nacos-logback.xml
nacos/conf/cluster.conf.example
nacos/conf/application.properties
nacos/bin/startup.sh
nacos/bin/startup.cmd
nacos/bin/shutdown.sh
nacos/bin/shutdown.cmd

设置数据库使用 MySQL [可选]

[root@localhost local]# vim /usr/local/nacos/conf/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=root
db.password=root

运行

进入:cd /usr/local/nacos/bin 执行:sh startup.sh -m standalone

[root@localhost local]# cd /usr/local/nacos/bin
[root@localhost bin]# sh startup.sh -m standalone
/usr/local/java/jdk1.7.0_80/bin/java  -Xms512m -Xmx512m -Xmn256m -Dnacos.standalone=true -Dnacos.member.list= -Djava.ext.dirs=/usr/local/java/jdk1.7.0_80/jre/lib/ext:/usr/local/java/jdk1.7.0_80/lib/ext -Xloggc:/usr/local/nacos/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -Dloader.path=/usr/local/nacos/plugins/health,/usr/local/nacos/plugins/cmdb -Dnacos.home=/usr/local/nacos -jar /usr/local/nacos/target/nacos-server.jar  --spring.config.location=classpath:/,classpath:/config/,file:./,file:./config/,file:/usr/local/nacos/conf/ --logging.config=/usr/local/nacos/conf/nacos-logback.xml --server.max-http-header-size=524288
nacos is starting with standalone
nacos is starting,you can check the /usr/local/nacos/logs/start.out
  • 查看日志
[root@localhost bin]# tail -500f /usr/local/nacos/logs/start.out
/usr/java/jdk1.8.0_271-amd64/bin/java  -Xms512m -Xmx512m -Xmn256m -Dnacos.standalone=true -Dnacos.member.list= -Djava.ext.dirs=/usr/java/jdk1.8.0_271-amd64/jre/lib/ext:/usr/java/jdk1.8.0_271-amd64/lib/ext -Xloggc:/usr/local/nacos/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M -Dloader.path=/usr/local/nacos/plugins/health,/usr/local/nacos/plugins/cmdb -Dnacos.home=/usr/local/nacos -jar /usr/local/nacos/target/nacos-server.jar  --spring.config.location=classpath:/,classpath:/config/,file:./,file:./config/,file:/usr/local/nacos/conf/ --logging.config=/usr/local/nacos/conf/nacos-logback.xml --server.max-http-header-size=524288

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

2021-01-09 22:51:59,779 INFO Bean 'org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration' of type [org.springframework.security.config.annotation.configuration.ObjectPostProcessorConfiguration$$EnhancerBySpringCGLIB$$a20f53ea] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2021-01-09 22:52:00,055 INFO Bean 'objectPostProcessor' of type [org.springframework.security.config.annotation.configuration.AutowireBeanFactoryObjectPostProcessor] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2021-01-09 22:52:00,064 INFO Bean 'org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler@5717c37' of type [org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2021-01-09 22:52:00,071 INFO Bean 'org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration' of type [org.springframework.security.config.annotation.method.configuration.GlobalMethodSecurityConfiguration$$EnhancerBySpringCGLIB$$c6e3f69c] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2021-01-09 22:52:00,085 INFO Bean 'methodSecurityMetadataSource' of type [org.springframework.security.access.method.DelegatingMethodSecurityMetadataSource] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

2021-01-09 22:52:01,655 INFO Tomcat initialized with port(s): 8848 (http)

2021-01-09 22:52:02,132 INFO Root WebApplicationContext: initialization completed in 11648 ms

2021-01-09 22:52:16,853 INFO Initializing ExecutorService 'applicationTaskExecutor'

2021-01-09 22:52:17,599 INFO Adding welcome page: class path resource [static/index.html]

2021-01-09 22:52:18,952 INFO Creating filter chain: Ant [pattern='/**'], []

2021-01-09 22:52:19,082 INFO Creating filter chain: any request, [org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter@1eef9aef, org.springframework.security.web.context.SecurityContextPersistenceFilter@388ffbc2, org.springframework.security.web.header.HeaderWriterFilter@7e70bd39, org.springframework.security.web.csrf.CsrfFilter@2a76b80a, org.springframework.security.web.authentication.logout.LogoutFilter@4de025bf, org.springframework.security.web.savedrequest.RequestCacheAwareFilter@4da855dd, org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter@2e5c7f0b, org.springframework.security.web.authentication.AnonymousAuthenticationFilter@11389053, org.springframework.security.web.session.SessionManagementFilter@6de54b40, org.springframework.security.web.access.ExceptionTranslationFilter@2f4854d6]

2021-01-09 22:52:19,407 INFO Exposing 2 endpoint(s) beneath base path '/actuator'

2021-01-09 22:52:19,487 INFO Initializing ExecutorService 'taskScheduler'

2021-01-09 22:52:19,833 INFO Tomcat started on port(s): 8848 (http) with context path '/nacos'

2021-01-09 22:52:19,843 INFO Nacos Log files: /usr/local/nacos/logs

2021-01-09 22:52:19,849 INFO Nacos Log files: /usr/local/nacos/conf

2021-01-09 22:52:19,850 INFO Nacos Log files: /usr/local/nacos/data

2021-01-09 22:52:19,851 INFO Nacos started successfully in stand alone mode. use embedded storage

添加服务自启动

创建服务脚本

vim /lib/systemd/system/nacos.service

脚本内容:

[Unit]
Description=nacos
After=network.target
 
[Service]
Type=forking
ExecStart=/usr/local/nacos/bin/startup.sh -m standalone
ExecReload=/usr/local/nacos/bin/shutdown.sh
ExecStop=/usr/local/nacos/bin/shutdown.sh
PrivateTmp=true
 
[Install]
WantedBy=multi-user.target

启动服务

systemctl enable nacos.service # 开启自启动 (.service可省)
systemctl start nacos.service # 运行服务  (.service可省)
systemctl status nacos.service # 查看状态

Nacos 管理界面支持的操作

反正都是图形化界面,一看就懂。

内容

操作

后台权限

支持:管理员账号、角色、权限的常规操作

名称空间

支持:增、删、查、改

配置文件

支持:增、删、查、改。克隆、回滚。列出正在监听配置变化的服务列表。

服务管理


参考资料

官网:Nacos 快速开始官网:集群部署说明官网:Nacos 系统参数介绍官网:Nacos支持三种部署模式官网:Open API 指南(支持通过API接口方式操作)

官网:spring-cloud-alibaba 各组件版本说明