nacos

简介

视频:https://www.bilibili.com/video/BV1VJ411X7xX?spm_id_from=333.788.top_right_bar_window_custom_collection.content.click

程序通过在pom文件里引入nacos依赖(即agent)与nacos通信,在代码里写明nacos的配置参数信息获取配置。

安装

nacos的几种模式

参考:https://www.csdn.net/tags/NtzaYg3sMjk0NTktYmxvZwO0O0OO0O0O.html

MODE=standalone 单节点

=ip ip的方式暴露节点地址

=hostname 主机名的方式暴露节点地址

虚机单节点安装

依赖:jdk

虚机集群安装

官方推荐集群模式至少3个节点,其中1个leader,2个follower。

启动没有先后顺序,只是在conf/cluster.conf中指定集群的节点就行。

配置:

[root@ip-172-16-74-132 ~]# grep -v ^# /opt/nacos/conf/application.properties|grep -v ^$
server.servlet.contextPath=/nacos #默认路径
server.port=8848 #端口
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://数据库:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=
db.password=
management.endpoints.web.exposure.include=* #nacos暴露metrics数据
management.metrics.export.elastic.enabled=false
management.metrics.export.influx.enabled=false
server.tomcat.accesslog.enabled=false #关闭日志,否则日志产生太快
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i
server.tomcat.basedir=
nacos.security.ignore.urls=/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**
nacos.core.auth.system.type=nacos
nacos.core.auth.enabled=false #关闭登录功能
nacos.core.auth.default.token.expire.seconds=18000
nacos.core.auth.default.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789
nacos.core.auth.caching.enabled=false
nacos.istio.mcp.server.enabled=false

[root@ip-172-16-74-132 ~]# cat /opt/nacos/conf/cluster.conf
# ip:port
172.16.74.132:8848
172.16.75.212:8848
172.16.75.59:8848

docker单节点安装

最佳实践1:某生产环境(弃用,建议用最佳实践2)

参考:https://blog.csdn.net/qq_38616503/article/details/123497719

弃用原因:
20221228,用这种方式启动后秒退,而且发现数据没有存到custom.properties配置文件所配置的数据库里

  • 好像容器安装必须引用init.d/custom.properties文件,引用conf/cluster.conf不生效。但是其实init.d/custom.properties文件就是虚拟机安装的conf/application.properties文件
  • 9555这个端口是调试用的
[root@ecs-91b0 ~]# docker pull nacos/nacos-server:1.2.0
[root@ecs-91b0 ~]# mkdir -p /opt/nacos/logs
[root@ecs-91b0 ~]# mkdir -p /opt/nacos/init.d

改配置:
[root@ecs-91b0 ~]# cat /opt/nacos/init.d/custom.properties
server.servlet.contextPath=/nacos
server.port=8848
spring.datasource.platform=mysql
db.num=1
db.url.0=jdbc:mysql://数据库:3306/nacos?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
db.user=
db.password=
management.metrics.export.elastic.enabled=false
management.metrics.export.influx.enabled=false
server.tomcat.accesslog.enabled=false
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i
server.tomcat.basedir=
nacos.security.ignore.urls=/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**
nacos.core.auth.system.type=nacos
nacos.core.auth.enabled=false
nacos.core.auth.default.token.expire.seconds=18000
nacos.core.auth.default.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789
nacos.core.auth.caching.enabled=false
nacos.istio.mcp.server.enabled=false

注意:1、jvm配置应该不要超过机器的配置;2、数据挂载的位置;
[root@ecs-91b0 ~]# docker  run --name nacos -d -p 8848:8848 --privileged=true --restart=always -e JVM_XMS=2g -e JVM_XMX=2g -e MODE=standalone -e PREFER_HOST_MODE=standalone -v /opt/nacos/logs:/home/nacos/logs -v /opt/nacos/init.d/custom.properties:/home/nacos/init.d/custom.properties nacos/nacos-server:1.2.0

最佳实践2:某测试环境(和docker集群安装基本类似)

启动容器,确认要挂载的目录的位置:

