jetty官网:http://www.eclipse.org/jetty/
具体配置流程如下1, 2:
1. Nginx 添加监听域名和路由端口.
#当前应用的可用流(可无限添加)
upstream pisaTbt0 {
server 10.221.12.182:8051 weight=1;
server 10.221.12.182:8052 weight=1;
keepalive 256;
}
upstream pisaTbt1 {
server 10.221.12.182:8061 weight=1;
server 10.221.12.182:8062 weight=1;
keepalive 256;
}
#监听域名,此处可配置监听多个端口
server {
listen 80;
server_name 203.195.182.194;
location ^~ /pisa-tbt/ {
proxy_pass http://pisaTbt0;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
server {
listen 8080;
server_name 203.195.182.194;
location ^~ /pisa-tbt/ {
proxy_pass http://pisaTbt1;
proxy_set_header Host $host:8080;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
2. 配置和启动Jetty独立服务
1) 拷贝davinciDev或WukongDev为自己的应用目录(除了配置文件和日志目录其余全部为软连接, 不用担心版本问题)
3) 进入{Jetty目录}/standalone/{自己的应用地址}
4) 修改server.conf, 指定当前服务的两个端口号, 和上方的Nginx流内的端口号对应.
5) 修改etc/jetty-http.xml中的jetty.host为要启动的host
6) 执行 server.sh start 启动服务, 启动后将自动在两个端口号内选择一个执行.
7) 如果需要热重启服务时则执行 server.sh switch 进行切换, 脚本会启动新服务并保证可用的情况下关闭老服务. Nginx则会自动路由请求到新服务.
Jetty独立服务介绍:
目录结构:
./data 存储PID和启动进度信息.
./etc 应用目录配置, 启动监听配置.(设置项会自动配置, 所以无需更改),此目录中主要包括jetty-deploy.xml和jetty-started.xml,这两个文件会替代全局配置
./log jetty的启动日志(针对当前独立应用)
./server.conf 启动端口号配置
./server.sh 独立服务执行脚本(包括三个命令, server.sh start:启动服务, server.sh stop:关闭服务, server.sh switch:切换服务)
./webapps 应用存放位置
jetty-deploy.xml样例
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<!-- =============================================================== -->
<!-- Create the deployment manager -->
<!-- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -->
<!-- The deplyment manager handles the lifecycle of deploying web -->
<!-- applications. Apps are provided by instances of the -->
<!-- AppProvider interface. -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Call name="addBean">
<Arg>
<New id="DeploymentManager" class="org.eclipse.jetty.deploy.DeploymentManager">
<Set name="contexts">
<Ref refid="Contexts" />
</Set>
<Call name="setContextAttribute">
<Arg>org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern</Arg>
<Arg>.*/servlet-api-[^/]*\.jar$</Arg>
</Call>
<!-- Add a customize step to the deployment lifecycle -->
<!-- uncomment and replace DebugBinding with your extended AppLifeCycle.Binding class
<Call name="insertLifeCycleNode">
<Arg>deployed</Arg>
<Arg>starting</Arg>
<Arg>customise</Arg>
</Call>
<Call name="addLifeCycleBinding">
<Arg>
<New class="org.eclipse.jetty.deploy.bindings.DebugBinding">
<Arg>customise</Arg>
</New>
</Arg>
</Call> -->
<Call id="webappprovider" name="addAppProvider">
<Arg>
<New class="org.eclipse.jetty.deploy.providers.WebAppProvider">
<Set name="monitoredDirName"><Property name="jetty.webapps"/></Set>
<Set name="defaultsDescriptor"><Property name="jetty.home" default="." />/etc/webdefault.xml</Set>
<Set name="scanInterval">0</Set>
<Set name="extractWars">true</Set>
<Set name="configurationManager">
<New class="org.eclipse.jetty.deploy.PropertiesConfigurationManager"></New>
</Set>
</New>
</Arg>
</Call>
</New>
</Arg>
</Call>
</Configure>
jetty-started.xml样例
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_0.dtd">
<!-- =============================================================== -->
<!-- Mixin the Start FileNoticeLifeCycleListener -->
<!-- =============================================================== -->
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<Call name="addLifeCycleListener">
<Arg>
<New class="org.eclipse.jetty.util.component.FileNoticeLifeCycleListener">
<Arg><Property name="jetty.state"/></Arg>
</New>
</Arg>
</Call>
</Configure>
server.sh样例
#!/usr/bin/env bash
#Author Simon
# set profile
source ~/.bash_profile
# set project directory
if [ -z "$PROJECT_HOME" ]
then
START_SH=$0
case "$START_SH" in
/*) PROJECT_HOME=${START_SH%/*} ;;
./*/*/*) PROJECT_HOME=${START_SH%/*} ;;
./*) PROJECT_HOME=. ;;
esac
fi
cd "$PROJECT_HOME"
PROJECT_HOME=$PWD
# set jetty home directory
JETTY_HOME=${PROJECT_HOME%/*/*}
cd "$JETTY_HOME"
# create project log directory
logDir="$PROJECT_HOME/log"
if [ ! -d ${logDir} ]
then
$(mkdir -p ${logDir})
fi
# create project data directory
dataDir="$PROJECT_HOME/data"
if [ ! -d ${dataDir} ]
then
$(mkdir -p ${dataDir})
fi
source "$PROJECT_HOME/server.conf"
# jetty status
if [ -z "$JETTY_STATE" ]
then
JETTY_STATE=$dataDir/jetty.state
fi
rm -f $JETTY_STATE
if [ -z "$JAVA" ]
then
JAVA=$(which java)
fi
# Add jetty properties to Java VM options.
TMPDIR=${TMPDIR:-/tmp}
JAVA_OPTIONS+=("-Djetty.home=$JETTY_HOME" "-Djava.io.tmpdir=$TMPDIR")
# This is how the Jetty server will be started
JETTY_START=$JETTY_HOME/start.jar
[ ! -f "$JETTY_START" ] && JETTY_START=$JETTY_HOME/lib/start.jar
START_INI=$(dirname $JETTY_START)/start.ini
[ -r "$START_INI" ] || START_INI=""
CONFIGS=(
${JETTY_HOME}/etc/jetty-logging.xml
${PROJECT_HOME}/etc/jetty-deploy.xml
${PROJECT_HOME}/etc/jetty-started.xml
)
JETTY_ARGS=(
"jetty.logs"=${PROJECT_HOME}/log
"jetty.webapps"=${PROJECT_HOME}/webapps
"jetty.state"=${JETTY_STATE}
)
usage()
{
echo "Usage: ${0##*/} {start|stop|switch}"
exit 1
}
[ $# -gt 0 ] || usage
isRun()
{
kill -0 "$1" 2>/dev/null
}
getPid()
{
echo $(cut -d\| -f 1 ${dataDir}/project_$1.pid 2>/dev/null)
}
#引用自jetty.sh
started()
{
# wait for 60s to see "STARTED" in PID file, needs jetty-started.xml as argument
for T in 1 2 3 4 5 6 7 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
do
sleep 4
[ -z "$(grep STARTED $1 2>/dev/null)" ] || return 0
[ -z "$(grep STOPPED $1 2>/dev/null)" ] || return 1
[ -z "$(grep FAILED $1 2>/dev/null)" ] || return 1
local PID=$2 || return 1
kill -0 "$PID" 2>/dev/null || return 1
echo -n "."
done
return 1
}
shutdownServer()
{
PID=$1
if isRun $PID
then
kill "$PID" 2>/dev/null
TIMEOUT=30
while isRun $PID; do
if (( TIMEOUT-- == 0 )); then
kill -KILL "$PID" 2>/dev/null
fi
sleep 1
done
if! isRun $PID
then
return0
else
return1
fi
fi
return0
}
start()
{
local currentPort
local port=$1
if[ $port >0]
then
currentPort=$port
else
currentPort=${PORT[0]}
fi
pidFilePath=${dataDir}/project_${currentPort}.pid
PID=$(getPid $currentPort)
if isRun $PID
then
echo "jetty already running!"
exit
fi
#此处限制访问IP为内网
RUN_ARGS=(${JAVA_OPTIONS[@]}-jar "$JETTY_START" ${JETTY_ARGS[@]} jetty.host="10.207.169.50" jetty.port=${currentPort}"${CONFIGS[@]}")
RUN_CMD=("$JAVA" ${RUN_ARGS[@]})
#start jetty
echo "${RUN_CMD[@]}"
"${RUN_CMD[@]}">/dev/null&
JETTY_PID=$!
disown $!
if started "$JETTY_STATE""$JETTY_PID"
then
echo ""
echo "OK `date`"
else
echo ""
echo "FAILED `date`"
fi
if! isRun $JETTY_PID
then
echo "jetty run fail!"
exit
fi
echo "Ok! new server startup, PID:[$JETTY_PID], PORT:[$currentPort]"
echo "$JETTY_PID|$currentPort"> $pidFilePath
}
stop()
{
for port in ${PORT[@]}
do
PID=$(getPid $port)
if isRun $PID
then
shutdownServer $PID
if! isRun $PID
then
closeFlag=1
echo "Ok! PORT:[$port]"
fi
fi
done
if[! $closeFlag ]
then
echo "stop fail(not pid file | not start)"
fi
}
switch()
{
for port in ${PORT[@]}
do
local pid=$(getPid $port)
if isRun $pid
then
alreadyStartPort=$port
fi
done
if[! $alreadyStartPort ]
then
echo "jetty not start!"
exit
fi
local pid=$(getPid $alreadyStartPort)
echo "current running PID:[$pid], PORT:[$alreadyStartPort]"
for item in ${PORT[@]}
do
if((item != $alreadyStartPort))
then
#启动此端口的服务
echo "start another jetty PORT:[$item]"
start $item
sleep 2
if shutdownServer $pid
then
echo "Ok! old server shutdown, PID:[$pid], PORT:[$alreadyStartPort]"
else
echo "fail! server shutdown, PID:[$pid], PORT:[$alreadyStartPort]"
fi
fi
done
}
case"$1"in
start)
echo "Starting Jetty..."
start
echo "End"
;;
stop)
echo "Stopping Jetty..."
stop
echo "End"
;;
switch)
echo "Switching Jetty..."
switch
echo "End"
;;
esac
exit0
jetty war包解压设置
自定义的解压路径设置比较复杂,最简的就是在jetty根目录下增加一work目录,jetty就会把war包解压路径放在work下面
jetty根目录下的start.ini可以配置jvm的一些参数,简要示例如下,在末尾添加以下参数:
--exec
-Xmx1024m
-Xms1024m
-Xmn512m
-XX:PermSize=128m
-XX:MaxPermSize=128m
-XX:MaxTenuringThreshold=7
-XX:GCTimeRatio=19
-XX:+UseConcMarkSweepGC
-XX:ParallelGCThreads=8
-XX:+CMSClassUnloadingEnabled
-XX:+UseCMSCompactAtFullCollection
-XX:CMSFullGCsBeforeCompaction=5
-XX:-CMSParallelRemarkEnabled
-XX:+DisableExplicitGC
-XX:CMSInitiatingOccupancyFraction=70
-XX:SoftRefLRUPolicyMSPerMB=0
jetty根目录下的start.d下的http.ini可以配置使用start.jar启动时使用的端口,此处如果使用了第二步standalone方式的启动形式,则最好把jetty.port注掉,否则有可能会有冲突
etc/jetty-http.xml可配置jetty总的监听端口