現實需求:html
對於一些不是實時性的頁面,數據變化也不是很大的頁面,咱們能夠把該JSP或freemark頁面進行靜態化。java
這樣的好處,當咱們的服務掛掉了,經過nginx的代理,咱們依然能夠訪問相關的頁面。python
實現:linux
經過Shell腳本和linux的crontab的定時任務實現。這裏只講如何實現該腳本。nginx
1.首先咱們要學會使用curl腳本:json
下載單個文件,默認將輸出打印到標準輸出中(STDOUT)中centos
curl http://www.centos.org緩存
經過-o/-O選項保存下載的文件到指定的文件中:curl
-o:將文件保存爲命令行中指定的文件名的文件中jsp
-O:使用URL中默認的文件名保存文件到本地
# 將文件下載到本地並命名爲mygettext.html
curl -o mygettext.html http://www.gnu.org/software/gettext/manual/gettext.html
# 將文件保存到本地並命名爲gettext.html
curl -O http://www.gnu.org/software/gettext/manual/gettext.html
例子1:index.sh 具體的腳本以下: 說明: 1.http://127.0.0.1:8082/index?clean=true 是咱們本機的一個容器服務提供的JSP頁面。 2.status1 ,status2,status3,status 是爲了判斷服務返回的JSP頁面是否是合法的, 根據你的JSP頁面具體實現。由於一旦JSP頁面錯誤了,返回了錯誤的頁面覆蓋了好的頁面, 靜態化就沒有任何意義了。 3.cp ,mv 操做是爲了備份和覆蓋當前的靜態頁面。 4.sed的簡單用法,sed的操做不是必須的。http://www.cnblogs.com/dong008259/archive/2011/12/07/2279897.html #!/bin/sh #resin index #/opt/www/pages/index.tmp #index page http_code=`curl -o /opt/www/pages/index.tmp -s -w %{http_code} http://127.0.0.1:8082/index?clean=true`; if [ ${http_code} -eq 200 ] then status1=`cat /opt/www/pages/index.tmp |grep -o "content_item"|wc -l` status2=`cat /opt/www/pages/index.tmp |grep -o "photocdn"|wc -l` status3=`cat /opt/www/pages/index.tmp |grep -o "focus"|wc -l` status=`cat /opt/www/pages/index.tmp |grep -o "data-columnid"|wc -l` # echo "${status1},${status2},${status3},${status}" if [ ${http_code} -eq 200 ] && [ ${status} -ge 15 ] && [ ${status1} -ge 80 ] && [ ${status2} -ge 80 ] && [ ${status3} -ge 1 ] then date=`date +"%Y-%m-%d-%H-%M"`; cp /opt/www/pages/index.html /opt/www/pages/index.${date}; mv /opt/www/pages/index.tmp /opt/www/pages/index.html; #debug_str='<!--'`date +"%Y-%m-%d-%H-%M"`'-->' #sed -i '1a\'${debug_str} /opt/www/pages/index.html else echo "build index ${status} ${status2} ${status3} fail" fi; else echo "build index fail!"; fi;
例子2:靜態化相關的子頁面,test2.sh #!/bin/sh #pgc page cids=(70040000 70080000 70060000 70070000 70090000 70120000 70100000 70110000 71010000 71020000 71030000 71060000 71050000 71040000 71070000 71080000 71090000 71100000 71220000 71110000 71120000 71230000 71130000 71140000 71150000 711600 00 71170000 71180000 71200000 71260000 71270000) i=0 length=${#cids[*]} while [ $i -lt $length ] ; do cid=${cids[$i]} http_code=`curl -o /opt/www/pages/pgc$cid.tmp -s -w %{http_code} http://127.0.0.1:8082/pgc/$cid?clean=true`; status=`cat /opt/www/pages/pgc$cid.tmp |grep -o "data-columnid"|wc -l` if [ ${http_code} -eq 200 ] && [ ${status} -ge 1 ] then date=`date +"%Y-%m-%d-%H-%M"`; if [ -f /opt/www/pages/pgc$cid.html ] then cp /opt/www/pages/pgc$cid.html /opt/www/pages/pgc$cid.${date}; fi; mv /opt/www/pages/pgc$cid.tmp /opt/www/pages/pgc$cid.html; debug_str='<!--'`date +"%Y-%m-%d-%H-%M"`'-->' sed -i '1a\'${debug_str} /opt/www/pages/pgc$cid.html else echo "build hots$cid fail!" fi; let i++ done
例子3:test3.sh #!/bin/sh #hots page #精選: hots128.html 搞笑:hots132.html #福利:hots133.html 新聞:hots129.html #娛樂:hots130.html 體育:hots131.html #一分鐘:hots134.html 世界盃:hots177.html #美劇鬧:hots164.html #son hots249.html cids=(128 132 129 130 131 223 224 225 134 164 249) i=0 length=${#cids[*]} while [ $i -lt $length ] ; do cid=${cids[$i]} http_code=`curl -o /opt/www/pages/hots$cid.tmp -s -w %{http_code} http://127.0.0.1:8082/hots/$cid?clean=true`; if [ ${http_code} -eq 200 ] then date=`date +"%Y-%m-%d-%H-%M"`; if [ -f /opt/www/pages/hots$cid.html ] then cp /opt/www/pages/hots$cid.html /opt/www/pages/hots$cid.${date}; fi; mv /opt/www/pages/hots$cid.tmp /opt/www/pages/hots$cid.html; debug_str='<!--'`date +"%Y-%m-%d-%H-%M"`'-->' sed -i '1a\'${debug_str} /opt/www/pages/hots$cid.html else echo "build hots$cid fail!" fi; let i++ done
例子4:好比咱們網站通用的引用的頭部和尾部,要常常改, 咱們不能每次去修改JSP再去上線吧。這些能夠有網站的編輯維護, 咱們按期下載相關的文件就行了。 #!/bin/sh wget http://tv.baidu.com/s2012/frag/83/20092861_344978361.inc -q -O /opt/www/h5-server/WEB-INF/includes/footer_tmp.jsp >/dev/null 2>&1 cd /opt/www/h5-server/WEB-INF/includes/ iconv -f gb2312 -t UTF-8 footer_tmp.jsp -o footer.jsp rm -rf footer_tmp.jsp 例子5:若是咱們想定時刷新某個接口,或者定時讓某個數據刷入緩存。 咱們也能夠作一個定時任務的腳本,定時執行腳本。 #!/bin/sh curl -o /tmp/cooperation.tmp http://localhost:8082/cooperation/listall.json?clean=true > /dev/null 2>&1 exit 0
例子8:test8 #!/bin/sh wget http://tv.baidu.com/s2013/frag/78/120173_381017846.inc -q -O /opt/www/h5-server/WEB-INF/includes/navProms_tmp.jsp >/dev/null 2>&1 cd /opt/www/h5-server/WEB-INF/includes/ iconv -f GBK -t UTF-8 navProms_tmp.jsp -o navProms.jsp sed -i '1i<%@ page language=\"java\" pageEncoding=\"UTF-8\" contentType=\"text/html;charset=utf-8\"%>' /opt/www/h5-server/WEB-INF/includes/navProms.jsp #yes|cp -af /opt/www/h5-server/WEB-INF/includes/navProms.jsp /opt/www/HotHtml5/WEB-INF/includes/navProms.jsp rm -rf navProms_tmp.jsp