用压缩工具来提高web应用程序效率

    在网站性能优化时候,Yahoo Performance Team的rules for high performance web sites是较好的调优指南(或者yahoo的大牛Steve Souders在其《High Performance Web Sites》书中提出提高网页效率的14条准则),Firefox的插件Firebug及Yahoo提供的Firebug 插件Yslow(http://developer.yahoo.com/yslow/)也是进行性能调优较好的工具。

    按照调优指南,一个重要的原则就是对js、css等文件采用Gzip、deflate等压缩工具进行压缩,以降低网络传输带宽。

1、网页压缩原理

    网页压缩是一项由WEB服务器(应用服务器)和浏览器之间共同遵守的协议,也即WEB服务器(应用服务器)和浏览器都必须支持该技术,现在流行的浏览器(IE、FireFox、Opera )都是支持的;Lighttpd、Apache 、Ngix、 IIS、Tomcat等应用服务器或web服务器都支持。双方的协商过程如下:

  1. 浏览器请求某URL,并在请求的头 (head) 中设置属性 accept-encoding 值为 gzip, deflate,表明浏览器支持 gzip 和 deflate 这两种压缩方式(事实上 deflate 也是使用 gzip 压缩协议);
  2. WEB 服务器接收到请求后判断浏览器是否支持压缩,如果支持就传送压缩后的响应内容,否则传送不经过压缩的内容;
  3. 浏览器获取响应内容后,判断内容是否被压缩,如果是则解压缩,然后显示响应页面的内容。

    具体的交互过程可以利用Livehttpheader来查看http的交互过程。

2、网页压缩的方法

对JS、CSS文件压缩分为两大方面:

  • 对js、CSS采用yuicompressor、JsPacker这样的工具对js等文件进行压缩。主要是删除诸如空行、回车换行、注释等无用的字符,减少文件本身的大小,这也是诸如jquery、prototype等javascript库发布时候所采用的方法。
  • 采用应用服务器(web服务器)及浏览器对gzip、deflate等压缩方法的支持来对请求进行实时的压缩

    在实际应用时候,应当结合两种方法来使用。在大型应用中,会采用lighttpd、apache这样的web服务器来做前端,可以在lighttpd或apache中配置gzip、deflate支持。这里只是简单说明一下采用Jboss对gzip的支持来提高web应用程序的效率

3、修改jboss配置,让其支持gzip压缩

    这里使用jboss 4.2.2,servlet容器采用的是tomcat,因此让jboss支持gzip的配置方法,实际上就是修改tomcat的配置

    修改jboss-4.2.2.GA/server/default/deploy/jboss-web.deployer/server.xml,增加如下内容

    <Connector port=”80″ address=”0.0.0.0″

         maxThreads=”1500″ maxHttpHeaderSize=”8192″

         emptySessionPath=”true” protocol=”HTTP/1.1″

         enableLookups=”false” redirectPort=”8443″ acceptCount=”100″

         connectionTimeout=”20000″ disableUploadTimeout=”true”

         compression=”on”      

         compressableMimeType=”text/html,text/xml,text/plain,text/css,text/javascript,application/xhtml+xml,

         application/x-javascript,application/javascript,text/xhtml”

        />

    在Tomcat与gzip相关的几个参数如下,具体可以参考

http://www.jboss.org/file-access/default/members/jbossweb/freezone/docs/2.1.0/config/printer/http.html

或:

http://tomcat.apache.org/tomcat-5.5-doc/config/http.html

  • compressableMimeType

    The value is a comma separated list of MIME types for which HTTP compression may be used. The default value is text/html,text/xml,text/plain.

  • compression

    The Connector may use HTTP/1.1 GZIP compression in an attempt to save server bandwidth. The acceptable values for the parameter is “off” (disable compression), “on” (allow compression, which causes text data to be compressed), “force” (forces compression in all cases), or a numerical integer value (which is equivalent to “on”, but specifies the minimum amount of data before the output is compressed). If the content-length is not known and compression is set to “on” or more aggressive, the output will also be compressed. If not specified, this attribute is set to “off”.

  • noCompressionUserAgents

  The value is a comma separated list of regular expressions matching user-agents of HTTP clients for which compression should not be used, because these clients, although they do advertise support for the feature, have a broken implementation. The default value is an empty String (regexp matching disabled).

采用gzip压缩后,一个典型例子为struts2中所采用的dojo.js,原来大小为258K,压缩后只有72K,压缩的效果还是很显著的

4、调试

    在web开发时候firefox提供了比IE等浏览器更好的web开发工具

    Firebug:http://www.getfirebug.com/

    Yslow:http://developer.yahoo.com/yslow

    Livehttpheader:http://livehttpheaders.mozdev.org/

    Web Developer: http://chrispederick.com/work/web-developer/

4.1、采用Yslow查看对网站的建议

yslow

3.2、采用Firebug参看页面交互情况

firebug

另外今天是Firefox3正式发布的日子,下载支持一下,感谢Firefox提供了如此好的工具。

Jboss虚拟主机安装部署指南

1. 环境说明:

x.x.x.137:应用服务器,内网地址:192.168.1.137

x.x.x.180:数据库服务器,内网地址:192.168.1.180

数据库服务器和应用服务器之间通信通过内网地址通信。

2. 操作系统

操作系统版本:Redhat AS 5

安装:操作系统按照缺省的安装方式安装,无特殊要求。

由于没有硬件防火墙,因此需要安装iptables,建议不要安装selinux。

相关应用安装到/opt目录下,包括jdk、应用等。

3. 防火墙配置

防火墙策略:

只允许应用服务器x.x.x.137(192.168.1.137)访问数据库服务器x.x.x.180(192.168.1.180)的3306端口,且允许数据库服务器 x.x.x.180(192.168.1.180)能够访问应用服务器的x.x.x.137(192.168.1.137)的[1024,65535]端口(socket连接的随机端口)。

配置方法:

在数据库服务器x.x.x.180上执行如下操作:

1) 直接修改iptables的配置文件/etc/sysconfig/iptables

