`
frank1998819
  • 浏览: 758210 次
  • 性别: Icon_minigender_1
  • 来自: 南京
文章分类
社区版块
存档分类

阿羅 全新起航 正确、安全地停止SpringBoot应用服务(转)

 
阅读更多

Spring Boot,作为Spring框架对“约定优先于配置(Convention Over Configuration)”理念的最佳实践的产物,它能帮助我们很快捷的创建出独立运行、产品级别的基于Spring框架的应用,大部分Spring Boot应用只需要非常少的配置就可以快速运行起来,是一个与微服务(MicroServices)相当契合的微框架。

1. Spring Boot应用打包

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

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
...
 
<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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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] ------------------------------------------------------------------------

或在eclipse中运行run -> Maven build...,在Goals中填写clean package -Dmaven.test.skip=true,运行,打包完成。

2. Spring Boot应用启动

Spring Boot的启动命令为:

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
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)

3. Spring Boot应用关闭

下面主要有两种方式进行Spring Boot的关闭:通过HTTP发送shutdown信号,或者通过service stop的方式。

Spring Boot应用关闭的前提条件是POM.xml添加以下内容:

1
2
3
4
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

application.properties中添加:

1
2
3
4
#启用shutdown
endpoints.shutdown.enabled=true
#禁用密码验证
endpoints.shutdown.sensitive=false

关闭命令为:

1
2
3
4
5
6
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..."}

如果要配置路径,需要在application.properties中添加management.context-path=/manage,则关闭命令变为curl -X POST host:port/manage/shutdown。

4. 安全验证

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

1
2
3
4
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-security</artifactId>
</dependency>

application.properties中添加:

1
2
3
4
5
6
7
8
9
10
11
12
#开启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
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..."}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

 

 

 https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html#production-ready-endpoints-security

分享到:
评论

相关推荐

    诺贝尔经济学奖获得者论文arrow_74阿罗

    肯尼斯·约瑟夫·阿罗(Kenneth J.Arrow,1921年8月23日—),美国经济学家,于1972年因在一般均衡理论方面的突出贡献与约翰·希克斯共同荣获诺贝尔经济学奖。

    数据交易如何破局?——数据要素市场中的阿罗信息悖论与法律应对doc文档合集整理.zip

    数据交易如何破局?——数据要素市场中的阿罗信息悖论与法律应对doc文档合集整理.zip

    阿罗

    JavaScript是一种广泛应用于Web开发的编程语言,它在浏览器端运行,为网页添加交互性,同时也常用于服务器端开发,如Node.js环境。阿罗项目可能是一个JavaScript库、框架或者工具,旨在解决特定的开发问题或提高开发...

    chunaloha.rar_阿罗华

    纯阿罗华系统的仿真实验,能很好的出来结果

    绘本(阿罗有支彩色笔)教案.pdf

    《阿罗有支彩色笔》是一本深受孩子们喜爱的绘本,通过讲述小主人公阿罗用他的彩色笔创造出各种奇妙情景的故事,激发孩子们的想象力、创造力和语言表达能力。该绘本教案旨在引导教师有效地利用教材,设计有趣且富有...

    阿罗在北极_精美学习课件ppt

    根据提供的信息来看,本次讨论的主题围绕“阿罗在北极”的精美学习课件PPT展开,主要涉及PPT模板的设计与应用、以及相关的文档资源下载等内容。下面将对这些知识点进行详细的解析。 ### PPT模板的设计与应用 #### ...

    《阿罗在北极》绘本故事PPT模板

    《阿罗在北极》绘本故事PPT 现在是圣诞前夜,阿罗必须在圣诞老人到来之前找到一棵圣诞树。所以,他戴着连指手套和温暖的羊毛帽子,拿着彩色笔,伴着月亮,朝着北边的树林出发了。阿罗望着北斗七星,确信是朝着北方...

    儿童绘本故事:阿罗房间要挂画

    那是一架喷气式飞机飞行速度特别快 那儿可能要发生一起严重的事故了 阿罗在山中找到一块洼地 这儿很适合开凿好让铁路通过 铁路从一片宽阔的原野中通过来 阿罗在铁路边画了些花草和小鸟 人们喜欢坐在火车里欣赏

    芭比阿罗

    【标题】:“芭比阿罗”可能是指一个与编程教育相关的项目或课程,可能是通过JavaScript来构建用户界面,结合了Orientação(可能是面向对象编程的概念)和Alura平台的教学内容。 【描述】:JavaScript是一种广泛...

    aloalo

    在IT行业中,C#是一种广泛使用的编程语言,尤其在开发Windows桌面应用、Web应用以及游戏开发领域占据着重要地位。"aloalo"可能是项目或软件的名称,而"阿罗阿洛"可能是指该软件的特定特点或者口号,不过由于信息有限...

    模糊数学在国民经济中的应用

    模糊数学,作为一种处理不精确或不确定信息的数学理论,自20世纪60年代由美国数学家赞恩·阿罗提出以来,已经在国民经济的多个领域展现出其独特的优势。这种理论的核心在于对“模糊”概念的量化,使得我们可以对那些...

    论文研究 - 尼日利亚西南伊塞因东部奥克-阿罗铁矿石矿床调查的地磁测量

    进行了地磁勘测,以调查南艾奥伊州伊西因某地区(南纬9.99883°N至南纬7.99933°N,长3.57900°E至长3.57990°E)的铁矿石的存在。尼日利亚西部。 西向东延伸了10条磁道,每条磁道的长度为100 m,相距5 m。...

    DeepChem教程(基于python)

    在“生命科学深度学习的基础工具”部分,教程会引导初学者了解深度学习在生命科学中的应用基础。这包括了理解深度学习的基本概念,如神经网络、反向传播以及优化算法,这些都是构建和训练深度学习模型的关键。此外,...

    关于LoRa长距离低功耗物联网传输技术.docx

    LPWAN 物联网网络的要素有:网络架构、通信范围、电池寿命和功耗、抗干扰性、网络容量、网络安全性、单向或者双向传播、服务的应用等。 LoRaWAN 是一种 LPWAN 技术,网络架构采用网状架构,每个单独的端结点传递...

    Hear2Read_TTS_Tamil_Engine_Android:Hear2Read Tamil TTS应用程序的源代码-Android app source code

    该应用程序包装了Flite引擎,以便可以将其作为应用程序安装在Android设备上。 该应用程序将作为TTS库安装,从而允许其他应用程序使用Flite进行泰米尔语综合。 具体来说,此应用已通过@Voice Aloud Reader,Voice ...

    aurproxy:负载均衡管理器,了解Apache Aurora的服务发现机制并与Aurora的任务生命周期集成

    阿罗匹西 aurproxy是一位负载平衡器管理器,了解服务发现机制以及与Aurora任务生命周期的集成。 它是一个python应用程序,用于管理功能齐全但不支持Aurora的负载平衡器(当前仅支持nginx)。 发生Aurora服务发现事件...

    信息经济学培训课件.docx

    信息经济学是经济学的一个分支,主要研究在信息不对称情况下,经济主体如何决策、交易和治理。这门学科的诞生和发展可以追溯到...学习信息经济学有助于我们更好地理解和应对现代社会中由信息不对称引发的各种经济现象。

Global site tag (gtag.js) - Google Analytics