给出配置客户端bootstrap.yml:
spring:
...
cloud:
config:
uri: ${SPRING_CONFIG_URI:http://localhost:9000}
fail-fast: true
需要在运行时设置环境变量SPRING_CONFIG_URI
。我们可以再次讨论该代码的气味,但是对于本练习,只有在服务启动时才知道此URI(与获取随机SRV记录有关)。
是否可以通过Spring方法设置/覆盖某些bootstrap.yaml
参数?
这是我尝试过的:
@SpringBootApplication
public class Application {
public static void main( String[] args ) throws Exception
{
String configServerPort = DnsHelper.resolveSrv("configserver.local");
// This doesn't work
System.setProperty("SPRING_CONFIG_URI",configServerPort);
// This doesn't work
// Use reflection to change the in-memory environment variables
// @see https://stackoverflow.com/a/7201825/1938889
setEnv( Map.of( "SPRING_CONFIG_URI",configServerPort ) );
// After the environment variable is set,then start the service
Application.run( Application.class,args );
}
...