[root@ip-172-16-224-155 ~]# docker run -itd nacos/nacos-server:1.2.0 /bin/bash
90a71ba3d7425ea694b0d2c5c352148cc8612ecbabdf303d0cd0f8356c47924f
[root@ip-172-16-224-155 ~]# docker ps
CONTAINER ID   IMAGE                      COMMAND                  CREATED         STATUS         PORTS      NAMES
90a71ba3d742   nacos/nacos-server:1.2.0   "bin/docker-startup.…"   4 seconds ago   Up 3 seconds   8848/tcp   kind_hermann
[root@ip-172-16-224-155 ~]# docker start kind_hermann
kind_hermann
[root@ip-172-16-224-155 ~]# docker exec -it kind_hermann /bin/bash
[root@90a71ba3d742 nacos]# ls /home/nacos/
LICENSE  NOTICE  bin  conf  init.d  logs  nohup.out  target  work


如果docker启动几秒后就退出,用这个命令:
docker run -itd -e PREFER_HOST_MODE=ip -e MODE=standalone  --name nacos --network=host --privileged=true --restart=always nacos/nacos-server:1.2.0

退出容器后,创建nacos目录,并把容器目录拷贝到本地:

[root@ip-172-16-224-155 ~]# mkdir -p /opt/nacos/
[root@ip-172-16-224-155 ~]# docker cp kind_hermann:/home/nacos/logs /opt/nacos/
[root@ip-172-16-224-155 ~]# docker cp kind_hermann:/home/nacos/bin /opt/nacos/
[root@ip-172-16-224-155 ~]# docker cp kind_hermann:/home/nacos/conf /opt/nacos/
[root@ip-172-16-224-155 ~]# docker cp kind_hermann:/home/nacos/data /opt/nacos/

修改配置文件(主要是修改数据库地址连接信息、开启nacos登录验证 nacos.core.auth.enabled=true 、开启nacos监控 management.endpoints.web.exposure.include=* ):

[root@ip-172-16-224-155 ~]# cp /opt/nacos/conf/application.properties /opt/nacos/conf/application.properties.bak
[root@ip-172-16-224-155 ~]# vim /opt/nacos/conf/application.properties
# spring
server.servlet.contextPath=/nacos
server.contextPath=/nacos
server.port=8848
spring.datasource.platform=mysql
nacos.cmdb.dumpTaskInterval=3600
nacos.cmdb.eventTaskInterval=10
nacos.cmdb.labelTaskInterval=300
nacos.cmdb.loadDataAtStart=false
db.num=1
db.url.0=jdbc:mysql://数据库/nacos_platform?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
#db.url.1=jdbc:mysql://${MYSQL_SERVICE_HOST}:${MYSQL_SERVICE_PORT:3306}/${MYSQL_SERVICE_DB_NAME}?${MYSQL_SERVICE_DB_PARAM:characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true}
db.user=
db.password=
### The auth system to use, currently only 'nacos' is supported:
nacos.core.auth.system.type=nacos
nacos.core.auth.enabled=true


### The token expiration in seconds:
nacos.core.auth.default.token.expire.seconds=18000

### The default token:
nacos.core.auth.default.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789

### Turn on/off caching of auth information. By turning on this switch, the update of auth information would have a 15 seconds delay.
nacos.core.auth.caching.enabled=false
nacos.core.auth.enable.userAgentAuthWhite=false
nacos.core.auth.server.identity.key=serverIdentity
nacos.core.auth.server.identity.value=security
server.tomcat.accesslog.enabled=false
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D
# default current work dir
server.tomcat.basedir=
## spring security config
### turn off security
nacos.security.ignore.urls=/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**
# metrics for elastic search
management.metrics.export.elastic.enabled=false
management.metrics.export.influx.enabled=false

nacos.naming.distro.taskDispatchThreadCount=10
nacos.naming.distro.taskDispatchPeriod=200
nacos.naming.distro.batchSyncKeyCount=1000
nacos.naming.distro.initDataRatio=0.9
nacos.naming.distro.syncRetryDelay=5000
nacos.naming.data.warmup=true
management.endpoints.web.exposure.include=*

