准备工作:
Nacos版本:1.3.1
JDK版本:1.8
系统:Win10
数据库:MySQL8.0
Nacos集群部署官方文档:https://nacos.io/zh-cn/docs/cluster-mode-quick-start.html(我按照官方文档部署没有成功,可能官方文档的教程只适用于Linux/Unix/Mac)
本次部署是在一台机器上,使用三个不同的端口,模拟三台Nacos机器
一、下载Nacos
Github:https://github.com/alibaba/nacos/releases
二、配置数据库
解压下载好的Nacos,在conf文件下有一个nacos-mysql.sql文件,执行该SQL文件,把数据导入到数据库中
修改Nacos下的conf文件下的application.properties文件(去掉注释,修改数据库即可)
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=123456
三、配置集群IP
在Nacos下的conf文件下有一个cluster.conf.example,复制该文件并修改文件名为cluster.conf。
编辑cluster.conf文件
#
# Copyright 1999-2018 Alibaba Group Holding Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#it is ip
#example
192.168.0.106:8848
192.168.0.106:8849
192.168.0.106:8850
主要修改最下面的IP:PORT,192.168.0.106是我电脑的内网IP,8848、8849、8850是Nacos端口
坑1:IP一定要是内网IP,不能是localhost或者127.0.0.1
四、启用集群模式(2020-11-17修改)
Nacos1.3.2版本之后默认就是集群模式,可以跳过此步骤
此步骤非常重要,否则不会启用集群模式(都是坑啊)
不知道是不是因为Nacos更新了的原因,现在的Nacos默认启动的是单节点模式,需要手动修改为集群模式
修改Nacos下的bin目录下的startup.cmd文件
大致在第27行左右修改set MODE="standalone“为set MODE="cluster"
五、修改端口
到上面第四步Nacos集群就已经配置好了,但是现在还只有一份Nacos,需要在这个Nacos配置文件的基础上在复制出2份Nacos
复制之后需要修改一个地方,就是启动端口。
修改nacos下的conf下的application.properties文件(注意三个nacos的端口不一样,分别是8848、8849、8850,和cluster.conf文件配置的一样)
### Default web server port:
server.port=8849
六、启动Nacos
Win10下只要双击运行nacos下的bin下的startup.cmd即可(三个Nacos都要启动)
启动成后访问 http://localhost:8848/nacos/,左侧的集群管理下的节点列表可以看到目前有三个节点,而且都是在线状态。
七、nginx配置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#设定负载均衡的服务器列表
upstream nacos{
#weigth参数表示权值,权值越高被分配到的几率越大
server 192.168.1.126:8849 weight=5;
server 192.168.1.126:8848 weight=5;
server 192.168.1.126:8847 weight=5;
}
server {
listen 80;
server_name localhost;
location / {
root E:/nginx/nginx-1.18.0/html/dist;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
location /prod-api/{
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass http://localhost:8088/;
}
}
server {
listen 8846;
server_name localhost;
charset utf-8;
location / {
root html;
index index.html index.htm;
#请求转向my_server1定义的服务器列表
proxy_pass http://nacos;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
项目填写转发的端口8846,全部稳妥。