Java 终止 mvn spring-boot:run 不会停止 tomcat

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23432651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:58:44  来源:igfitidea点击:

Terminating mvn spring-boot:run doesn't stop tomcat

javaspringmaventomcatspring-boot

提问by szxnyc

I can successfully start spring-boot with mvn spring-boot, the documentationmentions to gracefully exit the application hit ctrl-c.

我可以使用 成功启动 spring-boot mvn spring-boot文档提到优雅地退出应用程序 hit ctrl-c

Terminate batch job (Y/N)? Y

The maven process does terminate but Tomcat is still running and I can still hit the web-page. When I try to start spring-boot again it fails to start Tomcat because the port is in use.

maven 进程确实终止了,但 Tomcat 仍在运行,我仍然可以访问网页。当我再次尝试启动 spring-boot 时,它无法启动 Tomcat,因为该端口正在使用中。

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v1.1.0.BUILD-SNAPSHOT)

2014-05-02 12:13:57.666  INFO 6568 --- [           main] Example                                  : Starting Example on challenger with PID 6568 (E:\workspace\SpringBoot\target\cla
sses started by steven in E:\workspace\SpringBoot)
2014-05-02 12:13:57.707  INFO 6568 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWeb
ApplicationContext@11ecab7c: startup date [Fri May 02 12:13:57 EDT 2014]; root of context hierarchy
2014-05-02 12:13:58.097  INFO 6568 --- [           main] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'beanNameViewResolver': replacing [Root bean
: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfi
gure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class pat
h resource [org/springframework/boot/autoconfigure/web/ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration.class]] with [Root bean: class [null]; scope=; abstract=false; laz
yInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration$WebMvcAuto
ConfigurationAdapter; factoryMethodName=beanNameViewResolver; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfig
ure/web/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class]]
2014-05-02 12:13:58.682  INFO 6568 --- [           main] .t.TomcatEmbeddedServletContainerFactory : Server initialized with port: 8080
2014-05-02 12:13:58.892  INFO 6568 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2014-05-02 12:13:58.892  INFO 6568 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/7.0.53
2014-05-02 12:13:58.981  INFO 6568 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2014-05-02 12:13:58.981  INFO 6568 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1277 ms
2014-05-02 12:13:59.453  INFO 6568 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2014-05-02 12:13:59.455  INFO 6568 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2014-05-02 12:13:59.570 ERROR 6568 --- [           main] o.a.coyote.http11.Http11NioProtocol      : Failed to start end point associated with ProtocolHandler ["http-nio-8080"]

java.net.BindException: Address already in use: bind
        at sun.nio.ch.Net.bind0(Native Method)
        at sun.nio.ch.Net.bind(Net.java:344)
        at sun.nio.ch.Net.bind(Net.java:336)
        at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:199)
        at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
        at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:473)
        at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:647)
        at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:449)
        at org.apache.catalina.connector.Connector.startInternal(Connector.java:1007)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.StandardService.startInternal(StandardService.java:459)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:731)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.startup.Tomcat.start(Tomcat.java:341)
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:79)
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.<init>(TomcatEmbeddedServletContainer.java:69)
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getTomcatEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:270)
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:145)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:159)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:132)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:476)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:680)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:313)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:930)
        at Example.main(Example.java:16)

2014-05-02 12:13:59.571 ERROR 6568 --- [           main] o.apache.catalina.core.StandardService   : Failed to start connector [Connector[org.apache.coyote.http11.Http11NioProtocol-
8080]]

org.apache.catalina.LifecycleException: Failed to start component [Connector[org.apache.coyote.http11.Http11NioProtocol-8080]]
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
        at org.apache.catalina.core.StandardService.startInternal(StandardService.java:459)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:731)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        at org.apache.catalina.startup.Tomcat.start(Tomcat.java:341)
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:79)
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.<init>(TomcatEmbeddedServletContainer.java:69)
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getTomcatEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:270)
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:145)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:159)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:132)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:476)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:680)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:313)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:930)
        at Example.main(Example.java:16)
Caused by: org.apache.catalina.LifecycleException: service.getName(): "Tomcat";  Protocol handler start failed
        at org.apache.catalina.connector.Connector.startInternal(Connector.java:1014)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
        ... 18 common frames omitted
