当前位置: 首页 > news >正文

Spring Boot实践--项目打包、启动、关闭的方法

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

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

1.应用打包

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>

...

打包命令为:

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的启动命令为:

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: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的关闭:通过HTTP发送shutdown信号,或者通过service stop的方式。ps -ef(-aux) | grep project_name --> kill -9 XXX 

方式一:HTTP发送shutdown信号

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

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

application.properties中添加:

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

关闭命令为:

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。

方式二:服务方式

简单关闭:ps -ef(-aux) | grep project_name --> kill -9 XXX 

startTest.sh内容如下: 

#!/bin/sh  
java -jar Test.jar &       #注意:必须有&让其后台执行,否则没有pid生成  
echo $! > java.pid         #将jar包启动对应的pid写入文件中,为停止时提供pid  

stopTest.sh内容如下:  

#!/bin/sh  
PID=$(cat java.pid )  
kill -9 $PID

 

4.安全验证

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

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

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

关闭命令为:

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://my.oschina.net/spinachgit/blog/1623234

相关文章:

  • centos7 安装 gitolite (git服务器)
  • 项目去掉svn管理标志
  • SSM-MyBatis-09:Mybatis中SqlSession的close为什么能造成事务的回滚
  • Javascript理解this对象
  • GNUPG
  • 零基础Python爬虫实现(百度贴吧)
  • 我对CopyOnWrite的思考
  • RabbitMQ入门-路由-有选择的接受消息
  • 报告称国产智能手机全球市场份额33.1% 超过韩国
  • iOS下JS与OC互相调用(六)--WKWebView + WebViewJavascriptBridge
  • 深入理解java虚拟机 精华总结(面试)
  • Spring框架
  • DTS-071007 表结构在源库和目标库中不一致
  • 算法学习之路|聪明的打字员
  • [学习笔记—Objective-C]《Objective-C-基础教程 第2版》第二章~第七章
  • 【108天】Java——《Head First Java》笔记(第1-4章)
  • 【5+】跨webview多页面 触发事件(二)
  • 【跃迁之路】【444天】程序员高效学习方法论探索系列(实验阶段201-2018.04.25)...
  • es的写入过程
  • extjs4学习之配置
  • HTTP中的ETag在移动客户端的应用
  • JS基础之数据类型、对象、原型、原型链、继承
  • js数组之filter
  • Linux快速配置 VIM 实现语法高亮 补全 缩进等功能
  • Object.assign方法不能实现深复制
  • Python_网络编程
  • Spring Cloud Feign的两种使用姿势
  • 安卓应用性能调试和优化经验分享
  • 高程读书笔记 第六章 面向对象程序设计
  • 诡异!React stopPropagation失灵
  • 基于HAProxy的高性能缓存服务器nuster
  • 可能是历史上最全的CC0版权可以免费商用的图片网站
  • 如何用vue打造一个移动端音乐播放器
  • 实现简单的正则表达式引擎
  • 手写双向链表LinkedList的几个常用功能
  • 首页查询功能的一次实现过程
  • 我建了一个叫Hello World的项目
  • 关于Android全面屏虚拟导航栏的适配总结
  • 国内唯一,阿里云入选全球区块链云服务报告,领先AWS、Google ...
  • ​LeetCode解法汇总1410. HTML 实体解析器
  • ​创新驱动,边缘计算领袖:亚马逊云科技海外服务器服务再进化
  • #控制台大学课堂点名问题_课堂随机点名
  • #数学建模# 线性规划问题的Matlab求解
  • $(document).ready(function(){}), $().ready(function(){})和$(function(){})三者区别
  • ()、[]、{}、(())、[[]]命令替换
  • (27)4.8 习题课
  • (6)添加vue-cookie
  • (pojstep1.3.1)1017(构造法模拟)
  • (笔试题)合法字符串
  • (二十三)Flask之高频面试点
  • (转)C语言家族扩展收藏 (转)C语言家族扩展
  • ******之网络***——物理***
  • .NET Framework 3.5中序列化成JSON数据及JSON数据的反序列化,以及jQuery的调用JSON
  • .NET Framework 的 bug?try-catch-when 中如果 when 语句抛出异常,程序将彻底崩溃
  • .NETCORE 开发登录接口MFA谷歌多因子身份验证