通过Wrapper 配置文件的分析与配置进一步了解构建Wrapper 服务需要注意的关键点及重要部分。
首先,打开conf 文件夹下的wrapper.conf配置文件,此配置文件时Wrapper 的主配置文件也是关键配置文件,下面开始一项一项的开始分析。
1.文件编码及子配置文件
文件头部包含了配置文件编码格式,子配置文件等相关信息,如下所示:
#文件编码,每个配置文件起始位置必须指定该文件的编码格式
encoding=UTF-8
# 如果包含配置文件出现问题可以使用debug调试模式,去掉一个"#",格式为#include.debug
#include.debug
# 包含子配置文件,可以是配置信息也可以是许可信息
include ../conf/wrapper-license.conf
include ../conf/wrapper2.conf
# 是否开启许可文件debug模式
wrapper.license.debug=TRUE
通过子配置文件的配置可以使主配置文件关联最多10级子配置,例如:wrapper.conf 包含 wrapper2.conf ,wrapper2.conf 包含wrapper3.conf ..... wrapper9.conf 包含wrapper10.conf,文件结构如下:
wrapper.conf |-wrapper2.conf |-wrapper3.conf ..... |-wrapper9.conf |-wrapper10.conf
也就是说配置文件嵌套层级最大可达10级,引用一张官方图片可以很好的说明:
如果子配置文件不存在时,那么它将被忽略,不会导致程序运行错误。
2.Wrapper 语言设置
通过这两项的设置可以指定Wrapper 的语言种类,可以在Wrapper 官网下到这些语言包支持,目前不支持中文。
# 指定Wrapper语言,默认使用系统语言
wrapper.lang=en_US
#指定Wrapper 语言资源位置,如果该文件不存在则默认设置为en_US
wrapper.lang.folder=../lang
3.Wrapper Java 相关属性配置
(1)java运行环境设置
# Java 程序配置:
# (1)默认使用PATH环境变量配置信息则使用下列配置形式
wrapper.java.command=java
# (2)如果想单独配置运行程序,则可采用此种配置方式
set.JAVA_HOME=/java/path
wrapper.java.command=%JAVA_HOME%/bin/java
# java程序日志级别
wrapper.java.command.loglevel=INFO
(2)程序入口
# Java Main class,也就是程序入口 #该类需要实现WrapperListener 接口并保证WrapperManager 得到初始化(调用WrapperManager.start(WrapperListener listener, String[] args) 方法)。 wrapper.java.mainclass=com.helloworld.hello.HelloWorld
(3)类库设置
# Java Classpath配置,必须从序号"1"开始,添加新的jar包后序号递增
wrapper.java.classpath.1=../lib/wrapper.jar
wrapper.java.classpath.2=../lib/hello.jar
# Java 类库路径 (Wrapper.DLL 或 libwrapper.so 依赖文件的存放位置)
wrapper.java.library.path.1=../lib
(4)JVM相关配置
# 32/64位选择,true为自动选择
wrapper.java.additional.auto_bits=TRUE
# Java附加参数
wrapper.java.additional.1=
附加参数即为java命令可选参数,如下所示:
-d32 use a 32-bit data model if available -d64 use a 64-bit data model if available -server to select the "server" VM The default VM is server. -cp <class search path of directories and zip/jar files> -classpath <class search path of directories and zip/jar files> A : separated list of directories, JAR archives, and ZIP archives to search for class files. -D<name>=<value> set a system property -verbose[:class|gc|jni] enable verbose output -version print product version and exit -version:<value> require the specified version to run -showversion print product version and continue -jre-restrict-search | -jre-no-restrict-search include/exclude user private JREs in the version search -? -help print this help message -X print help on non-standard options -ea[:<packagename>...|:<classname>] -enableassertions[:<packagename>...|:<classname>] enable assertions -da[:<packagename>...|:<classname>] -disableassertions[:<packagename>...|:<classname>] disable assertions -esa | -enablesystemassertions enable system assertions -dsa | -disablesystemassertions disable system assertions -agentlib:<libname>[=<options>] load native agent library <libname>, e.g. -agentlib:hprof see also, -agentlib:jdwp=help and -agentlib:hprof=help -agentpath:<pathname>[=<options>] load native agent library by full pathname -javaagent:<jarpath>[=<options>] load Java programming language agent, see java.lang.instrument -splash:<imagepath> show splash screen with specified image
内存大小设置:
# Java Heap 初始化大小(单位:MB) wrapper.java.initmemory=3 # Java Heap 最大值(单位:MB) wrapper.java.maxmemory=64
应用程序参数设置:
# 应用程序参数,也就是main函数的String[] args参数值,序号需从"1"开始,例如: wrapper.app.parameter.1=g21121 wrapper.app.parameter.2=http://286.iteye.com/
在main函数和start方法中添加了参数打印语句来观察参数是否已经传入,代码如下:
package com.helloworld.hello;
import org.tanukisoftware.wrapper.WrapperListener;
import org.tanukisoftware.wrapper.WrapperManager;
public class HelloWorld implements WrapperListener {
public static void main(String[] args) {
// 打印参数
for (String arg : args)
System.out.println(arg);
WrapperManager.start(new HelloWorld(), args);
}
@Override
public void controlEvent(int event) {
System.out.println("controlEvent(" + event + ")");
if ((event == WrapperManager.WRAPPER_CTRL_LOGOFF_EVENT) && (WrapperManager.isLaunchedAsService() || WrapperManager.isIgnoreUserLogoffs())) {
} else {
WrapperManager.stop(0);
}
}
@Override
public Integer start(String[] args) {
// 打印参数
for (String arg : args)
System.out.println(arg);
System.out.println("hello world!");
return null;
}
@Override
public int stop(int exitCode) {
System.out.println("stop(" + exitCode + ")");
return exitCode;
}
}
重新运行服务,结果如下:
root@TFS:/usr/local/wrapper/bin# ./hello console Running helloWorld... wrapper | --> Wrapper Started as Console wrapper | Java Service Wrapper Community Edition 64-bit 3.5.20 wrapper | Copyright (C) 1999-2013 Tanuki Software, Ltd. All Rights Reserved. wrapper | http://wrapper.tanukisoftware.com wrapper | wrapper | Launching a JVM... jvm 1 | g21121 jvm 1 | http://286.iteye.com/ jvm 1 | WrapperManager: Initializing... jvm 1 | g21121 jvm 1 | http://286.iteye.com/ jvm 1 | hello world! jvm 1 | stop(0) wrapper | <-- Wrapper Stopped
4.Wrapper 日志配置
# 是否显示debug日志 wrapper.debug=TRUE # 控制台信息输出格式 wrapper.console.format=PM # 控制台日志级别 wrapper.console.loglevel=INFO # 日志文件位置及名称 wrapper.logfile=../logs/wrapper.log # 日志文件输出格式 wrapper.logfile.format=LPTM # 日志文件日志级别 wrapper.logfile.loglevel=INFO # 限制日志文件大小,0为不限制,参数:k,m,g等 wrapper.logfile.maxsize=10m # 限制最大日志文件数,0为不限制 wrapper.logfile.maxfiles=0 # syslog 日志级别 wrapper.syslog.loglevel=NONE
5.Wrapper 基本属性配置
# 允许使用非连续编号的属性,例如:path的序号可以打乱
wrapper.ignore_sequence_gaps=TRUE
# 如果pid文件已经存在则不启动程序
wrapper.pidfile.strict=TRUE
# 控制台启动时显示的标题
wrapper.console.title=------------Wrapper Console------------
6.Wrapper JVM 检查
# 检测JVM中的死锁线程(需要标准版Wrapper)
wrapper.check.deadlock=TRUE
#间隔,单位:秒
wrapper.check.deadlock.interval=10
#出现死锁时处理事件
wrapper.check.deadlock.action=RESTART
#信息输出级别,FULL:全部;SIMPLE:精简;NONE:无;
wrapper.check.deadlock.output=FULL
DEBUG :
will cause a debug message to be logged. This is only really useful in helping to understand when the action is fired.
DUMP :
will invoke a thread dump.
GC (Since ver. 3.5.7) :
will invoke a full garbage collection sweep in the JVM. Be aware that doing this frequently can affect performance of the JVM as a full sweep will often cause all threads to freeze for the duration of the GC.
RESTART :
will stop the current JVM and then restart a new invocation.
SHUTDOWN :
will stop the JVM as well as the Wrapper.
USER_<n> (Professional Edition) :
will cause a user defined event to be fired. This can be either the sending of an email, or the execution of an external system command. The command could be anything from performing clean up operations to raising an SNMP trap.
PAUSE :
will pause the Java application if pausing is enabled and the JVM is running. See the wrapper.pausable property for details.
RESUME :
will resume the Java application if it is in a paused state. This could be used if the JVM is not stopped when paused. See the wrapper.pausable property for details.
SUCCESS (Since ver. 3.5.5) :
will tell the Wrapper to reset its internal failed invocation count and count the current JVM invocation as "successful". This is probably not useful in this context, but here for consistency with other properties.
NONE :
# 内存溢出检测,Wrapper提供了几种不同的匹配机制 wrapper.filter.trigger.999=wrapper.filter.trigger.*java.lang.OutOfMemoryError wrapper.filter.allow_wildcards.999=TRUE wrapper.filter.action.999=NONE wrapper.filter.trigger.1000=[Loaded java.lang.OutOfMemoryError wrapper.filter.action.1000=NONE wrapper.filter.trigger.1001=java.lang.OutOfMemoryError #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.event.default.email.debug=TRUE #smtp服务器地址 wrapper.event.default.email.smtp.host= #smtp服务器端口 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> # 指定文件内容 wrapper.event.jvm_restart.email.body=The JVM was restarted.\n\nPlease check on its status.\n
# 邮件日志相关配置 wrapper.event.default.email.attach_log=TRUE wrapper.event.default.email.maillog.lines=50 wrapper.event.default.email.maillog.format=LPTM wrapper.event.default.email.maillog.loglevel=INFO
# 触发事件,即当以下事件为true时发送邮件
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
以上是对wrapper.conf配置文件主要属性的介绍,Wrapper更多特性及应用还需要自己去结合文档深入学习,以下是Wrapper官网文档地址:
http://wrapper.tanukisoftware.com/doc/english/properties.html