linux memcached Session共享

memcached

memcached是高性能的分布式缓存服务器
用来集中缓存数据库查询结果,减少数据库访问次数
提高动态web应用的响应速度

传统web架构的问题
许多web应用都将数据保存在RDBMS中,应用从服务器中读取数据
并在浏览器中显示
随着数据量的增大,访问的集中,就会出现RDBMS的负担加重
数据库响应恶化,网站显示延迟等重大影响

数据存储位置对比:
性能
cpu缓存》内存》磁盘》数据库

价格
cpu缓存》内存》磁盘》数据库

memcached支持多平台
linux
freebsd
solaris
mac os x
windows

memcached特征
协议简单,基于libevent的事件处理,内置内存存储方式

内置管理机制:
传统内存分配机制
使用完通过分配的内存后回收内存,这种方式容易产生内存碎片
并降低操作系统对内存的管理效率
slab allocation机制:
他按照预先的大小,将分配的内存分割成特定长度的内存块(chunk)
再把尺寸相同的内存块分成组(chunk集合),这些内存块不会释放,可重复利用
slab allocation的主要术语
分配给slab的内存空间默认是1mb,分配之后根据slab定义的大小切分为chunk
chunks是用于缓存记录的内存空间
slab class是特定大小的chunk组
memcached使用名为least recently used(lru)机制来分配空间
删除 "最近最少使用"的记录的机制
当memcached的内存空间不足时,从最近未被使用的记录中搜索
并将其空间分配给新的记录

——————————————————————————————————————————————————————————————
部署memcached
官方网站:http://memcached.org/

安装事件库libevent
[root@web2 ~]# tar -xf libevent-2.0.21-stable.tar.gz
[root@web2 libevent-2.0.21-stable]# ./configure
[root@web2 libevent-2.0.21-stable]# make
[root@web2 libevent-2.0.21-stable]# make install
[root@web2 ~]# echo "/usr/local/lib" > /etc/ld.so.conf.d/libevent.conf(链接库)
[root@web2 ~]# ldconfig(刷新链接)

安装memcached
[root@web2 ~]# tar -xf memcached-1.4.24.tar.gz
[root@web2 memcached-1.4.24]# ./configure
[root@web2 memcached-1.4.24]# make
[root@web2 memcached-1.4.24]# make install
[root@web2 ~]# /usr/local/bin/memcached -h(查看帮助)

启动memcached
[root@web2 ~]# memcached -u root -m 64M -d
常用选项
-u:Memcache程序运行时使用的用户身份必须是root用户
-p:设置Memcache监听的端口,默认为11211
-l:监听的服务器IP地址
-c:服务的最大连接数
-vvv:超详细信息
-n:chunk size的最小空间是多少,单位为字节
-f:chunk size大小增长的倍数,默认1.25倍
-m:指定使用本机多少物理内存存数据
-d:在后台启动

查看memcached的内部状态
[root@web2 ~]# telnet 127.0.0.1 11211(端口11211)
set key 0 200 3(key存了abc,0代表存的时候不压缩 ,200表示存200秒 3表示存三个字符)
abc(key的值等于abc)
get key(get查询key的值)
VALUE key 0 3
abc
ctrl + [键 +]键,然后输入quit退出

memcached常用指令
add name 0 180 10:添加新数据
set name 0 180 10:添加或替换变量
replace name 0 180 10:替换数据
get name:读取变量
append name 0 180 10:向变量中追加数据
delete name:删除变量
stats:查看状态
flush_all:清空所有

实例:
add hydra 0 100 10(添加hydra)
1234567890
STORED
get hydra(查询)
VALUE hydra 0 10
1234567890
END
append hydra 0 100 10(添加)
hail hydra
STORED
get hydra(查询)
VALUE hydra 0 20
1234567890hail hydra
END

——————————————————————————————————————————————————————————

实验php连接memcached

准备工作

配置nginx
[root@web2 ]# vim /usr/local/nginx/conf/nginx.conf
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;(遇到动态页面转发到9000端口)
fastcgi_index index.php;
include fastcgi.conf;(parm改为conf)
}
[root@web2 ~]# netstat -nutlp | grep 80(查看http服务端口)
[root@web2 ~]# netstat -nutlp | grep 9000(查看php端口)

