【问题标题】:Spring Boot with spring-boot-maven plug-in - unable to RESTART the application server after running integration tests带有 spring-boot-maven 插件的 Spring Boot - 运行集成测试后无法重新启动应用程序服务器
【发布时间】:2019-01-07 20:16:22
【问题描述】:

我有一个 Spring Boot 应用程序,并且该应用程序具有集成测试。

这是我的 pom.xml 中用于运行集成测试的相关 sn-p。

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
      <mainClass>com.xyz.Application</mainClass>
      <executable>true</executable>
      <fork>true</fork>
      <jmxPort>7654</jmxPort>
    </configuration>
    <executions>
      <execution>
        <id>pre-integration-test</id>
        <goals>
          <goal>start</goal>
        </goals>
        <configuration>
          <fork>true</fork>
          <jmxPort>7654</jmxPort>
        </configuration>
      </execution>
      <execution>
        <id>post-integration-test</id>
        <goals>
          <goal>stop</goal>
        </goals>
        <configuration>
          <fork>true</fork>
          <jmxPort>7654</jmxPort>
        </configuration>
      </execution>
    </executions>
  </plugin>

当我运行mvn verify 时,我看到单元测试首先运行。然后应用服务器启动并且集成测试成功运行。 mvn 进程成功退出。

我还在控制台日志中看到以下内容,这让我相信应用程序服务器正在正确关闭 -

