Keepalived+nginx+redis主从+tomcat一机多实例实现会话共享 2014-09-09 14:14:25

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://lovelace.blog.51cto.com/1028430/1550198

### keepalived配置

### nginx安装培训

- 安装nginx

1
2
3
``` cpp
yum install nginx -y
```

- 调整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
``` cpp
[root@redis ~]# sed -e 's@ @@g;/^$/d;/^#/d' /etc/nginx/nginx.conf
worker_processes1;
events{
worker_connections1024;
}
http{
includemime.types;
default_typeapplication/octet-stream;
sendfileon;
keepalive_timeout65;
upstreammycluser
{
server192.168.58.30:8080;
server192.168.58.30:8081;
server192.168.58.10:8080;
server192.168.58.10:8081;
}
server{
listen80;
server_namelocalhost;
location/{
roothtml;
indexindex.htmlindex.htmindex.jsp;
proxy_passhttp://mycluser;
proxy_set_headerX-Real-IP$remote_addr;
proxy_set_headerHost$host;
proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;
proxy_redirectoff;
}
error_page500502503504/50x.html;
location=/50x.html{
roothtml;
}
}
}
```

### redis主从配置

- 安装redis

1
2
3
``` cpp
yum install redis -y 
```

- slave redis上添加slaveof 192.168.58.30 6379这一行,这就是二者的区别

1
2
3
4
5
``` cpp
[root@mongo1 tmp]# sed -n '/^slaveof/p' /etc/redis.conf
slaveof 192.168.58.30 6379
[root@mongo1 tmp]# 
```

- 测试会话共享

- master上

1
2
3
4
5
6
7
8
``` cpp
[root@redis ~]# redis-cli -h 192.168.58.30
redis 192.168.58.30:6379> set name zhuima
OK
redis 192.168.58.30:6379> get name
"zhuima"
redis 192.168.58.30:6379> 
```

- slave上

1
2
3
4
5
6
7
``` cpp
[root@mongo1 webapps]# redis-cli -h 192.168.58.10
redis 192.168.58.10:6379> get name
"zhuima"
redis 192.168.58.10:6379> 
redis 192.168.58.10:6379> 
```

### tomcat一机多实例配置

- 配置jdk

1
2
3
``` cpp
[root@redis tmp]# tar xf jdk-7u60-linux-x64.gz  -C /usr/local
```

- 配置jdk环境变量

1
2
3
4
5
6
7
8
9
10
11
12
``` cpp
[root@redis local]# cat /etc/profile.d/java.sh 
export JAVA_HOME=/usr/local/jdk1.7.0_60
export PATH=$PATH:$JAVA_HOME/bin
export JRE_HOME=$JAVA_HOME/jre
[root@redis local]# source /etc/profile.d/java.sh
[root@redis local]# java -version
java version "1.7.0_60"
Java(TM) SE Runtime Environment (build 1.7.0_60-b19)
Java HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)
[root@redis local]# 
```

- 配置多实例tomcat

1
2
3
4
5
``` cpp
[root@redis local]# tar xf apache-tomcat-7.0.54.tar.gz -C /usr/local/
[root@redis local]# mv apache-tomcat-7.0.54/ tomcat1
[root@redis local]# cp -Rf tomcat1 tomcat2
```

- 修改第二个tomcat的三个端口的配置信息

### 修改tomcat的content.xml文件

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
``` cpp
[root@www conf]# cat context.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>
    <Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />  
    <Manager className="com.radiadesign.catalina.session.RedisSessionManager"  
         host="192.168.58.30"  
         port="6379"   
         database="0"   
         maxInactiveInterval="60"/>  
<!-- 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" />
    -->
</Context>
```

- 复制给其他tomcat

