网络通信 频道

大型网站集群构建之:Session讨论篇

    【IT168 报道】本文章主要讲解大型网站构建,全部采用开源软件和开源的操作系统,构架采用 LAMP (Linux+Apache+Mysql+Php)模式实现。

大型网站集群构建之: 需求分析篇
大型网站集群构建之:DNS配置篇
大型网站集群构建之:共享session讨论篇
大型网站集群构建之:负载均衡篇

大型网站集群构建之:共享session讨论篇

在集群中共享session是一个问题。
方案: 1、把session放到共享的设备上去,如nfs。
 2、放到 memcache 中,这种方法被很多人推崇。
memcache install:
download http://www.danga.com/memcached
install memcache support modules:
download http://www.monkey.org/~provos/libevent/
./configure
make
make install
install memcache
./configure --prefix=/usr/local/memcached --enable-threads
make
make install
创建一个启动文件 /etc/init.d/memcached
#!/bin/sh
#
# memcached: MemCached Daemon
#
# chkconfig: - 90 25
# description: MemCached Daemon
#
# Source function library.
. /etc/rc.d/init.d/functions
. /etc/sysconfig/network

#[ ${NETWORKING} = "no" ] && exit 0
#[ -r /etc/sysconfig/dund ] || exit 0
#. /etc/sysconfig/dund
#[ -z "$DUNDARGS" ] && exit 0
start()
{
echo -n $"Starting memcached: "
daemon $MEMCACHED -u daemon -d -m 1024 -l 192.168.0.100 -p 11211
echo
}

stop()
{
echo -n $"Shutting down memcached: "
killproc memcached

echo
}

MEMCACHED="/usr/local/memcached/bin/memcached"
[ -f $MEMCACHED ] || exit 1

# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
sleep 3
start
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 1
esac

exit 0php memcahce modules install:
download http://pecl.php.net/package/memcache
phpize
./configure --enable-memcache
make
make install
 

编辑 php.ini 在 [Session] 添加指定你安装的memcache.so的位置。

extension=memcache.so
extension_dir = "/usr/local/php/lib/php/extensions/"
memcache.allow_failover = 1
memcache.max_failover_attempts = 20
memcache.chunk_size = 8192
memcache.default_port = 11211
session.save_handler = memcache
session.save_path = "udp://192.168.0.100:11211,tcp://192.168.0.101:11211"

一个简单的php测试脚本 test.php
<
//http://www./a.php?act=write
//http://www./a.php?act=read
session_start();
if($_GET['act']=='write')
$_SESSION['name']='0009847';
elseif($_GET['act']=='read')
var_dump($_SESSION);
else
echo 'invalid argument';
>

0
相关文章