当前位置: 首页 > 知识库问答 >
问题:

Spring Boot 2.1.6无法作为SystemD服务启动

齐文栋
2023-03-14

我正在尝试在亚马逊 Linux EC2 实例上启动Spring启动应用程序即服务。

我将服务打包为:

$ mvn clean package spring-boot:repackage -Dstart-class=com.acme.MyMicroserviceApplication

当我手动启动服务(没有服务)时,它会运行correclty:

$ /usr/bin/java -Djava.security.egd=file:/dev/./urandom -Dspring.profiles.active=pro -server -d64 -Xms256m -Xmx2048m -Xss256m -XX:+UseParallelGC -XX:MaxHeapSize=2048m -XX:MaxMetaspaceSize=1024m -XX:MaxHeapFreeRatio=75 -XX:+HeapDumpOnOutOfMemoryError -jar /home/ec2-user/jars/my-microservice-1.0.0-RELEASE.jar

信息 [main] 组织.springframework.boot.web.embedded.tomcatWebServer: Tomcat 在端口上启动: 8080 (http) 与上下文路径 '' 信息 [主] 组织.springframework.boot.StartupInfoLogger: 启动 Facebook 服务在 12.17 秒内启动应用程序 (JVM 运行 12.861) 信息 [Servlet 'dispatcherServlet' 信息:在 23 毫秒内完成初始化

但当我启动服务时,Spring Boot找不到该类:

    < Li > org . spring framework . boot . loader . jar launcher

错误为:

my-microservice[27205]: Error: Could not find or load main class org.springframework.boot.loader.JarLauncher
systemd[1]: squint-facebook.service: main process exited, code=exited, status=1/FAILURE
systemd[1]: Unit my-micro.service entered failed state.
systemd[1]: my-micro.service failed.
systemd[1]: Stopped my-microservice.

我的服务文件是:

  • /etc/systemd/system/my-micro.service
[Unit]
Description=my-microservice
After=syslog.target network.target
Wants=redis.service

[Service]
Type=simple
EnvironmentFile=/etc/sysconfig/my-microservice
Environment="JAVA_HOME=$(dirname $(dirname $(readlink $(readlink $(which java)))))"
Environment="PATH=$PATH:$JAVA_HOME/bin"
Environment="CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib"
Environment="SPRING_PROFILES_ACTIVE=pro"
Environment="JAVA_OPTS=-Djava.security.egd=file:/dev/./urandom -Dspring.profiles.active=pro -server -d64 -Xms256m -Xmx2048m -Xss256m -XX:+UseParallelGC -XX:MaxHeapSize=2048m -XX:MaxMetaspaceSize=1024m -XX:MaxHeapFreeRatio=75 -XX:+HeapDumpOnOutOfMemoryError"
User=springboot
# The configuration file application.properties should be here:
WorkingDirectory=/home/ec2-user/jars
# Run ExecStartPre with root-permissions
PermissionsStartOnly=true
ExecStartPre=/bin/mkdir -p /var/log/my-microservice
ExecStartPre=/bin/chown -R springboot:syslog /var/log/my-microservice
ExecStartPre=/bin/chmod -R 775 /var/log/my-microservice
ExecStart=/usr/bin/java \
        -Djava.security.egd=file:/dev/./urandom \
        -Dspring.profiles.active=pro \
        -server \
        -d64 \
        -Xms256m \
        -Xmx2048m \
        -Xss256m \
        -XX:+UseParallelGC \
        -XX:MaxHeapSize=2048m \
        -XX:MaxMetaspaceSize=1024m \
        -XX:MaxHeapFreeRatio=75 \
        -XX:+HeapDumpOnOutOfMemoryError \
        -jar my-microservice-1.0.0-RELEASE.jar
SuccessExitStatus=143
StandardOutput=journal
StandardError=journal
KillSignal=SIGINT
TimeoutStopSec=20
Restart=always
RestartSec=5
StartLimitInterval=0
StartLimitBurst=10
SyslogIdentifier=my-microservice

[Install]
WantedBy=multi-user.target

我的项目的pom.xml文件是:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.teamknowlogy.squint</groupId>
    <artifactId>my-microservice</artifactId>
    <version>1.0.0-RELEASE</version>
    <name>my-microservice</name>
    <description>...</description>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
        <spring-session.version>1.3.5.RELEASE</spring-session.version>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <!-- The main class to start by executing "java -jar" -->
        <start-class>com.acme.MyMicroserviceApplication</start-class>
        <spring.boot.mainclass>com.acme.MyMicroserviceApplication</spring.boot.mainclass>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-aws</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-loader -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-loader</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <artifactId>auth</artifactId>
            <groupId>software.amazon.awssdk</groupId>
        </dependency>
        <dependency>
            <artifactId>aws-core</artifactId>
            <groupId>software.amazon.awssdk</groupId>
        </dependency>
        <dependency>
            <artifactId>s3</artifactId>
            <groupId>software.amazon.awssdk</groupId>
        </dependency>
        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>23.0</version>
        </dependency>
        <dependency>
            <groupId>net.sourceforge.htmlunit</groupId>
            <artifactId>htmlunit</artifactId>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.jsonwebtoken</groupId>
            <artifactId>jjwt</artifactId>
            <version>0.9.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.jsoup/jsoup -->
        <dependency>
            <groupId>org.jsoup</groupId>
            <artifactId>jsoup</artifactId>
            <version>1.12.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.commons/commons-pool2 -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- https://mvnrepository.com/artifact/software.amazon.awssdk/bom -->
            <dependency>
                <groupId>software.amazon.awssdk</groupId>
                <artifactId>bom</artifactId>
                <version>2.6.5</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- https://spring.io/projects/spring-session -->
            <dependency>
                <groupId>org.springframework.session</groupId>
                <artifactId>spring-session-bom</artifactId>
                <version>Bean-SR3</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
        </resources>
        <!-- <sourceDirectory>src</sourceDirectory> -->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <skipTests>true</skipTests>
                    <source>1.8</source>
                    <target>1.8</target>
                    <executable>true</executable>
                    <mainClass>${spring.boot.mainclass}</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                        <configuration>
                            <mainClass>${spring.boot.mainclass}</mainClass>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

Java版本:

$ /usr/bin/java -version
openjdk version "1.8.0_201"
OpenJDK Runtime Environment (build 1.8.0_201-b09)
OpenJDK 64-Bit Server VM (build 25.201-b09, mixed mode)

亚马逊操作系统实例的版本:

$ cat /etc/*release
NAME="Amazon Linux"
VERSION="2"
ID="amzn"
ID_LIKE="centos rhel fedora"
VERSION_ID="2"
PRETTY_NAME="Amazon Linux 2"
ANSI_COLOR="0;33"
CPE_NAME="cpe:2.3:o:amazon:amazon_linux:2"
HOME_URL="https://amazonlinux.com/"
Amazon Linux release 2 (Karoo)

共有1个答案

呼延宪
2023-03-14

在单元文件环境配置中使用 shell 参数扩展 ($):

EnvironmentFile=/etc/sysconfig/my-microservice
Environment="JAVA_HOME=$(dirname $(dirname $(readlink $(readlink $(which java)))))"
Environment="PATH=$PATH:$JAVA_HOME/bin"
Environment="CLASSPATH=.:$JAVA_HOME/jre/lib:$JAVA_HOME/lib"
Environment="SPRING_PROFILES_ACTIVE=pro"
Environment="JAVA_OPTS=-Djava.security.egd=file:/dev/./urandom -Dspring.profiles.active=pro -server -d64 -Xms256m -Xmx2048m -Xss256m -XX:+UseParallelGC -XX:MaxHeapSize=2048m -XX:MaxMetaspaceSize=1024m -XX:MaxHeapFreeRatio=75 -XX:+HeapDumpOnOutOfMemoryError"

Systemd不支持shell参数扩展:

Environment=为执行的进程设置环境变量。采用以空格分隔的变量赋值列表。此选项可以指定多次,在这种情况下,将设置所有列出的变量。如果将同一变量设置两次,则后面的设置将覆盖前面的设置。如果将空字符串分配给此选项,则会重置环境变量列表,之前的所有分配都无效。字符串内不执行变量扩展,但可以扩展说明符。$字符没有特殊含义。如果需要将包含空格或等号的值赋给变量,请使用双引号(“)进行赋值。

我建议在应用程序的普通< code>setenv.sh文件中定义环境变量。

 类似资料:
  • 我有一个spring boot可执行罐,装在数字海洋水滴中。我可以使用现在我想让我作为一个服务运行。 我已经创建了文件/etc/systemd/system/myapp。用这些内容服务 然后使其在系统启动时启动 我正在尝试启动服务 但是我得到了这个错误 无法启动myapp.service:未知单位:myapp.service详情请参阅系统日志和'systemctl statusmyapp.serv

  • 我希望payara服务器作为服务运行。我以sudo的身份登录asadmin,并使用create-service命令。给出了以下输出。 这将在 /etc/init.d/ 文件夹中创建payara_production脚本,但一旦重新启动计算机,就不会执行此脚本。我必须手动启动payara才能运行它。 “您已经创建了服务,但是您需要自己启动它”是什么意思,我在之前使用的GlassFish版本中没有类似

  • 问题内容: 我最近将计算机从Ubuntu升级到。我面临使用服务即服务的问题。我安装 使用: 现在命令显示了这个结果: 尽管我的机器上安装了Java,但是我可以使用此命令启动服务器。 我有点卡在这里。任何帮助将不胜感激。 编辑 设置后根的错误: 问题答案: 我找到了解决此问题的方法。解决方案来自该讨论线程- 无法在Elastic网站上使用Ubuntu 16.04启动Elasticsearch 。 似

  • 我很久以前做了一些自定义systemd服务,都有相同的配置(当然ExecStart除外) 这种配置已经运行了很多年,从18.04版LTS开始,我已经启动并运行了ubuntu,但是现在看起来有些systemd服务根本没有启动,配置如下(myapp.service): 服务已启用: 如果在重新启动后执行“systemctl status myapp”: 如果我在重新启动后执行“Journal alct

  • 问题内容: 码头工人给我一个困难时期。我按照以下说明进行操作,以便在运行由strato.de托管的Ubuntu 14.04的虚拟服务器上安装docker。 执行此行直接将我带入此错误消息: 安装完成后,我安装了上述两个软件包。现在我的问题是我无法让docker运行。 结果是: 结果是 和 结果是 有人对缺少哪些依赖项有任何线索吗?还有什么可能出问题了?码头工人是否提供任何日志? 我正在来回寻找解决

  • 当收到来自服务器的数据报文时,显示错误日志: