Nginx+Tomcat+Memcached集群配置--亲测通过

步骤一:安装memcached缓冲系统

安装方法

打开cmd 进入memcached解压到的路径D:\tomcat7collection\memcached 输入:memcached.exe –d install 回车安装windows服务

输入:memcached.exe –p 11211 –d start 回车启动memcached服务

 

步骤二:配置tomcat----memcached相关)

1下载所需lib包到每一台应用服务器的 tomcat lib目录下

1.       准备jar

msm要用的包有: 
 
 javolution-5.4.3.1.jar
 
 memcached-2.5.jar
 
 memcached-session-manager-1.3.0.jar
 
 msm-javolution-serializer-1.3.0.jar
 
 msm-javolution-serializer-cglib-1.3.0.jar
 
 msm-javolution-serializer-jodatime-1.3.0.jar

将这些包考到tomcatlib下即可

·                  javolution-5.4.3.1.jar (442.1 KB)

·                  memcached-2.5.jar (260.9 KB)

·                  memcached-session-manager-1.3.0.jar (79.1 KB)

·                  msm-javolution-serializer-1.3.0.jar (61 KB)

·                  msm-javolution-serializer-cglib-1.3.0.jar (3.7 KB)

·                  msm-javolution-serializer-jodatime-1.3.0.jar (5 KB)

 

 2、在每个tomcat conf/context.xml 中分别添加如下配置:<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"

memcachedNodes="node1:localhost:11211"

requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"

/>

其中:Localhost为安装memcached的主机地址

步骤三:配置server.xml----tomcat内部配置


   对于tomcat大家都很熟悉,只需要修改server.xml配置文件即可,这里我们以apache-tomcat-6.0.14为例,分别在server目录,解压缩并命名为:apache-tomcat-6.0.14_1、apache-tomcat-6.0.14_2。


    第一处端口修改:

Xml代码  

1.      <!--  修改port端口:18006 俩个tomcat不能重复,端口随意,别太小-->  

2.      <Server port="18006" shutdown="SHUTDOWN">  

 
   第二处端口修改:

Xml代码  

1.      <!-- port="18081" tomcat监听端口,随意设置,别太小 -->  

2.      <Connector port="18081" protocol="HTTP/1.1"   

3.                     connectionTimeout="20000"   

4.                     redirectPort="8443" />  

5.        

6.         

 

   第三处端口修改:

Java代码  

1.      <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />  

 

   Engine元素增加jvmRoute属性:

Xml代码  

1.      <Engine name="Catalina" defaultHost="localhost" jvmRoute="tomcat1">  

 
    两个tomcat的端口别重复,保证能启动起来,另一个tomcat配置希捷省略,监听端口为18080,附件中我们将上传所有的配置信息。

 

步骤四:nginx.conf配置

   Nginx配置文件默认在conf目录,主要配置文件为nginx.conf,我们安装在D:\server\nginx-0.8.20、默认主配置文件为D:\server\nginx-0.8.20\nginx.conf。下面是nginx作为前端反向代理服务器的配置。

Nginx.conf代码  

1.      #Nginx所用用户和组,window下不指定  

2.      #user  niumd niumd;  

3.        

4.      #工作的子进程数量(通常等于CPU数量或者2倍于CPU)  

5.      worker_processes  2;  

6.        

7.      #错误日志存放路径  

8.      #error_log  logs/error.log;  

9.      #error_log  logs/error.log  notice;  

10.   error_log  logs/error.log  info;  

11.     

12.   #指定pid存放文件  

13.   pid        logs/nginx.pid;  

14.     

15.   events {  

16.       #使用网络IO模型linux建议epoll,FreeBSD建议采用kqueue,window下不指定。  

17.       #use epoll;  

18.         

19.       #允许最大连接数  

20.       worker_connections  2048;  

21.   }  

22.     

