Nacos最新2.x集群安装以及Nginx负载均衡配置

Nacos2.0版本相比1.X新增了gRPC的通信方式,因此需要增加2个端口。新增端口是在配置的主端口(server.port)基础上,进行一定偏移量自动生成。

Nacos安装使用

Nacos是阿里开源的一款微服务注册中心,一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台。

文档地址:https://nacos.io/zh-cn/docs/what-is-nacos.html

官方依赖:

  1. 64 bit OS,支持 Linux/Unix/Mac/Windows,推荐选用 Linux/Unix/Mac。
  2. 64 bit JDK 1.8+;下载 & 配置

下载安装

可以从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,密码在登录之后可以修改,已经集成了用户相关管理。

注意这里默认情况下是使用内嵌的derby数据库,可以在data/derby-data看到数据文件。

image-20211222141358932

配置MySQL数据库

nacos支持使用MySQL数据库,可以使用MySQL数据库代替内嵌的derby数据库。

  1. 安装MySQL数据库
  2. 新建一个MySQL数据库账户:nacos/12345678
  3. 初始化mysql数据库nacos,数据库初始化文件:nacos-mysql.sql
  4. 配置application.properties

配置文件application.properties中,把MySQL相关配置注释放开,并根据实际情况配置:

### 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表中看到数据了:

image-20201127112044046

集群启动

nacos可以以集群启动,3个或3个以上Nacos节点才能构成集群。

集群配置

这里以单机不同端口启动测试,复制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

集群启动

配置完成之后,分别启动,不用带参数了,默认就以集群模式启动,可以编写一个bat文件:

start call nacos8851/bin/startup.cmd
start call nacos8861/bin/startup.cmd
start call nacos8871/bin/startup.cmd

启动完成之后可以通过任何一个端口的节点访问,登录后可以看到集群节点状态,表示已经配置好了:

image-20211222141628500

官方推荐集群部署后使用VIP或者域名访问,不过也可以使用直连模式:

配置完成之后,就可以在相关项目中使用了。

spring:
  cloud:
    nacos:
      server-addr: 10.181.10.242:8851,10.181.10.242:8861,10.181.10.242:8871

注意cluster端口

# 端口规则
server.port(默认8848)
raft port: ${server.port} - 1000
grpc port: ${server.port} + 1000
grpc port for server: ${server.port} + 1001

单机集群启动的时候不能使用连续的端口了,会报错:

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没有配置安全性,添加用户和角色之后也没用,集群下需要每个节点都要添加。

启用安全性

需要在nacos目录下的application.properties中先配置好启用安全性

# nacos.core.auth.enabled=false
nacos.core.auth.enabled=true

然后启动nacos服务器,现在添加用户就可以了。

配置权限

新建用户:

image-20211223152117640

绑定角色:

image-20211223152025255

给角色配置权限,配置读写权限才可以,注册中心是要有写入操作,配置中心也需要修改配置项:

image-20211223152156801

客户端使用

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请求服务端端口,用于客户端向服务端发起连接和请求
9849 1001 服务端gRPC请求服务端端口,用于服务间同步等

使用VIP/nginx请求时,需要配置成TCP转发,不能配置http2转发,否则连接会被nginx断开。

图片来源于nacos官网:

先检查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协议相关的地址和端口:

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已经有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

然后就可以正常使用了。

评论

  1. Dtouch
    2年前
    2022-3-11 11:26:40

    太赞了,nacos2.0端口偏移那个搞了我好久,看这篇总算是解决了

  2. 薄荷
    2年前
    2022-1-25 12:07:18

    ヾ(≧∇≦*)ゝ感谢,写得超好!~

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