一.背景
tomcat7 mysql5 oracle11g
在我的上一篇文章實現了簡單的用戶無感的熱部署集羣結構。tomcat集羣是通過tomcat廣播去實現session同步的。
https://blog.csdn.net/qq_37372909/article/details/80328374
結構圖如下:
上面的結構對代碼沒有侵入性,但tomcat節點太多的時候也會有些問題。今天介紹通過數據庫(mysql和oracle)保存session來實現集羣,依然不侵入代碼,在tomcat節點增多的時候負荷增加不會太明顯(但比redis存儲session會差一些)。結構如下:
二.存儲tomcat的session到mysql
1.建表
CREATE TABLE `tomcat_sessions` (
`session_id` varchar(100) NOT NULL,
`valid_session` char(1) NOT NULL,
`max_inactive` int(11) NOT NULL,
`last_access` bigint(20) NOT NULL,
`app_name` varchar(255) DEFAULT NULL,
`session_data` mediumblob,
PRIMARY KEY (`session_id`),
KEY `kapp_name` (`app_name`)
)
ENGINE=MyISAM DEFAULT CHARSET=utf8;
2.複製驅動包到tomcat
複製mysql-connector-java-5.1.47.jar到tomcat/lib。
3.配置tomcat
在tomcat/conf/context.xml文件中</Context>之前添加下面的配置,配置一目瞭然就不詳細解釋了。
<Manager className="org.apache.catalina.session.PersistentManager"
maxIdleBackup="1"
minIdleSwap="0"
maxIdleSwap="0"
processExpiresFrequency="1"
saveOnRestart='true'>
<Store className="org.apache.catalina.session.JDBCStore"
connectionURL="jdbc:mysql://localhost:3306/sessions?user=root&password=root"
driverName="com.mysql.jdbc.Driver"
sessionAppCol="app_name"
sessionDataCol="session_data"
sessionIdCol="session_id"
sessionLastAccessedCol="last_access"
sessionMaxInactiveCol="max_inactive"
sessionTable="tomcat_sessions"
sessionValidCol="valid_session" />
</Manager>
4.驗證
1)nginx修改配置指向兩個tomcat,nginx/conf/nginx.conf文件內容如下:
worker_processes 4;
events {
worker_connections 10240;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 120;
keepalive_requests 200;
upstream services {
server 127.0.0.1:8080;
server 127.0.0.1:8180;
}
server {
listen 8082;
server_name localhost;
client_body_buffer_size 128k;
client_max_body_size 16m;
proxy_connect_timeout 5;#10
proxy_send_timeout 60;#600
proxy_read_timeout 60;#600
proxy_buffer_size 4k;
proxy_buffers 32 4k;
proxy_busy_buffers_size 64k;
port_in_redirect on;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host:$proxy_port;
location / {
proxy_pass http://services;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
2)準備兩個tomcat
tomcat1和tomcat2端口分別設置爲8080和8082.tomcat1/webapps/ROOT/index.jsp修改爲:
<%@page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>ckl JSP~</title>
</head>
<body>
<%
out.println("<h2><font color='red'>this page on 8080 !</font></h2>");
%>
<br/>
<% session.setAttribute("ckl.com","ckl.com"); %>
<% out.println("<font size='4'>sessionid is<font>"); %>
<%=session.getId() %><br/>
<% out.println("sessiontime is"); %>
<%=session.getCreationTime() %>
</body>
</html>
tomcat2/webapps/ROOT/index.jsp修改爲:
<%@page contentType="text/html;charset=GB2312" %>
<html>
<head>
<title>ckl JSP~</title>
</head>
<body>
<%
out.println("<h2><font color='blue'>this page on 8180 !</font></h2>");
%>
<br/>
<% session.setAttribute("ckl.com","ckl.com"); %>
<% out.println("<font size='4'>sessionid is<font>"); %>
<%=session.getId() %><br/>
<% out.println("sessiontime is"); %>
<%=session.getCreationTime() %>
</body>
</html>
3)修改2個tomcat的tomcat/conf/context.xml文件
<?xml version='1.0' encoding='utf-8'?>
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<Manager className="org.apache.catalina.session.PersistentManager"
maxIdleBackup="1"
minIdleSwap="0"
maxIdleSwap="0"
processExpiresFrequency="1"
saveOnRestart='true'>
<Store className="org.apache.catalina.session.JDBCStore"
connectionURL="jdbc:mysql://localhost:3306/sessions?user=root&password=root"
driverName="com.mysql.jdbc.Driver"
sessionAppCol="app_name"
sessionDataCol="session_data"
sessionIdCol="session_id"
sessionLastAccessedCol="last_access"
sessionMaxInactiveCol="max_inactive"
sessionTable="tomcat_sessions"
sessionValidCol="valid_session" />
</Manager>
</Context>
修改內容都是一樣的。
4)運行結果
第一次
刷新後
數據庫中已經看到存儲了session
三.存儲tomcat的session到oracle
1.建表
CREATE TABLE tomcat_sessions (
session_id varchar2(100) NOT NULL primary key,
valid_session varchar2(1) NOT NULL,
max_inactive int NOT NULL,
last_access long NOT NULL,
app_name varchar2(255) DEFAULT NULL,
session_data blob
) ;
create index kapp_name on tomcat_sessions(app_name);
2.複製驅動包到tomcat
複製ojdbc14-10.2.0.3.0.jar到tomcat/lib。
3.配置tomcat
在tomcat/conf/context.xml文件中</Context>之前添加下面的配置,配置一目瞭然就不詳細解釋了。
<Manager className="org.apache.catalina.session.PersistentManager"
maxIdleBackup="1"
minIdleSwap="0"
maxIdleSwap="0"
processExpiresFrequency="1"
saveOnRestart='true'>
<Store className="org.apache.catalina.session.JDBCStore"
connectionURL="jdbc:oracle:thin:@10.xx.0.20:1521:jwellabc"
connectionName="jwwaasl"
connectionPassword="jwsdwl"
driverName="oracle.jdbc.driver.OracleDriver"
sessionAppCol="app_name"
sessionDataCol="session_data"
sessionIdCol="session_id"
sessionLastAccessedCol="last_access"
sessionMaxInactiveCol="max_inactive"
sessionTable="tomcat_sessions"
sessionValidCol="valid_session" />
</Manager>
4.驗證
方法同存儲到mysql的方式,就不重複寫了。
四.參考資料
https://blog.csdn.net/iteye_16572/article/details/82641993
https://www.cnblogs.com/xiaoheike/p/9501996.html
https://blog.csdn.net/Ice_cap1995/article/details/80238371