nginx+tomcat+memcached-session-manager组成简单的负载均衡和集群

1、搭建环境

  192.168.29.128(luxh-01.com)  安装nginx,参考 http://www.cnblogs.com/luxh/p/4067038.html

  192.168.29.129(luxh-02.com)  安装tomcat和memcached

  192.168.29.130(luxh-03.com)  安装tomcat和memcached

2、安装memcached

  1)使用yum直接安装

[root@Luxh-02 ~]# yum install memcached

  2)启动memcached,默认的启动端口是11211

[root@Luxh-02 ~]# memcached -u memcached -d

  -u 指定用户

  3)测试是否启动成功

[root@Luxh-02 ~]# telnet 127.0.0.1 11211

3、安装Tomcat7

  参考:http://www.cnblogs.com/luxh/p/3188736.html

4、添加memcached-session-manager相关的jar到tomcat中

  将以下jar放到tomcat安装路径的lib目录中

memcached-session-manager-1.8.2.jar
memcached-session-manager-tc7-1.8.2.jar
spymemcached-2.11.1.jar

  注意tomcat版本不同,用的jar版本也不同,具体可参考 http://code.google.com/p/memcached-session-manager/wiki/SetupAndConfiguration

5、将serializers相关的jar添加到web应用中

  这里使用 kryo-serializer实现序列化。

  项目使用了maven,直接添加依赖即可

<dependency>
    <groupId>de.javakaffee.msm</groupId>
    <artifactId>msm-kryo-serializer</artifactId>
    <version>1.8.0</version>
    <scope>runtime</scope>
</dependency>

   如果没有使用maven,自行添加以下的jar到项目中

msm-kryo-serializer-1.8.0.jar
kryo-serializers-0.11.jar
kryo-1.04.jar
asm-3.2.jar
reflectasm-1.01.jar
minlog-1.2.jar
jsr305-1.3.9.jar
annotations-1.3.9.jar

6、在tomcat中配置memcached-session-manager

  在tomcat的context.xml文件中加入以下内容(<Context></Context>标签中)。

  非粘性session配置,两台memcached服务器。

<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
    memcachedNodes="n1:luxh-02.com:11211,n2:luxh-03.com:11211"
    sticky="false"
    sessionBackupAsync="false"
    requestUriIgnorePattern=".*.(ico|png|gif|jpg|css|js)$"
    transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
    />

7、配置nginx

  1)nginx.conf文件中的http节点内容加入

 upstream luxh-01.com {
            server luxh-02.com:8080;
            server luxh-03.com:8080;
 }

  2)nginx.conf文件中的server节点内容加入

 location / {
            proxy_pass http://luxh-01.com;
            proxy_set_header X-Real-IP $remote_addr;
 }

8、把应用分别部署到tomcat服务器中。

  1)启动memcached服务

  2)启动tomcat

  3)启动nginx

 9、直接访问http://luxh-01.com/webapp  (我部署的项目是通过webapp访问)

原文地址:https://www.cnblogs.com/luxh/p/4072376.html