简体   繁体   English

Spring Boot配置覆盖被忽略

[英]Spring Boot configuration overrides are being ignored

I have written a number of services that communicate via REST API calls. 我编写了许多通过REST API调用进行通信的服务。 The services can be configured to use HTTP or HTTPS. 可以将服务配置为使用HTTP或HTTPS。 Any given client has security configuration that defines the connection to a server. 任何给定的客户端都具有定义与服务器的连接的安全性配置。 "Default" configuration properties are set by values in application.yml and this has worked well up to this point. “默认”配置属性由application.yml的值设置,并且到目前为止效果良好。

I've come to realize, however, that this does not work for a more realistic situation. 但是,我已经意识到,这对于更现实的情况不起作用。 The problem is that I'm attempting to set specific arguments, like a server host/port when a client is launched, and the values I'm setting are being ignored. 问题是我试图设置特定的参数,例如启动客户端时的服务器主机/端口,而忽略我设置的值。

An example: 一个例子:

Service A (client) will communicate with Service B (server) for some purpose. 服务A(客户端)将出于某种目的与服务B(服务器)通信。 Service A has security configuration set up like the following: 服务A的安全配置如下设置:

@Configuration
@EnableConfigurationProperties({ClientConnectionProperties.class,
    SecurityCertificateProperties.class})
public class ClientSecurityConfiguration {
  ...
}

@ConfigurationProperties("client.connection")
public class ClientConnectionProperties {
  ...
}

@ConfigurationProperties("server.ssl")
public class SecurityCertificateProperties {
  ...
}

application.yml has defnitions for the various properties that are assigned to values in the configuration objects. application.yml具有定义给配置对象中的值的各种属性的定义。

client:
  connection:
    scheme: https
    host: localhost
    port: 8443

server:
  port: 7081
  ssl:
    enabled: true
    protocol: TLS
    trust-store-type: JKS
    trust-store: classpath:server.truststore
    trust-store-password: <password>
    key-store-type: JKS
    key-store: classpath:server.keystore
    key-store-password: <password>

This is all well and good when both client and server are running on localhost. 当客户端和服务器都在本地主机上运行时,这一切都很好。 However, I'm testing running them where they reside on different hosts, so I need to override client.connection.host from localhost to the actual host. 但是,我正在测试将它们运行在它们驻留在不同主机上的位置,因此我需要将client.connection.host从本地主机覆盖到实际主机。 I do this by specifying -Dclient.connection.host=<host> as a VM argument when launching the client. 我通过在启动客户端时将-Dclient.connection.host=<host>作为VM参数来执行此操作。 Unfortunately, this does not work. 不幸的是,这不起作用。 The client ends up trying to connect to localhost and fails (for obvious reasons). 客户端最终尝试连接到localhost并失败(出于明显的原因)。

How can I get my override values set into these configuration items? 如何将我的替代值设置到这些配置项中? Is there a way to defer or delay the "default" loading so that my values take effect? 有没有办法推迟或延迟“默认”加载,使我的值生效? Is there some other technique to use to get them in there? 还有其他一些技巧可以使它们进入那里吗?

You can override Spring Boot properties by using Spring arguments instead of JVM arguments. 您可以使用Spring参数而不是JVM参数来覆盖Spring Boot属性。

Example: 例:

java -jar your-app.jar --client.connection.host=<host>

Related question: https://stackoverflow.com/a/37053004/12355118 相关问题: https : //stackoverflow.com/a/37053004/12355118

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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