Bootstrap

Nacos集群设置开机自启动

一、搭建前提需要的环境

-rw-rw-rw-. 1 root root   8491533 Mar  5 20:05 apache-maven-3.3.9-bin.tar.gz
-rw-rw-rw-. 1 root root 189815615 Mar 23  2018 jdk-8u162-linux-x64.tar.gz
-rw-r--r--. 1 root root     25548 Apr  7  2017 mysql57-community-release-el7-10.noarch.rpm
-rw-rw-rw-. 1 root root  52115827 Mar  6 04:03 nacos-server-1.1.4.tar.gz
-rw-rw-rw-. 1 root root   1073948 Mar  6 18:26 nginx-1.22.1.tar.gz

二、确保mysql 服务是开机启动的,不然nacos 开机连不上Mysql

三、配置nginx 开机启动

1:创建/etc/systemd/system/nginx.service 文件

[Unit]
Description=nginx service
After=network.target

[Service]
Type=forking

ExecStart=/soft/nginx/sbin/nginx
ExecReload=/soft/nginx/sbin/nginx -s reload
ExecStop=/soft/nginx/sbin/nginx -s quit
PrivateTmp=true

[Install]
WantedBy=multi-user.target

2:赋予可执行权限

chmod 755 nginx.service

3:设置开机启动

systemctl daemon-reload
systemctl enable nginx

4:启动命令检查是否Ok

# 启动nginx服务
systemctl start nginx.service

# 重新启动nginx服务
systemctl restart nginx.service

# 查看nginx服务当前状态
systemctl status nginx.service

# 停止开机自启动
systemctl disable nginx.service

四、nacos开机启动

1、修改bin目录下的startup.sh  nacos启动脚本

vi /soft/nacos/nacos/bin/startup.sh

2、JAVA_HOME后面要改成自己的jdk路径,并注释下面三行内容,不然启动服务会提示找不到jdk

[ ! -e "$JAVA_HOME/bin/java" ] && JAVA_HOME=/soft/jdk8/jdk1.8.0_162
#[ ! -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

3、修改集群启动需要的内存。不然虚拟机内存太小导致启动nacos集群失败。

我改为了1G,我的虚拟机内存4G。

#===========================================================================================
# 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 -Xms1g -Xmx1g -Xmn512m -XX:MetaspaceSize=64m -XX:MaxMetaspaceSize=160m"
    JAVA_OPT="${JAVA_OPT} -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=${BASE_DIR}/logs/java_heapdump.hprof"
    JAVA_OPT="${JAVA_OPT} -XX:-UseLargePages"

fi

2、修改/etc/rc.local 文件并赋予权限,即可开机启动

vim /etc/rc.local
#!/bin/bash
# THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES
#
# It is highly advisable to create own systemd services or udev rules
# to run scripts during boot instead of using this file.
#
# In contrast to previous versions due to parallel execution during boot
# this script will NOT be run after all other services.
#
# Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure
# that this script will be executed during boot.

touch /var/lock/subsys/local

# nacos 集群启动命令
bash /soft/nacos/nacos/bin/startup.sh -p 3333
bash /soft/nacos/nacos/bin/startup.sh -p 4444
bash /soft/nacos/nacos/bin/startup.sh -p 5555

  赋予/etc/rc.local 可执行权限

chmod +x /etc/rc.d/rc.local

四、nacos 错误配置开机启动

注意 创建服务文件 /etc/systemd/system/nacos.service 方式启动nacos集群不行,亲测。

 

 后来发现编写自动启动脚本xx.service只能起单个进程,所以起集群在这不合适。

;