写在前面:
最近几天一直做nginx的负载均衡,折腾了将近一周,在网上查了很多资料,终于在今天将一系列的流程及功能跑通。在学习的过程中,发现网上大多数资料都写的很笼统,或者就是写的让新手迷迷糊糊,似懂非懂,很多的配置参数等都不是很明白,配置的时候也就造成很多地方不确切。现在我对我这几天的配置学习予以总结,从下载到安装、环境的搭建,以及负载均衡的配置,参数的详细解释,以及memcached的session共享,都会一一介绍,并保证所用的代码和配置都经过本人的测试并实现功能,希望给予对亟需解决linux+tomcat+nginx+memcached实现负载均衡以及session共享的同行特别是新手有一些帮助。
一、环境的介绍:
操作系统采用: CentOS-5.6-i386
项目部署在:tomcat6.0,tomcat一共有两个,分别命名为tomcat6.0_1、tomcat.0_2.这两台机器都运行在一台服务器上,IP为192.168.128.129
负载均衡采用版本:nginx-1.2
session的管理:memcached-1.4.15
由于资源有限,所有的程序安装测试均在一台服务器上完成。
二、jdk的环境的搭建tomcat的安装与项目的部署:
1、具体的部署请参考我前两天的文章:http://blog.csdn.net/jessonlv/article/details/7987434,所有的代码都通过测试。按照上面的方式分别安装部署两个tomcat,并分别命名为tomcat6.0_1、tomcat6.0_2.
2、这里重点说下文章之外的配置:由于两个tomcat运行在一台服务器上,所以要分别修改tomcat的端口,故修改/conf/server.xml,让其能在一台机器上运行。具体代码如下:
tomcat6.0_1配置:
- <Server port="18005" shutdown="SHUTDOWN">
- <Connector port="18080" protocol="HTTP/1.1"
- connectionTimeout="20000"
- redirectPort="8443" />
- <Connector port="18009" protocol="AJP/1.3" redirectPort="8443" />
- <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">
tomcat6.0_2配置:
- <Server port="18006" shutdown="SHUTDOWN">
- <Connector port="18081" protocol="HTTP/1.1"
- connectionTimeout="20000"
- redirectPort="8443" />
- <Connector port="18010" protocol="AJP/1.3" redirectPort="8443" />
- <!--此处同上为修改Engine节点的jvmRoute为tomcat2-->
- <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat2">
至此,一台服务器上的两个tomcat配置完毕。
三、nginx的安装及负载均衡的配置:
nginx的安装一般采用两种方式,一种是yum命令的安装,第二种是下载tar.gz包,采用编译的方式安装。
第一种比较简单,但是一般服务器都不支持yum命令,在此做简单介绍:
01.1、rpm -Uvh http://nginx.org/packages/centos/5/noarch/RPMS/nginx-release-centos-5-0.el5.ngx.noarch.rpm
01.2、执行安装:yum安装nginx:
yum --disablerepo=* --enablerepo=base --enablerepo=nginx install nginx
此安装方式有可能需要安装gcc编译器:gcc安装:下载:http://www.linuxfromscratch.org/blfs/view/5.1/general/gcc2.html
安装命令:yum install gcc gcc-c++ kernel-devel
第二种安装方式是重点:
01、下载所需的安装包:
下载nginx:http://nginx.org/en/download.html
下载pcre,安装pcre,nginx支持重写:ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/
下载zlib:http://download.csdn.net/detail/jessonlv/4603322
下载gcc:http://download.csdn.net/detail/jessonlv/4603333
Nginx依赖一些其他PCRE、openssl(依赖libssl-dev),甚至是zlib、gcc等,下面我们简单说下如何安装PCRE:
- tar zxvf pcre-8.01.tar.gz
- cd pcre-8.01
- ./configure
- make
- make install
测试pcre是否安装成功:
- [root@localhost conf]# rpm -qa pcre
- pcre-6.6-6.el5
- [root@localhost conf]#
返回安装的pcre的版本好则表示成功。或者:
- [root@localhost conf]# ls -al /usr/lib | grep pcre
- lrwxrwxrwx 1 root root 19 Sep 19 13:11 libpcrecpp.so.0 -> libpcrecpp.so.0.0.0
- -rwxr-xr-x 1 root root 29608 Mar 6 2011 libpcrecpp.so.0.0.0
- lrwxrwxrwx 1 root root 21 Sep 19 13:11 libpcreposix.so.0 -> libpcreposix.so.0.0.0
- -rwxr-xr-x 1 root root 6616 Mar 6 2011 libpcreposix.so.0.0.0
- [root@localhost conf]#
也是成功。
安装nginx时如需其他依赖库,上面都注有下载地址,安装方式大致相同,在此不再都一一赘述。
02、安装完成后,接下来所要做的就是nginx最核心的负载均衡的配置:
配置目录是nginx安装目录下的/conf/nginx.conf文件。下面张贴我的配置文件:
- #user nobody;
- worker_processes 1;
- #error_log /usr/local/nginx/logs/error.log;
- #error_log /usr/local/nginx/logs/error.log notice;
- #error_log /usr/local/nginx/logs/error.log info;
- #pid /usr/local/nginx/logs/nginx.pid;
- events {
- use epoll;
- worker_connections 1024;
- }
- http {
- include /etc/nginx/mime.types;
- default_type application/octet-stream;
- sendfile on;
- #keepalive_timeout 0;
- keepalive_timeout 65;
- gzip on;
- gzip_min_length 1k;
- gzip_buffers 4 4k;
- gzip_http_version 1.1;
- gzip_comp_level 1;
- gzip_types test/plain application/x-javascript text/css application/xml;
- gzip_proxied any;
- gzip_disable "MSIE[1-6]\.(?!.*SV1)";
- upstream www.ijietu.com{
- server 192.168.128.129:18081 weight=2;
- server 192.168.128.129:18080 weight=2;
- }
- server {
- listen 80;
- server_name www.ijietu.com;
- #charset koi8-r;
- access_log /var/log/nginx/www.ijietu.com.log;
- location / {
- proxy_pass http://www.ijietu.com;
- proxy_set_header Host $host;
- proxy_set_header X-Real-IP $remote_addr;
- proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
- #root html;
- #index index.html index.htm;
- }
- log_format www_ijietu_com '$remote_addr - $remote_user [$time_local] $request '
- '"$status" $body_bytes_sent "$http_referer" '
- '"$http_user_agent" "$http_x_forwarded_for"';
- access_log /var/log/nginx/www.log www_ijietu_com;
- error_page 500 502 503 504 /50x.html;
- location = /50x.html {
- root html;
- }
- }
- # another virtual host using mix of IP-, name-, and port-based configuration
- #
- #server {
- # listen 8000;
- # listen somename:8080;
- # server_name somename alias another.alias;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- # HTTPS server
- #
- #server {
- # listen 443;
- # server_name localhost;
- # ssl on;
- # ssl_certificate cert.pem;
- # ssl_certificate_key cert.key;
- # ssl_session_timeout 5m;
- # ssl_protocols SSLv2 SSLv3 TLSv1;
- # ssl_ciphers HIGH:!aNULL:!MD5;
- # ssl_prefer_server_ciphers on;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- }
关于gzip的各个参数的具体含义请参考我的另一篇文章:http://blog.csdn.net/jessonlv/article/details/8016284
各个模块参数含义请参考;http://wiki.nginx.org/NginxChs
或者参考我的资源:http://download.csdn.net/detail/jessonlv/4603422 这上面有关于nginx各个参数的详细解释.
至此,nginx的安装及配置算是基本完成。下面进行测试:
03、测试:
启动tomcat6.0_1和tomcat6.0_2.具体命令在此不再赘述,相信大家都很熟悉,不然就别做Java了。
启动nginx:
- [root@localhost conf]# nginx -c /usr/local/nginx/nginx-1.2.3/conf/nginx.conf
上面试测试时nginx的安装路径,启动时注意你自己的安装路径。没有报错即为启动成功。
查看nginx进程:
- [root@localhost conf]# ps -ef | grep nginx
- root 16597 1 0 12:00 ? 00:00:00 nginx: master process nginx -c /usr/local/nginx/nginx-1.2.3/conf/nginx.conf
- nginx 16598 16597 0 12:00 ? 00:00:01 nginx: worker process
- root 17385 3878 0 17:07 pts/2 00:00:00 grep nginx
- [root@localhost conf]#
在浏览器输入:http://192.168.128.129 访问项目主页。
至此,linux下tomcat+nginx的负载均衡是做好了。但是这才是个开始,memcached的session共享才是重中之重。
四、memcached的下载安装及完成session共享的配置
1、memcached管理session共享:
安装memcached一般需要安装libevent支持组件。
服务器端主要是安装memcache服务器端,目前的最新版本是 memcached-1.4.5
官网下载:http://memcached.org/
另外,Memcache用到了libevent这个库用于Socket的处理,所以还需要安装libevent,libevent的最新版本是libevent-2.0.20。(如果你的系统已经安装了libevent,可以不用安装)官网下载:http://libevent.org/
1.先安装libevent。这个东西在配置时需要指定一个安装路径,即./configure --prefix=/usr;然后make;然后make install;
2.再安装memcached,只是需要在配置时需要指定libevent的安装路径即./configure --with-libevent=/usr;然后make;然后make install;
这样就完成了Linux下Memcache服务器端的安装。详细的方法如下:
- 2.1先安装libevent:
- # tar zxvf libevent-2.0.20.tar.gz
- # cd libevent-2.0.20
- # ./configure --prefix=/usr
- # make
- # make install
- 2.2安装memcached,同时需要安装中指定libevent的安装位置:
- # cd /tmp
- # tar zxvf memcached-1.4.15.tar.gz
- # cd memcached-1.4.15
- # ./configure --with-libevent=/usr
- # make
- # make install
测试libevent是否安装成功:
- # ls -al /usr/lib | grep libevent
测试memcached是否安装成功:
- # ls -al /usr/local/bin/mem*
memcached必须启动才能完成服务,启动memcached:
- memcached -d -m 20 -u root -i 192.168.128.129 -p 11211 -c 1024 -P /tmp/memcached.pid
具体各个参数的含义请看:
- -p 监听的端口
- -l 连接的IP地址, 默认是本机
- -d start 启动memcached服务
- -d restart 重起memcached服务
- -d stop|shutdown 关闭正在运行的memcached服务
- -d install 安装memcached服务
- -d uninstall 卸载memcached服务
- -u 以的身份运行 (仅在以root运行的时候有效)
- -m 最大内存使用,单位MB。默认64MB
- -M 内存耗尽时返回错误,而不是删除项
- -c 最大同时连接数,默认是1024
- -f 块大小增长因子,默认是1.25-n 最小分配空间,key+value+flags默认是48
- -h 显示帮助
2、配置
memcached有多种session管理方式,这里采用官网称最有效率的一种kryo。
附各种方式所需要的jar包:
- kryo-serializer: msm-kryo-serializer, kryo-serializers, kryo, minlog, reflectasm, asm-3.2
- javolution-serializer: msm-javolution-serializer, javolution-5.4.3.1
- xstream-serializer: msm-xstream-serializer, xstream, xmlpull, xpp3_min
- flexjson-serializer: msm-flexjson-serializer, flexjson
各种方式的序列化方案以及jar的下载请参考gong1208的文章:http://gong1208.iteye.com/blog/1596120以及chenzhou123520的文章:http://chenzhou123520.iteye.com/blog/1650212 写的相当的不错,对我有很大帮助。
不管你选择哪种序列化策略,你都需要 memcached-session-manager-${version}.jar ,如果你使用的是tomcat6,则还需要下载 memcached-session-manager-tc6-${version}.jar ,如果使
用的是tomcat7则下载 memcached-session-manager-tc7-${version}.jar 。同时还需要下载 spymemcached-2.7.3.jar.下载这完这些jar包后把jar包放到 $CATALINA_HOME/lib/目录
在此特别需要注意的是:jar包的版本非常重要,不然容易造成jar之间的互相冲突,造成非配置性的错误。
在此我使用的是:
- spymemcached-2.7.1.jar
- reflectasm-1.01.jar
- msm-kryo-serializer-1.6.3.jar
- minlog-1.2.jar
- memcached-session-manager-tc6-1.6.3.jar
- memcached-session-manager-1.6.3.jar
- kryo-serializers-0.10.jar
- kryo-1.04.jar
- asm-3.2.jar
spymemcached-2.7.1.jar下载:http://download.csdn.net/detail/jessonlv/4603643
reflectasm-1.01.jar下载:http://download.csdn.net/detail/jessonlv/4603628
msm-kryo-serializer-1.6.3.jar下载:http://download.csdn.net/detail/jessonlv/4603638
minlog-1.2.jar下载:http://download.csdn.net/detail/jessonlv/4603636
memcached-session-manager-tc6-1.6.3.jar下载:http://download.csdn.net/detail/jessonlv/4603633
memcached-session-manager-1.6.3.jar下载:http://download.csdn.net/detail/jessonlv/4603632
kryo-serializers-0.10.jar下载:http://download.csdn.net/detail/jessonlv/4603628
kryo-1.04.jar下载:http://download.csdn.net/detail/jessonlv/4603624
asm-3.2.jar下载:http://download.csdn.net/detail/jessonlv/4603621
尤其注意的是我之前安装的是memcached-1.2.0不能与各个jar包很好的兼容,造成session共享的失败,换成memcached-1.4.15之后,兼容报错问题也就随之解决。
我们还需要修改 $CATALINA_HOME/conf/context.xml文件中Context节点下的内容,添加memcached-session-manager配置。
这里我们采用非粘性session管理配置:
- <Context>
- ...
- <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
- memcachedNodes="n1:192.168.128.129:11211"
- failoverNodes="n1"
- sticky="false" //定义session方式为黏性或非黏性,默认为true
- requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
- transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
- />
- </Context>
tomcat6.0_2的配置与此相同。
至此,memcached的配置也完毕,启动两个tomcat,再启动nginx,再启动memcached,进行与session有关的操作,然后可以关掉其中任意一台tomcat查看sessionid,之后同样的方法测试另外一台,可以发现,不管是那个tomcat在运行,sessionid的值是相同的。
到此,已经完成了负载均衡以及session的全部配置。希望对大家有所帮助。
转载请注明出处:http://blog.csdn.net/jessonlv/article/details/8025132