1
2
3
4
``` cpp
[root@redis conf]# yes | cp context.xml /usr/local/tomcat2/conf/
cp: overwrite `/usr/local/tomcat2/conf/context.xml'? [root@redis conf]# 
```
1
2
3
4
5
6
7
8
``` cpp
[root@redis conf]# for x in tomcat{1,2};do scp context.xml 192.168.58.10:/usr/local/$x/conf/;done
root@192.168.58.10's password: 
context.xml                                                                            100% 1678     1.6KB/s   00:00    
root@192.168.58.10's password: 
context.xml                                                                            100% 1678     1.6KB/s   00:00    
[root@redis conf]# 
```

### 客户端验证会话共享

- 测试文件192.168.58.30上面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
``` cpp
[root@redis webapps]# cat index.jsp 
<%@ page language="java" %>
<html
 <head><title>TomcatB</title></head
 <body
  <h1><font color="blue">192.168.58.30:8081 Tomcat2 </h1
  <table align="centre" border="1"
   <tr
    <td>Session ID</td
  <% session.setAttribute("abc","abc"); %> 
    <td><%= session.getId() %></td
   </tr
   <tr
    <td>Created on</td
    <td><%= session.getCreationTime() %></td
   </tr
  </table
 </body
</html>
```

- 测试文件192.168.58.10上面

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
``` cpp
<%@ page language="java" %>
<html
 <head><title>TomcatB</title></head
 <body
  <h1><font color="red">192.168.58.10:8080 Tomcat1 </h1
  <table align="centre" border="1"
   <tr
    <td>Session ID</td
  <% session.setAttribute("abc","abc"); %> 
    <td><%= session.getId() %></td
   </tr
   <tr
    <td>Created on</td
    <td><%= session.getCreationTime() %></td
   </tr
  </table
 </body
