前沿知识点:

  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. Android 开发中关于layoutinflater

    Inflater英文意思是膨胀,在Android中应该是扩展的意思吧. LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout ...

  2. Gson解析复杂JSON对象

    例如以下格式JSON: 建立对应的Java对象,注意内部类要定义成静态的 public class HResult { public String total; public String recor ...

  3. Android中读取assets文件夹中的子文件夹内容

    文件结构如下:assets/info/info AssetManager am = this.getResources().getAssets(); InputStream input = null; ...

  4. 查询反模式 - 正视NULL值

    一.提出问题 不可避免地,我们都数据库总有一些字段是没有值的.不管是插入一个不完整的行,还是有些列可以合法地拥有一些无效值.SQL 支持一个特殊的空值,就是NULL. 在很多时候,NULL值导致我们的 ...

  5. shell脚本中局部变量local

    shell脚本中局部变量 在shell中定义函数可以使代码模块化,便于复用代码.不过脚本本身的变量和函数的变量的作用域问题可能令你费解,在这里梳理一下这个问题. (1)Shell脚本中定义的变量是gl ...

  6. Message Authentication Code

  7. Linux Security模块

    一.Linux Security Modules Linux Security Modules (LSM) 是一种 Linux 内核子系统,旨在将内核以模块形式集成到各种安全模块中.在 2001 年的 ...

  8. jQuery 动画之 添加商品到购物车

    前台页面 <link href="MyCar.css" rel="stylesheet" /> <script src="../jq ...

  9. (转)25个增强iOS应用程序性能的提示和技巧--中级篇

    在性能优化时,当你碰到一些复杂的问题,应该注意和使用如下技巧: 9.重用和延迟加载View10.缓存.缓存.缓存11.考虑绘制12.处理内存警告13.重用花销很大的对象14.使用Sprite Shee ...

  10. linux ssh-keygen

    用ssh client 客户端 远程登录服务器,避免每次都得输入密码: 解决方法: ssh-keygen  复制 id_rsa.pub 中的内容 到 远程连接的服务器的~/.ssh/authorize ...