【问题标题】:Add additional parameters to Spring Boot app向 Spring Boot 应用程序添加附加参数
【发布时间】:2015-09-23 09:21:32
【问题描述】:

我想知道是否可以将 spring 的附加参数(例如 -Dspring.profiles.active=prod)添加到 spring boot 应用程序中,以防它作为 service 运行。

我检查了spring-boot-maven-plugin自动生成的脚本:

command="$javaexe -jar -Dsun.misc.URLClassPath.disableJarChecking=true $jarfile $@"

所以也许它可以通过 maven 插件的选项来完成,但是除了 JVM 参数之外找不到任何东西,这不是那么有用...

【问题讨论】:

    标签: spring maven service spring-boot init.d


    【解决方案1】:

    我找不到任何解决方案,包括我在问题中描述的那个 - 插件的附加参数似乎也不起作用。

    最后我使用systemd service approach解决了这个问题。

    看起来像这样并且完美运行:

    [Unit]
    Description=Some app
    After=syslog.target
    
    [Service]
    ExecStart=java -Dspring.profiles.active=production -jar /home/apps/monitoring-app-1.0.0.jar
    
    [Install]
    WantedBy=multi-user.target
    

    【讨论】:

    • 你确定这有效吗?对我来说失败了。
    • 类似“无效语法”的东西。抱歉,我不能更具体。
    • 是的,我无法启动服务
    • 应该说 java -Dspring.profiles.active=production -jar /home/apps/monitoring-app-1.0.0.jar (注意 monitoring-app-1.0.0.jar 遵循 - jar 参数)我相应地编辑了答案。
    【解决方案2】:

    例如,您可以使用外部配置文件。

    根据文档,如果您在执行的 jar 旁边的 ./config 目录中提供 application.properties 文件,则可以通过该属性文件设置活动配置文件。

    只需在./config/application.properties 中使用spring.profiles.active=myprofile

    http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html#boot-features-external-config-application-property-files

    http://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

    【讨论】:

    • 好像连jar文件里面默认的application.properties都没有覆盖,太好了。
    【解决方案3】:

    在与可执行 jar 同名的同一目录中创建一个 .conf 文件,例如

    server-1.0-SNAPSHOT.jar server-1.0-SNAPSHOT.conf
    
    JAVA_OPTS="-Xmx500m \
    -Dspring.profiles.active=myprofile"
    

    https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html#deployment-script-customization-conf-file

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-01
      • 2019-02-11
      • 1970-01-01
      • 2011-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多