Nacos 原生支持高可用,部署集群非常简单,仅仅需要修改每一个节点的 cluster.conf 配置文件即可,将所有节点(包括自己)ip:port 按行写入此文件即可。
/conf/cluster.conf
192.168.1.124:8848 192.168.1.130:8848 192.168.1.137:8848客户端在连接 NacosServer 的时候配置集群中所有节点。
spring: application: name: nacos-client-user-service profiles: active: dev cloud: nacos: discovery: server-addr: 192.168.1.124:8848,192.168.1.130:8848,192.168.1.137:8848 config: server-addr: 192.168.1.124:8848,192.168.1.130:8848,192.168.1.137:8848 file-extension: yaml官方推荐的集群部署方式是在 NacosServer 集群上面搭建一个代理层,并对外提供一个域名给客户端访问。这种部署方式要求代理层也是高可用的,可选的方案有 域名+ Vip+Nginx,相关端口需要层层暴露。
WebUI默认访问地址: :8848/nacos
配置管理->配置列表:nacos作为统一配置中心管理的微服务配置文件,存储在mysql中
服务管理->服务列表:已注册的微服务列表
集群管理->节点列表:nacosServer 集群节点展示。
以 SpringBoot 2.5.3 为例
pom主要引入spring-boot-starter-web,spring-cloud-starter-alibaba-nacos-discovery,spring-cloud-starter-alibaba-nacos-config,spring-cloud-starter-bootstrap
<properties> <java.version>11</java.version> <spring.cloud.dependencies>2020.0.3</spring.cloud.dependencies> <spring.cloud.alibaba.dependencies>2021.1</spring.cloud.alibaba.dependencies> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring.cloud.dependencies}</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-alibaba-dependencies</artifactId> <version>${spring.cloud.alibaba.dependencies}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <!-- nacos-discovery --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId> </dependency> <!-- nacos-config --> <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId> </dependency> </dependencies> bootstrap.yml server: port: 8818 spring: application: name: nacos-client-user-service profiles: active: dev cloud: nacos: discovery: server-addr: 192.168.1.124:8848,192.168.1.130:8848,192.168.1.137:8848 config: server-addr: 192.168.1.124:8848,192.168.1.130:8848,192.168.1.137:8848 file-extension: yaml logging: level: com.alibaba.nacos.common.http.client: debug 主启动类需要增加 @EnableDiscoveryClient 注解
客户端集成 启动测试启动客户端之前需要确保 NacosServer 集群正常运行,各个节点的 WebUI 可以正常打开,集群管理--->节点列表中各个节点状态为 UP。
启动 SpringBoot 客户端程序,若没有报错,在每一个节点 服务管理--->服务列表 中可以看到服务已经注册成功。
若启动失败,报异常了,或者服务未在所有节点完成注册,可通过如下思路排查:
详细阅读控制台输出日志
使用postman访问服务端提供的获取配置详情接口,测试是否可以通过直连某节点获取到配置信息
断点调试 NacosRestTemplate ,客户端获取服务配置文件是通过访问服务端http接口完成的,而客户端发起请求是使用的NacosRestTemplate
确保端口可以连通,7848,8848,9849,9848
详细阅读 Nacos.log 和 access_log.2022-02-09.log
集群心跳日志在 nacos-cluster.log 中
问题记录 报错信息NacosServer 启动报错
Error: Could not find or load main class Caused by: java.lang.ClassNotFoundException: 解决办法/bin/startup.sh 修改为如下内容
#!/bin/bash # 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 # # # 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. cygwin=false darwin=false os400=false case "`uname`" in CYGWIN*) cygwin=true;; Darwin*) darwin=true;; OS400*) os400=true;; esac error_exit () { echo "ERROR: $1 !!" exit 1 } [ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=$HOME/jdk/java [ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=http://www.likecs.com/usr/java [ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=http://www.likecs.com/opt/taobao/java [ ! -e "$JAVA_HOME/bin/java" ] && unset JAVA_HOME if [ -z "$JAVA_HOME" ]; then if $darwin; then if [ -x '/usr/libexec/java_home' ] ; then export JAVA_HOME=`/usr/libexec/java_home` elif [ -d "/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home" ]; then export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home" fi else JAVA_PATH=`dirname $(readlink -f $(which javac))` if [ "x$JAVA_PATH" != "x" ]; then export JAVA_HOME=`dirname $JAVA_PATH 2>/dev/null` fi fi if [ -z "$JAVA_HOME" ]; then error_exit "Please set the JAVA_HOME variable in your environment, We need java(x64)! jdk8 or later is better!" fi fi export SERVER="nacos-server" export MODE="cluster" export FUNCTION_MODE="all" export MEMBER_LIST="" export EMBEDDED_STORAGE="" while getopts ":m:f:s:c:p:" opt do case $opt in m) MODE=$OPTARG;; f) FUNCTION_MODE=$OPTARG;; s) SERVER=$OPTARG;; c) MEMBER_LIST=$OPTARG;; p) EMBEDDED_STORAGE=$OPTARG;; ?) echo "Unknown parameter" exit 1;; esac done export JAVA_HOME export JAVA="$JAVA_HOME/bin/java" export BASE_DIR=`cd $(dirname $0)/..; pwd` export CUSTOM_SEARCH_LOCATIONS=file:${BASE_DIR}/conf/ #=========================================================================================== # JVM Configuration #=========================================================================================== if [[ "${MODE}" == "standalone" ]]; then JAVA_OPT="${JAVA_OPT} -Xms512m -Xmx512m -Xmn256m" JAVA_OPT="${JAVA_OPT} -Dnacos.standalone=true" else if [[ "${EMBEDDED_STORAGE}" == "embedded" ]]; then JAVA_OPT="${JAVA_OPT} -DembeddedStorage=true" fi JAVA_OPT="${JAVA_OPT} -server -Xms2g -Xmx2g -Xmn1g -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m" JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${BASE_DIR}/logs/java_heapdump.hprof" JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages" fi if [[ "${FUNCTION_MODE}" == "config" ]]; then JAVA_OPT="${JAVA_OPT} -Dnacos.functionMode=config" elif [[ "${FUNCTION_MODE}" == "naming" ]]; then JAVA_OPT="${JAVA_OPT} -Dnacos.functionMode=naming" fi JAVA_OPT="${JAVA_OPT} -Dnacos.member.list=${MEMBER_LIST}" JAVA_MAJOR_VERSION=$($JAVA -version 2>&1 | sed -E -n 's/.* version "([0-9]*).*$/\1/p') if [[ "$JAVA_MAJOR_VERSION" -ge "9" ]] ; then JAVA_OPT="${JAVA_OPT} -Xlog:gc*:file=${BASE_DIR}/logs/nacos_gc.log:time,tags:filecount=10,filesize=102400" else JAVA_OPT_EXT_FIX="-Djava.ext.dirs=${JAVA_HOME}/jre/lib/ext:${JAVA_HOME}/lib/ext" JAVA_OPT="${JAVA_OPT} -Xloggc:${BASE_DIR}/logs/nacos_gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=10 -XX:GCLogFileSize=100M" fi JAVA_OPT="${JAVA_OPT} -Dloader.path=${BASE_DIR}/plugins/health,${BASE_DIR}/plugins/cmdb" JAVA_OPT="${JAVA_OPT} -Dnacos.home=${BASE_DIR}" JAVA_OPT="${JAVA_OPT} -jar ${BASE_DIR}/target/${SERVER}.jar" JAVA_OPT="${JAVA_OPT} ${JAVA_OPT_EXT}" JAVA_OPT="${JAVA_OPT} --spring.config.additional-location=${CUSTOM_SEARCH_LOCATIONS}" JAVA_OPT="${JAVA_OPT} --logging.config=${BASE_DIR}/conf/nacos-logback.xml" JAVA_OPT="${JAVA_OPT} --server.max-http-header-size=524288" if [ ! -d "${BASE_DIR}/logs" ]; then mkdir ${BASE_DIR}/logs fi echo "$JAVA $JAVA_OPT_EXT_FIX ${JAVA_OPT}" if [[ "${MODE}" == "standalone" ]]; then echo "nacos is starting with standalone" else echo "nacos is starting with cluster" fi # check the start.out log output file if [ ! -f "${BASE_DIR}/logs/start.out" ]; then touch "${BASE_DIR}/logs/start.out" fi # start echo "$JAVA $JAVA_OPT_EXT_FIX ${JAVA_OPT}" > ${BASE_DIR}/logs/start.out 2>&1 & nohup $JAVA $JAVA_OPT_EXT_FIX ${JAVA_OPT} nacos.nacos >> ${BASE_DIR}/logs/start.out 2>&1 & echo "nacos is starting,you can check the ${BASE_DIR}/logs/start.out" 报错信息