</html>
```

### 查看进程存在与否

1
2
3
4
5
6
7
8
9
``` cpp
[root@redis conf]# ps -ef | egrep "[r]edis|[j]ava|[n]ginx"
root      5814     1  1 10:34 pts/0    00:00:41 /usr/local/jdk1.7.0_60/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat1/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat1/endorsed -classpath /usr/local/tomcat1/bin/bootstrap.jar:/usr/local/tomcat1/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat1 -Dcatalina.home=/usr/local/tomcat1 -Djava.io.tmpdir=/usr/local/tomcat1/temp org.apache.catalina.startup.Bootstrap start
root      5830     1  1 10:35 pts/0    00:00:41 /usr/local/jdk1.7.0_60/jre/bin/java -Djava.util.logging.config.file=/usr/local/tomcat2/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.dirs=/usr/local/tomcat2/endorsed -classpath /usr/local/tomcat2/bin/bootstrap.jar:/usr/local/tomcat2/bin/tomcat-juli.jar -Dcatalina.base=/usr/local/tomcat2 -Dcatalina.home=/usr/local/tomcat2 -Djava.io.tmpdir=/usr/local/tomcat2/temp org.apache.catalina.startup.Bootstrap start
redis     5921     1  0 11:07 ?        00:00:02 /usr/sbin/redis-server /etc/redis.conf
root      5989     1  0 11:19 ?        00:00:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx     5991  5989  0 11:19 ?        00:00:00 nginx: worker process                   
[root@redis conf]# 
```

### 延伸:关于tomcat部署项目的几种方式

- 直接放到webapps的ROOT下面

- 删除ROOT下的所有文件,把你的项目包解压过去即可

- 直接放到webapps下面

- 不用操作原来的webapps下面的任何东西,server.xml也不用调整,直接解压到该目录下即可

- 定义context来定义项目文件目录

- 通过修改server.xml来定义虚拟目录

- 定义.conf/name/localhost定义一个xml文件

- 通过定义xml文件来定义虚拟目录

Keepalived+nginx+redis主从+tomcat一机多实例实现会话共享的更多相关文章

  1. 用Redis存储Tomcat集群的Session实现session共享

    一.存储 前段时间,我花了不少时间来寻求一种方法,把新开发的代码推送到到生产系统中部署,生产系统要能够零宕机.对使用用户零影响. 我的设想是使用集群来搞定,通过通知负载均衡Nginx,取下集群中的To ...

  2. Linux keepalived+nginx实现主从模式

    双机高可用方法目前分为两种: 主从模式:一台主服务器和一台从服务器,当配置了虚拟vip的主服务器发送故障时,从服务器将自动接管虚拟ip,服务将不会中断.但主服务器不出现故障的时候,从服务器永远处于浪费 ...

  3. Nginx+redis部署tomcat集群

    一.部署环境: 两个tomcat实例部署在Ubuntu 14上,IP地址分别为192.168.1.110和192.168.1.111,Nginx和redis部署在windows7上,IP地址为192. ...

  4. redis主从架构宕机问题手动解决

    1    主机宕机 1.  设置端口6379是主机,端口6380是从机,全部都正常启动 2.  验证在6379写入数据,在6380也能得到数据 3.  现在将6379主机停掉,模拟主机宕机 4.  由 ...

  5. Keepalived+Nginx搭建主从高可用并带nginx检测

    应用环境:部分时候,WEB访问量一般,或者测试使用,利用Keepalived给Nginx做高可用即可满足要求. 测试环境:   搭建步骤: 1. 安装软件 在Nginx-A和Nginx-B上: ~]# ...

  6. nginx+redis安装配置(内存型数据库)实现session的共享

    注意:借鉴原文章:http://www.cnblogs.com/roy-blog/p/7196054.html 感兴趣的可以加一下481845043 java交流群,共同进步. 1 session的概 ...

  7. Tomcat通过自带的Cluster方式实现Session会话共享环境操作记录

    一般来说,在多个tomcat集群业务中,session会话共享是必须的需求,不然前端nginx转发过来的请求不知道之前请求在哪台tomcat节点上,从而就找不到session以至于最终导致请求失败.要 ...

  8. keepalived+nginx+tomcat+redis实现负载均衡和session共享(原创)

    keepalived+nginx+tomcat+redis实现负载均衡和session共享 直接上链接,码了一天,就不再重写了,希望能帮到大家,有问题欢迎留言交流.

  9. keepalived+nginx实现niginx高可用,宕机自动重启

    nginx作为http服务器,在集群中 用于接受客户单发送过来的请求,并且根据配置的策略将请求 转发给具体的哪台服务器 如果在nginx服务器使用轮询策略处理客户端的请求,出现了tomcat 宕机的情 ...

随机推荐

  1. css-移动&缩放元素

    transform 属性向元素应用 2D 或 3D 转换.该属性允许我们对元素进行旋转.缩放.移动或倾斜. 用法:object.style.transform="rotate(7deg)&q ...

  2. JZYZOJ1539[haoi2015]T2 树链剖分

    http://172.20.6.3/Problem_Show.asp?id=1539 在学校的OJ又写了一次,RE了好多次,原来haoi的时候这道题需要开栈+快读,裸数据结构30分,加上快读50分.o ...

  3. getDimension,getDimensionPixelOffset和getDimensionPixelSize

    dimens.xml里写上三个变量: <dimen name="activity_vertical_margin1">16dp</dimen> <di ...

  4. HDU 4305 Lightning(计算几何,判断点在线段上,生成树计数)

    Lightning Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  5. css/js(工作中遇到的问题)-6

    页面resize方法 if(document.createEvent) { const event = document.createEvent ("HTMLEvents"); e ...

  6. 如何在form初始化时自动隐藏FOLDER列

    方法1:直接设定PROMPT列和数据列ITEM的VISIBLE属性为No 方法2:在WHEN-NEW-FORM-INSTANCE触发器里: l_old_itm := :system.cursor_it ...

  7. android基础知识复习——RelativeLayout布局属性、背景、半透明设置(XML设置)

    转自:http://blog.csdn.net/fansongy/article/details/6817968 复习布局与XML,写了一个空的登录界面.XML的注释我写在当行的后面了.程序运行图: ...

  8. [Guava] EventBus

    1.  发布-订阅模式 发布-订阅模式(publish-subscribe)是一种编程范式,发布方不发布消息给特定的接收方,而是由订阅方选择性接收.这使得发布方和订阅方相对独立,减少了耦合性. 在发布 ...

  9. C/C++ 语言中的表达式求值

    在此,首先向裘老师致敬! 裘宗燕:C/C++ 语言中的表达式求值 经常可以在一些讨论组里看到下面的提问:“谁知道下面C语句给n赋什么值?” m = 1; n = m+++m++; 最近有位不相识的朋友 ...

  10. java/eclipse/myeclipse建立.properties文件的方法

      相比较来说,Java程序的编写相对简单很多,多数文件都可以通过编写文本文件生成!     对于不用IDE的java程序来讲,只需要右键新建>文本文件,建立好以后写好代码,另存为xx.prop ...