マルチコンテキストのスプリング・ブートアプリケーション:それぞれの子コンテキストのための標準的な春ブートプロパティを定義する方法

ルスランStelmachenko:

良いがある質問複数の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);      
}

私はこのfutherに行くと、別の質問をしたいです:

プロファイルサフィックスファイル名などのように、すべての標準的な春ブートの機能のサポートにより、いくつかの子の特定のアプリケーションのプロパティを読み取るために、各子アプリケーションコンテキストを作成する方法はあります

それを達成するための1つの仮想的な方法:

3つの別々のプロパティファイルを持っています:

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

そして、コンテキストごとに一つのファイルを設定する機能を持っています。

しかし、彼らはすべての春ブート魔法をサポートしなければならないことを、私には重要なものです。私は、コマンドライン引数を渡すように新しいプロファイルを有効にした場合たとえば、--spring.profiles.active=dev、その後、自動的にこれらの3つのファイルが(各コンテキストの1)にロードされなければならないだけでなく、(存在する場合)、ファイルの別のセットも自動的にロードする必要があります。

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

もちろん、これらのファイルは、定義された順序などで標準のスプリング・ブートのサポートパスで検索する必要があります

いくつかのコーディングで多分の箱ならばそれは可能出ますか?

それを達成するためのもう一つの仮説的な方法

一つだけ持っているapplication.properties(プロファイルなどをサポートする)ファイルを、何とか子コンテキストの接頭辞プロパティに接頭辞プロパティをマップします。

例:

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

彼らはちょうどだった場合は、最初の子コンテキストは次のようにこれらのプロパティが表示されます。

server.port=8081
foo=bar

彼らはちょうどだった場合に、第2子コンテキストは次のようにこれらのプロパティが表示されます。

server.port=8082
foo=baz

だから、標準のスプリングブーツのautoconfigurationsが動作すると、正しくTomcatのポートなどを設定します


私は特定のアプローチを捜しているわけではない(しかし、あなたは私に言わせれば、私は第二のアプローチに傾いています)が、任意の最小apporachが私を十分でしょうcondingでアウトオブボックスまたは達成できる作業します。

アンディ・ウィルキンソン:

あなたは使用して最初の提案を達成することができます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}.propertiesし、child2{-profile}.propertiesそれぞれのconfigureサービス1、サービス2に。

例えば、とserver.port=8081child1.propertiesserver.port=8082child2.properties2つのつの子サービスで自動構成を使用し、上の依存関係を持っていますアプリを起動すると、次のような出力が表示されます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)

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=211668&siteId=1