Caused by: java.net.BindException: Address already in use: bind
        at sun.nio.ch.Net.bind0(Native Method)
        at sun.nio.ch.Net.bind(Net.java:344)
        at sun.nio.ch.Net.bind(Net.java:336)
        at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:199)
        at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
        at org.apache.tomcat.util.net.NioEndpoint.bind(NioEndpoint.java:473)
        at org.apache.tomcat.util.net.AbstractEndpoint.start(AbstractEndpoint.java:647)
        at org.apache.coyote.AbstractProtocol.start(AbstractProtocol.java:449)
        at org.apache.catalina.connector.Connector.startInternal(Connector.java:1007)
        ... 19 common frames omitted

2014-05-02 12:13:59.572  INFO 6568 --- [           main] o.apache.catalina.core.StandardService   : Stopping service Tomcat
2014-05-02 12:13:59.580  INFO 6568 --- [           main] .b.l.ClasspathLoggingApplicationListener : Application failed to start with classpath: [file:/E:/workspace/SpringBoot/src/m
ain/resources, file:/E:/workspace/SpringBoot/src/main/resources, file:/E:/workspace/SpringBoot/target/classes/, file:/C:/Users/steven/.m2/repository/org/springframework/boot/spring
-boot-starter-web/1.1.0.BUILD-SNAPSHOT/spring-boot-starter-web-1.1.0.BUILD-SNAPSHOT.jar, file:/C:/Users/steven/.m2/repository/org/springframework/boot/spring-boot-starter/1.1.0.BUI
LD-SNAPSHOT/spring-boot-starter-1.1.0.BUILD-SNAPSHOT.jar, file:/C:/Users/steven/.m2/repository/org/springframework/boot/spring-boot/1.1.0.BUILD-SNAPSHOT/spring-boot-1.1.0.BUILD-SNA
PSHOT.jar, file:/C:/Users/steven/.m2/repository/org/springframework/boot/spring-boot-autoconfigure/1.1.0.BUILD-SNAPSHOT/spring-boot-autoconfigure-1.1.0.BUILD-SNAPSHOT.jar, file:/C:
/Users/steven/.m2/repository/org/springframework/boot/spring-boot-starter-logging/1.1.0.BUILD-SNAPSHOT/spring-boot-starter-logging-1.1.0.BUILD-SNAPSHOT.jar, file:/C:/Users/steven/.
m2/repository/org/slf4j/jcl-over-slf4j/1.7.7/jcl-over-slf4j-1.7.7.jar, file:/C:/Users/steven/.m2/repository/org/slf4j/slf4j-api/1.7.7/slf4j-api-1.7.7.jar, file:/C:/Users/steven/.m2
/repository/org/slf4j/jul-to-slf4j/1.7.7/jul-to-slf4j-1.7.7.jar, file:/C:/Users/steven/.m2/repository/org/slf4j/log4j-over-slf4j/1.7.7/log4j-over-slf4j-1.7.7.jar, file:/C:/Users/st
even/.m2/repository/ch/qos/logback/logback-classic/1.1.2/logback-classic-1.1.2.jar, file:/C:/Users/steven/.m2/repository/ch/qos/logback/logback-core/1.1.2/logback-core-1.1.2.jar, f
ile:/C:/Users/steven/.m2/repository/org/yaml/snakeyaml/1.13/snakeyaml-1.13.jar, file:/C:/Users/steven/.m2/repository/org/springframework/boot/spring-boot-starter-tomcat/1.1.0.BUILD
-SNAPSHOT/spring-boot-starter-tomcat-1.1.0.BUILD-SNAPSHOT.jar, file:/C:/Users/steven/.m2/repository/org/apache/tomcat/embed/tomcat-embed-core/7.0.53/tomcat-embed-core-7.0.53.jar, f
ile:/C:/Users/steven/.m2/repository/org/apache/tomcat/embed/tomcat-embed-el/7.0.53/tomcat-embed-el-7.0.53.jar, file:/C:/Users/steven/.m2/repository/org/apache/tomcat/embed/tomcat-e
mbed-logging-juli/7.0.53/tomcat-embed-logging-juli-7.0.53.jar, file:/C:/Users/steven/.m2/repository/com/fasterxml/Hymanson/core/Hymanson-databind/2.3.3/Hymanson-databind-2.3.3.jar, fi
le:/C:/Users/steven/.m2/repository/com/fasterxml/Hymanson/core/Hymanson-annotations/2.3.0/Hymanson-annotations-2.3.0.jar, file:/C:/Users/steven/.m2/repository/com/fasterxml/Hymanson/co
re/Hymanson-core/2.3.3/Hymanson-core-2.3.3.jar, file:/C:/Users/steven/.m2/repository/org/springframework/spring-web/4.0.3.RELEASE/spring-web-4.0.3.RELEASE.jar, file:/C:/Users/steven/
.m2/repository/org/springframework/spring-aop/4.0.3.RELEASE/spring-aop-4.0.3.RELEASE.jar, file:/C:/Users/steven/.m2/repository/aopalliance/aopalliance/1.0/aopalliance-1.0.jar, file
:/C:/Users/steven/.m2/repository/org/springframework/spring-beans/4.0.3.RELEASE/spring-beans-4.0.3.RELEASE.jar, file:/C:/Users/steven/.m2/repository/org/springframework/spring-cont
ext/4.0.3.RELEASE/spring-context-4.0.3.RELEASE.jar, file:/C:/Users/steven/.m2/repository/org/springframework/spring-core/4.0.3.RELEASE/spring-core-4.0.3.RELEASE.jar, file:/C:/Users
/steven/.m2/repository/org/springframework/spring-webmvc/4.0.3.RELEASE/spring-webmvc-4.0.3.RELEASE.jar, file:/C:/Users/steven/.m2/repository/org/springframework/spring-expression/4
.0.3.RELEASE/spring-expression-4.0.3.RELEASE.jar]
Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.context.embedde
d.EmbeddedServletContainerException: Unable to start embedded Tomcat
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:135)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:476)
[INFO] ------------------------------------------------------------------------
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120)
[INFO] BUILD SUCCESS
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:680)
[INFO] ------------------------------------------------------------------------
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:313)
[INFO] Total time: 4.653 s
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:941)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:930)
        at Example.main(Example.java:16)
Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
[INFO] Finished at: 2014-05-02T12:13:59-05:00
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:106)
[INFO] Final Memory: 16M/232M
[INFO] ------------------------------------------------------------------------
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.<init>(TomcatEmbeddedServletContainer.java:69)
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getTomcatEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:270)
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletContainerFactory.java:145)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:159)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:132)
        ... 7 more
