前沿知识点:

  1. nginx负责负载均衡(反向代理)

  2. msm(memcached session manager)负责缓存会话信息,从而实现会话保持

所需包:

  1. nginx和memcached采用最新稳定版

  2. tomcat版本需要与msm版本一致,这里采用tomcat7

  3. msm包共计9个包,包名具体信息查看附件,msm的所有包放到$CATALINA_HOME/lib

配置过程:

nginx的配置信息如下-->


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
...
 
http {
 
    ...
 
    upstream tomcat {
 
        server node1:8080;
 
        server node2:8080;
 
#这里具体使用什么算法,暂定,不过我觉得ip_hash不好,会造成负载不均衡
 
    }
 
    server {
 
    ...
 
        location ~* ^/testapp {
 
            proxy_pass http://tomcat;
 
            proxy_redirect off;
 
            proxy_set_header X-real-ip $remote_addr;
 
            proxy_set_header X-forwarded-for $proxy_add_x_forwarded_for;
 
            proxy_set_header Host $host;
 
        }
 
    ...
 
    }
 
}

tomcat的配置信息如下-->


首先修改server.xml,在默认的host flag中添加context

1
2
3
4
5
6
7
8
9
10
11
12
13
...
 
    <host>
 
    ...
 
    <context path="/testapp" docbase="testapp/" />
 
    ...
 
    </host>
 
...

其次修改context.xml,在默认的context flag中添加manager

其中粘性session方式如下: Sticky 模式:tomcat 本地容器 session 为主session, memcached为备session。Request请求到来时, 判断tomcat容器是否发生变化,若变化(即原tomcat down掉),则从memcached加载备session到tomcat2,响应给客户端,请求结束后,重置session_id,并更新到memcached。


1
2
3
4
5
6
7
8
9
<Context>
  ...
  <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
    memcachedNodes="n1:host1.yourdomain.com:11211,n2:host2.yourdomain.com:11211"
   failoverNodes="n1"
    requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
    transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
    />
</Context>

非粘性session如下: Non-Sticky模式:tomcat session 为 中转session, memcached1 为主,memcached 2 为备session。Request请求到来时,从memcached 2加载备 session 到 tomcat,(另外,若只有一个memcached节点,或者memcached2 出错时,且tomcat本地容器中还没有session,则从memcached1加载主 session 到 tomcat),Request请求结束时,将tomcat session更新至 主memcached1和备memcached2,并且清除tomcat session 。以达到主备同步之目的,如下是non-sticky模式的响应流程图:(图片来源网络)。

此模式下,基于session的验证码将无法使用,因为此模式下,第一次session是用本地,然后存放到mem1和mem2中,之后客户再次请求,则路由到了另外一台tomcat上,又因为是同一个session_id,所以使用的是mem1中,但是mem1中确是旧的session.(但是验证码要求每一次都不一样...)

 

1
2
3
4
5
6
7
8
9
10
11
<Context>
  ...
 <ManagerclassName="de.javakaffee.web.msm.MemcachedBackupSessionManager"
   memcachedNodes="n1:host1.yourdomain.com:11211,n2:host2.yourdomain.com:11211"
   sticky="false"
   sessionBackupAsync="false"
   lockingMode="uriPattern:/path1|/path2"
   requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
   transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
   />
</Context>


keepalived配置信息如下-->这里只贴出主的,从的就不贴了


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
! Configuration File for keepalived
 
global_defs {
   notification_email {
     acassen@firewall.loc
     failover@firewall.loc
     sysadmin@firewall.loc
   }
   notification_email_from Alexandre.Cassen@firewall.loc
   smtp_server 127.0.0.1
   smtp_connect_timeout 30
   router_id LVS_DEVEL
}
#<Spinestars
vrrp_script chk_keepalived_down {
    script "[ -f /var/run/keepaliveddown ] && exit 1 || exit 0"
    interval 2
    weight -2
}
#nginx_check_script
vrrp_script chk_nginx {
    script "killall -0 nginx && exit 0 || exit 1"
    interval 2
    weight -2
}
#/Spinestars>
vrrp_instance VI_1 {
    state MASTER
    interface eth1
    virtual_router_id 20
    mcast_src_ip 192.168.100.1
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    virtual_ipaddress {
        10.88.100.2
    }
    track_script {
    chk_nginx
    chk_keepalived_down
    }
}

nginx动静分离配置:

...

#<Spinestars
upstream tomcat_servers {
        server node1:;
        server node2:;
}
server {
        listen      *:;
        server_name  test.shop.com;
        root/var/www/shop;
        index index.html index.jsp index.htm;
#/Spinestars>

#<Spinestars

location ~* \.(html|jpg|png|jpeg|css|gif|ico)$ {

root /var/www;

}

location ~* \.(js|jhtml)$ {

proxy_pass http://tomcat_servers;

proxy_redirect off;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

}

if ( $host = 'test.shop.com' ){

rewrite ^/$ http://test.shop.com/shop permanent;

}
        #以下location是测试用的
        location ~* ^/testapp {

proxy_pass http://tomcat_servers;

proxy_redirect off;

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header Host $host;

proxy_set_header X-Real-IP $remote_addr;

}

#/Spinestars>

...

}

