大型网站集群构建之四 (共享session)

From: http://blog.chinaunix.net/u/31455/showart.php?id=482455

在集群中共享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 0

php 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';
?>

 » 相关连接:
大型网站集群构建之五 (load balan 大型网站集群构建之六 (Real Serve 基于直接路由(DR)的LVS的配置 LVS集群中的IP负载均衡技术
LVS集群的负载调度 Linux服务器集群系统 LVS集群的体系结构 Linux高性能计算集群
集群定义以及高性能计算环境方案. 解决方案 VMware5安装集群LVS实战 LVS集群学习笔记(NAT\\DR\\IP tunne Linux操作系统下的集群工作原理及
 » 本栏目最新帖:

Powered by PHPWind v6.0 Code © 2003-08