前言
通过调度器进行调度时,要保证后端不同服务器对客户的访问进行状态保持,也就是说要共享客户端与后端集群服务器之间建立的session会话不能丢失,各主机之间在处理用户请求时,都要能够对用户的会话进行处理。
接下来,我们来介绍一种常见的方式,也是实际生产中经常使用到的一种方式,使用memcached配合tomcat搭建Seesion Server。
要用到的jar包
可以到 网络上去下载以下jar包
https://github.com/magro/memcached-session-manager
javolution-5.4.3.1.jar
memcached-session-manager-1.8.3.jar
memcached-session-manager-tc7-1.8.3.jar
msm-javolution-serializer-1.8.3.jar
spymemcached-2.11.1.jar
三台主机
A:做调度器,使用nginx
B:安装tomcat和memcached
C:安装tomcat和memcached
A:172.18.25.53
B:172.18.25.51
C:172.18.25.52
nginx
vim /etc/nginx/nginx.conf
# Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;
events {
worker_connections 1024;
}
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
include /etc/nginx/mime.types;
default_type application/octet-stream;
index index.jsp index.php index.do index.html;#定义优先读取index.jsp文件
upstream tcsrvs {
server 172.18.25.51:8080; #定义后端B主机
server 172.18.25.52:8080; #定义后端A主机
}
# Load modular configuration files from the /etc/nginx/conf.d directory.
# See http://nginx.org/en/docs/ngx_core_module.html#include
# for more information.
include /etc/nginx/conf.d/*.conf;
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name _;
root /usr/share/nginx/html;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://tcsrvs;#这里在定义代理调度
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
include /etc/nginx/default.d/*.conf;
location / {
proxy_pass http://tcsrvs;
}
error_page 404 /404.html;
location = /40x.html {
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
}
}
}
这里的nginx只是起一个简单的反向代理的负载均衡的调度作用,就没有在上面做过多的配置。
tomcat
首先还是简单说一下tomcat的tomcat程序环境
tomcat的目录结构
bin:脚本,及启动时用到的类;
conf:配置文件目录;
lib:库文件,Java类库,jar;
logs:日志文件目录;
temp:临时文件目录;
webapps:webapp的默认目录;
work:工作目录,存放编译后的字节码文件;
# catalina.sh --help
debug Start Catalina in a debugger
debug -security Debug Catalina with a security manager
jpda start Start Catalina under JPDA debugger
run Start Catalina in the current window
run -security Start in the current window with security manager
start Start Catalina in a separate window
start -security Start in a separate window with security manager
stop Stop Catalina, waiting up to 5 seconds for the process to end
stop n Stop Catalina, waiting up to n seconds for the process to end
stop -force Stop Catalina, wait up to 5 seconds and then use kill -KILL if still running
stop n -force Stop Catalina, wait up to n seconds and then use kill -KILL if still running
configtest Run a basic syntax check on server.xml - check exit code for result
version What version of tomcat are you running?
rpm包安装的程序环境:
配置文件目录:/etc/tomcat
主配置文件:server.xml
webapps存放位置:/var/lib/tomcat/webapps/
examples
manager
host-manager
docs
Unit File:tomcat.service
环境配置文件:/etc/sysconfig/tomcat
我这里为了方便直接使用的base源里面的包,对了还有上面的nginx也是直接使用的base源里面的包。
[ root@B ~ ]# rpm -q tomcat
tomcat-7.0.69-10.el7.noarch
[ root@B ~ ]# rpm -q memcached
memcached-1.4.15-10.el7.x86_64
为了实验的隔离,自己创建一个文件夹
[ root@B ~ ]# cd /usr/share/tomcat/webapps/
[ root@B /usr/share/tomcat/webapps ]# mkdir -pv myapp/WEB-INF
[ root@C ~ ]# cd /usr/share/tomcat/webapps/
[ root@C /usr/share/tomcat/webapps ]# mkdir -pv myapp/WEB-INF
在myapp下面创建测试页面代码
[ root@B /usr/share/tomcat/webapps/myapp ]# cat index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h1><font color="red">TomcatA</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("magedu.com","magedu.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
[ root@C /usr/share/tomcat/webapps/myapp ]# cat index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h1><font color="blue">TomcatC</font></h1>
<table align="centre" border="1">
<tr>
<td>Session ID</td>
<% session.setAttribute("magedu.com","magedu.com"); %>
<td><%= session.getId() %></td>
</tr>
<tr>
<td>Created on</td>
<td><%= session.getCreationTime() %></td>
</tr>
</table>
</body>
</html>
把/etc/tomcat/下的web.xml文件复制到之前创建WEB-INF下
[ root@B /usr/share/tomcat/webapps/myapp/WEB-INF ]# cp /etc/tomcat/web.xml .
[ root@C /usr/share/tomcat/webapps/myapp/WEB-INF ]# cp /etc/tomcat/web.xml .
接下来修改/etc/tomcat/server.xml文件
分别在两个tomcat上的某host上定义一个用于测试的context容器,并在其中创建一个会话管理器,如下所示:
[ root@B /etc/tomcat ]# vim server.xml
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tcA">
<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
<!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context path="/myapp" docBase="myapp" reloadable="true" >
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="m1:172.18.25.51:11211,m2:172.18.25.52:11211"
failoverNodes="m2"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" />
</Context>
[ root@C /etc/tomcat ]# vim server.xml
<Engine name="Catalina" defaultHost="localhost" jvmRoute="tcA">
<!--For clustering, please take a look at documentation at:
/docs/cluster-howto.html (simple how to)
/docs/config/cluster.html (reference documentation) -->
<!--
<Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
-->
<!-- Use the LockOutRealm to prevent attempts to guess user passwords
via a brute-force attack -->
<Realm className="org.apache.catalina.realm.LockOutRealm">
<!-- This Realm uses the UserDatabase configured in the global JNDI
resources under the key "UserDatabase". Any edits
that are performed against this UserDatabase are immediately
available for use by the Realm. -->
<Realm className="org.apache.catalina.realm.UserDatabaseRealm"
resourceName="UserDatabase"/>
</Realm>
<Host name="localhost" appBase="webapps"
unpackWARs="true" autoDeploy="true">
<Context path="/myapp" docBase="myapp" reloadable="true" >
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="m1:172.18.25.51:11211,m2:172.18.25.52:11211"
failoverNodes="m2"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$" transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" />
</Context>
</Host>
</Engine>
</Service>
</Server>
到这里tomcat这边的配置就完成了。
memcached
memcached的配置文件特别的少,很简单的一个软件
[ root@B /etc/tomcat ]# rpm -ql memcached
/etc/sysconfig/memcached
/usr/bin/memcached
/usr/bin/memcached-tool
/usr/lib/systemd/system/memcached.service
/usr/share/doc/memcached-1.4.15
/usr/share/doc/memcached-1.4.15/AUTHORS
/usr/share/doc/memcached-1.4.15/CONTRIBUTORS
/usr/share/doc/memcached-1.4.15/COPYING
/usr/share/doc/memcached-1.4.15/ChangeLog
/usr/share/doc/memcached-1.4.15/NEWS
/usr/share/doc/memcached-1.4.15/README.md
/usr/share/doc/memcached-1.4.15/protocol.txt
/usr/share/doc/memcached-1.4.15/readme.txt
/usr/share/doc/memcached-1.4.15/threads.txt
/usr/share/man/man1/memcached-tool.1.gz
/usr/share/man/man1/memcached.1.gz
能修改的东西也非常少,但是有一点要注意,他默认的CACHESIZE只有64M
我们要把它变大一点,我这里设置的是1024M,在生产中要看实际的情况来设定。
[ root@B /etc/tomcat ]# vim /etc/sysconfig/memcached
PORT="11211"
USER="memcached"
MAXCONN="1024"
CACHESIZE="1024"
OPTIONS=""
其他的就基本没什么要修改的了。
启动服务。
[ root@A ~ ]# systemctl start nginx
[ root@B /etc/tomcat ]# systemctl start tomcat
[ root@B /etc/tomcat ]# systemctl start memcached
[ root@C /etc/tomcat ]# systemctl start tomcat
[ root@C /etc/tomcat ]# systemctl start memcached
实验部分
到这里实验就成功了。
对了,
还有如果发现在测试的时候碰见了sessionID不是固定的
可以使用命令查看memcached是否缓存了session信息
写进去了是有值的,如果没有值就要排错喽.....
[ root@C /etc/tomcat ]# memdump --servers=172.18.25.51:11211
5E1F1A437DA5335CC4A5FC482761F2B0-m1.tcA
最后
这段时间随着难度的加深,时间越来越少了,
只能抽时间把一些关键的东西拿出来写一些了,写的不详细,勿怪…