[INFO] --- spring-boot-maven-plugin:2.1.1.RELEASE:stop (post-integration-test) @ application ---
[DEBUG] Configuring mojo org.springframework.boot:spring-boot-maven-plugin:2.1.1.RELEASE:stop from plugin realm ClassRealm[plugin>org.springframework.boot:spring-boot-maven-plugin:2.1.1.RELEASE, parent: sun.misc.Launcher$AppClassLoader@42a57993]
[DEBUG] Configuring mojo 'org.springframework.boot:spring-boot-maven-plugin:2.1.1.RELEASE:stop' with basic configurator -->
[DEBUG]   (f) fork = true
[DEBUG]   (f) jmxPort = 7654
[DEBUG]   (f) project = MavenProject: com.xyz:application:1.0-SNAPSHOT @ /Users/me/code/my-project/pom.xml
[DEBUG]   (f) skip = false
[DEBUG] -- end configuration --
[INFO] Stopping application...
2019-01-07 14:18:18.375  INFO [-,,,] 38653 --- [on(4)-127.0.0.1] inMXBeanRegistrar$SpringApplicationAdmin : Application shutdown requested.
2019-01-07 14:18:18.375  INFO [-,,,] 38653 --- [on(4)-127.0.0.1] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@449465bf: startup date [Mon Jan 07 14:17:22 EST 2019]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@475a6eb6
2019-01-07 14:18:18.376  INFO [-,,,] 38653 --- [on(4)-127.0.0.1] ConfigServletWebServerApplicationContext : Closing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4cf0901e: startup date [Mon Jan 07 14:17:30 EST 2019]; parent: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@449465bf
2019-01-07 14:18:18.385  INFO [-,,,] 38653 --- [on(4)-127.0.0.1] o.e.jetty.server.AbstractConnector       : Stopped ServerConnector@e45db77{HTTP/1.1,[http/1.1]}{0.0.0.0:8081}
2019-01-07 14:18:18.385  INFO [-,,,] 38653 --- [on(4)-127.0.0.1] org.eclipse.jetty.server.session         : node0 Stopped scavenging
2019-01-07 14:18:18.387  INFO [-,,,] 38653 --- [on(4)-127.0.0.1] o.e.j.s.h.ContextHandler.application     : Destroying Spring FrameworkServlet 'dispatcherServlet'
2019-01-07 14:18:18.388  INFO [-,,,] 38653 --- [on(4)-127.0.0.1] o.e.jetty.server.handler.ContextHandler  : Stopped o.s.b.w.e.j.JettyEmbeddedWebAppContext@16b7a081{application,/,[file:///private/var/folders/sc/d2x30b2j6jj5hpvfkqx9p31n5jmbmc/T/jetty-docbase.6078950105283659171.8081/, jar:file:/Users/me/.m2/repository/io/springfox/springfox-swagger-ui/2.9.2/springfox-swagger-ui-2.9.2.jar!/META-INF/resources],UNAVAILABLE}
2019-01-07 14:18:18.390  INFO [-,,,] 38653 --- [on(4)-127.0.0.1] o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 2147483647
2019-01-07 14:18:18.393  INFO [-,,,] 38653 --- [on(4)-127.0.0.1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
2019-01-07 14:18:18.393  INFO [-,,,] 38653 --- [on(4)-127.0.0.1] o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans
2019-01-07 14:18:18.403  INFO [-,,,] 38653 --- [on(4)-127.0.0.1] s.c.a.AnnotationConfigApplicationContext : Closing FeignContext-cse-service: startup date [Mon Jan 07 14:17:27 EST 2019]; parent: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@449465bf
...
2019-01-07 14:18:18.472  WARN [-,,,] 38653 --- [pool-1-thread-5] c.n.c.sources.URLConfigurationSource     : No URLs will be polled as dynamic configuration sources.
2019-01-07 14:18:18.472  INFO [-,,,] 38653 --- [pool-1-thread-5] c.n.c.sources.URLConfigurationSource     : To enable URLs as dynamic configuration sources, define System property archaius.configurationSource.additionalUrls or make config.properties available on classpath.
2019-01-07 14:18:18.473  INFO [-,,,] 38653 --- [pool-1-thread-5] c.netflix.config.DynamicPropertyFactory  : DynamicPropertyFactory is initialized with configuration sources: com.netflix.config.ConcurrentCompositeConfiguration@139bf1b8
...
2019-01-07 14:18:18.628  INFO [-,,,] 38653 --- [on(4)-127.0.0.1] o.e.jetty.server.AbstractConnector       : Stopped ServerConnector@678b05be{HTTP/1.1,[http/1.1]}{0.0.0.0:8080}
2019-01-07 14:18:18.628  INFO [-,,,] 38653 --- [on(4)-127.0.0.1] org.eclipse.jetty.server.session         : node0 Stopped scavenging
2019-01-07 14:18:18.629  INFO [-,,,] 38653 --- [on(4)-127.0.0.1] o.e.j.s.h.ContextHandler.application     : Destroying Spring FrameworkServlet 'dispatcherServlet'
2019-01-07 14:18:18.629  INFO [-,,,] 38653 --- [on(4)-127.0.0.1] o.e.jetty.server.handler.ContextHandler  : Stopped o.s.b.w.e.j.JettyEmbeddedWebAppContext@4808c18e{application,/api/v1,[file:///private/var/folders/sc/d2x30b2j6jj5hpvfkqx9p31n5jmbmc/T/jetty-docbase.6019952027552218513.8080/, jar:file:/Users/me/.m2/repository/io/springfox/springfox-swagger-ui/2.9.2/springfox-swagger-ui-2.9.2.jar!/META-INF/resources],UNAVAILABLE}

随后,当我再次运行mvn verify 时,我得到以下绑定异常。

[DEBUG] Application argument(s): 
[DEBUG] Connecting to local MBeanServer at port 7654
[DEBUG] Waiting for spring application to start...
[DEBUG] Connected to local MBeanServer at port 7654
[DEBUG] Waiting for spring application to start...
[DEBUG] Spring application is not ready yet, waiting 500ms (attempt 1)
Error: Exception thrown by the agent : java.rmi.server.ExportException: Port already in use: 7654; nested exception is: 
        java.net.BindException: Address already in use (Bind failed)

使用netstatps 我可以确定有一些进程仍在占用端口7654,jps 告诉我是这个进程 -

47148 Application -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=7654 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=127.0.0.1 

我不知道如何让 JMX 进程关闭。我认为spring-boot-maven 插件可以做到这一点。而事实似乎并非如此。

我做错了什么,有人可以帮忙吗?非常感谢您花时间研究这个(潜在的)问题。

【问题讨论】:

  • 您是否忘记了某个旧会话?换句话说,它在重新启动后是否有效?
  • 如果我终止正在运行的进程 (kill &lt;PID&gt;) 然后调用 mvn verify 我没有得到绑定异常。我期望不必手动终止此进程,我的假设是spring-boot-maven 将作为其stop 目标的一部分来执行此操作。我的假设不正确吗? FWIW,我在 macOS Mojave 上使用我的雇主允许的最新补丁。 (我这么说的唯一原因是因为我不确定这个问题是否可能是特定于操作系统的问题。)
  • 用 visualvm 看看是否有 Spring 不知道的非守护线程。
  • 在 VisualVM 中,所有产生的进程都被关闭,除了一个名为 Application 的类,用 @SpringBootApplication 注释。传递给它的参数是--spring.application.admin.enabled=true --spring.application.admin.jmx-name=org.springframework.boot:type=Admin,name=SpringApplication。这个进程还有如下JMXJVM参数-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=7654 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=127.0.0.1
  • 此过程在应用服务器启动以运行集成测试时启动。这也是在随后调用mvn verify 时也会导致绑定异常的过程。我的机器上没有运行其他进程。

标签: java maven spring-boot jmx spring-boot-maven-plugin


【解决方案1】:

我遇到了同样的问题,在终止 JMX 进程之前无法重新运行测试。其次,我的 Azure DevOps 管道因为 JMX 进程没有停止而挂起。

The STDIO streams did not close within 10 seconds of the exit event from process '/usr/share/apache-maven-3.6.3/bin/mvn'. This may indicate a child process inherited the STDIO streams and has not yet exited.

当我浏览源代码:https://github.com/spring-projects/spring-boot/blob/master/spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/StartMojo.java#L128时,我发现如果我不 fork maven 进程,JMX 代理将无法启动。

我不确定这是否适用于你,但我本身不需要分叉,也不需要 JMX 指标,所以这为我解决了这个问题:

<configuration>
  <fork>false</fork>
</configuration>

我还尝试通过在插件配置中指定 jmx 参数 (-Dcom.sun.management.jmxremote) 来覆盖它们,但由于参数被添加(硬编码)作为最终参数,这对我不起作用.

【讨论】:

  • 好吧,我感激不尽。现在我只需要找出他们为什么这样设计它
猜你喜欢
  • 2022-08-21
  • 1970-01-01
  • 2019-05-02
  • 1970-01-01
  • 2020-11-05
  • 1970-01-01
  • 2017-04-14
  • 1970-01-01
  • 2020-04-25
相关资源
最近更新 更多