Caused by: java.lang.IllegalStateException: Tomcat connector in failed state
        at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:102)
        ... 12 more

To proceed I have to manually terminate the running process. Is this a bug or am I missing something?

要继续,我必须手动终止正在运行的进程。这是一个错误还是我错过了什么?

回答by Dave Syer

That only happens in Windows. https://github.com/spring-projects/spring-boot/issues/773

这只发生在 Windows 中。https://github.com/spring-projects/spring-boot/issues/773

Update: should be fixed now.

更新:现在应该修复。

回答by Haim Raman

It still happens to me on version 1.1.9 running on windows 7.

在 Windows 7 上运行的版本 1.1.9 上,它仍然发生在我身上。

So after hitting Ctrl C. The fastest way to kill the background java will be .

所以在按下 Ctrl C 之后,杀死后台 java 的最快方法是 .

Find the java PID

找到java PID

     c:\>netstat -ano | find "8080"
     TCP    0.0.0.0:8080           0.0.0.0:0              LISTENING       1196
     TCP    [::]:8080              [::]:0                 LISTENING       1196
     c:\>taskkill /F /PID 1196
     SUCCESS: The process with PID 1196 has been terminated.

I am assuming in the example above that you are running on http port 8080

我在上面的例子中假设你在 http 端口 8080 上运行

For Mac Users:

对于 Mac 用户:

     $lsof -i :8080
     COMMAND   PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
     java    SOME_PID user  417u  IPv6 0xa4b3be242c249a27      0t0  TCP *:name (LISTEN)
     kill -9 SOME_PID

回答by max_dev

In IDEA you have to Stop processbefore Runit again. That command will shut down embedded Tomcat.

在 IDEA 中,您必须在再次运行之前停止进程。该命令将关闭嵌入式 Tomcat。

回答by yasha02

Type the following commands in order.

按顺序键入以下命令。

$ ps -ef | grep -i java
$ kill -9 3361

回答by Digao

Edit: in ubuntu, use the command:

