Keepalived+nginx+redis主从+tomcat一机多实例实现会话共享

本文涉及的产品
云数据库 Tair(兼容Redis),内存型 2GB
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介:

### 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文件来定义虚拟目录



本文转自lovelace521 51CTO博客,原文链接:http://blog.51cto.com/lovelace/1550198,如需转载请自行联系原作者
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
4月前
|
关系型数据库 MySQL 应用服务中间件
《docker基础篇:8.Docker常规安装简介》包括:docker常规安装总体步骤、安装tomcat、安装mysql、安装redis
《docker基础篇:8.Docker常规安装简介》包括:docker常规安装总体步骤、安装tomcat、安装mysql、安装redis
213 7
|
5月前
|
NoSQL Java 关系型数据库
Liunx部署java项目Tomcat、Redis、Mysql教程
本文详细介绍了如何在 Linux 服务器上安装和配置 Tomcat、MySQL 和 Redis,并部署 Java 项目。通过这些步骤,您可以搭建一个高效稳定的 Java 应用运行环境。希望本文能为您在实际操作中提供有价值的参考。
313 26
|
7月前
|
NoSQL 关系型数据库 MySQL
Tomcat、MySQL、Redis最大支持说明
综上所述,Tomcat、MySQL、Redis的并发处理能力均非固定值,而是通过合理的配置与优化策略,结合系统硬件资源,共同决定了它们在实际应用中的表现。开发者应根据应用的具体需求和资源条件,对这些组件进行细致的调优,以达到最佳性能表现。
85 0
|
8月前
|
NoSQL 关系型数据库 Redis
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
mall在linux环境下的部署(基于Docker容器),docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongodb、minio详细教程,拉取镜像、运行容器
mall在linux环境下的部署(基于Docker容器),Docker安装mysql、redis、nginx、rabbitmq、elasticsearch、logstash、kibana、mongo
|
7月前
|
NoSQL 网络协议 应用服务中间件
redis,memcached,nginx网络组件
redis,memcached,nginx网络组件
50 0
|
10月前
|
应用服务中间件 开发工具 nginx
Nginx基础配置实例需求分析
Nginx基础配置实例需求分析
|
9月前
|
NoSQL Java 应用服务中间件
使用Redis和Nginx分别实现限制接口请求频率
这篇文章介绍了如何使用Redis和Nginx分别实现限制接口请求频率的方法,包括具体的命令使用、代码实现和配置步骤。
160 0
|
11月前
|
缓存 负载均衡 NoSQL
Redis系列学习文章分享---第十四篇(Redis多级缓存--封装Http请求+向tomcat发送http请求+根据商品id对tomcat集群负载均衡)
Redis系列学习文章分享---第十四篇(Redis多级缓存--封装Http请求+向tomcat发送http请求+根据商品id对tomcat集群负载均衡)
123 1
|
监控 NoSQL 应用服务中间件
elk+redis 搭建nginx日志分析平台
elk+redis 搭建nginx日志分析平台 logstash,elasticsearch,kibana 怎么进行nginx的日志分析呢?首先,架构方面,nginx是有日志文件的,它的每个请求的状态等都有日志文件进行记录。
1551 0
|
7天前
|
缓存 NoSQL Java
Redis:现代服务端开发的缓存基石与电商实践-优雅草卓伊凡
Redis:现代服务端开发的缓存基石与电商实践-优雅草卓伊凡
28 5
Redis:现代服务端开发的缓存基石与电商实践-优雅草卓伊凡