java - 使用maven同时启动两个spring boot应用程序

标签 java maven spring-boot tomcat server

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

一个在我的 src/main/resources 中作为 application.properties 另一个在 src/中作为 test.properties测试/资源

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

spring boot 应用程序在 localhost 但两个不同的端口上运行。

main 中的一个在 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 所以我不要以为它在做我想做的...

如何同时启动这些?

最佳答案

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

如果你想模拟一个外部依赖用于单元测试,你应该将调用包装在某种代理类中并使用@MockBean 来注入(inject)一个模拟实例。

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

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

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

主要应用程序.conf:

server.port=${MY_APP_PORT:28433}

模拟应用程序.conf:

server.port=${MOCK_APP_PORT:9119}

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

关于java - 使用maven同时启动两个spring boot应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57467202/

相关文章:

java - Java中nio HTTP解压

java - @RequestParam 带 swagger ui 注释的方法不显示描述

java - 打开 Whisper Systems Signal Build Maven 构建错误

docker - Spring Cloud 服务查找错误 : Load balancer does not have available server for client

java - mvn spring-boot :run slow compared to java -jar xxx. jar

java - Spring 中的 Restful 服务导致 400 Bad request

java - 如果地址是别名,如何使用 ews-java-api 识别收件人电子邮件?

java - 如何在java项目中使用commited库

java - 由于 API 不兼容,sonar/jacoco 插件失败

java - 为什么 Spring Boot WebClient OAuth2 (client_credentials) 要求为每个请求提供新 token ?