编辑:在 ubuntu 中,使用命令:

fuser -k 8080/tcp

to kill the process. I'm using spring-boot 1.3.0-Build-snapshot and the problem still persist. My SO is Ubuntu and the problem occurs either running from Eclipse or from the console. The discussion about this issue is here. This seems to be an issue with spring-boot libraries, at least concerning tomcat libraries. I'm also in the line for the this fix. Unfortunately this is the price we pay for trying to use cutting edge technologies until they get matured.

杀死进程。我正在使用 spring-boot 1.3.0-Build-snapshot 并且问题仍然存在。我的 SO 是 Ubuntu,问题发生在从 Eclipse 或控制台运行时。关于这个问题的讨论是here。这似乎是 spring-boot 库的一个问题,至少与 tomcat 库有关。我也在排队进行此修复。不幸的是,这是我们尝试使用尖端技术直到它们成熟为止所付出的代价。

回答by Nailgun

Here is what I do on Mac:

这是我在 Mac 上所做的:

kill `lsof -i -n -P | grep TCP | grep 8080 | tr -s " " "\n" | sed -n 2p`

It finds the PID using 8080 and kills it.

它使用 8080 找到 PID 并杀死它。

回答by Tomasz Mularczyk

I've had this problem when running spring boot app from netbeans 8.1 on Mac. The java process was not terminated when I hit red square button in netbeans so when I relaunched the app I always get "bind exception, adress already in use" thing. This is known bug.

我在 Mac 上从 netbeans 8.1 运行 spring boot 应用程序时遇到了这个问题。当我在 netbeans 中点击红色方形按钮时,java 进程没有终止,所以当我重新启动应用程序时,我总是得到“绑定异常,地址已经在使用”的东西。这是已知的错误。

The solution was to add spring-boot:runcommand to run goals of project...

解决方案是 添加 spring-boot:run命令来运行项目的目标......

...and also if you get "No plugin found for prefix 'spring-boot' in the current project and in the plugin groups" you might need to add this to dependencies:

...而且如果你得到“在当前项目和插件组中没有找到前缀‘spring-boot’的插件”,你可能需要将它添加到依赖项中:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
</parent>

<repositories>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>

回答by K_Bist

I know i am too late to answer but may be some one get help with this. STS users can use the Relaunchbutton rather than Runto ensure that any existing instance is closed.
For Reference :
https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-running-your-application.html

我知道我回答得太晚了,但可能有人会得到帮助。STS 用户可以使用Relaunch按钮而不是Run确保关闭任何现有实例。
参考:https:
//docs.spring.io/spring-boot/docs/current/reference/html/using-boot-running-your-application.html

回答by Ramz_the_dev

I encountered same problem running SpringBoot application in eclipse on Mac. I manually had to find all the PID's using 8080 and kill them. But, fortunately, I realized that we can kill that tomcat instance right from the eclipse "console" view, by hitting the stop(Red square icon) button and run the spring boot application again.

我在 Mac 上的 eclipse 中运行 SpringBoot 应用程序时遇到了同样的问题。我必须手动找到所有使用 8080 的 PID 并杀死它们。但是,幸运的是,我意识到我们可以直接从 Eclipse 的“控制台”视图中杀死那个 tomcat 实例,方法是点击停止(红色方块图标)按钮并再次运行 Spring Boot 应用程序。

回答by Jason Pyeron

You cause a shutdown by calling curl -v -X POST http://127.0.0.1:8091/shutdownprovided you configured spring-boot properly.

如果您curl -v -X POST http://127.0.0.1:8091/shutdown正确配置了 spring-boot,您可以通过调用来导致关机 。

To support this you will have to update your application properties. This is partially described in https://stackoverflow.com/a/26563344/58794.

为了支持这一点,您必须更新您的应用程序属性。这在https://stackoverflow.com/a/26563344/58794 中有部分描述。

updating application.yml by adding:

通过添加以下内容更新 application.yml:

management:
  security:
    enabled: false
endpoints:
  shutdown:
    enabled: true

or updating application.properties by adding:

或通过添加以下内容来更新 application.properties:

management.security.enabled=false
endpoints.shutdown.enabled=true

Once called

一次叫

