SpringBoot单机启动和集群启动的区别:
总而言之,单机启动适用于开发和测试环境,对并发请求要求不高的小型应用程序。而双机集群启动适用于生产环境,以确保应用程序的稳定性和可扩展性。
一、安装Java运行时环境,这里我们使用开源免费的OpenJDK11
yum list|grep java-11-openjdk
yum install -y java-11-openjdk.x86_64
[root@localhost ~]# java -version
openjdk version "11.0.21" 2023-10-17 LTS
OpenJDK Runtime Environment (Red_Hat-11.0.21.0.9-1.el7_9) (build 11.0.21+9-LTS)
OpenJDK 64-Bit Server VM (Red_Hat-11.0.21.0.9-1.el7_9) (build 11.0.21+9-LTS, mixed mode, sharing)
[root@localhost ~]# which java
/usr/bin/java
[root@localhost ~]# ll /usr/bin/java
lrwxrwxrwx 1 root root 22 Jan 2 16:13 /usr/bin/java -> /etc/alternatives/java
[root@localhost ~]# ll /etc/alternatives/java
lrwxrwxrwx 1 root root 64 Jan 2 16:13 /etc/alternatives/java -> /usr/lib/jvm/java-11-openjdk-11.0.21.0.9-1.el7_9.x86_64/bin/java
[root@localhost ~]# ll /usr/lib/jvm/java-11-openjdk-11.0.21.0.9-1.el7_9.x86_64/bin/java
-rwxr-xr-x 1 root root 13192 Nov 15 00:51 /usr/lib/jvm/java-11-openjdk-11.0.21.0.9-1.el7_9.x86_64/bin/java
vi /etc/profile
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-11.0.21.0.9-1.el7_9.x86_64
export PATH=$PATH:$JAVA_HOME/bin
source /etc/profile
二、编写启动、停止SpringBoot服务的Shell脚本
#!/bin/bash
APP_NAME="gitegg-application.jar" # 命令号提示服务名
APP_PATH="/opt/server/gitegg-application.jar" # jar包文件路径
LOG_FILE="/opt/server/gitegg-application.log" # 日志输出文件路径,一般我们服务会记录日志,如果不想在这里输出,那么可以填写 /dev/null
#使用说明,提示输入参数
usage() {
echo "Usage: sh start_server.sh [start|stop|restart|status]"
exit 1
}
#检查服务是否正在运行
is_running(){
pid=`ps -ef|grep $APP_PATH|grep -v grep|awk '{print $2}' `
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
#启动服务
start(){
is_running
if [ $? -eq "0" ]; then
echo "${APP_NAME} is already running. pid=${pid} ."
else
nohup java -jar $APP_PATH > $LOG_FILE 2>&1 &
fi
}
#停止服务
stop(){
is_running
if [ $? -eq "0" ]; then
kill -9 $pid
else
echo "${APP_NAME} is not running"
fi
}
#显示运行状态
status(){
is_running
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is NOT running."
fi
}
#重启服务
restart(){
stop
start
}
#选择要运行的命令
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac
chmod +x /opt/server/start_server.sh
#!/bin/bash
APP_NAME="gitegg-application.jar" # 命令号提示服务名
APP_PATH="/opt/server/gitegg-application.jar" # jar包文件路径
LOG_FILE="/opt/server/gitegg-application.log" # 日志输出文件路径,一般我们服务会记录日志,如果不想在这里输出,那么可以填写 /dev/null
#使用说明,提示输入参数
usage() {
echo "Usage: sh start_server.sh [start|stop|restart|status]"
exit 1
}
#检查服务是否正在运行
is_running(){
pid=`ps -ef|grep $APP_PATH|grep -v grep|awk '{print $2}' `
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
#启动服务
start(){
is_running
if [ $? -eq "0" ]; then
echo "${APP_NAME} is already running. pid=${pid} ."
else
nohup java -jar $APP_PATH > $LOG_FILE 2>&1 &
fi
}
#停止服务
stop(){
is_running
if [ $? -eq "0" ]; then
kill -9 $pid
else
echo "${APP_NAME} is not running"
fi
}
#显示运行状态
status(){
is_running
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is NOT running."
fi
}
#重启服务
restart(){
stop
start
}
#选择要运行的命令
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac
三、编写启动、停止SpringBoot服务的Shell脚本
在CentOS中的服务是由systemd管理的,可以通过systemctl命令进行服务运行的相关操作。
vi /lib/systemd/system/gitegg-server.service
[Unit]
Description=gitegg-server
After=docker.socket early-docker.target network.target
Wants=network-online.target docker.service
Requires=docker.socket
[Service]
Type=forking
ExecStart=/opt/server/start_server.sh start
ExecStop=/opt/server/start_server.sh stop
ExecSatus=/opt/server/start_server.sh status
ExecRestart=/opt/server/start_server.sh restart
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable gitegg-server.service
# 启动服务
systemctl start gitegg-server.service
# 关闭服务
systemctl stop gitegg-server.service
# 查看服务状态
systemctl status gitegg-server.service
# 重启服务
systemctl restart gitegg-server.service
单机启动是指在一台机器上启动一个Spring Boot应用程序。在单机启动的情况下,应用程序运行在一个独立的进程中,并且只能处理一个请求。单机启动适用于开发和测试环境,以及对并发请求要求不高的小型应用程序。
通过以上配置,当服务器重新启动时,jar文件也将自动运行。请注意,这只是在CentOS系统实现的一种方法,实际操作可能因操作系统版本和个人设置而有所不同。