`
heziya
  • 浏览: 1699 次
文章分类
社区版块
存档分类
最新评论

Spring Boot的应用启动与关闭

阅读更多
1. Spring Boot应用打包
Spring Boot应用可以打成jar包,其中内嵌tomcat,因此可以直接启动使用。但是在Spring Boot应用启动之前,首先需要进行打包,本文讲述的是Maven工程的打包,打包需要的前提条件(pom.xml文件中的内容)是:


...

<packaging>jar</packaging>

...

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

...

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.***.Application</mainClass>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
打包命令为:

mvn clean package -Dmaven.test.skip=true


# Demo

$ mvn clean package -Dmaven.test.skip=true
[INFO] Scanning for projects...
[WARNING]
[WARNING] Some problems were encountered while building the effective model for com.example:myproject:jar:0.0.1-SNAPSHOT
[WARNING] 'build.plugins.plugin.version' for org.springframework.boot:spring-boot-maven-plugin is missing. @ line 38, column 17
[WARNING]
[WARNING] It is highly recommended to fix these problems because they threaten the stability of your build.
[WARNING]
[WARNING] For this reason, future Maven versions might no longer support building such malformed projects.
[WARNING]
[INFO]                                                                        
[INFO] ------------------------------------------------------------------------
[INFO] Building myproject 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ myproject ---
[INFO] Deleting /Users/ltc/Spring Boot Demo/target
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ myproject ---
[WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent!
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ myproject ---
[INFO] Changes detected - recompiling the module!
[WARNING] File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent!
[INFO] Compiling 1 source file to /Users/ltc/Spring Boot Demo/target/classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ myproject ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ myproject ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ myproject ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myproject ---
[INFO] Building jar: /Users/ltc/Spring Boot Demo/target/myproject-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:1.5.0.RC1:repackage (default) @ myproject ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.861 s
[INFO] Finished at: 2017-01-13T15:31:32+08:00
[INFO] Final Memory: 26M/308M
[INFO] ------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
或在eclipse中运行run -> Maven build...,在Goals中填写clean package -Dmaven.test.skip=true,运行,打包完成。

2. Spring Boot应用启动
Spring Boot的启动命令为:

java -jar application.jar


# Demo

$ java -jar target/myproject-0.0.1-SNAPSHOT.jar

  .   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::        (v1.4.3.RELEASE)

2017-01-13 15:31:36.911  INFO 62119 --- [           main] com.test.Example                         : Starting Example on local with PID 62119 (/Users/ltc/Spring Boot Demo/target/myproject-0.0.1-SNAPSHOT.jar started by liutianchi in /Users/ltc/Spring Boot Demo)
2017-01-13 15:31:36.916  INFO 62119 --- [           main] com.test.Example                         : No active profile set, falling back to default profiles: default
2017-01-13 15:31:36.981  INFO 62119 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@b1a58a3: startup date [Fri Jan 13 15:31:36 CST 2017]; root of context hierarchy
2017-01-13 15:31:38.602  INFO 62119 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-01-13 15:31:38.615  INFO 62119 --- [           main] o.apache.catalina.core.StandardService   : Starting service Tomcat
2017-01-13 15:31:38.616  INFO 62119 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.6
2017-01-13 15:31:38.718  INFO 62119 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-01-13 15:31:38.718  INFO 62119 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1740 ms
2017-01-13 15:31:38.927  INFO 62119 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-01-13 15:31:38.932  INFO 62119 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'metricsFilter' to: [/*]
2017-01-13 15:31:38.932  INFO 62119 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-01-13 15:31:38.932  INFO 62119 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-01-13 15:31:38.932  INFO 62119 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-01-13 15:31:38.932  INFO 62119 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-01-13 15:31:38.932  INFO 62119 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'webRequestLoggingFilter' to: [/*]
2017-01-13 15:31:38.932  INFO 62119 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'applicationContextIdFilter' to: [/*]
2017-01-13 15:31:39.217  INFO 62119 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@b1a58a3: startup date [Fri Jan 13 15:31:36 CST 2017]; root of context hierarchy
2017-01-13 15:31:39.310  INFO 62119 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/]}" onto java.lang.String com.test.Example.home()
2017-01-13 15:31:39.313  INFO 62119 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2017-01-13 15:31:39.313  INFO 62119 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2017-01-13 15:31:39.338  INFO 62119 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-01-13 15:31:39.338  INFO 62119 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-01-13 15:31:39.378  INFO 62119 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2017-01-13 15:31:39.665  INFO 62119 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/manage/metrics/{name:.*}],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.MetricsMvcEndpoint.value(java.lang.String)
2017-01-13 15:31:39.665  INFO 62119 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/manage/metrics || /manage/metrics.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-13 15:31:39.666  INFO 62119 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/manage/mappings || /manage/mappings.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-13 15:31:39.667  INFO 62119 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/manage/trace || /manage/trace.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-13 15:31:39.667  INFO 62119 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/manage/info || /manage/info.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-13 15:31:39.668  INFO 62119 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/manage/configprops || /manage/configprops.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-13 15:31:39.669  INFO 62119 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/manage/heapdump || /manage/heapdump.json],methods=[GET],produces=[application/octet-stream]}" onto public void org.springframework.boot.actuate.endpoint.mvc.HeapdumpMvcEndpoint.invoke(boolean,javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse) throws java.io.IOException,javax.servlet.ServletException
2017-01-13 15:31:39.669  INFO 62119 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/manage/autoconfig || /manage/autoconfig.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-13 15:31:39.673  INFO 62119 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/manage/env/{name:.*}],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EnvironmentMvcEndpoint.value(java.lang.String)
2017-01-13 15:31:39.673  INFO 62119 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/manage/env || /manage/env.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-13 15:31:39.674  INFO 62119 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/manage/health || /manage/health.json],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint.invoke(java.security.Principal)
2017-01-13 15:31:39.675  INFO 62119 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/manage/dump || /manage/dump.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-13 15:31:39.677  INFO 62119 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/manage/shutdown || /manage/shutdown.json],methods=[POST]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.ShutdownMvcEndpoint.invoke()
2017-01-13 15:31:39.678  INFO 62119 --- [           main] o.s.b.a.e.mvc.EndpointHandlerMapping     : Mapped "{[/manage/beans || /manage/beans.json],methods=[GET],produces=[application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter.invoke()
2017-01-13 15:31:39.799  INFO 62119 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2017-01-13 15:31:39.809  INFO 62119 --- [           main] o.s.c.support.DefaultLifecycleProcessor  : Starting beans in phase 0
2017-01-13 15:31:39.944  INFO 62119 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-01-13 15:31:39.949  INFO 62119 --- [           main] com.test.Example                         : Started Example in 4.292 seconds (JVM running for 4.726)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
3. Spring Boot应用关闭
Spring Boot应用关闭的前提条件是POM.xml添加以下内容:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
1
2
3
4
application.properties中添加:

#启用shutdown
endpoints.shutdown.enabled=true
#禁用密码验证
endpoints.shutdown.sensitive=false
1
2
3
4
关闭命令为:

curl -X POST host:port/shutdown


# Demo

$ curl -X POST http://localhost:8080/shutdown
{"message":"Shutting down, bye..."}


$ curl -X POST http://localhost:8080/manage/shutdown
{"message":"Shutting down, bye..."}
1
2
3
4
5
6
7
8
9
10
11
如果要配置路径,需要在application.properties中添加management.context-path=/manage,则关闭命令变为curl -X POST host:port/manage/shutdown。

4. 安全验证
如果在关闭时需要安全验证,则在pom.xml文件中添加:

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-security</artifactId>
</dependency>
1
2
3
4
application.properties中添加:

#开启shutdown的安全验证
endpoints.shutdown.sensitive=true

#验证用户名
security.user.name=admin

#验证密码
security.user.password=admin
#角色
management.security.role=SUPERUSER

# 指定端口
management.port=8081

# 指定地址
management.address=127.0.0.1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
关闭命令为:

curl -u admin:admin -X POST http://127.0.0.1:8081/manage/shutdown

# Demo

$ curl -u admin:admin -X POST http://127.0.0.1:8081/manage/shutdown
{"message":"Shutting down, bye..."}
分享到:
评论

相关推荐

    Spring Boot的应用启动与关闭的方法

    Spring Boot应用启动与关闭的方法 Spring Boot作为Spring框架对“约定优先于配置(Convention Over Configuration)”理念的最佳实践的产物,是一个与微服务(MicroServices)相当契合的微框架。下面将介绍Spring Boot...

    spring boot部署启动脚本

    标题中的“Spring Boot部署启动脚本”指的是在Spring Boot应用开发完成后,为了自动化部署和启动应用程序而创建的脚本。Spring Boot是一个简化Spring应用程序开发的框架,它提倡“开箱即用”的理念,使得开发者可以...

    详解Springboot应用启动以及关闭时完成某些操作

    在 Spring boot 应用程序中,关闭操作可以通过实现 ApplicationListener&lt;ContextClosedEvent&gt; 接口和 DisposableBean 接口来实现。 A、非安全验证 1. 在 pom.xml 文件中添加依赖包:&lt;dependency&gt;&lt;groupId&gt;org....

    Spring Boot 应用加速 - Web

    这些配置类中有一些是为测试环境或者特定场景准备的,而在生产环境中可能并不需要,因此可以将它们排除以减少不必要的初始化工作,从而加速Spring Boot应用的启动和运行。 接下来,关于Spring MVC的配置,文件提到...

    Spring Boot参考指南(最新版)

    SpringApplication 是 Spring Boot 的核心类之一,用于启动和管理 Spring Boot 应用程序。 24. 外化配置 Spring Boot 提供了外化配置特性,以简化应用程序的配置。 25. 访问应用程序参数 开发者可以使用 Spring ...

    Java版本ngrok集成spring-boot随应用启动快速映射内网地址

    4. **处理停止逻辑**:同样,需要创建一个监听器来在Spring Boot应用关闭时停止ngrok服务。监听`ApplicationStoppingEvent`事件: ```java @Component public class NgrokShutdown implements Application...

    Spring Boot参考指南.pdf

    - **自定义Banner**:解释如何定制Spring Boot应用启动时显示的Banner图像。 - **自定义SpringApplication**:介绍如何扩展SpringApplication类来自定义应用的行为。 - **流式构建API**:介绍一种流式的构建方式...

    spring-boot中文参考指南

    - **Spring CLI示例快速入门**:通过命令行创建Spring Boot应用示例。 - **从早期版本升级**:指导如何从旧版本平滑地升级到最新版本。 - **开发第一个Spring Boot应用**: - **创建POM**:定义项目的元数据和依赖...

    Java Spring Boot面试题

    Spring Boot 是 Spring 开源组织下的子项目,是 Spring 组件一站式解决方案,主要是简化了使用 Spring 的难度,简省了繁重的配置,提供了各种启动器,开发者能快速上手。 Spring Boot 的优点包括:容易上手、开箱即...

    spring boot实战.pdf高清无水印

    第8章 部署Spring Boot应用程序 139 8.1 衡量多种部署方式 139 8.2 部署到应用服务器 140 8.2.1 构建WAR文件 141 8.2.2 创建生产Profile 142 8.2.3 开启数据库迁移 145 8.3 推上云端 150 8.3.1 部署...

    java spring-boot shell 启动器

    Java Spring-Boot Shell 启动器是一个实用工具,它提供了通过Shell脚本来管理Spring-Boot应用的便捷方式。这个启动器允许用户启动、停止、重启和查看应用日志,而无需手动使用`kill`命令来结束进程。在IT环境中,...

    spring-boot-reference-1.5.7

    1. 创建Spring Boot应用:使用Spring Initializr在线生成项目结构,或通过命令行工具初始化项目。 2. 配置:通过application.properties或YAML文件进行配置,也可以使用@ConfigurationProperties绑定自定义配置类。 ...

    Spring Boot 通过web开启和关闭定时任务

    总结,通过以上步骤,你可以在Spring Boot应用中实现通过Web接口动态开启和关闭定时任务。这有助于在运行时根据业务需求灵活调整任务的执行状态,提高了应用的可扩展性和灵活性。在实际开发中,可以根据项目需求选择...

    Spring Boot多数据源(JdbcTemplate)配置与使用

    在Spring Boot应用中,我们经常需要处理来自多个数据源的数据,比如在开发微服务架构时,每个服务可能对应一个特定的数据源。`JdbcTemplate`是Spring框架提供的一个用于简化数据库操作的工具,它使得数据库访问更为...

    spring boot linux启动方式详解

    在生产环境中,Spring Boot 应用程序需要在 Linux 操作系统下启动和运行。这篇文章将详细介绍 Spring Boot Linux 启动方式详解,包括前台启动、后台启动和脚本启动等方式。 前台启动 前台启动是指在 Linux 终端下...

    Spring Boot学习笔记

    总结,Spring Boot学习笔记涵盖了Thymeleaf模板引擎的使用,如何实现国际化,以及如何将MyBatis和JDBC整合到Spring Boot应用中,这些都是在构建现代Java后端应用时不可或缺的技术。通过深入理解并掌握这些知识点,...

    Spring Boot 系列教程 热部署-devtools模块

    在Spring Boot应用开发中,热部署(Hot Deployment)是一个非常重要的功能,它允许开发者在代码修改后无需手动重启应用即可看到改动效果,极大地提升了开发效率。`devtools`模块是Spring Boot提供的一项专为开发环境...

    Spring Boot实战 ,丁雪丰 (译者) 中文版

    第8章 部署Spring Boot应用程序 139 8.1 衡量多种部署方式 139 8.2 部署到应用服务器 140 8.2.1 构建WAR文件 141 8.2.2 创建生产Profile 142 8.2.3 开启数据库迁移 145 8.3 推上云端 150 8.3.1...

    Spring Boot 中文文档 参考手册 中文版.pdf

    - **申请退出**:控制Spring Boot应用的优雅关闭过程。 综上所述,Spring Boot提供了一系列强大且灵活的功能,极大地简化了基于Spring框架的应用开发流程。无论是对于新手还是经验丰富的开发者而言,Spring Boot都...

    spring-boot中文手册

    - **main方法**:演示了如何定义main方法来启动Spring Boot应用。 - **运行示例**:指导用户如何运行示例程序。 - **创建一个可执行jar**:介绍了如何将项目打包成独立的jar文件。 - **接下来阅读什么** - 建议...

Global site tag (gtag.js) - Google Analytics