• 搭建nacos集群


    安装jdk1.8

    jdk为1.8的jdk-8u171-linux-x64.tar.gz版本先去orcle注册下载,上传到服务器
    1.解压缩

    tar -zxvf jdk-8u171-linux-x64.tar.gz;
    
    • 1

    2.目录下新建一个java文件夹

    mkdir /usr/local/java;
    
    • 1

    3.把解压的文件移动到新增的这个文件夹中

    mv jdk1.8.0_171/ /usr/local/java;
    
    • 1

    4.修改配置文件,添加环境变量,通过命令。vim /etc/profile在文件末尾添加一下内容

    export JAVA_HOME=/usr/local/java/jdk1.8.0_171
    export JRE_HOME=${JAVA_HOME}/jre
    export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib:$CLASSPATH
    export JAVA_PATH=${JAVA_HOME}/bin:${JRE_HOME}/bin
    export PATH=$PATH:${JAVA_PATH}
    
    • 1
    • 2
    • 3
    • 4
    • 5

    5.让profile文件生效

    source /etc/profile
    
    • 1

    安装maven

    先下载maven依赖包上传到服务器,版本为3.8.6maven下载地址
    1.创建目录并解压

    mkdir /usr/local/maven
    tar -zxvf apache-maven-3.8.6-bin.tar.gz
    mv ./apache-maven-3.8.6/* /usr/local/maven/
    rm -rf ./apache-maven-3.8.6
    
    • 1
    • 2
    • 3
    • 4

    2.配置环境变量 vim /etc/profile,在最下面添加如下

    MAVEN_HOME=/usr/local/maven
    export PATH=${MAVEN_HOME}/bin:${PATH}
    
    • 1
    • 2

    3.生效

    source /etc/profile
    
    • 1

    4.修改setting文件,配置阿里云仓库地址

    vim /usr/local/maven/conf/settings.xml
    
    • 1

    将文件中的标签内容替换成下面内容

    <mirror>
          <id>alimaven</id>
          <name>aliyun maven</name>
          <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
          <mirrorOf>central</mirrorOf>
     </mirror> 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    下载nacos安装文件

    lz这里安装的是1.1.4版本,下载地址
    在这里插入图片描述

    解压安装

    上传到服务器后解压安装
    1.上传文件到服务器,解压安装

    mkdir /usr/local/nacos/
    tar -zxvf nacos-server-1.1.4.tar.gz
    mv ./nacos/* /usr/local/nacos/
    rm -rf ./nacos
    
    • 1
    • 2
    • 3
    • 4

    2.配置conf目录下的cluster.conf文件,每行配置成ip:port。IP通过hostname -i获取,不要使用127.0.0.1或者localhost

    cd /usr/local/nacos/conf
    cp cluster.conf.example cluster.conf
    
    • 1
    • 2

    编辑cluster.conf文件vim cluster.conf,把里面的内容替换成如下,lz的ip为192.168.254.5,根据自身情况配置

    192.168.254.5:3333
    192.168.254.5:4444
    192.168.254.5:5555
    
    • 1
    • 2
    • 3

    3.初始化 MySQL 数据库,修改application.properties配置文件
    搭建MySQL5.7版本数据库,使用Navicat创建名称为nacos_config数据库,执行nacos的conf目录下的nacos-mysql.sql文件,这里就不做演示
    在这里插入图片描述
    在这里插入图片描述
    编辑application.properties文件vim application.properties,在文件最下面添加如下配置

    spring.datasource.platform=mysql
    
    db.num=1
    db.url.0=jdbc:mysql://192.168.17.22:3307/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
    db.user=root
    db.password=123456
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    修改bin目录下的startup.sh文件

    先备份sh文件,再进行修改

    cp startup.sh startup.sh.bak
    vim startup.sh
    
    • 1
    • 2

    找到如图的位置,改成和图片一致。新增p:与如下内容

            p)
                PORT=$OPTARG;;
    
    • 1
    • 2

    在这里插入图片描述
    修改文件末尾,添加jar文件的启动端口信息-Dserver.port=${PORT}参数
    在这里插入图片描述
    startup.sh

    #!/bin/sh
    # 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.
    
    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=/usr/java
    [ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=/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"
    while getopts ":m:f:s:p:" opt
    do
        case $opt in
            m)
                MODE=$OPTARG;;
            f)
                FUNCTION_MODE=$OPTARG;;
            s)
                SERVER=$OPTARG;;
    	p)
    	    PORT=$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 DEFAULT_SEARCH_LOCATIONS="classpath:/,classpath:/config/,file:./,file:./config/"
    export CUSTOM_SEARCH_LOCATIONS=${DEFAULT_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
        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_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} -cp .:${BASE_DIR}/plugins/cmdb/*.jar:${BASE_DIR}/plugins/mysql/*.jar"
      JAVA_OPT="${JAVA_OPT} -Xlog:gc*:file=${BASE_DIR}/logs/nacos_gc.log:time,tags:filecount=10,filesize=102400"
    else
      JAVA_OPT="${JAVA_OPT} -Djava.ext.dirs=${JAVA_HOME}/jre/lib/ext:${JAVA_HOME}/lib/ext:${BASE_DIR}/plugins/cmdb:${BASE_DIR}/plugins/mysql"
      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} -Dnacos.home=${BASE_DIR}"
    JAVA_OPT="${JAVA_OPT} -Dloader.path=${BASE_DIR}/plugins/health -jar ${BASE_DIR}/target/${SERVER}.jar"
    JAVA_OPT="${JAVA_OPT} ${JAVA_OPT_EXT}"
    JAVA_OPT="${JAVA_OPT} --spring.config.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}"
    
    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}" > ${BASE_DIR}/logs/start.out 2>&1 &
    nohup $JAVA -Dserver.port=${PORT} ${JAVA_OPT} nacos.nacos >> ${BASE_DIR}/logs/start.out 2>&1 &
    echo "nacos is starting,you can check the ${BASE_DIR}/logs/start.out"
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135

    启动nacos

    启动

    cd /usr/local/nacos/bin
    ./startup.sh -p 3333
    ./startup.sh -p 4444
    ./startup.sh -p 5555
    
    • 1
    • 2
    • 3
    • 4

    查看nacos集群情况

    ps -ef|grep nacos|grep -v grep |wc -l
    
    • 1

    报错(已经在配置中修改,正常操作不会出现这个问题,请忽略):发现只有第一个nacos可以启动,第二个nacos启动就会报端口被占用,报错信息如下

    原因: 经过长达一天的排查总算找到了原因,是因为启动指令./startup.sh -p 3333的-p中间多了一个空格,导致传进去的端口参数没有生效所以才会有端口被占用的问题,吐血了。

    cat /usr/local/nacos/logs/start.out
    
    • 1

    在这里插入图片描述
    根据提示需要查看nacos.log文件具体的日志信息

    cat /usr/local/nacos/logs/nacos.log
    
    • 1
    Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
    
    2022-11-11 19:16:39,908 ERROR 
    
    ***************************
    APPLICATION FAILED TO START
    ***************************
    
    Description:
    
    The Tomcat connector configured to listen on port 8080 failed to start. The port may already be in use or the connector may be misconfigured.
    
    Action:
    
    Verify the connector's configuration, identify and stop any process that's listening on port 8080, or configure this application to listen on another port.
    
    
    2022-11-11 19:16:39,910 INFO Shutting down ExecutorService 'taskScheduler'
    
    2022-11-11 19:16:39,912 INFO Shutting down ExecutorService 'applicationTaskExecutor'
    
    2022-11-11 19:16:45,001 ERROR Unexpected error occurred in scheduled task.
    
    java.lang.IllegalStateException: unable to find local peer: 192.168.254.5:8080, all peers: [192.168.254.5:3333, 192.168.254.5:4444, 192.168.254.5:5555]
    	at com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeerSet.local(RaftPeerSet.java:224)
    	at com.alibaba.nacos.naming.monitor.PerformanceLoggerThread.collectMetrics(PerformanceLoggerThread.java:100)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:498)
    	at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)
    	at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    	at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
    	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
    	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    	at java.lang.Thread.run(Thread.java:748)
    2022-11-11 19:17:00,001 ERROR Unexpected error occurred in scheduled task.
    
    java.lang.IllegalStateException: unable to find local peer: 192.168.254.5:8080, all peers: [192.168.254.5:3333, 192.168.254.5:4444, 192.168.254.5:5555]
    	at com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeerSet.local(RaftPeerSet.java:224)
    	at com.alibaba.nacos.naming.monitor.PerformanceLoggerThread.collectMetrics(PerformanceLoggerThread.java:100)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    	at java.lang.reflect.Method.invoke(Method.java:498)
    	at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84)
    	at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    	at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:93)
    	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
    	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    	at java.lang.Thread.run(Thread.java:748)
    2022-11-11 19:17:15,001 ERROR Unexpected error occurred in scheduled task.
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61

    整合nginx,修改nginx.conf文件配置

    lz这里使用的ng版本是1.16,修改conf下的nginx.conf文件。这里忽略ng的安装步骤

       #gzip  on;
        upstream cluster{
            server 127.0.0.1:3333;
            server 127.0.0.1:4444;
            server 127.0.0.1:5555;
    
    	#keepalive 256;
        }
    
        server {
            listen       1111;
            server_name  localhost;
    
            #charset koi8-r;
    
            #access_log  logs/host.access.log  main;
    
            location / {
               # root   html;
               # index  index.html index.htm;
               proxy_pass http://cluster;
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    启动nginx命令

    /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
    
    • 1

    访问项目

    访问地址:http://192.168.254.5:1111/nacos/,并且尝试添加测试文件
    在这里插入图片描述

  • 相关阅读:
    ftp登录时,报错“530 Login incorrect.Login failed”
    超级强大的菜鸟工具库
    java集合 list转map一些常用的方式(Stream流,,,)
    CSS 之 posiiton:fixed 固定定位在父元素含有 tranform 属性时会失效,变成 absolute 的效果
    登录业务实现(单点登录+微信扫码+短信服务)
    【无标题】django中logging配置
    MongoDB数据库新手入门
    高级IO---多路转接模型的实现:select模型、poll模型、epoll模型
    项目初始配置-gitee关联
    分布式事务之CP架构、AP架构解决方案
  • 原文地址:https://blog.csdn.net/qq_42910468/article/details/127811284