【问题标题】:Grails Application With SpringBoot using different yml files for different environmentsGrails Application With SpringBoot 对不同环境使用不同的 yml 文件
【发布时间】:2017-11-15 06:21:42
【问题描述】:

我正在使用 Grails 3.3.1 和 Spring Boot。构建工具是gradle。 所有应用相关的属性都在 application.yml 文件中配置。

现在我想为不同的环境(开发、测试等)访问不同的 .yml 文件。为此,我为每个环境创建了不同的 .yml 文件。 用于运行服务器的命令是:grails -Dgrails.env=test run-app 现在,当我访问任何属性时,它会按照预期为我提供 application-test.yml 文件中的值。

但是当我访问 server.port 时,它是从 application.yml 而不是 application-test.yml 读取这个属性。

谁能帮助我使用 application-test.yml 文件中的 server.port 来运行应用程序

application.yml如下:

服务器: 端口:8081

上下文路径:/ssp

application-test.yml如下:

服务器: 端口:8443

上下文路径:/ssp

我希望服务器在端口 8443 上运行,因为此属性位于 application-test.yml 中。但是服务器在 application.yml 中提到的 8081 端口上运行

【问题讨论】:

  • 端口 8443 是 https 的默认端口,执行 'grails run-app -https'。

标签: grails spring-boot gradle yaml


【解决方案1】:

我可以通过在 .yml 文件中为每个环境添加 server.port 来解决上述问题

#application.yml:

环境:

development:
    dataSource:
        dbCreate: none
        url: //url
        logSql: true
        username: //username
        password: //password
        driverClassName: "oracle.jdbc.driver.OracleDriver"
        pooled: true
        jmxExport: true
    server:
        port: 8081
        contextPath : /ssp
test:
    dataSource:
        dbCreate: none
        url: //url
        username: //username
        password: //password
        driverClassName: "oracle.jdbc.driver.OracleDriver"
        pooled: true
        jmxExport: true
        logSql: true
    server:
        port: 8082
        contextPath : /ssp

【讨论】:

    【解决方案2】:

    您可以在启动时使用--server.port-Dserver.port 直接覆盖端口

    如果你想要完全不同的YML,你可以设置spring.config.location作为参数。这是一个例子:

    # start service
    /opt/jdk/bin/java \
      -Dserver.port=<your port> \
      -jar /opt/service/<your warfile> --spring.config.location=<your YML>
    

    参考资料: How to configure port for a Spring Boot application

    https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html#howto-use-short-command-line-argumentsboot-application

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-07-12
      • 2014-07-02
      • 2015-03-27
      • 1970-01-01
      • 2020-02-22
      • 2017-11-21
      • 1970-01-01
      • 2011-01-15
      相关资源
      最近更新 更多