tengine做集群负载的配置和注意事项,tengine集群


tengine是淘宝发起的一个项目,在nginx的基础上改造的,从配置上来讲,跟nginx几乎一样的。

我之前在世纪联华的项目中,做过负载均衡,现在根据当时记录的资料,现在写下来。

一、各种安装

除了tengine的包以外,还需要zlib、openssl、pcre

先把zlib、openssl、pcre的包,都放到usr/local下面,并且解压

以及进入目录安装,比如zlib,执行./configure --prefix=/usr/local/zlib,然后make,再make install

然后就是下载tengine,以及安装:

#wget http://tengine.taobao.org/download/tengine-2.1.1.tar.gz

#tar zxvf tengine-2.1.1.tar.gz

#cd tengine-2.1.1./configure --prefix=/usr/local/nginx --with-openssl=/usr/local/openssl-1.0.0a --with-pcre=/usr/local/pcre-8.13 --with-zlib=/usr/local/zlib-1.2.8makemake install

中间可能会遇到问题要解决,如果一切顺利,那么太美好了。

-----------------------------------------

二、配置 nginx.conf

1、负载

upstream tomcat_server {

#这里的“tomcat_server”,跟下面server配置的proxy_pass http://tomcat_server,要对应。

#upstream的负载均衡,weight是权重,可以根据机器配置定义权重。weigth参数表示权值,权值越高被分配到的几率越大。

server 221.173.80.121:80 weight=3;

server 221.173.80.122:80 weight=2;

server 221.173.80.123:80 weight=3;

}

server {

        listen       80;

        server_name  hlbw.zjlianhua.com;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {

            proxy_set_header Host $host;

            proxy_set_header X-Forwarded-For $remote_addr;

            proxy_pass http://tomcat_server;

        }

2、动静分离

静态资源,最好不要走tomcat了,这样速度会快很多,以及可以用缓存,比如图片,视频,静态html。

server {

        listen       80;

        server_name  localhost;

root /usr/local/data;

        location / {

            root   html;

            index  index.html index.htm;

        }

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|js|css|html|mp3)$ #设定访问静态文件直接读取不经过tomcat  

        {  

proxy_cache cache_one;

    proxy_set_header Host $host; 

            expires      1h;  

        }

-----------------------------------------

三、注意事项

1、缓存同步

各种框架,都有缓存同步机制的。标准版的hibernate是用ehcache做缓存,是用了RMI的方式。当然,其他框架都是有对应的同步框架的。靠大家分享了。

2、session同步

当时比较偷懒,用nginx的iphash,控制某个ip,固定访问某一台服务器。另外的方式,比如tomcat7以上有提供session同步,springsession等,都可以去学习一下,这个靠各位了。

3、静态变量同步

因为每台服务器存了自己的静态变量方便调用,所以,这部分,在做集群时,尽量迁移到redis上。否则就要存数据库中,或者发通知同步就慢了。其他更好的方式,我也没想到。

相关内容

    暂无相关文章