您好,登錄后才能下訂單哦!
php session memcache tomcat
1,php編譯安裝
yum -y install libcurl-devel bzip2-devel libxml2-devel mhash-devel libmcrypt-devel gcc
./configure --prefix=/usr/local/php --enable-mbstring --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --enable-sockets --enable-fpm --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-bz2 --with-mysql=mysqlnd --with-pdo-mysql=mysqlnd --with-mysqli=mysqlnd
make && make install
cp php.ini-production /etc/php.ini
[root@node2 bin]# vim /etc/profile.d/lnmp.sh
export PATH=$PATH:/usr/local/php/bin
[root@node2 etc]# cp php-fpm.conf.default php-fpm.conf
[root@node2 etc]# vim php-fpm.conf
listen = /tmp/php-fpm
[root@node2 fpm]# pwd
/usr/local/php-5.4.26/sapi/fpm
[root@node2 fpm]# cp init.d.php-fpm /etc/init.d/php
[root@node2 fpm]# chmod u+x /etc/init.d/php
[root@node2 fpm]# service php start
2,安裝nginx
yum -y install nginx
location ~ \.php$ {
fastcgi_pass unix://tmp/php-fpm;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME /www/htdocs$fastcgi_script_name;
include fastcgi_params;
}
service nginx start
[root@node2 php.d]# cat /www/htdocs/index.php
<?php
phpinfo();
?>
3,yum -y install memcached
service memcached start
4,安裝php的memcache拓展
tar xf memcache-2.2.7.tgz
cd memcache-2.2.7
phpize
./configure --with-php-config=/usr/local/php/bin/php-config --enable-memcache
make && make install
[root@node2 memcache-2.2.7]# ls /usr/local/php/lib/php/extensions/no-debug-non-zts-20100525
memcache.so
[root@node2 php.d]# echo 'extension=/usr/local/php/lib/php/extensions/no-debug-non-zts-20100525/memcache.so' >memcache.ini
vim /etc/php.ini
;session.save_handler = files
session.save_handler = memcache
session.save_path = "tcp://192.168.100.102:11211?persistent=1&weight=1&timeout=1&retry_interval=15"
service php restart
新建php頁面setsess.php,為客戶端設置啟用session:
<?php
session_start();
if (!isset($_SESSION['www.MageEdu.com'])) {
$_SESSION['www.MageEdu.com'] = time();
}
print $_SESSION['www.MageEdu.com'];
print "<br><br>";
print "Session ID: " . session_id();
?>
新建php頁面showsess.php,獲取當前用戶的會話ID:
<?php
session_start();
$memcache_obj = new Memcache;
$memcache_obj->connect('172.16.200.11', 11211);
$mysess=session_id();
var_dump($memcache_obj->get($mysess));
$memcache_obj->close();
?>
-----------------
sessino保存到文件,可以掛一個共享的NFS就實現了session綁定
session.save_handler = files
session.save_path = "/tmp"
-------------------
jsp編譯,把嵌入了html代碼的jsp文件編譯成servlet能夠執行的代碼
servlet執行jsp編譯過的代碼,java面向cgi的實現
tomcat = jdk + servlet + jsp
jsp-->servlet-->class-->jvm
MVC架構:
Controller,Model和View各自獨立,一個流行的開源實現是Apache Structs框架;目今,設計優良的Web應用程序通常用相就的技術實現相應的功能,比如:
1、Servlet用于實現應用邏輯;
2、JSP用于內容展示;
3、標簽庫和JSP擴展語言用于替換在JSP內部嵌入Java代碼,進而降低了HTML維護的復雜度;
4、MVC框架用于實現展示和應用邏輯的分離;
C存在的目的則是確保M和V的同步,一旦M改變,V應該同步更新。
現在我們總結MVC的處理過程,首先控制器接收用戶的請求,并決定應該調用哪個模型來進行處理,然后模型用業務邏輯來處理用戶的請求并返回數據,最后控制器用相應的視圖格式化模型返回的數據,并通過表示層呈現給用戶。
--------------
tomcat-session-memcached
1,安裝好兩臺tomcat,
#!/bin/sh
# Tomcat init script for Linux.
#
# chkconfig: 2345 96 14
# description: The Apache Tomcat servlet/JSP container.
# JAVA_OPTS='-Xms64m -Xmx128m'
JAVA_HOME=/usr/java/lastest
CATALINA_HOME=/usr/local/tomcat
export JAVA_HOME CATALINA_HOME
case $1 in
start)
exec $CATALINA_HOME/bin/catalina.sh start ;;
stop)
exec $CATALINA_HOME/bin/catalina.sh stop;;
restart)
$CATALINA_HOME/bin/catalina.sh stop
sleep 2
exec $CATALINA_HOME/bin/catalina.sh start ;;
*)
echo "Usage: `basename $0` {start|stop|restart}"
exit 1
;;
esac
修改配置文件
#vim server.xml
<Host name="localhost" appBase="/www/htdocs/test"
unpackWARs="true" autoDeploy="true">
<Context path="" docBase="/www/htdocs/test" />
2,下載memcached-session-manager的jar包到/usr/local/tomcat/lib
memcached-session-manager項目地址,http://code.google.com/p/memcached-session-manager/
memcached-session-manager-${version}.jar
memcached-session-manager-tc${6,7,8}-${version}.jar
spymemcached-${version}.jar
msm-javolution-serializer-${version}.jar
javolution-${version}.jar
3,安裝好兩臺memcached,可以是一臺,兩臺是為了高可用
4,修改tomcat配置文件,在<Context>中加
# vim /usr/local/tomcat/conf/context.xml
<Context>
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="n1:192.168.100.102:11211,n2:192.168.100.103:11211"
requestUriIgnorePattern=".*/.(png|gif|jpg|css|js)$"
sticky="false"
sessionBackupAsync="false"
sessionBackupTimeout="1800000"
/>
5,配置一個haproxy做輪詢的負載均衡
6,寫一個測試的頁面
# mkdir -pv /www/htdocs/test/WEB-INF/{classes,lib}
這個目錄要在上面的server.xml的host中定義好,當然也可以是別的
[root@node3 ~]# vim /www/htdocs/test/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatB</title></head>
<body>
<h2><font color="blue">TomcatB.magedu.com</font></h2>
<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>
在另一個tomcat節點上
# mkdir -pv /www/htdocs/test/WEB-INF/{classes,lib}
[root@node2 ~]# vim /www/htdocs/test/index.jsp
<%@ page language="java" %>
<html>
<head><title>TomcatA</title></head>
<body>
<h2><font color="red">TomcatA.magedu.com</font></h2>
<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>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。