登录后台

页面导航

本文编写于 2049 天前,最后修改于 1258 天前,其中某些信息可能已经过时。

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

应用打包

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,运行,打包完成。

应用启动

java -jar 直接运行

直接运行

java -jar app.jar

特点:当前ssh窗口被锁定,可按 CTRL + C 打断程序运行,或直接关闭窗口,程序退出
那如何让窗口不锁定?

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)

&

java -jar app.jar &

& 代表在后台运行。

特定:当前 ssh 窗口不被锁定,但是当窗口关闭时,程序中止运行。
继续改进,如何让窗口关闭时,程序仍然运行?

nohup 命令

语法:nohup Command [ Arg ... ] [ & ]
描述:nohup 命令运行由 Command 参数和任何相关的 Arg 参数指定的命令,忽略所有挂断(SIGHUP)信号。在注销后使用 nohup 命令运行后台中的程序。要运行后台中的 nohup 命令,添加 & ( 表示“and”的符号)到命令的尾部。

nohup java -jar app.jar &

nohup 意思是不挂断运行命令,当账户退出或终端关闭时,程序仍然运行

指定输出文件

默认输出到 nohup.out 文件

nohup java -jar app.jar >output 2>&1 &

解释
操作系统中有三个常用的流:

**0:标准输入流 stdin
1:标准输出流 stdout
2:标准错误流 stderr**

一般当我们用 > output,实际是 1>output 的省略用法;< input ,实际是 0 < input 的省略用法。

& 的命令行,即使 terminal(终端)关闭,或者电脑死机程序依然运行(前提是你把程序递交到服务器上);

2>&1 的意思

这个意思是把标准错误(2)重定向到标准输出中(1),而标准输出又导入文件 output 里面,所以结果是标准错误和标准输出都导入文件 output 里面了

至于为什么需要将标准错误重定向到标准输出的原因,那就归结为 stderr 没有缓冲区,而 stdout 有。这就会导致 >output 2>output 文件 output 被两次打开,而 stdoutstderr 将会竞争覆盖。
这就是为什么有人会写成:nohup ./command.sh >output 2>output 出错的原因了

jobs 命令:查看后台运行任务

fg 命令:将某个作业调回前台控制

系统服务

在 Spring Boot 的 Maven 插件中,还提供了构建完整可执行程序的功能,什么意思呢?就是说,我们可以不用 java -jar,而是直接运行 jar 来执行程序。这样我们就可以方便的将其创建成系统服务在后台运行了。

主要步骤如下:

  1. 在pom.xml中添加Spring Boot的插件,并注意设置executable配置
<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <executable>true</executable>
    </configuration>
</plugin>
  1. 在完成上述配置后,使用 mvn package 进行打包,构建一个可执行的 jar 包
  2. 给jar包授权
    chmod u+x app.jar
  3. 创建软连接到 /etc/init.d/ 目录下
    ln -s /usr/local/app/app.jar /etc/init.d/app
  4. 在完成软连接创建之后,我们就可以通过如下命令对app.jar应用来控制启动、停止、重启操作了
    service app start|stop|restart|status

默认的应用 pid/var/run/app/app.pid
默认的日志目录/var/log/app.log

应用关闭

下面主要有两种方式进行 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

安全验证

如果在关闭时需要安全验证,则在 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..."}

本文内容来自:

  1. https://my.oschina.net/spinachgit/blog/1623234
  2. https://www.jianshu.com/p/563497a6e1a7