$ curl -v -X POST http://127.0.0.1:8091/shutdown
* STATE: INIT => CONNECT handle 0x600057990; line 1423 (connection #-5000)
* Added connection 0. The cache now contains 1 members
*   Trying 127.0.0.1...
* TCP_NODELAY set
* STATE: CONNECT => WAITCONNECT handle 0x600057990; line 1475 (connection #0)
* Connected to 127.0.0.1 (127.0.0.1) port 8091 (#0)
* STATE: WAITCONNECT => SENDPROTOCONNECT handle 0x600057990; line 1592 (connection #0)
* Marked for [keep alive]: HTTP default
* STATE: SENDPROTOCONNECT => DO handle 0x600057990; line 1610 (connection #0)
> POST /shutdown HTTP/1.1
> Host: 127.0.0.1:8091
> User-Agent: curl/7.56.1
> Accept: */*
>
* STATE: DO => DO_DONE handle 0x600057990; line 1689 (connection #0)
* STATE: DO_DONE => WAITPERFORM handle 0x600057990; line 1814 (connection #0)
* STATE: WAITPERFORM => PERFORM handle 0x600057990; line 1824 (connection #0)
* HTTP 1.1 or later with persistent connection, pipelining supported
< HTTP/1.1 200
< X-Application-Context: application:h2:8091
< Content-Type: application/vnd.spring-boot.actuator.v1+json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Fri, 22 Dec 2017 07:01:04 GMT
<
* STATE: PERFORM => DONE handle 0x600057990; line 1993 (connection #0)
* multi_done
* Connection #0 to host 127.0.0.1 left intact
* Expire cleared
{"message":"Shutting down, bye..."}

the container will shutdown

容器将关闭

o.s.c.support.DefaultLifecycleProcessor  : Stopping beans in phase 0
o.s.j.e.a.AnnotationMBeanExporter        : Unregistering JMX-exposed beans on shutdown
j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'

but if you launched from mvn spring-boot:runyou will likely get a:

但如果你从你启动,mvn spring-boot:run你可能会得到:

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 35.613 s
[INFO] Finished at: 2017-12-22T02:01:05-05:00
[INFO] Final Memory: 25M/577M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.7.RELEASE:run (default-cli) on project PROJECTNAME: Could not exec java: Application finished with exit code: 1 -> [Help 1]

If you do not have management.security.enabled=falseyou may be presented with the following error:

如果没有management.security.enabled=false,可能会出现以下错误:

$ curl -v -X POST http://127.0.0.1:8091/shutdown
> POST /shutdown HTTP/1.1
> Host: 127.0.0.1:8091
> User-Agent: curl/7.56.1
> Accept: */*
>
< HTTP/1.1 401
< X-Application-Context: application:h2:8091
< Content-Type: application/vnd.spring-boot.actuator.v1+json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Fri, 22 Dec 2017 06:56:19 GMT
<
{"timestamp":1513925779265,"status":401,"error":"Unauthorized","message":"Full authentication is required to access this resource.","path":"/shutdown"}

If you do not have endpoints.shutdown.enabled=trueyou will see:

如果你没有,endpoints.shutdown.enabled=true你会看到:

$ curl -v -X POST http://127.0.0.1:8091/shutdown
> POST /shutdown HTTP/1.1
> Host: 127.0.0.1:8091
> User-Agent: curl/7.56.1
> Accept: */*
>
< HTTP/1.1 404
< X-Application-Context: application:h2:8091
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Fri, 22 Dec 2017 06:58:52 GMT
<
{"timestamp":1513925932345,"status":404,"error":"Not Found","message":"No message available","path":"/shutdown"}

If you try a GET instead of POST this error will be presented:

如果您尝试使用 GET 而不是 POST ,则会出现此错误:

$ curl -v http://127.0.0.1:8091/shutdown
> GET /shutdown HTTP/1.1
> Host: 127.0.0.1:8091
> User-Agent: curl/7.56.1
> Accept: */*
>
< HTTP/1.1 405
< X-Application-Context: application:h2:8091
< Allow: POST
< Content-Type: application/json;charset=UTF-8
< Transfer-Encoding: chunked
< Date: Fri, 22 Dec 2017 06:54:12 GMT
<
{"timestamp":1513925652827,"status":405,"error":"Method Not Allowed","exception":"org.springframework.web.HttpRequestMethodNotSupportedException","message":"Request method 'GET' not supported","path":"/shutdown"}