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

后端 未结 1 1404
陌清茗
陌清茗 2021-01-13 05:45

There is good question about ability to add multiple tomcat connectors and bind them to separate controllers each.

The essense of Andy Wilkinson\'s good answer is he

相关标签:
1条回答
  • 2021-01-13 06:01

    You can achieve your first suggestion using 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);
    }
    

    You can then use child1{-profile}.properties and child2{-profile}.properties to configure service one and service two respectively.

    For example, with server.port=8081 in child1.properties and server.port=8082 in child2.properties, you should see output similar to the following when starting an app that's using auto-configuration in the two child services and has a dependency on 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)
    
    0 讨论(0)
提交回复
热议问题