Nacos2.0版本相比1.X新增了gRPC的通信方式,因此需要增加2個端口。新增端口是在配置的主端口(server.port)基礎上,進行一定偏移量自動生成。
Nacos安裝使用
Nacos是阿里開源的一款微服務注冊中心,一個更易于構建云原生應用的動態(tài)服務發(fā)現(xiàn)、配置管理和服務管理平臺。
文檔地址:https://nacos.io/zh-cn/docs/what-is-nacos.html
官方依賴:
下載安裝
可以從github下載最新的release版本:
下載地址:https://github.com/alibaba/nacos/releases
github上下載比較慢:https://ghproxy.com/https://github.com/alibaba/nacos/releases/download/2.0.3/nacos-server-2.0.3.tar.gz
下載完成之后得到nacos-server-2.0.3.zip,解壓之后就可以運行了。
單機啟動
Windows啟動命令(standalone代表著單機模式運行,非集群模式,直接雙擊是集群模式):
startup.cmd -m standalone
啟動后可以查看圖形界面的控制臺:
http://localhost:8848/nacos/#/login
默認用戶名和密碼:nacos/nacos,密碼在登錄之后可以修改,已經(jīng)集成了用戶相關管理。
注意這里默認情況下是使用內嵌的derby數(shù)據(jù)庫,可以在data/derby-data看到數(shù)據(jù)文件。

配置MySQL數(shù)據(jù)庫
nacos支持使用MySQL數(shù)據(jù)庫,可以使用MySQL數(shù)據(jù)庫代替內嵌的derby數(shù)據(jù)庫。
- 安裝MySQL數(shù)據(jù)庫
- 新建一個MySQL數(shù)據(jù)庫賬戶:nacos/12345678
- 初始化mysql數(shù)據(jù)庫nacos,數(shù)據(jù)庫初始化文件:nacos-mysql.sql
- 配置application.properties
配置文件application.properties中,把MySQL相關配置注釋放開,并根據(jù)實際情況配置:
### 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=nacos
db.password=12345678
再次啟動就可以了。
隨便新建一個配置項做測試,可以在config_info表中看到數(shù)據(jù)了:

集群啟動
nacos可以以集群啟動,3個或3個以上Nacos節(jié)點才能構成集群。
集群配置
這里以單機不同端口啟動測試,復制nacos目錄3份,分別修改對應的application.properties文件中的端口為8851、8861、8871
server.port=8851
復制cluster.conf.example為cluster.conf,并配置IP和端口。
10.181.10.242:8851
10.181.10.242:8861
10.181.10.242:8871
集群啟動
配置完成之后,分別啟動,不用帶參數(shù)了,默認就以集群模式啟動,可以編寫一個bat文件:
start call nacos8851/bin/startup.cmd
start call nacos8861/bin/startup.cmd
start call nacos8871/bin/startup.cmd
啟動完成之后可以通過任何一個端口的節(jié)點訪問,登錄后可以看到集群節(jié)點狀態(tài),表示已經(jīng)配置好了:

官方推薦集群部署后使用VIP或者域名訪問,不過也可以使用直連模式:
配置完成之后,就可以在相關項目中使用了。
spring:
cloud:
nacos:
server-addr: 10.181.10.242:8851,10.181.10.242:8861,10.181.10.242:8871
注意cluster端口
# 端口規(guī)則
server.port(默認8848)
raft port: ${server.port} - 1000
grpc port: ${server.port} + 1000
grpc port for server: ${server.port} + 1001
單機集群啟動的時候不能使用連續(xù)的端口了,會報錯:
Caused by: java.net.BindException: Address already in use: bind
at java.base/sun.nio.ch.Net.bind0(Native Method)
at java.base/sun.nio.ch.Net.bind(Net.java:455)
at java.base/sun.nio.ch.Net.bind(Net.java:447)
at java.base/sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:227)
安全性配置
默認情況下nacos沒有配置安全性,添加用戶和角色之后也沒用,集群下需要每個節(jié)點都要添加。
啟用安全性
需要在nacos目錄下的application.properties中先配置好啟用安全性
# nacos.core.auth.enabled=false
nacos.core.auth.enabled=true
然后啟動nacos服務器,現(xiàn)在添加用戶就可以了。
配置權限
新建用戶:

綁定角色:

給角色配置權限,配置讀寫權限才可以,注冊中心是要有寫入操作,配置中心也需要修改配置項:

客戶端使用
springboot項目中需要配置用戶名和密碼才能訪問了,否則會有403錯誤
spring:
cloud:
nacos:
server-addr: 10.181.10.242:8851,10.181.10.242:8861,10.181.10.242:8871
username: dev
password: 123456
Nginx配置負載均衡
Nacos2.0版本相比1.X新增了gRPC的通信方式,因此需要增加2個端口。新增端口是在配置的主端口(server.port)基礎上,進行一定偏移量自動生成。
| 端口 | 與主端口的偏移量 | 描述 |
|---|---|---|
| 9848 | 1000 | 客戶端gRPC請求服務端端口,用于客戶端向服務端發(fā)起連接和請求 |
| 9849 | 1001 | 服務端gRPC請求服務端端口,用于服務間同步等 |
使用VIP/nginx請求時,需要配置成TCP轉發(fā),不能配置http2轉發(fā),否則連接會被nginx斷開。
圖片來源于nacos官網(wǎng):

先檢查nginx是否有stream模塊,輸出中包含:--with-stream
nginx -V
nginx version: nginx/1.21.0
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
配置http模塊
在nginx的http下面配置http協(xié)議相關的地址和端口:
http {
# nacos服務器http相關地址和端口
upstream nacos-server {
server 10.181.10.242:8851;
server 10.181.10.242:8861;
server 10.181.10.242:8871;
}
server {
listen 8848;
location / {
proxy_pass http://nacos-server/;
}
}
}
另外需要配置grpc,需要nginx有stream模塊支持
配置grpc
# nacos服務器grpc相關地址和端口,需要nginx已經(jīng)有stream模塊
stream {
upstream nacos-server-grpc {
server 10.181.10.242:9851;
server 10.181.10.242:9861;
server 10.181.10.242:9871;
}
server {
listen 9848;
proxy_pass nacos-server-grpc;
}
}
配置后重啟nginx
sudo nginx -s reload
應用配置
spring:
cloud:
nacos:
server-addr: 10.181.10.xxx:8848
然后就可以正常使用了。