在:RH-Firewall-1-INPUT – [0:0]增加如下内容:

-A INPUT -p tcp -s x.x.x.137 –sport 1024:65535 -d x.x.x.180 –dport 3306 -m state –state NEW,ESTABLISHED -j ACCEPT

-A OUTPUT -p tcp -s x.x.x.180 –sport 3306 -d x.x.x.137 –dport 1024:65535 -m state –state ESTABLISHED -j ACCEPT

-A INPUT -p tcp -s 192.168.1.137 –sport 1024:65535 -d 192.168.1.180 –dport 3306 -m state –state NEW,ESTABLISHED -j ACCEPT

-A OUTPUT -p tcp -s 192.168.1.180 –sport 3306 -d 192.168.1.137 –dport 1024:65535 -m state –state ESTABLISHED -j ACCEPT

2) 重启iptables

service iptables restart

注意还要确认应用服务器(x.x.x.137)上开通了80端口,允许从公网访问:

-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT

4. 在域名服务器上做域名地址映射

在域名服务器上做映射,将e.yeeach.com映射到x.x.x.137地址,没有做URL指向转发

为了简化代理商销售人员记忆,避免记忆ip及复杂域名,因此采用二级域名+虚拟主机方式来实现此目标。

5. 安装JDK

使用Java SE 6 Update 6版本

wget -O jdk-6u6-linux-i586.bin \

http://cds.sun.com/is-bin/INTERSHOP.enfinity/WFS/CDS-CDS_Developer-Site/en_US/-/USD/VerifyItem-Start/jdk-6u6-linux-i586.bin?BundledLineItemUUID=A_JIBe.mKjcAAAEZ.0gv8eBL&OrderID=dy5IBe.mH10AAAEZ70gv8eBL&ProductID=VXZIBe.ootIAAAEZTrBAkQve&FileName=/jdk-6u6-linux-i586.bin

chmod 755 jdk-6u6-linux-i586.bin

./jdk-6u6-linux-i586.bin  