php本身不支持链接memcached,为php添加模块,so库
[root@web2 ]# tar -xf memcache-2.2.5.tgz
[root@web2 ]# cd memcache-2.2.5
[root@web2 memcache-2.2.5]# /usr/local/php5/bin/phpize(扫描有没有扩展包)
[root@web2 memcache-2.2.5]# ./configure --with-php-config=/usr.local/php5/bin/php-config --enable-memcache
[root@web2 memcache-2.2.5]# make
[root@web2 memcache-2.2.5]# make install
installing shared extensions: /usr.local/php5/lib/php/extensions/no-debug-non-zts-20100525(提示模块存放目录memcache.so)
[root@web2 ~]# vim /usr/local/php5/etc/php.ini
[root@web2 ~]# vim /usr/local/php5/etc/php.ini
extension_dir = "/usr/local/php5/lib/php/extensions/no-debug-non-zts-20100525/"(添加扩展包路径,700多行)
...........
extension_dir = "memcache.so"(加载模块,800多行)
。。。。。。
[root@web2 ~]# service php-fpm restart(配置后重启)

[root@web2 html]# vim hydra.php(测试访问页面)
<?php
$memcache=new Memcache;
$memcache->connect('localhost',11211) or die ('could not connect!!');
$memcache->set('key','test');
$get_values=$memcache->get('key');
echo $get_values;
?>
[root@web2 nginx]# firefox http://127.0.0.1/hydra.php(测试访问)
[root@web2 ~]# telnet 192.168.2.200 11211(测试)
Trying 192.168.2.200...
Connected to 192.168.2.200.
Escape character is '^]'.
get key
VALUE key 0 4
test
END

——————————————————————————————————————————————————————————————————————

Session共享

session&cookies
session:存储在服务器端,保存用户名,密码等信息
cookies:由服务器下发给客户端,保存在客户端的一个文件里,
保存的内容主要包括sessionID,账户名,过期时间路径和域

测试环境
[root@beiqiang ~]# ifconfig eth0 192.168.4.1

[root@daili nginx~]# ifconfig eth0 192.168.4.5
eth1 192.168.2.5
[root@web tomcat~]# ifconfig eth1 192.168.2.100
[root@web2 tomcat~]# ifconfig eth1 192.168.2.200

[root@daili nginx~]nginx做upstream(集群),调度后台两台tomcat服务器
访问192.168.4.5的时候调度后两台web

[root@daili nginx]# vim conf/nginx.conf
upstream tomcatgrp {
server 192.168.2.100:8080;(tomcat服务器ip,端口)
server 192.168.2.200:8080;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
proxy_pass http://tomcatgrp;(调度tomcat)
}
[root@daili nginx]# nginx -s reload

tomcat不能直接连接memcached,为tomcat加模块
安装Session共享库,把这些架包拷贝到/usr/loacl/tomcat/lib/
asm-5.1.jar
memcached-session-manager-tc8-1.9.2.jar
minlog-1.3.0.jar
msm-kryo-serializer-1.9.2.jar
reflectasm-1.11.1.jar
kryo-3.0.3.jar
spymemcached-2.11.1.jar
kryo-serializers-0.34.jar
memcached-session-manager-1.9.2.jar
有了这些扩展包才能连接memcached
[root@web session]# cp *.jar /usr/local/tomcat/lib/
[root@web2 session]# cp *.jar /usr/local/tomcat/lib/

验证session
编写网页文件
[root@web ~]# vim /usr/local/tomcat/webapps/ROOT/test.jsp(去web2做相同的操作)
<html>
<body bgcolor="red">(指定背景颜色)
<center>
<%String s = session.getId();%>|(获取sessionid)
<%=s%>
<h1>tomcatA </h1>(web2的tomcatA要改为tomcatB)
</center>
</body>
</html>
[root@beiqiang ~]# firefox http://192.168.4.5/test.jsp(测试访问)
D4B601C82E1FA0C06D859969E4FB3EE5 (生成的sessionid号)
tomcatB (web2服务器生成的)

[root@web ~]# vim /usr/local/tomcat/conf/context.xml (在web2上做同样的操作)
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"(调类)
memcachedNodes="mem1:192.168.2.200:11211"(memcached服务器ip端口)
requestUriIgnorePattern=".*.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.JavaSerializationTranscoderFactory"/>(调类)
root@web~]# /usr/local/tomcat/bin/catalina.sh stop(关闭)在web2上做同样的操作
root@web~]# /usr/local/tomcat/bin/catalina.sh start(启动)在web2上做同样的操作
[root@beiqiang ~]# firefox http://192.168.4.5/test.jsp


————————————————————————————————————————————————————————————————————

原文地址:https://www.cnblogs.com/Hydraxx/p/7404544.html