Spring BootTakes an opinionated view of building production-ready Spring applications. Spring Boot favors convention over configuration and is designed to get you up and running as quickly as possible.html
SpringBoot项目为咱们构建Spring应用带来了极大的方便,同时SpringBoot在构建Spring应用方面也作出了很大建树java
众所周知,SpringBoot能够经过gradle或者maven插件构建Executable Jar/War Spring Boot Gradle Plugin Reference Guide
除了传统方式java -jar myapp.jar
运行外,还能够经过myapp.jar start|stop|restart
运行,安装为systemd服务,经过同名文件myapp.conf配置运行时参数等等高级功能 Installing Spring Boot Applications
讲到这里不少童鞋都会问,这一切都是如何作到的?linux
首先,咱们建立一个简单的示例git
package com.manerfan.springboot.theory; /** * @author manerfan * @date 2018/3/2 */ public class RunnableApp { public static void main(String[] args) { System.out.println("Hello You!"); } }
使用eclipse或idea或其余工具,编译并打包为jar(spring-boot-theory.jar),打包时选择main-class为com.manerfan.springboot.theory.RunnableAppgithub
对于Runnable Jar,总有一个META-INF/MANIFEST.MF文件,记录Main-Class、Class-Path等信息web
咱们能够经过java -jar spring-boot-theory.jar
来运行,但尝试直接运行spring-boot-theory.jar
时便会报错spring
这代表,spring-boot-theory.jar
仅仅为Runnable Jar,而不是Executable Jarshell
通常状况,咱们都会借助shell脚原本运行咱们的jar,以下 runJar.sh
springboot
#!/bin/sh JAR="/usr/local/spring-boot-theory.jar" java=java if test -n "$JAVA_HOME"; then java="$JAVA_HOME/bin/java" fi exec "$java" -jar $JAR "$@" exit 1
在此基础上,咱们能够加入更多控制,以实现runJar.sh start | stop | restart等特性bash
#!/bin/bash # chkconfig: 2345 85 85 # description: spring boot theory # processname: spring-boot-theory # Created By: manerfan (manerfan.china@gmail.com) JAR="/usr/local/spring-boot-theory.jar" PIDFILE=/data/sms-service/smss.pid java=java if test -n "$JAVA_HOME"; then java="$JAVA_HOME/bin/java" fi start() {} stop() {} restart() {} status() {} case "$action" in start) start "$@"; exit $?;; stop) stop "$@"; exit $?;; restart) restart "$@"; exit $?;; status) status "$@"; exit $?;; *) echo "Usage: $0 {start|stop|force-stop|restart|force-reload|status|run}"; exit 1; esac exit 0
能够参考 http://blog.csdn.net/zhanngle...
但这样也只是经过shell脚本控制jar的启动中止,如何作到Executable Jar呢?
一样,首先是一段shell脚本 runJar.sh
#!/bin/sh JAR =`which "$0" 2>/dev/null` [ $? -gt 0 -a -f "$0" ] && JAR="./$0" java=java if test -n "$JAVA_HOME"; then java="$JAVA_HOME/bin/java" fi exec "$java" -jar $JAR "$@" exit 1
经过如下语句将shell脚本与jar文件整合到一块儿 ~划重点~
cat runJar.sh spring-boot-theory.jar > exec-spring-boot-theory.jar && chmod +x exec-spring-boot-theory.jar
大功告成!
一样,在此基础上,咱们能够加入更多控制,以实现exec-spring-boot-theory.jar start | stop | restart等特性
能够参考 https://coderwall.com/p/ssuax...
springboot项目源码在https://github.com/spring-pro...,能够对照查看
咱们从 JarWriter 开始
public JarWriter(File file, LaunchScript launchScript) throws FileNotFoundException, IOException { FileOutputStream fileOutputStream = new FileOutputStream(file); if (launchScript != null) { // 将启动脚本写入文件 fileOutputStream.write(launchScript.toByteArray()); // 设置文件可执行属性 setExecutableFilePermission(file); } this.jarOutput = new JarArchiveOutputStream(fileOutputStream); this.jarOutput.setEncoding("UTF-8"); }
当执行gradle build或mvn package时,会使用JarWriter从新生成jar文件。JarWrite构造函数中,会首先将启动脚本写入文件,并设置文件的可执行属性。
除此以外,JarWriter还有众多方法,如writeManifest写入manifest文件、writeNestedLibrary写入第三方依赖等等,经过JarWriter以构建Executable Jar.
此过程,与上述将shell脚本与jar文件整合效果一致。
可是,launchScript又是什么?
public DefaultLaunchScript(File file, Map<?, ?> properties) throws IOException { // 加载启动脚本 String content = loadContent(file); this.content = expandPlaceholders(content, properties); } private String loadContent(File file) throws IOException { if (file == null) { // 默认launch.script return loadContent(getClass().getResourceAsStream("launch.script")); } return loadContent(new FileInputStream(file)); }
默认的LaunchScript为DefaultLaunchScript,在构造DefaultLaunchScript时,若不指定启动脚本,则取默认的launch.script,内容见 launch.script
launch.script实现较为复杂,此处不作解析,launch.script与上述shell脚本的实现思路基本相同,一样实现了start stop restart等功能,方便安装为systemd服务
不一样的是,launch.script会解析与jar文件同名的conf文件,以实现启动脚本定制化 Customizing a Script When It Runs
如,咱们实现一个简单的web接口
@SpringBootApplication @RestController public class WebApp { public static void main(String[] args) { SpringApplication.run(WebApp.class, args); } @RequestMapping("/") @GetMapping public String hello() { return "Hello You!"; } }
使用spring-boot-gradle-plugin插件打包,执行./spring-boot-theory-1.0.0.jar
,能够看到输出
访问 http://localhost:8080 能够看到 Hello You! 字样
若要对启动参数,如监听端口作修改,除了使用java -jar spring-boot-theory-1.0.0.jar --server.port=8000
外,还能够新建同名文件 spring-boot-theroy-1.0.0.conf
,填入内容
RUN_ARGS="--server.port=8000"
再次执行./spring-boot-theory-1.0.0.jar
监听端口由默认的8080变为指定的8000
conf配置文件可配置的内容较多,如使用JAVA_OPTS配置jvm运行参数,使用MODE=service
可将程序放入后台运行等等 Customizing a Script When It Runs
以以下conf配置为例
MODE=service JAVA_OPTS="-Xms1g -Xmx1g -Dfile.encoding=utf-8" RUN_ARGS="--server.port=8000"
执行./spring-boot-theory-1.0.0.jar start
查看该进程运行参数
/usr/bin/java -Dsun.misc.URLClassPath.disableJarChecking=true -Xms1g -Xmx1g -Dfile.encoding=utf-8 -jar /Users/manerfan/Project/learning/javaspring-boot-theory/build/libs/spring-boot-theory-1.0.0.jar --server.port=8000
SpringBoot实现ExecutableJar的原理,即是将启动脚本及原有的jar文件(以及第三方依赖包)写入同一个文件,并给该文件赋可执行权限,结合conf配置文件,使RunnableJar变为ExecutableJar的同时,得以更加便捷的控制程序的启动/运行参数