mv  jdk1.6.0_06/ /opt/jdk

修改/etc/bashrc(或者也可以修改用户的bash配置文件:~/.bashrc_profile) ,在尾部增加如下内容:

export JAVA_HOME=/opt/jdk

export PATH=$JAVA_HOME/bin:$PATH

6. 安装Jboss 4.2.2

wget http://jaist.dl.sourceforge.net/sourceforge/jboss/jboss-4.2.2.GA.zip

unzip jboss-4.2.2.GA.zip

mv jboss-4.2.2.GA  /opt

7. 增加Jboss APR支持

参看使用APR( Apache Portable Runtime)来提升jboss性能

wget http://www.jboss.org/file-access/default/members/jbossweb/freezone/dist/2.0.4.GA/jboss-native-2.0.4-linux2-x86-ssl.tar.gz

mkdir jboss-native

mv  jboss-native-2.0.4-linux2-x86-ssl.tar.gz  jboss-native/

cd jboss-native/

tar zxvf jboss-native-2.0.4-linux2-x86-ssl.tar.gz

mv  bin/META-INF/bin/linux2/x86/*  /opt/jboss-4.2.2.GA/bin/

mkdir /opt/jboss-4.2.2.GA/bin/native

cp –r bin/META-INF/lib/linux2/x86/*   /opt/jboss-4.2.2.GA/bin/native

8. 修改Jboss 参数

1) 修改JVM参数

修改bin/run.conf,修改参数:

if [ "x$JAVA_OPTS" = "x" ]; then

         JAVA_OPTS=”-Xms512m -Xmx1024m -server -XX:MaxPermSize=300m -XX:MaxNewSize=300m -server -Dsun.rmi.dgc.client.gcInterval=3600000 -Dsun.rmi.dgc.server.gcInterval=3600000″

fi

2) 修改jboss-4.2.2.GA/server/default/deploy/jboss-web.deployer/server.xml,将

将8080修改为80

将${jboss.bind.address}修改为0.0.0.0,允许从eth0,eth1,lo都能够访问jboss 80端口

  也可以在启动参数中指定ip地址选项,指定绑定的ip地址:run.sh -b x.x.x.137
9. 去除jboss的冲突包

由于jboss自身带的hibernate-annotations.jar版本与项目使用的版本存在冲突,因此去除hibernate-annotations.jar包

mv /opt/jboss-4.2.2.GA/server/default/lib/hibernate-annotations.jar /opt/jboss-4.2.2.GA/server/default/lib/hibernate-annotations.jar.bak

 
10. 部署esales.war到/opt/jboss-4.2.2.GA/server/default/deploy

修改数据库连接池等应用相关参数

11. 增加虚拟主机Host节点

在/opt/jboss-4.2.2.GA/server/default/deploy/jboss-web.deployer/server.xml 中的 Engine加一个 Host 节点,内容如下:

<Host name=”esales” autoDeploy=”false”         

      deployOnStartup=”false” deployXML=”false”>

    <Alias>e.yeeach.com</Alias>

    <Valve className=”org.apache.catalina.valves.AccessLogValve”

           prefix=”esales” suffix=”.log” pattern=”common”         

           directory=”${jboss.server.home.dir}/log”/>

    <DefaultContext cookies=”true” crossContext=”true” override=”true”/>

</Host>

12. 在eSales应用中配置虚拟主机

在/opt/jboss-4.2.2.GA/server/default/deploy/esales.war/WEB-INF下创建文件jboss-web.xml,内容如下:

<jboss-web>

    <context-root>/</context-root>

    <virtual-host>e.yeeach.com</virtual-host>

</jboss-web>

 

 

使用APR( Apache Portable Runtime)来提升jboss性能

从tomcat5.0后,可以用 Apache Portable Runtime 来通过jni来使用native接口,提升Tomcat及Jboss的性能,同时可以提升Web静态页面的处理能力,从理论上可以不再需要专门的Web Server来处理静态页面了。当然考虑到负载均衡、rewrite、虚拟主机等支持的考虑,还会采用lighttpd来作为前端的Web Server。

在对eSales系统部署时候,采用Jboss 4.2.2.GA,简单描述一下Jboss-Tomcat 使用APR的方法,关于Tomcat支持APR的参看相关文档。

1、下载boss-4.2.2.GA,最新稳定版本为4.2.2

wget http://jaist.dl.sourceforge.net/sourceforge/jboss/jboss-4.2.2.GA.zip

2、下载APR,由于Jboss对APR有一些特别的改动,因此需要从Jboss网站下载:

wget http://www.jboss.org/file-access/default/members/jbossweb/freezone/dist/2.0.4.GA/jboss-native-2.0.4-linux2-x86-ssl.tar.gz

3、解压jboss,假设安装路径为/opt

unzip jboss-4.2.2.GA.zip

mv jboss-4.2.2.GA  /opt

4、启动jboss,测试一下Jboss安装情况

cd jboss-4.2.0.GA/bin
./run.sh
 
注意console输出中类似如下信息:
21:51:56,325 INFO  [AprLifecycleListener] The Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /opt/jdk/jre/lib/i386/server:/opt/jdk/jre/lib/i386:/opt/jdk/jre/../lib/i386:/usr/java/packages/lib/i386:/lib:/usr/lib

退出Jboss

5、解压jboss-native-2.0.4-linux2-x86-ssl.tar.gz

mkdir jboss-native

mv  jboss-native-2.0.4-linux2-x86-ssl.tar.gz  jboss-native/

cd jboss-native/

tar zxvf jboss-native-2.0.4-linux2-x86-ssl.tar.gz

mv  bin/META-INF/bin/linux2/x86/*  /opt/jboss-4.2.2.GA/bin/

mkdir /opt/jboss-4.2.2.GA/bin/native

cp –r bin/META-INF/lib/linux2/x86/*   /opt/jboss-4.2.2.GA/bin/native

6、再次启动jboss

/opt//jboss-4.2.2.GA/bin/run.sh &

注意console输出的如下信息:

21:57:36,150 INFO  [AprLifecycleListener] Loaded Apache Tomcat Native library 1.1.13.

21:57:36,156 INFO  [AprLifecycleListener] APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true].

21:57:44,286 INFO  [Http11AprProtocol] Initializing Coyote HTTP/1.1 on http-127.0.0.1-8080

21:57:44,288 INFO  [AjpAprProtocol] Initializing Coyote AJP/1.3 on ajp-127.0.0.1-8009

说明apr已经正常启用

参考文档:

http://wiki.jboss.org/wiki/HowToAddAprToJBoss

 

Technorati 标签: ,,,

Jboss 4.2.2.GA启动后只能从本机访问,不能从远程访问的解决办法

在Redhat AS 4服务器(多网卡)上安装Jboss 4.2.2 GA后,只能从本地地址localhost或127.0.0.1访问80或8080端口,也即http://127.0.0.1,而不能使用本机的公网地址访问http://www.yeeach.com

开始以为是iptables防火墙的问题,修改了防火墙策略,还是不行。用netstat –na|grep 80 参看,发现绑定到127.0.0.1上:

tcp        0      0 127.0.0.1:80              0.0.0.0:*                   LISTEN   

因此应该是jboss启动时候绑定到了本地回路(Local Loopback )lo网卡上,修改方法如下:

修改jboss-4.2.2.GA/server/default/deploy/jboss-web.deployer/server.xml,将

1、将8080修改为80

2、将${jboss.bind.address}修改为0.0.0.0,允许从eth0,eth1,lo都能够访问jboss 80端口

注意:

1、在/etc/sysconfig/iptables中,已经允许80端口对外开放,如果没有,请授权

-A RH-Firewall-1-INPUT -m state –state NEW -m tcp -p tcp –dport 80 -j ACCEPT

2、在Jboss 4.2.2.GA中,Tomcat已经与Jboss整合到一起,已经没有以前版本中的jbossweb-tomcat50.sar目录,相关配置主要在jboss-web.deployer中

Technorati 标签: ,,,