附件列表

nginx+keepalived+tomcat之具体配置档的更多相关文章

  1. nginx+keepalived+tomcat+memcache实现双VIP高可用及Session会话保持

    Nginx+Keepalived+Tomcat+Memcached 实现双VIP负载均衡及Session会话保持 IP 信息列表: 名称         IP                      ...

  2. Nginx+Memcached+Tomcat集群配置(MSM--win7 64bit)

    本次主要是在win7 64 上演示操作. web应用构建 Memcached安装配置启动 Tomcat配置 所需jar包 memcached-session-manager 序列化 contextxm ...

  3. nginx+keepalived+tomcat之tomcat性能调优

    body{ font-family: Nyala; font-size: 10.5pt; line-height: 1.5;}html, body{ color: ; background-color ...

  4. Nginx与Tomcat安装、配置与优化

    Nginx与Tomcat安装.配置与优化 Nginx与Tomcat安装.配置与优化 Nginx的安装与使用 Nginx是一款优秀的反向代理服务器 安装: rpm(或者是pkg安装),是预编译好的程序包 ...

  5. 使用nginx搭建tomcat集群配置

    软件准备: (1)jdk-8u73-linux-x64.tar.gz (2)apache-tomcat-7.0.57.tar.gz (3)nginx-1.7.7.tar.gz 准备3台Linux机器, ...

  6. 【Linux运维-集群技术进阶】Nginx+Keepalived+Tomcat搭建高可用/负载均衡/动静分离的Webserver集群

    额.博客名字有点长.. . 前言 最终到这篇文章了,心情是有点激动的. 由于这篇文章会集中曾经博客讲到的全部Nginx功能点.包含主要的负载均衡,还有动静分离技术再加上这篇文章的重点.通过Keepal ...

  7. Nginx+Memcached+Tomcat集群配置实践(Sticky Session)

    准备工作 创建一个简单的web应用,名为session.其中有两个页面,分别如下所示: 页面login.jsp <%@ page language="java" conten ...

  8. nginx+keepalived主从高可用配置

    上面有4台web服务器  我们实验条件限制,就开两台web服务器1.117  1.119 一.环境准备: 系统环境:CentOS 6.5 x86_64 Nginx版本:nginx v1.6.2 Kee ...

  9. Nginx+Keepalived+Tomcat高可用负载均衡,Zookeeper集群配置,Mysql(MariaDB)搭建,Redis安装,FTP配置

    JDK 安装步骤 下载 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html rpm ...

随机推荐

  1. 使用pip install 或者easy_install安装Python的各种包出现cc failed with exit status 1

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  2. IOS 文件管理 2

    IOS开发-文件管理(二) 五.Plist文件 String方式添加               NSString *path = [NSHomeDirectory( )  stringByAppen ...

  3. TCP/IP他人笔记学习--地址收录

    <TCP/IP详解,卷1:协议>学习笔记——1. 概述   http://www.blogjava.net/amigoxie/archive/2007/08/22/138674.html ...

  4. LeetCode_Simplify Path

    Given an absolute path for a file (Unix-style), simplify it. For example, path = "/home/", ...

  5. android showAsDropDown的用法属性介绍

    使用PopupWindow可实现弹出窗口效果,,其实和AlertDialog一样,也是一种对话框,两者也经常混用,但是也各有特点.下面就看看使用方法.首先初始化一个PopupWindow,指定窗口大小 ...

  6. 【转】Ubuntu10.04上编译Android源码(Build Android source in Ubuntu10.04 Platform)

    原文网址:http://blog.csdn.net/chenyafei617/article/details/6570928 一.Introduction 今天我们就来谈谈如何在Ubuntu平台上面编 ...

  7. LCS算法思想

    LCS问题就是求两个字符串最长公共子串的问题.解法就是用一个矩阵来记录两个字符串中所有位置的两个字符之间的匹配情况,若是匹配则为1,否则为0.然后求出对角线最长的1序列,其对应的位置就是最长匹配子串的 ...

  8. 【剑指offer】面试题27:二叉搜索树与双向链表

    题目: 输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建任何新的结点,只能调整树中结点指针的指向. 思路: 假设已经处理了一部分(转换了左子树),则得到一个有序的双向链表,现在 ...

  9. Calculate Number Of Islands And Lakes 解答

    Question 1 1 1 1 1 01 0 1 0 0 11 0 1 0 0 11 1 0 1 1 1 1 is earth, 0 is water. i) count the number of ...

  10. HTTP缓存 1.0 vs 1.1

    在“使用ETag跟踪用户”中有一点被忽略了,因为要用这张小图统计统计uv, 所以要求浏览器必须每次都要发送这个图片的请求.这需要服务器对图片的缓存策略做设置. http/1.0 和 http/1.1 ...