23.   http {  

24.       include       mime.types;  

25.       default_type  application/octet-stream;  

26.     

27.           #定义日志格式  

28.       #log_format  main  '$remote_addr - $remote_user [$time_local] $request '  

29.       #                  '"$status" $body_bytes_sent "$http_referer" '  

30.       #                  '"$http_user_agent" "$http_x_forwarded_for"';  

31.     

32.       #access_log  off;  

33.       access_log  logs/access.log;  

34.     

35.       client_header_timeout  3m;  

36.       client_body_timeout    3m;  

37.       send_timeout           3m;  

38.      

39.       client_header_buffer_size    1k;  

40.       large_client_header_buffers  4 4k;  

41.     

42.       sendfile        on;  

43.       tcp_nopush      on;  

44.       tcp_nodelay     on;  

45.     

46.       #keepalive_timeout  75 20;  

47.     

48.       include    gzip.conf;  

49.       upstream localhost {  

50.         #根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。  

51.         #同一机器在多网情况下,路由切换,ip可能不同  

52.         #ip_hash;   

53.         server localhost:18081;  

54.         server localhost:18080;  

55.        }  

56.     

57.       server {  

58.               listen       80;  

59.               server_name  localhost;     

60.     

61.               location / {  

62.                       proxy_connect_timeout   3;  

63.                       proxy_send_timeout      30;  

64.                       proxy_read_timeout      30;  

65.                           proxy_pass http://localhost;  

66.               }  

67.                 

68.      }  

69.   }  

 
   代理设置如下:(找不到则新建一个)

Proxy.conf代码  

1.      proxy_redirect          off;  

2.      proxy_set_header        Host $host;  

3.      proxy_set_header        X-Real-IP $remote_addr;  

4.      proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;  

5.      client_max_body_size    10m;  

6.      client_body_buffer_size 128k;  

7.      proxy_connect_timeout   300;  

8.      proxy_send_timeout      300;  

9.      proxy_read_timeout      300;  

10.   proxy_buffer_size       4k;  

11.   proxy_buffers           4 32k;  

12.   proxy_busy_buffers_size 64k;  

13.   proxy_temp_file_write_size 64k;  

 

   gzip压缩相关配置如下:(找不到则新建一个)

Gzip.conf代码  

1.      gzip              on;  

2.      gzip_min_length      1000;  

3.      gzip_types         text/plain text/css application/x-javascript;  

 

 

 

PS:

1、需要一台服务器上装 nginx  tomcat memCached (假设在192.168.39.206上均安装了);

2、启动步骤:

(1)、启动memcached缓存系统

(2)、启动ngnix服务器

(3)、启动tomcat


参考地址:

http://ari.iteye.com/blog/833153(有用)

 

http://www.cnblogs.com/phirothing/archive/2013/12/05/3459814.html

 

http://blog.sina.com.cn/s/blog_4d6c7dea0100uqqd.html

 

http://blog.csdn.net/liuzhigang1237/article/details/8880752 (有用)

 

http://blog.sina.com.cn/s/blog_7842b12b01011auy.html (有用)

 

 本人亲测成功,需要DEMO的同学,可以留下邮箱!


部分朋友在评论中表示有session会断开,解决方法:
修改tomcat下context配置文件中session的配置为:
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"   
    sticky = "false"      
    memcachedNodes="n1:192.168.1.121:11211"       
    requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"       
    sessionBackupAsync="false"       
    sessionBackupTimeout="100"       
    transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"       
    copyCollectionsForSerialization="false"       
    />  
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
1,在tomcat中找到文件apache-tomcat-6.0.37\conf\context.xml 加入内部 <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:192.168.1.65:11211" requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$" sessionBackupAsync="false" sessionBackupTimeout="100" transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" copyCollectionsForSerialization="false" /> 加入之后的content.xml的内容为 <?xml version='1.0' encoding='utf-8'?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- The contents of this file will be loaded for each web application --> <Context> <!-- Default set of monitored resources --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> --> <!-- Uncomment this to enable Comet connection tacking (provides events on session expiration as well as webapp lifecycle) --> <!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:192.16
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值