【问题标题】:Launching two spring boot apps at the same time using maven使用 maven 同时启动两个 Spring Boot 应用程序
【发布时间】:2019-12-19 09:18:58
【问题描述】:

我有两个带有自己的 application.properties 的 Spring Boot 应用程序。

一个在我的src/main/resources 中为application.properties,另一个在src/test/resources 中为test.properties

我想要它,以便当我在 main 中启动我的 spring boot 应用程序时,它也会同时在我的测试中启动 spring boot 应用程序。我想在 Maven 命令行中执行此操作,但我也想知道是否也可以在 Spring Boot 中执行此操作,也许以编程方式?

Spring Boot 应用程序在 localhost 上运行,但有两个不同的端口。

主要的一个在localhost:28433 上运行,另一个在localhost:9119 上运行。

我试过运行这个 mvn 命令

mvn spring-boot:run -Drun.arguments="--server.port=9119""--server.port=28433"

我不确定这是否有效.. 当我在终端中运行它时,它会输出:

t.TomcatWebServer  : Tomcat initialized with port(s): 28433 (http)
2019-08-12 15:25:51.641  INFO 37469 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2019-08-12 15:25:51.641  INFO 37469 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/9.0.12
2019-08-12 15:25:51.649  INFO 37469 --- [           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/asluborski/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.]
2019-08-12 15:25:51.707  INFO 37469 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-08-12 15:25:51.707  INFO 37469 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 934 ms
2019-08-12 15:25:51.731  INFO 37469 --- [           main] o.s.b.w.servlet.ServletRegistrationBean  : Servlet messageDispatcherServlet mapped to [/nulogix/ws/*]
2019-08-12 15:25:51.732  INFO 37469 --- [           main] o.s.b.w.servlet.ServletRegistrationBean  : Servlet dispatcherServlet mapped to [/]
2019-08-12 15:25:51.735  INFO 37469 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-08-12 15:25:51.735  INFO 37469 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-08-12 15:25:51.735  INFO 37469 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'formContentFilter' to: [/*]
2019-08-12 15:25:51.736  INFO 37469 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2019-08-12 15:25:51.760 DEBUG 37469 --- [           main] c.n.b.service.PredictionEngineService    : billing.engine.address=127.0.0.1
2019-08-12 15:25:51.760 DEBUG 37469 --- [           main] c.n.b.service.PredictionEngineService    : billing.engine.port=9119
2019-08-12 15:25:51.760 DEBUG 37469 --- [           main] c.n.b.service.PredictionEngineService    : Using http://127.0.0.1:9119
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by com.sun.xml.bind.v2.runtime.reflect.opt.Injector (file:/Users/asluborski/.m2/repository/com/sun/xml/bind/jaxb-impl/2.2.11/jaxb-impl-2.2.11.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int)
WARNING: Please consider reporting this to the maintainers of com.sun.xml.bind.v2.runtime.reflect.opt.Injector
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
2019-08-12 15:25:52.037 DEBUG 37469 --- [           main] c.n.billing.ws.endpoint.AnalyzeEndPoint  : billing.engine.api.version=0.97
2019-08-12 15:25:52.038 DEBUG 37469 --- [           main] c.n.billing.ws.endpoint.AnalyzeEndPoint  : billing.engine.core.version=0.97
2019-08-12 15:25:52.038 DEBUG 37469 --- [           main] c.n.billing.ws.endpoint.AnalyzeEndPoint  : billing.engine.core.name=Nulogix_Patient_Responsibility
2019-08-12 15:25:52.039 DEBUG 37469 --- [           main] c.n.b.ws.endpoint.GetVersionEndPoint     : billing.engine.api.version=0.97
2019-08-12 15:25:52.184  INFO 37469 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-08-12 15:25:52.398  INFO 37469 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 28433 (http) with context path ''
2019-08-12 15:25:52.402  INFO 37469 --- [           main] com..billing.App                  : Started App in 1.995 seconds (JVM running for 9.911)

上面说它正在使用127.0.0.1:9119,但它似乎只启动我的主应用程序而不是我的测试应用程序,它专门称为mockServerApp,所以我认为它没有做我想做的事......

如何同时启动这些?

【问题讨论】:

    标签: java maven spring-boot tomcat server


    【解决方案1】:

    我不知道 mockServerApp 的用途,但我认为 bootRun 不能处理多个应用程序,甚至不会扫描您的测试类。

    如果您想模拟外部依赖项以进行单元测试,您应该将调用包装在某种代理类中并使用@MockBean 注入模拟实例。

    如果您想为开发目的部署存根外部依赖项,您可以为存根创建一个单独的项目作为父目录,该目录将在启动时调用 maven。您甚至可以在不同的框架或语言中实现模拟。

    最直接的选择是编写一个launches and kills both services 的shell 脚本。

    为了指定端口,在每个的 application.properties 中将 server.port 设置为环境变量:

    主应用程序.conf:

    server.port=${MY_APP_PORT:28433}
    

    模拟应用程序.conf:

    server.port=${MOCK_APP_PORT:9119}
    

    要在集成测试中启动外部依赖项,请查看Testcontainers

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-19
      • 2021-06-06
      • 1970-01-01
      • 1970-01-01
      • 2018-08-31
      • 2019-02-22
      相关资源
      最近更新 更多