如何构建Springd jar文件,systemd可以直接将其作为服务执行?

Der*_*har 6 java service systemd spring-boot

如何构建一个Springd引导jar文件,systemd可以直接将其作为服务执行?

遵循Installation作为systemd服务中的示例,我创建了以下systemd服务,该服务直接执行Spring Boot jarfile

[Unit]
Description=CRS Self-certification Service
Documentation=
Requires=postgresql.service
After=postgresql.service

[Service]
Environment=LOADER_PATH='lib/,config/,/etc/opes/crs/selfcertification'
ExecStart=/opt/opes/crs/selfcertification/crs-selfcertification-1.0.0-SNAPSHOT.jar
Restart=always
RestartSec=10
User=crs

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

但是,启动此服务时,systemd抱怨jarfile不可执行:

Nov 29 10:57:59 ubuntu systemd[24109]: selfcertification.service: Failed at step EXEC spawning /opt/opes/crs/selfcertification/crs-selfcertification-1.0.0-SNAPSHOT.jar: Exec format error
Nov 29 10:57:59 ubuntu systemd[1]: selfcertification.service: Main process exited, code=exited, status=203/EXEC
Nov 29 10:57:59 ubuntu systemd[1]: selfcertification.service: Unit entered failed state.
Nov 29 10:57:59 ubuntu systemd[1]: selfcertification.service: Failed with result 'exit-code'.
Run Code Online (Sandbox Code Playgroud)

jarfile的权限为755(所有人均可执行):

administrator@ubuntu:~$ ls -la /opt/opes/crs/selfcertification/crs-selfcertification-1.0.0-SNAPSHOT.jar
-rwxr-xr-x 1 crs selfcertification 35978778 Nov 22 17:16 /opt/opes/crs/selfcertification/crs-selfcertification-1.0.0-SNAPSHOT.jar
Run Code Online (Sandbox Code Playgroud)

为了为systemd服务构建可执行的jarfile,我必须对以下Gradle构建脚本进行哪些更改?

buildscript {
    ext {
        springBootVersion = '1.4.2.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

jar {
    baseName = 'crs-selfcertification'
    version = '1.0.0-SNAPSHOT'
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

repositories {
    mavenCentral()
}

springBoot {
    mainClass = "com.opessoftware.crs.selfcertification.Application"
    layout = "ZIP"
}

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile("org.springframework.boot:spring-boot-starter-thymeleaf")
    compile("org.springframework.boot:spring-boot-starter-data-jpa")
    compile("org.springframework.boot:spring-boot-starter-mail")
    testCompile("org.springframework.boot:spring-boot-starter-test")
    compile group: 'org.postgresql', name: 'postgresql', version: '9.4.1208.jre7'
    compile group: 'org.apache.commons', name: 'commons-dbcp2', version: '2.1.1'
    compile group: 'junit', name: 'junit', version: '4.12'

}
Run Code Online (Sandbox Code Playgroud)

请注意,如果不是尝试直接运行jarfile,而是systemd 使用Java虚拟机(JVM)从外壳程序脚本启动该服务,则该服务将成功运行:

[Unit]
Description=CRS Self-certification Service
Documentation=
Requires=postgresql.service
After=postgresql.service

[Service]
Environment=LOADER_PATH='lib/,config/,/etc/opes/crs/selfcertification'
#ExecStart=/opt/opes/crs/selfcertification/crs-selfcertification-1.0.0-SNAPSHOT.jar
ExecStart=/opt/opes/crs/selfcertification/startCrsSelfCertification
Restart=always
RestartSec=10
User=crs

[Install]
WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

Shell脚本/opt/opes/crs/selfcertification/startCrsSelfCertification使用JVM调用jarfile:

#!/bin/sh

java -Dloader.path='lib/,config/,/etc/opes/crs/selfcertification' -jar /opt/opes/crs/selfcertification/crs-selfcertification-1.0.0-SNAPSHOT.jar
Run Code Online (Sandbox Code Playgroud)

Spring Boot jarfile中可能缺少什么,阻止了systemd直接执行jarfile?

tan*_*an9 5

您应该指示Spring Boot将您的项目重新打包为完全可执行的形式:

springBoot {
    executable = true
}
Run Code Online (Sandbox Code Playgroud)

并且此功能仅适用于Spring Boot 1.4.0+。

有关更多信息,请参阅http://docs.spring.io/spring-boot/docs/current/reference/html/build-tool-plugins-gradle-plugin.html#build-tool-plugins-gradle-repackage-configuration

在Spring Boot 2.X +中,使用:

  bootJar {
      launchScript()
  }
Run Code Online (Sandbox Code Playgroud)

来源:可执行Spring Boot 2 jar

  • 将`your-app.conf`放在`your-app.jar`旁边,内容为`JAVA_OPTS=-Xmx2048M`,参考【部署脚本定制】(https://github.com/spring-projects/spring-boot/ blob/master/spring-boot-docs/src/main/asciidoc/deployment.adoc#deployment-script-customization-conf-file) 指南或 [launch.script](https://github.com/spring-projects/ spring-boot/blob/master/spring-boot-tools/spring-boot-loader-tools/src/main/resources/org/springframework/boot/loader/tools/launch.script)了解详情。 (2认同)