简体   繁体   中英

Using external properties file with Springs as init.d service

I am trying to run a Spring-Boot application as a init.d linux service using this method provided by Spring. While the service will successfully run with

service vxmonitor start RUN_ARGS=--spring.config.location=/var/vxmonitor/config.properties

the external properties file will not load. The properties file is located in the same folder as the jar file. If I run the application using

java -jar monitor-0.0.1-SNAPSHOT.jar --spring.config.location=config.properties

the external properties loads fine.

My question is how to get the Springs service to point to the external config file, and if that is possible, how to integrate the location of the external config file into the service itself so RUN_ARGS doesnt need to be specified. Not sure what files would be helpful to help solve this problem.

config.properties

server.port=8099

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>

<groupId>com.valgoix</groupId>
<artifactId>monitor</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>vlgx-monitoring-svc</name>
<description>Client side server monitoring web service</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.2.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-websocket</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.restdocs</groupId>
        <artifactId>spring-restdocs-mockmvc</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.2-1003-jdbc4</version>
    </dependency>
</dependencies>

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

If there is another file needed please tell me. Thank you

EDIT: I attempted to modify the service created by changing

arguments=(-Dsun.misc.URLClassPath.disableJarChecking=true $JAVA_OPTS -jar "$jarfile" $RUN_ARGS "$@")

to

arguments=(-Dsun.misc.URLClassPath.disableJarChecking=true $JAVA_OPTS -jar "$jarfile" --spring.config.location=/var/vxmonitor/config.properties $RUN_ARGS "$@")

except when I run the service I get the error

Error: Invalid or corrupt jarfile /var/vxmonitor/vxmonitor.jar

written in the log file.

您需要对spring.config.location使用以下文件URL:

--spring.config.location=file:///var/vxmonitor/config.properties

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM