目录
一、集群结构图
二、创建nacos数据库及表
1、建库
create database nacos;
use nacos;
2、执行nacos-server包下conf/mysql-schema.sql文件创建相关表
三、集群nacos-server配置
1、下载地址
https://github.com/alibaba/nacos/releases/tag/2.3.1
2、重命名cluster.conf.example为cluster.conf
3、nacos端口说明,Nacos2.0版本相比1.X新增了gRPC的通信方式,因此需要增加2个端口。
8848:主端口,web页面端口,对应application.properties中的server.port。
7848:主端口-1000,集群数据同步接口。
9848:主端口+1000,客户端gRPC请求服务端端口,用于客户端向服务端发起连接和请求。
9849:主端口+1001,服务端gRPC请求服务端端口,用于服务间同步等。
4、在cluster.conf文件里配置集群服务(二选一)
# 注意:主端口对应的偏移量端口不能占用,所以端口不要连号
192.168.10.104:10002
192.168.10.104:10004
192.168.10.104:10006
# 使用该配置最好把application.properties的nacos.inetutils.ip-address=127.0.0.1开启
# 否则nacos启动会向cluster.conf里追加192.168.10.104:10002配置
127.0.0.1:10002
127.0.0.1:10004
127.0.0.1:10006
5、在application.properties文件,放开数据库配置注释并修改
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&useUnicode=true&useSSL=false&serverTimezone=UTC
db.user.0=root
db.password.0=123456
6、修改application.properties文件中nacos服务端口
server.port=10002
7、将已经改好配置的nacos-server复制两份,服务端口分别改为10004,10006
8、启动三个nacos-server
四、nginx配置
1、下载nginx.zip并解压,nacos2.x需要下载含有stream模块的nginx,1.8.1版本不含
https://nginx.org/en/download.html
2、修改nginx.conf配置,在http里增加以下配置,nacos2.x需要配置stream模块
http {
# 负载到集群
upstream nacoscluster {
server 127.0.0.1:10002;
server 127.0.0.1:10004;
server 127.0.0.1:10006;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
# 反向代理到nacos
location /nacos {
proxy_pass http://nacoscluster;
}
}
}
# nacos2.x必须配置这个TCP代理
stream {
# 主端口+1000
upstream nacoscluster {
server 127.0.0.1:11002;
server 127.0.0.1:11004;
server 127.0.0.1:11006;
}
server {
# http端口+1000
listen 1080;
proxy_pass nacoscluster;
}
}
3、启动/停止nginx
start nginx.exe
taskkill /f /im nginx.exe
五、访问nacos-server集群注册中心
http://localhost/nacos
六、nacos-client配置注册中心地址
spring:
application:
name: order-service
profiles:
# 运行环境,对应Data ID的一部分
active: dev
cloud:
nacos:
# nacos 服务端地址
server-addr: localhost:80
七、 注意事项
1、nacos2.x需要在nginx.conf配置stream模块
2、nginx需要确认是否支持stream模块,1.8.1不支持,1.24.0支持
3、开启com.alibaba.nacos日志Debug模式启动会报错,可忽略,grpc通过尝试获取目标类来判断是否启用某些特定版本的功能
4、debug报错如下:
java.lang.ClassNotFoundException: com.alibaba.nacos.shaded.io.perfmark.impl.SecretPerfMarkImpl$PerfM...