把Spring boot jar作为Windows服务运行
把Spring boot jar作為Windows服務運行
當你用Spring boot時,通過Java -jar就可以啟動,但你不能每次開機都這樣執行一遍吧?特別是當你把jar包發布給客戶的時候,你總不能要求客戶每次開機后都手動執行啟動。所以,最好的方式就是作為服務,隨機器開機啟動。
Java Service Wrapper
本文就講一講如何把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的服務或守護進程。
Java Service Wrapper下載
下載地址為 https://wrapper.tanukisoftware.com/doc/english/download.jsp,最新版本為3.5.34,根據自己的情況選擇自己對應的操作系統和位數。
通過下載頁面我們可以看到Wrapper幾乎支持所有的系統環境,Windows32位社區版是免費使用的,Windows 64位沒有免費版本。我們這里主要講Windows系統。
Java Service Wrapper主要目錄
解壓后的文件目錄如下圖:
這些目錄并不是你做服務必須的,有些可以不不要。各目錄的主要作用如下:
?
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(可以配置改名的)
Java Service Wrapper配置
然后,將你的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.console.title=@app.long.name@
?
#********************************************************************
# 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=<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=<Sender email>
#wrapper.event.default.email.recipient=<Recipient email>
?
# 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(也可以修改為自己應用的名字)即可安裝服務。
總結
以上是生活随笔為你收集整理的把Spring boot jar作为Windows服务运行的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【SSH进阶之路】一步步重构MVC实现S
- 下一篇: java armeabi_armeabi