簡體   English   中英

多上下文 spring-boot 應用程序:如何為每個子上下文定義標准的 spring-boot 屬性

[英]Multi-context spring-boot application: how to define standard spring-boot properties for each child context

關於添加多個 tomcat 連接器並將它們綁定到每個單獨的控制器的能力有一個很好的問題

安迪威爾金森的好答案的本質在這里:

public static void main(String[] args) {
    SpringApplicationBuilder parentBuilder 
            = new SpringApplicationBuilder(ApplicationConfiguration.class);
    parentBuilder.child(ServiceOneConfiguration.class)
            .properties("server.port:8080").run(args);
    parentBuilder.child(ServiceTwoConfiguration.class)
            .properties("server.port:8081").run(args);      
}

我想繼續下去並問另一個問題:

有沒有辦法讓每個子應用程序上下文在支持所有標准 spring-boot 的功能(如配置文件后綴文件名等)的情況下讀取一些特定於子應用程序的屬性。

實現它的一種假設方法:

擁有三個獨立的屬性文件:

  • application.properties
  • child1.properties
  • child2.properties

並且能夠為每個上下文設置一個文件。

但對我來說重要的是,它們必須支持所有 spring-boot 魔法。 例如,如果我像傳遞命令行參數一樣激活新配置文件--spring.profiles.active=dev ,那么不僅應該自動加載這三個文件(每個上下文一個),而且還應該加載另一組文件(如果它們存在)也應該自動加載:

  • application-dev.properties
  • child1-dev.properties
  • child2-dev.properties

當然,這些文件應該按照定義的順序在標准 spring-boot 支持的路徑中搜索。

如果盒子里有一些編碼,是否有可能?

實現它的另一種假設方式

只有一個application.properties文件(支持配置文件等),但以某種方式將前綴屬性映射到子上下文的無前綴屬性。

例子:

child1.server.port=8081
child1.foo=bar
child2.server.port=8082
child2.foo=baz

第一個子上下文應該看到這些屬性,就像它們只是:

server.port=8081
foo=bar

第二個子上下文應該看到這些屬性,就像它們只是:

server.port=8082
foo=baz

因此,標准 spring-boot 的自動配置將工作並正確設置 tomcat 的端口等。


我不是在尋找特定的方法(但如果你問我,我傾向於第二種方法),但任何開箱即用的工作或可實現的最小 conding apporach 就足夠了。

您可以使用spring.config.name實現您的第一個建議:

public static void main(String[] args) {
    SpringApplicationBuilder parentBuilder =
            new SpringApplicationBuilder(ParentApplication.class)
                    .web(WebApplicationType.NONE);
    parentBuilder.run(args);
    parentBuilder.child(ServiceOneConfiguration.class)
            .properties("spring.config.name=child1").run(args);
    parentBuilder.child(ServiceTwoConfiguration.class)
            .properties("spring.config.name=child2").run(args);
}

然后可以使用child1{-profile}.propertieschild2{-profile}.properties分別配置服務一和服務二。

例如,使用server.port=8081 in child1.propertiesserver.port=8082 in child2.properties ,當啟動一個在兩個子服務中使用自動配置並具有依賴項的應用程序時,您應該看到類似於以下的輸出在spring-boot-starter-web

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.2.RELEASE)

2019-01-22 13:38:09.690  INFO 80968 --- [           main] com.example.parent.ParentApplication     : Starting ParentApplication on …
2019-01-22 13:38:09.692  INFO 80968 --- [           main] com.example.parent.ParentApplication     : No active profile set, falling back to default profiles: default
2019-01-22 13:38:09.842  INFO 80968 --- [           main] com.example.parent.ParentApplication     : Started ParentApplication in 0.371 seconds (JVM running for 0.644)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.2.RELEASE)

2019-01-22 13:38:10.046  INFO 80968 --- [           main] com.example.parent.ParentApplication     : No active profile set, falling back to default profiles: default
2019-01-22 13:38:10.584  INFO 80968 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
2019-01-22 13:38:10.604  INFO 80968 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-01-22 13:38:10.605  INFO 80968 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.14]
2019-01-22 13:38:10.613  INFO 80968 --- [           main] o.a.catalina.core.AprLifecycleListener   : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [/Users/awilkinson/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2019-01-22 13:38:10.668  INFO 80968 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-01-22 13:38:10.668  INFO 80968 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 612 ms
2019-01-22 13:38:10.829  INFO 80968 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-01-22 13:38:10.981  INFO 80968 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path ''
2019-01-22 13:38:10.981  INFO 80968 --- [           main] com.example.parent.ParentApplication     : Started ParentApplication in 0.955 seconds (JVM running for 1.784)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.2.RELEASE)

2019-01-22 13:38:11.003  INFO 80968 --- [           main] com.example.parent.ParentApplication     : No active profile set, falling back to default profiles: default
2019-01-22 13:38:11.093  INFO 80968 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8082 (http)
2019-01-22 13:38:11.095  INFO 80968 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-01-22 13:38:11.096  INFO 80968 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.14]
2019-01-22 13:38:11.100  INFO 80968 --- [           main] o.a.c.c.C.[Tomcat-1].[localhost].[/]     : Initializing Spring embedded WebApplicationContext
2019-01-22 13:38:11.101  INFO 80968 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 97 ms
2019-01-22 13:38:11.135  INFO 80968 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-01-22 13:38:11.164  INFO 80968 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8082 (http) with context path ''
2019-01-22 13:38:11.165  INFO 80968 --- [           main] com.example.parent.ParentApplication     : Started ParentApplication in 0.183 seconds (JVM running for 1.967)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM