---Memcache缓存服务器 ---(缓存在内存中)
端口号 11211
-传统web架构的问题
~许多web应用都将数据保存到RDBMS中,应用服务器从中读取数据并在浏览器中显示
~随着数据量的增大、访问的集中,就会出现RDBMS的负担加重,数据库响应恶化、网站显示延迟等重大影响
-Memcache是高性能的分布式缓存服务器
~用来集中缓存数据库查询结果,减少数据库访问次数,以提高动态web应用的响应速度。
-内存管理机制
Slab Allocation机制
~按照预先规定的大小,将分配的内存分割成特定长度的内存块(chunk),再把尺寸相同的内存块分成组(chunk集合),这些内存块不会释放,可以重复利用。
Memcached使用名为Least Recently Used(LRU)机制来分配空间
-删除“最近最少使用”的记录
-当memcache的内存空间不足时,从最近未从被使用的记录中搜索,并将其空间分配给新的记录。
##############################
---部署Memcached
/etc/sysconfig/memcached //配置文件
yum -y install memcached
systemctl restart memcached
systemctl enable memcached
netstat -tlpn | grep memcache (:11211)
使用telnet测试memcached服务
yum -y install telnet
telnet 127.0.0.1 11211
set name 0 180 3 #设置变量名为name 0--不压缩 180--数据缓存时间 3--存储的数据字节数量
aaa
STORED
get name #获取变量name的值
aaa
add user 0 180 3
bbb
STORED
get user
bbb
quit
#################################
--LNMP+Memcached
部署LNMP+memcached网站平台,通过PHP页面实现对memcached服务器的数据操作
yum -y install gcc gcc-c++ pcre-devel openssl-devel zlib-devel //安装依赖包
useradd -s /sbin/nologin nginx
tar -xzf nginx-1.8.0...
cd nginx-1.8.0
./configure \
>--user=nginx \
>--group=nginx \
>--prefix=/usr/local/nginx \
>--with-http_ssl_module
make&&make install
yum - y install php php-mysql
yum -y localinstall php-fpm...
yum -y install mariadb-server mariadb-devel
yum -y install php-pecl-memcached #为 PHP 添加 memcache 扩展
systemctl restart mariadb 3306
systemctl restart php-fpm 9000
/usr/local/nginx/sbin/nginx 80
netstat -tlnp | grep mariadb /php-fpm /nginx
修改nginx配置文件
/usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.php index.html;
}
location ~\.php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
#fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi.conf;
}
nginx -t #检查语法
nginx -s reload
创建测试页面并访问
vim /usr/local/nginx/html/test.php
<?php
$memcache=new Memcache; //创建memcache对象
$memcache->connect(‘localhost’,11211) or die (‘could not connect!!’);
$memcache->set(‘key’,‘test’); //定义变量
$get_values=$memcache->get(‘key’); //获取变量值
echo $get_values;
?>
firefox 192.168.4.1/test.php
test
###################################
---使用Tomcat 设置Session
Session?
--存储在服务器端,保存用户名、密码等信息。
简单理解为:在一个网站的登录页面中输入用户名密码等信息后,再打开此网站中其他网页,不需要再次进行用户登录。
Cookies?
--由服务器下发给客户端,保存在客户端的一个文件里。保存的内容主要包括:sessionid,账户名,过期时间等
简单理解:登录淘宝,加入购物车,退出登录,然后再次打开淘宝首页,发现购物车中有提示,进入到购物车中后,能够看到信息。
(1)部署nginx调度服务器 #配置nginx将用户请求发往后端的tomcat服务器
/etc/local/nginx/conf/nginx.conf
upstream tomcat {
server 192.168.4.2:8080
server 192.168.4.3:8080
}
server {
location / {
proxy_pass http://tomcat;
...
}
}
(2)部署两台虚拟机,并部署tomcat服务器
【4.2】
tar xzf apache-tomcat-8.0.30.tar.gz
mv apache-tomcat.. /usr/local/tomcat
cd /usr/local/tomcat
bin/startup.sh
设置测试页
vim webapps/ROOT/test.jsp
<html>
<body bgcolor="yellow">
<%String s = session.getId();%>
<%=s%>
tomcat web server A
</body>
</html>
【4.3】
tar xzf apache-tomcat-8.0.30.tar.gz
mv apache-tomcat.. /usr/local/tomcat
cd /usr/local/tomcat
bin/startup.sh
设置测试页
vim webapps/ROOT/test.jsp
<html>
<body bgcolor="yellow">
<%String s = session.getId();%>
<%=s%>
tomcat web server A
</body>
</html>
bin/shutdown.sh
bin/startup.sh
//重启tomcat失败,查看8005端口是否开启 netstat -tlnp | grep 8005
//解决:修改 /usr/lib/jvm/jre/lib/security/java.security
//securerandom.source=file:/dev/urandom
firefox 192.168.4.1/test.jsp
session ID 不同
【4.2】【4.3】
---实现session共享
拷贝msm相关的jar包
cp /root/lnmp../session/*.jar /usr/local/tomcat/lib
修改tomcat,使其可以连接到memcache服务器
/usr/local/tomcat/conf/context.xml
<Context> 在原有的Context中加入以下说明
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="mem1:192.168.4.1:11211"
requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.JavaSerializationTranscoderFactory"/>
</Context>
bin/shutdown.sh
bin/startup.sh
firefox 192.168.4.1/test.jsp
session id 相同
MEMCACHE缓存服务器
原创
©著作权归作者所有:来自51CTO博客作者ww魏伟的原创作品,请联系作者获取转载授权,否则将追究法律责任
下一篇:redis数据库

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
并发服务器
实现并发服务器
客户端 套接字 子进程 -
Linux之搭建memcache缓存服务器(一)
Linux之搭建memcache缓存服务器
Linux memcache duyuheng -
Linux之搭建memcache缓存服务器(二)
Linux之搭建memcache缓存服务器(nginx+php+memcache+mysql)
Linux 搭建 duyuhneng -
memcache服务器搭建
memcache服务器搭建
memcache 服务器搭建