把Spring boot jar作为Windows服务运行
当你用Spring boot时,通过Java -jar就可以启动,但你不能每次开机都这样执行一遍吧?特别是当你把jar包发布给客户的时候,你总不能要求客户每次开机后都手动执行启动。所以,最好的方式就是作为服务,随机器开机启动。
本文就讲一讲如何把Spring boot做成服务。
笔者最先研究了JavaService,下载地址为:http://forge.ow2.org/projects/javaservice/,按照文档即可配置,比较简单,但是在执行.bat脚本文件的时候需要具有管理员权限才能安装服务,考虑到客户可能存在较难获取管理员权限的情况,遂放弃。
考虑使用Java Service Wrapper,Java Service Wrapper就轻松而简单的为我们解决了这些问题。"Java Service Wrapper"顾名思义,将我们的Java程序包装成系统服务,这样就可以随着系统的运行而自动运行,当然Java Service Wrapper(下面简称Wrapper)的功能绝不仅于此。
以下是官方给出的一些Wrapper的优点:
(1) 使用我们的产品无须在你的程序中添加任何额外的代码。
(2) 当你的程序或JVM出现问题时会自动响应事先定制的策略。
(3) 当出现问题时会及时进行通知。
(4) 完善的日志记录功能可以更好为您提供支持。
(5) 在不同的系统上你可以指定一个标准的流程相同流程,也就是说相同的程序可以不必修改即运行于不同系统。
(6) 可以将你的应用安装成windows或unix的服务或守护进程。
下载地址为 https://wrapper.tanukisoftware.com/doc/english/download.jsp,最新版本为3.5.34,根据自己的情况选择自己对应的操作系统和位数。
通过下载页面我们可以看到Wrapper几乎支持所有的系统环境,Windows32位社区版是免费使用的,Windows 64位没有免费版本。我们这里主要讲Windows系统。
解压后的文件目录如下图:
这些目录并不是你做服务必须的,有些可以不不要。各目录的主要作用如下:
1. bin目录下主要的文件有:InstallTestWrapper-NT.bat、TestWrapper.bat、UninstallTestWrapper-NT.bat、wrapper.exe
2. conf目录下主要文件有:wrapper.conf
3. lib目录下主要文件有:wrapper.dll、wrapper.jar、wrappertest.jar
4. logs目录下主要文件有:wrapper.log(可以配置改名的)
然后,将你的jar包放入lib目录,接下来是对conf/wrapper.conf进行修改,这是最重要的修改,直接关系到你的服务是否能创建成功。内容如下:
#encoding=UTF-8
# Configuration files must begin with a line specifying the encoding
# of the the file.
#********************************************************************
# Wrapper License Properties (Ignored by Community Edition)
#********************************************************************
# Professional and Standard Editions of the Wrapper require a valid
# License Key to start. Licenses can be purchased or a trial license
# requested on the followingpages:
# http://wrapper.tanukisoftware.com/purchase
# http://wrapper.tanukisoftware.com/trial
# Include file problems can be debugged by leaving only one '#'
# at the beginning of thefollowing line:
##include.debug
# The Wrapper will look for either of the following optional filesfor a
# valid License Key. License Key properties can optionally beincluded
# directly in thisconfiguration file.
#include ../conf/wrapper-license.conf
#include ../conf/wrapper-license-%WRAPPER_HOST_NAME%.conf
# The following property will output information about which LicenseKey(s)
# are being found, and canaid in resolving any licensing problems.
#wrapper.license.debug=TRUE
#********************************************************************
# Wrapper Localization
#********************************************************************
# Specify the language and locale which the Wrapper should use.
#wrapper.lang=en_US # en_US or ja_JP
# Specify the location of the language resource files (*.mo).
wrapper.lang.folder=../lang
#********************************************************************
# Wrapper Java Properties
#********************************************************************
# Java Application
# Locate the java binary onthe system PATH:
wrapper.java.command=java
# Specify a specific javabinary:
#set.JAVA_HOME=/java/path
#wrapper.java.command=%JAVA_HOME%/bin/java
wrapper.java.command=../lib/jre/bin/java
# Tell the Wrapper to log the full generated Java command line.
#wrapper.java.command.loglevel=INFO
# Java Main class. This classmust implement the WrapperListener interface
# or guarantee that theWrapperManager class is initialized. Helper
# classes are provided to dothis for you.
# See the following page fordetails:
# http://wrapper.tanukisoftware.com/doc/english/integrate.html
wrapper.java.mainclass=org.tanukisoftware.wrapper.WrapperSimpleApp
# Java Classpath (include wrapper.jar) Add class path elements as
# needed starting from 1
wrapper.java.classpath.1=../lib/这里是你自己的jar包名称
wrapper.java.classpath.2=../lib/wrapper.jar
# Java Library Path (location of Wrapper.DLL or libwrapper.so)
wrapper.java.library.path.1=../lib
# Java Bits. On applicableplatforms, tells the JVM to run in 32 or 64-bit mode.
wrapper.java.additional.auto_bits=TRUE
# Java Additional Parameters
wrapper.java.additional.1=
# Initial Java Heap Size (in MB)
#wrapper.java.initmemory=3
# Maximum Java Heap Size (in MB)
#wrapper.java.maxmemory=64
# Application parameters. Addparameters as needed starting from 1
#这里很重要,这是Spring boot启动的入口
wrapper.app.parameter.1=org.springframework.boot.loader.JarLauncher
#********************************************************************
# Wrapper Logging Properties
#********************************************************************
# Enables Debug output from the Wrapper.
# wrapper.debug=TRUE
# Format of output for the console. (See docs for formats)
wrapper.console.format=PM
# Log Level for console output. (See docs for log levels)
wrapper.console.loglevel=INFO
# Log file to use for wrapper output logging.
wrapper.logfile=../logs/wrapper.log 日志的位置和名称
# Format of output for the log file. (See docs for formats)
wrapper.logfile.format=LPTM
# Log Level for log file output. (See docs for log levels)
wrapper.logfile.loglevel=INFO
# Maximum size that the log file will be allowed to grow to before
# the log is rolled. Size isspecified in bytes. The default value
# of 0, disables logrolling. May abbreviate with the 'k'(kb) or
# 'm' (mb) suffix. For example: 10m = 10 megabytes.
wrapper.logfile.maxsize=0
# Maximum number of rolled log files which will be allowed beforeold
# files are deleted. The default value of 0 implies no limit.
wrapper.logfile.maxfiles=0
# Log Level for sys/event log output. (See docs for log levels)
wrapper.syslog.loglevel=NONE
#********************************************************************
# Wrapper General Properties
#********************************************************************
# Allow for the use of non-contiguous numbered properties
wrapper.ignore_sequence_gaps=TRUE
# Do not start if the pid file already exists.
wrapper.pidfile.strict=TRUE
# Title to use when running as a console
#********************************************************************
# Wrapper JVM Checks
#********************************************************************
# Detect DeadLocked Threads in the JVM. (Requires Standard Edition)
wrapper.check.deadlock=TRUE
wrapper.check.deadlock.interval=60
wrapper.check.deadlock.action=RESTART
wrapper.check.deadlock.output=FULL
# Out Of Memory detection.
# Ignore -verbose:classoutput to avoid false positives.
wrapper.filter.trigger.1000=[Loaded java.lang.OutOfMemoryError
wrapper.filter.action.1000=NONE
# (Simple match)
wrapper.filter.trigger.1001=java.lang.OutOfMemoryError
# (Only match text in stack traces if -XX:+PrintClassHistogram isbeing used.)
#wrapper.filter.trigger.1001=Exception in thread "*"java.lang.OutOfMemoryError
#wrapper.filter.allow_wildcards.1001=TRUE
wrapper.filter.action.1001=RESTART
wrapper.filter.message.1001=The JVM has run out of memory.
#********************************************************************
# Wrapper Email Notifications. (Requires Professional Edition)
#********************************************************************
# Common Event Email settings.
#wrapper.event.default.email.debug=TRUE
#wrapper.event.default.email.smtp.host=
#wrapper.event.default.email.smtp.port=25
#wrapper.event.default.email.subject=[%WRAPPER_HOSTNAME%:%WRAPPER_NAME%:%WRAPPER_EVENT_NAME%]Event Notification
#wrapper.event.default.email.sender=
#wrapper.event.default.email.recipient=
# Configure the log attached to event emails.
#wrapper.event.default.email.maillog=ATTACHMENT
#wrapper.event.default.email.maillog.lines=50
#wrapper.event.default.email.maillog.format=LPTM
#wrapper.event.default.email.maillog.loglevel=INFO
# Enable specific event emails.
#wrapper.event.wrapper_start.email=TRUE
#wrapper.event.jvm_prelaunch.email=TRUE
#wrapper.event.jvm_start.email=TRUE
#wrapper.event.jvm_started.email=TRUE
#wrapper.event.jvm_deadlock.email=TRUE
#wrapper.event.jvm_stop.email=TRUE
#wrapper.event.jvm_stopped.email=TRUE
#wrapper.event.jvm_restart.email=TRUE
#wrapper.event.jvm_failed_invocation.email=TRUE
#wrapper.event.jvm_max_failed_invocations.email=TRUE
#wrapper.event.jvm_kill.email=TRUE
#wrapper.event.jvm_killed.email=TRUE
#wrapper.event.jvm_unexpected_exit.email=TRUE
#wrapper.event.wrapper_stop.email=TRUE
# Specify custom mail content
wrapper.event.jvm_restart.email.body=The JVM wasrestarted.\n\nPlease check on its status.\n
#********************************************************************
# Wrapper Windows Service Properties
#********************************************************************
# WARNING - Do not modify any of these properties when anapplication
# using this configurationfile has been installed as a service.
# Please uninstall theservice before modifying this section. The
# service can then bereinstalled.
# Name of the service
wrapper.name=这里是你的服务的名字
# Display name of the service
wrapper.displayname=这里是你的服务显示的名称
# Description of the service
wrapper.description=这里是你的服务的描述
# Service dependencies. Adddependencies as needed starting from 1
wrapper.ntservice.dependency.1=
# Mode in which the service is installed. AUTO_START, DELAY_START or DEMAND_START
#这里是服务的启动方式
wrapper.ntservice.starttype=AUTO_START
# Allow the service to interact with the desktop (Windows NT/2000/XPonly).
wrapper.ntservice.interactive=FALSE
其他地方可以根据自己的情况决定是否要配置,至此,已经配置完毕。运行InstallTestWrapper-NT.bat(也可以修改为自己应用的名字)即可安装服务。