nginx、memcached、tomcat7实现tomcat集群

   nginx负责负载均衡,Memcached负责同步Tomcat的Session,Tomcat负责业务服务。

一、安装、配置nginx,如果你的机器不能连外网,则需要按照下列方式安装

1.安装openssl,官网:http://www.openssl.org/
安装版本:openssl-1.0.1p.tar.gz
安装命令:
#tar -zxvf openssl-1.0.1p.tar.gz
#cd  openssl-1.0.1p
#sudo ./config --prefix=/usr/local/openssl-1.0.1p    #prefix指定安装目录
#sudo make
#sudo make install

2.安装pcre,官网:http://www.pcre.org/
安装版本:pcre-8.35.tar.gz
安装命令:
#tar -zxvf pcre-8.35.tar.gz
#cd pcre-8.35
#sudo ./configure --prefix=/usr/local/pcre-8.35    #prefix指定安装目录
#sudo make
#sudo make install

3.安装zlib,官网:http://www.zlib.net/
安装版本:zlib-1.2.8.tar.gz
安装命令:
#tar -zxvf zlib-1.2.8.tar.gz
#cd zlib-1.2.8
#sudo ./configure --prefix=/usr/local/zlib-1.2.8    #prefix指定安装目录
#sudo make
#sudo make install

4.安装Nginx,安装版本:nginx-1.8.0.tar.gz
安装命令:
#tar -zxvf nginx-1.8.0.tar.gz
#cd nginx-1.8.0
#sudo ./configure
--prefix=/usr/local/nginx-1.8.0                #prefix指定安装目录
--with-openssl=/home/mysql/src/ openssl-1.0.1p    #指的是openssl源码路径
--with-pcre=/home/mysql/src/pcre-8.3.5          #指的是pcre的源码路径
--with-zlib=/home/mysql/src/zlib-1.2.8           #指的是zlib 的源码路径
--with-http_ssl_module
#sudo make
#make install

5.配置Nginx
配置文件目录:/usr/local/nginx-1.8.0/conf/nginx.conf
# sudo vi nginx.conf
#Nginx所用用户和组
#user  niumd niumd;
#工作的子进程数量(通常等于CPU数量或者2倍于CPU)
worker_processes  2;

#错误日志存放路径
#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;

#指定pid存放文件
pid        logs/nginx.pid;

events {
    #使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue
    #use epoll;
    #允许最大连接数
    worker_connections  2048;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    #定义日志格式
    #log_format  main  '$remote_addr - $remote_user [$time_local] $request '
    #                  '"$status" $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';
    #access_log  off;
    access_log  logs/access.log;
    client_header_timeout  3m;
    client_body_timeout    3m;
    send_timeout           3m;
    client_header_buffer_size    1k;
    large_client_header_buffers  4 4k;
    sendfile        on;
    tcp_nopush      on;
    tcp_nodelay     on;
    #keepalive_timeout  75 20;
    include    gzip.conf;
    upstream localhost {
      server localhost:8080;
      server localhost:8081;
     }
    server {
            listen       80;
            server_name  localhost;   
            location / {
                    proxy_connect_timeout   3;
                    proxy_send_timeout      3;
                    proxy_read_timeout      3;
                proxy_pass http://localhost;
            }
   }
}
# sudo vi gzip.conf
gzip              on;
gzip_min_length      1000;
gzip_types         text/plain text/css application/x-javascript;

6.验证
启动nginx后,查看
# ps -ef|grep nginx
root     19170     1  0 Oct21 ?        00:00:00 nginx: master process /usr/local/nginx-1.8.0/sbin/nginx -c /usr/local/nginx-1.8.0/conf/nginx.conf
nobody   19171 19170  0 Oct21 ?        00:00:00 nginx: worker process                                                      
nobody   19172 19170  0 Oct21 ?        00:01:13 nginx: worker process   


二、Memcached安装
1.安装livevent,官网:http://libevent.org/
查看是否已安装:# rpm qa | grep libevent
如果已安装且版本低于1.3,则先通过:
# rpm -e libevent --nodeps 进行卸载。
# tar zxvf libevent-2.0.21-stable.tar.gz
# cd libevent-2.0.21-stable
# sudo ./configure --prefix=/usr/local/libevent-2.0.21-stable      #prefix指定安装路径
# sudo make
# sudo make install

