Tomcat实现session保持的三种方式、使用msm方式搭建jsp网站
Tomcat简单的来说类似于php的功能,主要实现java程序的编译,最后呈现给用户的是html格式的代码,使用用户可以在浏览器中访问。Tomcat是Java语言研发的,所以依赖于java的虚拟机(jvm)。一、使用前端调度器实现实现session保持实现的原理如下图,会话保持使用前端的调度器实现。例如:使用Ngnix调度时,使用ip_hash算法就可以实现。配置步骤:
·
一、使用前端调度器实现实现session保持
配置步骤:
1、安装配置 Tomcat
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
|
操作系统: CentOS 6,5
目标主机:172.16.10.122 172.10.10.133
第一步:安装Java虚拟机
### 获得jdk软件 jdk-6u31-linux-x64-rpm.bin 这里说明:对应的jdk版本有bin版本和rpm版本,
可以根据自己的需要下载。下载地址是:
### 安装步骤
chmod +x jdk-6u31-linux-x64-rpm.bin
. /jdk-6u31-linux-x64-rpm .bin
## cat /etc/profile.d/java.sh
export JAVA_HOME= /usr/java/latest
export PATH= /usr/java/latest/bin/ :$PATH
###
source /etc/profile .d /java .sh
第二步:安装tomcat
### 获得tomcat软件 apache-tomcat-7.0.55.tar.gz
### 安装步骤
tar xf ache-tomcat-7.0.55. tar .gz -C /usr/local
ln -sv apache-tomcat-7.0.55 tomcat
### cat /etc/profile.d/tomcat.sh
export CATALINA_HOME= /usr/local/tomcat
export PATH=$CATALINA_HOME /bin :$PATH
###
第三步:添加测试站点,站点的目录数如下:
### tree /usr/local/tomcat/webapps/test/
/usr/local/tomcat/webapps/test/
|-- WEB-INF
| |-- classes
| `-- lib
`-- index.jsp
3 directories, 1 file
# 3个目录WEB-INF classes lib 1个文件
# 文件内容:172.16.10.122
<%@ page language= "java" %>
<html>
< head ><title>TomcatA< /title >< /head >
<body>
<h1><font color= "red" >TomcatA.example.com< /h1 >
<table align= "centre" border= "1" >
< tr >
<td>Session ID< /td >
<% session.setAttribute( "example.com" , "example.com" ); %>
<td><%= session.getId() %>< /td >
< /tr >
< tr >
<td>Created on< /td >
<td><%= session.getCreationTime() %>< /td >
< /tr >
< /table >
< /body >
< /html >
# 文件内容:172.16.10.133
<%@ page language= "java" %>
<html>
< head ><title>TomcatB< /title >< /head >
<body>
<h1><font color= "blue" >TomcatB.example.com< /h1 >
<table align= "centre" border= "1" >
< tr >
<td>Session ID< /td >
<% session.setAttribute( "example.com" , "example.com" ); %>
<td><%= session.getId() %>< /td >
< /tr >
< tr >
<td>Created on< /td >
<td><%= session.getCreationTime() %>< /td >
< /tr >
< /table >
< /body >
< /html >
第四步:启动tomcat
catalina.sh start
|
结果示例:
以上测试成功后,配置以下的内容。
2、配置前端调度器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
操作系统: CentOS 6,5
目标主机:172.16.10.1
第一步:安装Ngnix
### 可以使用源码编译安装,也可使用rpm方式安装(需配置好epel源)这里使用 rpm方式安装。
yum install ngnix -y
第二步:配置Ngnix前端调度
### cat /etc/nginx/conf.d/default.conf 主要配置如下:
upstream www.tomcat.org {
ip_hash;
server 172.16.10.122:8080;
server 172.16.10.133:8080;
}
server {
listen 80;
server_name www.tomcat.org;
location / {
proxy_pass http: //www .tomcat.org;
index index.jsp index.html index.htm;
}
}
|
结果测试:
二、使用tomcat集群保持session
1、配置tomcat集群
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
46
47
48
49
50
51
52
53
54
|
### 在 /usr/local/tomcat/conf/server.xml 的<Host> </Host>内部以下内容添加:
目标主机:172.16.10.122 172.16.10.133
<Cluster className= "org.apache.catalina.ha.tcp.SimpleTcpCluster"
channelSendOptions= "8" >
<Manager className= "org.apache.catalina.ha.session.DeltaManager"
expireSessionsOnShutdown= "false"
notifyListenersOnReplication= "true" />
<Channel className= "org.apache.catalina.tribes.group.GroupChannel" >
<Membership className= "org.apache.catalina.tribes.membership.McastService"
address= "228.10.10.14"
port= "45564"
frequency= "500"
dropTime= "3000" />
<Receiver className= "org.apache.catalina.tribes.transport.nio.NioReceiver"
address= "172.16.10.122/172.16.10.133"
# 注意此选项默认是auto,这里需要配置。每个节点配置自己的ip地址,
# 意思是接收session心跳消息的地址
port= "4000"
autoBind= "100"
selectorTimeout= "5000"
maxThreads= "6" />
<Sender className= "org.apache.catalina.tribes.transport.ReplicationTransmitter" >
<Transport className= "org.apache.catalina.tribes.transport.nio.PooledParallelSender" />
< /Sender >
<Interceptor className= "org.apache.catalina.tribes.group.interceptors.TcpFailureDetector" />
<Interceptor className= "org.apache.catalina.tribes.group.interceptors.MessageDispatch15Interceptor" />
< /Channel >
<Valve className= "org.apache.catalina.ha.tcp.ReplicationValve"
filter= "" />
<Valve className= "org.apache.catalina.ha.session.JvmRouteBinderValve" />
<Deployer className= "org.apache.catalina.ha.deploy.FarmWarDeployer"
tempDir= "/tmp/war-temp/"
deployDir= "/tmp/war-deploy/"
watchDir= "/tmp/war-listen/"
watchEnabled= "false" />
<ClusterListener className= "org.apache.catalina.ha.session.JvmRouteSessionIDBinderListener" />
<ClusterListener className= "org.apache.catalina.ha.session.ClusterSessionListener" />
< /Cluster >
### 在相应应用程序的web.xml中添加<distributable\>; 也可以全局添加
目标主机:172.16.10.122 172.16.10.133
cp /usr/local/tomcat/webapps/test/WEB-INF/web .xml /usr/local/tomcat/webapps/test/WEB-INF/
# 编辑 /usr/local/tomcat/webapps/test/WEB-INF/web.xml 在 <web-app> 中添加:
<distributable/>;
|
2、配置前端调度器
结果示例:
二、使用session服务器(memcached)来保持session
1、配置memcached
1
2
3
4
5
|
操作系统:CentOS 6.5
目标主机:172.16.10.1 172.16.10.9
yum install memcached -y
service memcached start
|
2、配置tomcat
1
2
3
4
5
6
7
8
9
10
11
12
|
目标主机:172.16.10.122 172.16.10.133
## 在试验一的基础上,继续配置
### 在 /usr/local/tomcat/conf/server.xml 的<Host> </Host>内部以下内容添加:
<Context path= "/test" docBase= "/usr/local/tomcat/webapps/test/" reloadable= "true" >
<Manager className= "de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes= "n1:172.16.10.9:11211,n2:172.16.10.1:11211"
failoverNodes= "n1"
requestUriIgnorePattern= ".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass= "de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
/>
< /Context >
|
3、配置Ngnix前端调度方法,同试验二一样。
补充说明:
四、Tomcat实例
1、配置数据节点 172.16.10.1
1
2
3
4
5
6
7
8
9
10
11
12
|
#### 配置 NFS 服务器
mkdir /tomcatdata
# 编辑 /etc/exports,添加:
/tomcatdata 172.16.0.0 /16 (rw)
service nfs start
### 配置 mysql
yum install mysql-server -y
servive mysqld start
mysql> CREATE DATABASE jcenter1;
mysql> GRANT ALL ON jcenter1.* TO jcenter@ '172.16.%.%' IDENTIFIED BY 'jcenter' ;
mysql> FLUSH PRIVILEGES;
|
2、配置tomcat
在试验一的基础上:
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
|
目标主机: 172.16.10.122 172.16.10.133
第一步:搭建站点
mkdir /tomcat/webapps/
mount -t nfs 172.16.10.1: /tomcatdata /tomcat/webapps/
# 获取jsp程序 JavaCenter_Home_2.0_GBK.tar.bz2 java程序代码
tar -xf JavaCenter_Home_2.0_GBK. tar .bz2 -C /tomcat/webapps/
cd /tomcat/webapps/
ln -sv JavaCenter_Home_2.0_GBK jct
# 配置 JavaCenter:/tomcatdata/jct/config.properties
# 主要修改以下:
# 数据库服务器地址(一般为本地localhost或127.0.0.1)
dbHost = 172.16.10.1
# 数据库服务器端口号(一般为3306)
dbPort = 3306
# 数据库用户名
dbUser = jcenter
# 数据库密码
dbPw = jcenter
# 数据库名
dbName = jcenter1
第二步:配置tomcat
<Host name= "www.tomcat.org" appBase= "/tomcat/webapps"
unpackWARs= "true" autoDeploy= "true" >
<Context path= "/jct" docBase= "jct" />
<Manager className= "de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes= "n1:172.16.10.9:11211,n2:172.16.10.1:11211"
failoverNodes= "n2"
requestUriIgnorePattern= ".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass= "de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"
/>
< /Context >
< /Host >
# 当然msm所依赖的jar包要拷贝到对应的目录下
重新启动tomcat.
|
结果示例:
更多推荐
所有评论(0)