首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Spring Boot应用程序即服务

Spring Boot应用程序即服务
EN

Stack Overflow用户
提问于 2014-02-02 05:27:09
回答 11查看 153.9K关注 0票数 208

如何在Linux系统中配置打包为可执行jar即服务的Spring Boot应用程序?这是推荐的方法,还是应该将此应用程序转换为war并将其安装到Tomcat中?

目前,我可以从screen会话运行Spring boot应用程序,这很好,但需要在服务器重新启动后手动启动。

如果我使用可执行jar的方法是正确的,我正在寻找的是一般的建议/指导或示例init.d脚本。

EN

回答 11

Stack Overflow用户

发布于 2014-03-02 07:01:03

下面是在Linux中将Java应用程序作为系统服务安装的最简单方法。

假设您使用的是systemd (现在的任何现代发行版都是这样):

首先,在/etc/systemd/system中创建一个名为javaservice.service的服务文件,其中包含以下内容:

代码语言:javascript
复制
[Unit]
Description=Java Service

[Service]
User=nobody
# The configuration file application.properties should be here:
WorkingDirectory=/data 
ExecStart=/usr/bin/java -Xmx256m -jar application.jar --server.port=8081
SuccessExitStatus=143
TimeoutStopSec=10
Restart=on-failure
RestartSec=5

[Install]
WantedBy=multi-user.target

其次,将新的服务文件通知systemd

代码语言:javascript
复制
systemctl daemon-reload

并启用它,这样它就可以在启动时运行:

代码语言:javascript
复制
systemctl enable javaservice.service

最后,您可以使用以下命令来启动/停止新服务:

代码语言:javascript
复制
systemctl start javaservice
systemctl stop javaservice
systemctl restart javaservice
systemctl status javaservice

如果您使用的是systemd,这是将Java应用程序设置为系统服务的最无干扰和最干净的方法。

我特别喜欢这个解决方案的一点是,您不需要安装和配置任何其他软件。附带的systemd为您完成了所有工作,您的服务的行为与任何其他系统服务一样。我现在已经在产品中使用了一段时间,在各种发行版上,它就像你所期望的那样工作。

另一个优点是,通过使用/usr/bin/java,您可以轻松地添加jvm参数,如-Xmx256m

还要阅读官方Spring Boot文档中的systemd部分:http://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html

票数 126
EN

Stack Overflow用户

发布于 2014-05-08 17:31:39

我刚刚开始自己做这件事,所以下面就是我到目前为止关于CentOS init.d服务控制器脚本的内容。到目前为止,它工作得很好,但我不是leet Bash黑客,所以我相信还有改进的空间,所以欢迎您考虑改进它。

首先,我为每个服务提供了一个简短的配置脚本/data/svcmgmt/conf/my-spring-boot-api.sh,用于设置环境变量。

代码语言:javascript
复制
#!/bin/bash
export JAVA_HOME=/opt/jdk1.8.0_05/jre
export APP_HOME=/data/apps/my-spring-boot-api
export APP_NAME=my-spring-boot-api
export APP_PORT=40001

我使用的是CentOS,因此为了确保我的服务在服务器重新启动后启动,我在/etc/init.d/my-spring-boot-api中有一个服务控制脚本

代码语言:javascript
复制
#!/bin/bash
# description: my-spring-boot-api start stop restart
# processname: my-spring-boot-api
# chkconfig: 234 20 80

. /data/svcmgmt/conf/my-spring-boot-api.sh

/data/svcmgmt/bin/spring-boot-service.sh $1

exit 0

如您所见,它调用初始配置脚本来设置环境变量,然后调用我用来重新启动所有Spring Boot服务的共享脚本。这个共享脚本就是它的精华所在:

代码语言:javascript
复制
#!/bin/bash

echo "Service [$APP_NAME] - [$1]"

echo "    JAVA_HOME=$JAVA_HOME"
echo "    APP_HOME=$APP_HOME"
echo "    APP_NAME=$APP_NAME"
echo "    APP_PORT=$APP_PORT"

function start {
    if pkill -0 -f $APP_NAME.jar > /dev/null 2>&1
    then
        echo "Service [$APP_NAME] is already running. Ignoring startup request."
        exit 1
    fi
    echo "Starting application..."
    nohup $JAVA_HOME/bin/java -jar $APP_HOME/$APP_NAME.jar \
        --spring.config.location=file:$APP_HOME/config/   \
        < /dev/null > $APP_HOME/logs/app.log 2>&1 &
}

function stop {
    if ! pkill -0 -f $APP_NAME.jar > /dev/null 2>&1
    then
        echo "Service [$APP_NAME] is not running. Ignoring shutdown request."
        exit 1
    fi

    # First, we will try to trigger a controlled shutdown using 
    # spring-boot-actuator
    curl -X POST http://localhost:$APP_PORT/shutdown < /dev/null > /dev/null 2>&1

    # Wait until the server process has shut down
    attempts=0
    while pkill -0 -f $APP_NAME.jar > /dev/null 2>&1
    do
        attempts=$[$attempts + 1]
        if [ $attempts -gt 5 ]
        then
            # We have waited too long. Kill it.
            pkill -f $APP_NAME.jar > /dev/null 2>&1
        fi
        sleep 1s
    done
}

case $1 in
start)
    start
;;
stop)
    stop
;;
restart)
    stop
    start
;;
esac
exit 0

停止时,它将尝试使用Spring Boot Actuator执行受控关闭。但是,如果Actuator没有配置或者未能在合理的时间范围内关闭(我给它5秒,这真的有点短),进程将被终止。

此外,该脚本还假设运行应用程序的java进程将是进程详细信息文本中唯一包含"my-spring-boot-api.jar“的进程。在我的环境中,这是一个安全的假设,意味着我不需要跟踪PID。

票数 19
EN

Stack Overflow用户

发布于 2015-06-19 21:40:09

如果你想在Spring Boot Maven插件1.3.0.M2中使用Spring Boot 1.2.5,这里有一个解决方案:

代码语言:javascript
复制
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
</parent>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <version>1.3.0.M2</version>
            <configuration>
                <executable>true</executable>
            </configuration>
        </plugin>
    </plugins>
</build>

<pluginRepositories>
    <pluginRepository>
        <id>spring-libs-milestones</id>
        <url>http://repo.spring.io/libs-milestone</url>
    </pluginRepository> 
</pluginRepositories>

然后编译为make:mvn clean package,创建一个符号链接ln -s /.../myapp.jar /etc/init.d/myapp,将其设置为可执行的chmod +x /etc/init.d/myapp,然后启动service myapp start (使用Ubuntu Server)

票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21503883

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档