2.安装Memcached,官网:http://memcached.org/
# tar zxvf memcached-1.4.24.tar.gz
# cd memcached-1.4.24
# sudo ./configure
--prefix=/usr/local/memcached-1.4.24
--with-libevent=/usr/local/libevent-2.0.21-stable
# sudo make
# sudo make install

3.启动
./memcached -d -v -p 12000 -m 200 -u mysql
./memcached -d -v -p 12001 -m 200 -u mysql
解释:-d表示以守护进程方式运行memcached;-v表示输出浸膏和错误信息;-p指定监听的端口号;-m指定能使用的最大内存,单位MB;-u指定运行memcached的账户,非root用户。

4.验证
telnet 10.10.15.208 12000
stats
STAT pid 27797
STAT uptime 511112
STAT time 1445931740
STAT version 1.4.24
STAT libevent 2.0.21-stable
STAT pointer_size 64
STAT rusage_user 23.899366
STAT rusage_system 8.561698
STAT curr_connections 15
STAT total_connections 42
STAT connection_structures 25
STAT reserved_fds 20
STAT cmd_get 25
STAT cmd_set 3
STAT cmd_flush 0
STAT cmd_touch 0
STAT get_hits 2
STAT get_misses 23
STAT delete_misses 1
STAT delete_hits 0
STAT incr_misses 0
STAT incr_hits 0
STAT decr_misses 0
STAT decr_hits 0
STAT cas_misses 0
STAT cas_hits 0
STAT cas_badval 0
STAT touch_hits 0
STAT touch_misses 0
STAT auth_cmds 0
STAT auth_errors 0
STAT bytes_read 1220
STAT bytes_written 1803
STAT limit_maxbytes 20971520
STAT accepting_conns 1
STAT listen_disabled_num 0
STAT threads 4
STAT conn_yields 0
STAT hash_power_level 16
STAT hash_bytes 524288
STAT hash_is_expanding 0
STAT malloc_fails 0
STAT bytes 309
STAT curr_items 1
STAT total_items 3
STAT expired_unfetched 1
STAT evicted_unfetched 0
STAT evictions 0
STAT reclaimed 1
STAT crawler_reclaimed 0
STAT crawler_items_checked 0
STAT lrutail_reflocked 0
END


三、安装、配置tomcat,官网:http://tomcat.apache.org/
apache-tomcat-7.0.64.tar.gz解压即可,我发布的是两个节点,改节点1的context.xml和节点2的context.xml、server.xml。

1. 修改server.xml
port部分都要改
<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm2">

2.context.xml
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"    
        memcachedNodes="n1:10.10.15.208:12000 n2:10.10.15.208:12001"    
        requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"    
        sessionBackupAsync="false"    
        sessionBackupTimeout="100"    
        transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"    
        copyCollectionsForSerialization="false"  
 />
 
3.在lib放入以下包,下列包证明在tomcat 7下是可用的,如果下载的包版本有对不上的,很可能tomcat启动不了。
couchbase-client-1.4.4.jar                 
javolution-5.4.3.1.jar                     
memcached-session-manager-1.6.5.jar        
memcached-session-manager-tc7-1.8.1.jar    
msm-javolution-serializer-1.6.5.jar        
spymemcached-2.10.3.jar                    

4.验证效果
分别访问http://10.10.15.208:8080,http://10.10.15.208:8080
再访问http://10.10.15.208,如果此时显示的是server1,则停止tomcat1,再次访问,观察session_id没变,变成server2,则说明成功。

tomcat1:
tomcat7_1/webapps/ROOT/index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>  
<html>  
<body>  
SessionID:<%=session.getId()%>  
<BR>  
SessionIP:<%=request.getServerName()%>  
<BR>  
SessionPort:<%=request.getServerPort()%>  
<BR> 
server1
</body>  
</html>  

tomcat2:
tomcat7_2/webapps/ROOTindex.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"  pageEncoding="UTF-8"%>  
<html>  
<body>  
SessionID:<%=session.getId()%>  
<BR>  
SessionIP:<%=request.getServerName()%>  
<BR>  
SessionPort:<%=request.getServerPort()%>  
<BR> 
server2
</body>  
</html> 

你可能感兴趣的:(nginx、memcached、tomcat7实现tomcat集群)