修改启动脚本(主要是配置jvm JAVA_OPT=" ):

[root@ip-172-16-224-155 ~]# vim /opt/nacos/bin/docker-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

#      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.
set -x
export DEFAULT_SEARCH_LOCATIONS="classpath:/,classpath:/config/,file:./,file:./config/"
export CUSTOM_SEARCH_LOCATIONS=${DEFAULT_SEARCH_LOCATIONS},file:${BASE_DIR}/conf/,${BASE_DIR}/init.d/
export CUSTOM_SEARCH_NAMES="application,custom"
PLUGINS_DIR="/home/nacos/plugins/peer-finder"
function print_servers(){
   if [[ ! -d "${PLUGINS_DIR}" ]]; then
    echo "" > "$CLUSTER_CONF"
    for server in ${NACOS_SERVERS}; do
    #for server in `cat ../conf/cluster.conf`; do
            echo "$server" >> "$CLUSTER_CONF"
    done
   else
    bash $PLUGINS_DIR/plugin.sh
   sleep 30
        fi
}
#===========================================================================================
# JVM Configuration
#===========================================================================================
if [[ "${MODE}" == "standalone" ]]; then

    JAVA_OPT="${JAVA_OPT} -Xms2048m -Xmx2048m -Xmn2048m"
    JAVA_OPT="${JAVA_OPT} -Dnacos.standalone=true"
else

  JAVA_OPT="${JAVA_OPT} -server -Xms${JVM_XMS} -Xmx${JVM_XMX} -Xmn${JVM_XMN} -XX:MetaspaceSize=${JVM_MS} -XX:MaxMetaspaceSize=${JVM_MMS}"
  if [[ "${NACOS_DEBUG}" == "y" ]]; then
    JAVA_OPT="${JAVA_OPT} -Xdebug -Xrunjdwp:transport=dt_socket,address=9555,server=y,suspend=n"
  fi
  JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${BASE_DIR}/logs/java_heapdump.hprof"
  JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages"
  print_servers
fi

#===========================================================================================
# Setting system properties
#===========================================================================================
# set  mode that Nacos Server function of split
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
# set nacos server ip
if [[ ! -z "${NACOS_SERVER_IP}" ]]; then
    JAVA_OPT="${JAVA_OPT} -Dnacos.server.ip=${NACOS_SERVER_IP}"
fi

if [[ ! -z "${USE_ONLY_SITE_INTERFACES}" ]]; then
    JAVA_OPT="${JAVA_OPT} -Dnacos.inetutils.use-only-site-local-interfaces=${USE_ONLY_SITE_INTERFACES}"
fi

if [[ ! -z "${PREFERRED_NETWORKS}" ]]; then
    JAVA_OPT="${JAVA_OPT} -Dnacos.inetutils.preferred-networks=${PREFERRED_NETWORKS}"
fi

if [[ ! -z "${IGNORED_INTERFACES}" ]]; then
    JAVA_OPT="${JAVA_OPT} -Dnacos.inetutils.ignored-interfaces=${IGNORED_INTERFACES}"
fi

### If turn on auth system:
if [[ ! -z "${NACOS_AUTH_ENABLE}" ]]; then
    JAVA_OPT="${JAVA_OPT} -Dnacos.core.auth.enabled=${NACOS_AUTH_ENABLE}"
fi

if [[ "${PREFER_HOST_MODE}" == "hostname" ]]; then
    JAVA_OPT="${JAVA_OPT} -Dnacos.preferHostnameOverIp=true"
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/health:${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} -jar ${BASE_DIR}/target/nacos-server.jar"
JAVA_OPT="${JAVA_OPT} ${JAVA_OPT_EXT}"
JAVA_OPT="${JAVA_OPT} --spring.config.location=${CUSTOM_SEARCH_LOCATIONS}"
JAVA_OPT="${JAVA_OPT} --spring.config.name=${CUSTOM_SEARCH_NAMES}"
JAVA_OPT="${JAVA_OPT} --logging.config=${BASE_DIR}/conf/nacos-logback.xml"
JAVA_OPT="${JAVA_OPT} --server.max-http-header-size=524288"

echo "nacos is starting,you can check the ${BASE_DIR}/logs/start.out"
echo "$JAVA ${JAVA_OPT}" > ${BASE_DIR}/logs/start.out 2>&1 &
nohup $JAVA ${JAVA_OPT} > ${BASE_DIR}/logs/start.out 2>&1 < /dev/null

启动:

[root@ip-172-16-224-155 ~]# docker run -itd \
-e PREFER_HOST_MODE=ip \
-e MODE=standalone \
-v /opt/nacos/logs:/home/nacos/logs \
-v /opt/nacos/data:/home/nacos/data \
-v /opt/nacos/conf:/home/nacos/conf \
-v /opt/nacos/bin:/home/nacos/bin \
--name nacos \
--network=host \
--privileged=true \
--restart=always \
nacos/nacos-server:1.2.0

docker集群安装

启动参数参考:https://blog.csdn.net/qq_31278353/article/details/124416564

注意:

  • 因为跨主机,用host网络模式。
  • 启动时每台间隔不宜过长,否则可能发现不了集群(集群启动失败)

最佳实践1:某生产(不推荐)

这种方方式是以环境变量的方式启动

[root@-prod-nacos-1 ~]# docker run -itd \
-e PREFER_HOST_MODE=ip \
-e MODE=cluster \
-e NACOS_SERVERS="172.20.1.226:8848 172.20.25.171:8848 172.20.24.55:8848" \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST=数据库 \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_DB_NAME=nacos_config \
-e MYSQL_SERVICE_USER= \
-e MYSQL_SERVICE_PASSWORD= \
-e MYSQL_DATABASE_NUM=1 \
-e JVM_XMS=2048m \
-e JVM_XMX=2048m \
-e JVM_XMN=2048m \
--name nacos01 \
--network=host \
--privileged=true \
--restart=always \
nacos/nacos-server:1.2.0
[root@-prod-nacos-2 ~]# docker run -itd \
-e PREFER_HOST_MODE=ip \
-e MODE=cluster \
-e NACOS_SERVERS="172.20.1.226:8848 172.20.25.171:8848 172.20.24.55:8848" \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST= \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_DB_NAME=nacos_config \
-e MYSQL_SERVICE_USER= \
-e MYSQL_SERVICE_PASSWORD= \
-e MYSQL_DATABASE_NUM=1 \
-e JVM_XMS=2048m \
-e JVM_XMX=2048m \
-e JVM_XMN=2048m \
--name nacos02 \
--network=host \
--privileged=true \
--restart=always \
nacos/nacos-server:1.2.0
[root@-prod-nacos-3 ~]# docker run -itd \
-e PREFER_HOST_MODE=ip \
-e MODE=cluster \
-e NACOS_SERVERS="172.20.1.226:8848 172.20.25.171:8848 172.20.24.55:8848" \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST= \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_DB_NAME=nacos_config \
-e MYSQL_SERVICE_USER= \
-e MYSQL_SERVICE_PASSWORD= \
-e MYSQL_DATABASE_NUM=1 \
-e JVM_XMS=2048m \
-e JVM_XMX=2048m \
-e JVM_XMN=2048m \
--name nacos03 \
--network=host \
--privileged=true \
--restart=always \
nacos/nacos-server:1.2.0

Nacos-docker的一些环境参数含义如下表所示:

Name	Description	Option
MODE	cluster模式/standalone模式	cluster/standalone default cluster
NACOS_SERVERS	nacos cluster地址	eg. ip1,ip2,ip3
PREFER_HOST_MODE	是否支持hostname	hostname/ip default ip
NACOS_SERVER_PORT	nacos服务器端口	default 8848
NACOS_SERVER_IP	多网卡下的自定义nacos服务器IP
SPRING_DATASOURCE_PLATFORM	standalone 支持 mysql	mysql / empty default empty
MYSQL_MASTER_SERVICE_HOST	mysql 主节点host
MYSQL_MASTER_SERVICE_PORT	mysql 主节点端口	default : 3306
MYSQL_MASTER_SERVICE_DB_NAME	mysql 主节点数据库
MYSQL_MASTER_SERVICE_USER	数据库用户名
MYSQL_MASTER_SERVICE_PASSWORD	数据库密码
MYSQL_SLAVE_SERVICE_HOST	mysql从节点host
MYSQL_SLAVE_SERVICE_PORT	mysql从节点端口	default :3306
MYSQL_DATABASE_NUM	数据库数量	default :2
JVM_XMS	-Xms	default :2g
JVM_XMX	-Xmx	default :2g
JVM_XMN	-Xmn	default :1g
JVM_MS	-XX:MetaspaceSize	default :128m
JVM_MMS	-XX:MaxMetaspaceSize	default :320m
NACOS_DEBUG	开启远程调试	y/n default :n
TOMCAT_ACCESSLOG_ENABLED	server.tomcat.accesslog.enabled	default :false

如果数据库为多个,则需要把主从数据库都给写上:

最佳实践2:某生产(已废弃)

[root@-prod-nacos-1 ~]# docker run -itd \
> -e PREFER_HOST_MODE=ip \
> -e MODE=cluster \
> -e NACOS_SERVERS="172.22.0.100:8848 172.22.0.153:8848 172.22.25.162:8848" \
> -e SPRING_DATASOURCE_PLATFORM=mysql \
> -e MYSQL_SERVICE_HOST=172.22.1.42 \
> -e MYSQL_SERVICE_PORT=3306 \
> -e MYSQL_SERVICE_DB_NAME=nacos_config \
> -e MYSQL_SERVICE_USER= \
> -e MYSQL_SERVICE_PASSWORD= \
> -e MYSQL_SLAVE_SERVICE_HOST=172.22.0.40 \
> -e MYSQL_SLAVE_SERVICE_PORT=3306 \
> -e MYSQL_DATABASE_NUM=2 \
> -e JVM_XMS=2048m \
> -e JVM_XMX=2048m \
> -e JVM_XMN=2048m \
> --name nacos01 \
> --network=host \
> --privileged=true \
> --restart=always \
> nacos/nacos-server:1.2.0

最佳实践3:某生产(用这种办法)

参考:https://blog.csdn.net/zhuocailing3390/article/details/123058379

这种办法是以挂载的形式启动。好处是:1.如果要修改多个配置,不需要找多个参数的环境变量名,直接本地改完重启就行

集群:
172.22.0.100:8848
172.22.0.153:8848
172.22.25.162:8848

下面以其中一台为例,三台操作都一样:
下载镜像:

[root@-prod-nacos-1 ~]# docker pull nacos/nacos-server:2.0.1

启动容器,确认要挂载的目录的位置:

[root@-prod-nacos-1 ~]# docker run -itd nacos/nacos-server:2.0.1
2fa53b4b56cef7ad7d99fb52d2dd6e5c47d4f364a862da77f73f797359691f2e
[root@-prod-nacos-1 ~]# docker ps
CONTAINER ID   IMAGE                      COMMAND                  CREATED         STATUS         PORTS      NAMES
2fa53b4b56ce   nacos/nacos-server:2.0.1   "bin/docker-startup.…"   2 seconds ago   Up 2 seconds   8848/tcp   awesome_visvesvaraya
[root@-prod-nacos-1 ~]# docker exec -it 2fa53b4b56ce /bin/bash
[root@2fa53b4b56ce nacos]# ls
LICENSE  NOTICE  bin  conf  data  init.d  logs  nohup.out  target  work
[root@2fa53b4b56ce nacos]# ls conf/
1.4.0-ipv6_support-update.sql  application.properties  cluster.conf  nacos-logback.xml  schema.sql
[root@2fa53b4b56ce nacos]# pwd
/home/nacos
[root@2fa53b4b56ce nacos]# ls logs/
alipay-jraft.log           config-memory.log      config-trace.log   nacos.log               naming-push.log      protocol-raft.log  tps-control-detail.log
cmdb-main.log              config-notify.log      core-auth.log      nacos_gc.log.0.current  naming-raft.log      remote-digest.log  tps-control-digest.log
config-client-request.log  config-pull-check.log  istio-main.log     naming-distro.log       naming-rt.log        remote-push.log    tps-control.log
config-dump.log            config-pull.log        nacos-address.log  naming-event.log        naming-server.log    remote.log
config-fatal.log           config-server.log      nacos-cluster.log  naming-performance.log  protocol-distro.log  start.out
[root@2fa53b4b56ce nacos]# ls data/
loader  naming  protocol  tps
[root@2fa53b4b56ce nacos]# ls bin/
docker-startup.sh

退出容器后,创建nacos目录,并把容器目录拷贝到本地:

[root@-prod-nacos-1 ~]# mkdir -p /opt/nacos
[root@-prod-nacos-1 ~]# docker cp 2fa53b4b56ce:/home/nacos/conf /opt/nacos/
[root@-prod-nacos-1 ~]# docker cp 2fa53b4b56ce:/home/nacos/logs /opt/nacos/
[root@-prod-nacos-1 ~]# docker cp 2fa53b4b56ce:/home/nacos/bin /opt/nacos/
[root@-prod-nacos-1 ~]# docker cp 2fa53b4b56ce:/home/nacos/data /opt/nacos/

修改配置文件(主要是修改数据库地址、开启nacos登录验证、开启nacos监控):

[root@-prod-nacos-1 ~]# vim /opt/nacos/conf/application.properties
# spring
server.servlet.contextPath=/nacos
server.contextPath=/nacos
server.port=8848
spring.datasource.platform=mysql
nacos.cmdb.dumpTaskInterval=3600
nacos.cmdb.eventTaskInterval=10
nacos.cmdb.labelTaskInterval=300
nacos.cmdb.loadDataAtStart=false
db.num=1
db.url.0=jdbc:mysql://:3306/nacos_config?characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true
#db.url.1=jdbc:mysql://${MYSQL_SERVICE_HOST}:${MYSQL_SERVICE_PORT:3306}/${MYSQL_SERVICE_DB_NAME}?${MYSQL_SERVICE_DB_PARAM:characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true}
db.user=
db.password=
### The auth system to use, currently only 'nacos' is supported:
nacos.core.auth.system.type=nacos
nacos.core.auth.enabled=true


### The token expiration in seconds:
nacos.core.auth.default.token.expire.seconds=18000

### The default token:
nacos.core.auth.default.token.secret.key=SecretKey012345678901234567890123456789012345678901234567890123456789

### Turn on/off caching of auth information. By turning on this switch, the update of auth information would have a 15 seconds delay.
nacos.core.auth.caching.enabled=false
nacos.core.auth.enable.userAgentAuthWhite=false
nacos.core.auth.server.identity.key=serverIdentity
nacos.core.auth.server.identity.value=security
server.tomcat.accesslog.enabled=false
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D
# default current work dir
server.tomcat.basedir=
## spring security config
### turn off security
nacos.security.ignore.urls=/,/error,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/**,/v1/console/health/**,/actuator/**,/v1/console/server/**
# metrics for elastic search
management.metrics.export.elastic.enabled=false
management.metrics.export.influx.enabled=false

nacos.naming.distro.taskDispatchThreadCount=10
nacos.naming.distro.taskDispatchPeriod=200
nacos.naming.distro.batchSyncKeyCount=1000
nacos.naming.distro.initDataRatio=0.9
nacos.naming.distro.syncRetryDelay=5000
nacos.naming.data.warmup=true
management.endpoints.web.exposure.include=*

修改集群配置文件:

[root@-prod-nacos-1 ~]# vim /opt/nacos/conf/cluster.conf
172.22.0.100:8848
172.22.0.153:8848
172.22.25.162:8848

修改启动脚本(主要是屏蔽cluster.conf文件写入和配置jvm):

[root@yungou-prod-nacos-1 ~]# vim /opt/nacos/bin/docker-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

#      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.
set -x
export CUSTOM_SEARCH_NAMES="application,custom"
export CUSTOM_SEARCH_LOCATIONS=${BASE_DIR}/init.d/,file:${BASE_DIR}/conf/

export MEMBER_LIST=""
PLUGINS_DIR="/home/nacos/plugins/peer-finder"
function print_servers() {
  if [[ ! -d "${PLUGINS_DIR}" ]]; then
    #echo "" >"$CLUSTER_CONF" #注释掉
    for server in `cat ../conf/cluster.conf`; do #这里改成`cat ../conf/cluster.conf`
      echo "$server" >>"$CLUSTER_CONF"
    done
  else
    bash $PLUGINS_DIR/plugin.sh
    sleep 30
  fi
}
#===========================================================================================
# JVM Configuration
#===========================================================================================
if [[ "${MODE}" == "standalone" ]]; then

  JAVA_OPT="${JAVA_OPT} -Xms${JVM_XMS} -Xmx${JVM_XMX} -Xmn${JVM_XMN}"
  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 -Xms2048m -Xmx2048m -Xmn2048m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m" #-XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=320m这2个参数不用改
  if [[ "${NACOS_DEBUG}" == "y" ]]; then
    JAVA_OPT="${JAVA_OPT} -Xdebug -Xrunjdwp:transport=dt_socket,address=9555,server=y,suspend=n"
  fi
  JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${BASE_DIR}/logs/java_heapdump.hprof"
  JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages"
  print_servers
fi

#===========================================================================================
# Setting system properties
#===========================================================================================
# set  mode that Nacos Server function of split
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
# set nacos server ip
if [[ ! -z "${NACOS_SERVER_IP}" ]]; then
  JAVA_OPT="${JAVA_OPT} -Dnacos.server.ip=${NACOS_SERVER_IP}"
fi

if [[ ! -z "${USE_ONLY_SITE_INTERFACES}" ]]; then
  JAVA_OPT="${JAVA_OPT} -Dnacos.inetutils.use-only-site-local-interfaces=${USE_ONLY_SITE_INTERFACES}"
fi

if [[ ! -z "${PREFERRED_NETWORKS}" ]]; then
  JAVA_OPT="${JAVA_OPT} -Dnacos.inetutils.preferred-networks=${PREFERRED_NETWORKS}"
fi

if [[ ! -z "${IGNORED_INTERFACES}" ]]; then
  JAVA_OPT="${JAVA_OPT} -Dnacos.inetutils.ignored-interfaces=${IGNORED_INTERFACES}"
fi

### If turn on auth system:
if [[ ! -z "${NACOS_AUTH_ENABLE}" ]]; then
  JAVA_OPT="${JAVA_OPT} -Dnacos.core.auth.enabled=${NACOS_AUTH_ENABLE}"
fi

if [[ "${PREFER_HOST_MODE}" == "hostname" ]]; then
  JAVA_OPT="${JAVA_OPT} -Dnacos.preferHostnameOverIp=true"
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} -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/health:${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} -jar ${BASE_DIR}/target/nacos-server.jar"
JAVA_OPT="${JAVA_OPT} ${JAVA_OPT_EXT}"
JAVA_OPT="${JAVA_OPT} --spring.config.additional-location=${CUSTOM_SEARCH_LOCATIONS}"
JAVA_OPT="${JAVA_OPT} --spring.config.name=${CUSTOM_SEARCH_NAMES}"
JAVA_OPT="${JAVA_OPT} --logging.config=${BASE_DIR}/conf/nacos-logback.xml"
JAVA_OPT="${JAVA_OPT} --server.max-http-header-size=524288"

echo "nacos is starting,you can check the ${BASE_DIR}/logs/start.out"
echo "$JAVA ${JAVA_OPT}" >${BASE_DIR}/logs/start.out 2>&1 &
nohup $JAVA ${JAVA_OPT} >${BASE_DIR}/logs/start.out 2>&1 </dev/null

启动(另外2台里nacos容器名字分别是nacos02、nacos03):

docker run -itd \
-e PREFER_HOST_MODE=ip \
-e MODE=cluster \
-e NACOS_SERVERS="172.22.0.100:8848 172.22.0.153:8848 172.22.25.162:8848" \
-v /opt/nacos/logs:/home/nacos/logs \
-v /opt/nacos/data:/home/nacos/data \
-v /opt/nacos/conf:/home/nacos/conf \
-v /opt/nacos/bin:/home/nacos/bin \
--name nacos01 \
--network=host \
--privileged=true \
--restart=always \
nacos/nacos-server:2.0.1

通过lb负载地址登录:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-fqRbmCPU-1677129905754)(assets/Pasted%20image%2020221208184007.png)]

docker-compose安装

参考:https://www.cnblogs.com/hellxz/p/nacos-cluster-docker.html

https://www.cnblogs.com/qdhxhz/p/14705388.html

实践证明,一台虚机跑2个nacos容器行不通

k8s安装

k8s参考版:

- name: NACOS_SERVER_PORT
              value: "8848"
            - name: NACOS_APPLICATION_PORT
              value: "8848"
            - name: PREFER_HOST_MODE
              value: "hostname"
            - name: JVM_XMX
              value: "1g"
            - name: JVM_XMS
              value: "1g"
            - name: JVM_XMN
              value: "512m"
            - name: JVM_MS
              value: "128m"
            - name: JVM_MMS
              value: "320m"

目录

[root@ip-172-16-74-132 nacos]# ls /opt/nacos/
bin  conf  data  LICENSE  logs  NOTICE  status  target  work
data目录不能删
work貌似可以删

启停

1.单节点启停:

[root@ip-172-16-68-117 nacos]# cd /opt/nacos/bin/
[root@ip-172-16-68-117 bin]# ls
shutdown.cmd  shutdown.sh  startup.cmd  startup.sh  work
[root@ip-172-16-68-117 bin]# ./shutdown.sh
[root@ip-172-16-68-117 bin]# sh startup.sh -m standalone

2.集群启停:

startup.sh ‐m cluster
或者
startup.sh

web界面操作

账号密码

默认:nacos/nacos

修改密码:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BQ2ArKZ5-1677129905755)(assets/Pasted%20image%2020221208184243.png)]

查看集群信息

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2w0u6x8R-1677129905755)(assets/image-20220326093253363-20220819134502-9yffkzs-16660964694791.png)]

查看版本

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-OEWtYRuB-1677129905756)(assets/image-20220711113814514-20220819134502-qvznxnk-16660964694802.png)]

新建命令空间

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-4Nl48f2g-1677129905756)(assets/Pasted%20image%2020221228132951.png)]

数据的导出

选择指定的命名空间下的配置,会导出成zip包:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-f8TsYMiy-1677129905756)(assets/Pasted%20image%2020221228134541.png)]

数据的导入

选择需要导入的命名空间,直接选择上传导出的zip文件:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Nn1OODAR-1677129905757)(assets/Pasted%20image%2020221228134708.png)]

配置管理

作用:将微服务中每个服务的配置文件(如数据库连接参数,启动参数等)进行统一管理。

好处:可以自动更新获取配置、历史版本回滚。

在web图形化界面发布配置,程序通过api(即一段方法代码)获取配置

配置管理模型

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LUGBbHeM-1677129905757)(assets/image-20220325201622453-20220819134502-30ow4qj-16660964694803.png)]

namespace:对不同的环境进行隔离,如测试、生产环境,不同用户等

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kRfTQ8JW-1677129905757)(assets/image-20220325203942390-20220819134502-v9hbcn7-16660964694804.png)]

group:配置集的分组

data id:配置集,就是一个配置文件

配置项:配置文件里的每一个kv(参数)

当服务端(nacos)有变化时,会通过api自动通知客户端(程序)

微服务:每个服务的数据库是独立的;服务之间通过接口调用

每个微服务中bootstrap.yml文件指定了在nacos中配置文件的坐标,然后在启动文件里用代码来动态获取配置文件(nacos依赖pom中spring cloud框架)

发布配置:是从本地往nacos发送

获取配置:从nacos获取

服务发现

服务发现:微服务中不同服务之间调用,所以需要发现知道对方的地址。

nacos在服务发现这个功能中,类似于k8s中的service,把各个服务的地址统一管理。

每个服务在各自的application.yml文件里指定nacos服务发现的地址,并在启动类中引入服务发现。

通过代码的方式实现负载均衡。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-KGBN2nog-1677129905757)(assets/image-20220326101045168-20220819134502-9l0tk18-16660964694815.png)]

数据模型:

namespace:命名空间

服务名:每个服务的名字(服务会是一个集群)

实例:一个服务集群下的一个实例

元信息:nacos给每个实例配置的标签信息

数据库

命名空间的表:
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-A14zbriI-1677129905758)(assets/Pasted%20image%2020221228134919.png)]
配置管理-配置列表里的表(从data_id字段能看出,能够直接导入yaml文件):
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-PmB1Kixw-1677129905758)(assets/Pasted%20image%2020221228135148.png)]

迁移

需要把nacos的数据库迁移。

case

1.从nacos中找不到注册实例,请检查服务名是否正确且服务是否启动成功

调用方b2b业务调用order-center-manage-web服务,调用方的错误日志:

2022-07-01 11:43:03,842 INFO  [192.168.2.154] [WCfB2BWindowsService] - [HttpPost],回执数据:{"msg":"该服务名【order-center-manage-web】从nacos中找不到注册实例,请检查服务名是否正确且服务是否启动成功","success":false,"code":"503"}

可能是b2b改了配置没重启

2、20221207,生产新搭建的nacos开发调试时报错:

ailed to req API:/nacos/v1/ns/instance after all servers([172.22.1.229:8848]) tried: ErrCode:503, ErrMsg:server is DOWN now, please try again later!

解决:
是配置了2台mysql的问题,把slave的配置去掉(就是按照单台数据库配置就行):

docker run -itd \
-e PREFER_HOST_MODE=ip \
-e MODE=cluster \
-e NACOS_SERVERS="172.22.0.100:8848 172.22.0.153:8848 172.22.25.162:8848" \
-e SPRING_DATASOURCE_PLATFORM=mysql \
-e MYSQL_SERVICE_HOST= \
-e MYSQL_SERVICE_PORT=3306 \
-e MYSQL_SERVICE_DB_NAME=nacos_config \
-e MYSQL_SERVICE_USER= \
-e MYSQL_SERVICE_PASSWORD= \
#-e MYSQL_SLAVE_SERVICE_HOST= \
#-e MYSQL_SLAVE_SERVICE_PORT=3306 \
-e MYSQL_DATABASE_NUM=2 \ #这里改成1
-e JVM_XMS=2048m \
-e JVM_XMX=2048m \
-e JVM_XMN=2048m \
--name nacos01 \
--network=host \
--privileged=true \
--restart=always \
nacos/nacos-server:1.2.0
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值