memcached缓存数据库服务及Session共享

memcached缓存数据库服务
高性能的分布式缓存服务器
用来集中缓存数据库查询结果,减少数据库访问次数
以提高动态Web应用的响应速度
它不是在数据库中一行一行查找,而是直接去查找数据相对应的位置
采用内存分配机制,容易产生内存碎片并降低操作系统对内存的管理效率
缺点:掉电数据会丢失

Slab Allocation机制,它是按照预先规定的大小,将分配的内存分割成特定长度
的内存块(chunk),再把尺寸相同的内存块组成(chunk集合),这些内存块不会释放,可以重复利用。

memcached使用名为Least Recently Used (LRU)机制来分配空间
当内存不足时,删除“最近最少使用”的记录,来分配给新的记录

-M参数禁止LRU功能,当内存不足时会返回错误。

选项:
-p 指定memcached监听的端口号,默认11211

-l memcached服务器的IP地址

-u memcached程序运行时使用的用户身份必须是root用户

-m 指定使用本机的多少物理内存存数据,默认64M

-c memcached服务的最大连接数

-vvv 显示详细信息

-n chunk size 的最小空间是多少,单位为字节

-f chunk size 大小增长的倍数,默认1.25倍

-d 在后台启动

服务器 192.168.4.1/24
一、基础应用
1、安装
[root@vh01 ~]# yum install -y memcached
[root@vh01 ~]# systemctl start memcached
[root@vh01 ~]# systemctl enable memcached
2、测试
[root@vh01 ~]# yum install -y telnet
[root@vh01 ~]# telnet 127.0.0.1 11211
set name 0 180 3 # 设置变量名为name,0表示不压缩,
#180表示name变量的缓存时间是180秒,
#3指的是name变量占用3字节空间
zzg #变量name的值
get name #获取变量name的值
add myname 0 180 5 #添加变量myname,如果变量存在则报错
hello #myname的值
set name 0 180 10 #添加或替换变量
replace name 0 180 10 #替换
append name 0 180 10 #向变量中追加数据
delete name #删除变量
stats #查看状态
flush_all #清空所有
quit #退出登录

二、LNMP+MEMCHACHE
1、在memcache服务器上安装LNMP结构
yum install -y gcc gcc-c++ pcre-devel openssl-devel zlib-devel 
useradd -s /sbin/nologin nginx
tar xzf nginx-1.8.0.tar.gz 
cd nginx-1.8.0/
./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_ssl_module
make && make install

2、安装mariadb
yum install -y mariadb-server mariadb-devel

3、安装php
yum install -y php php-mysql php-fpm-5.4.16-36.el7_1.x86_64.rpm

4、为 PHP 添加 memcache 扩展
yum install -y php-pecl-memcache

5、修改nginx配置文件
vim /usr/local/nginx/conf/nginx.conf
....
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;
}

6、检查nginx配置文件语法
/usr/local/nginx/sbin/nginx -t

7、起动相关服务
cp nginx.service /usr/lib/systemd/system/
systemctl start nginx.service 
systemctl start mariadb.service 
systemctl start php-fpm
systemctl enable nginx.service
systemctl enable mariadb.service
systemctl enable php-fpm
netstat -utnlp | grep :80

8、创建测试页面并访问
vim /usr/local/nginx/html/test.php
<?php
$memcache=new Memcache;
$memcache->connect('localhost', 11211) or die('could not connect');
$memcache->set('username', 'zhangsan');
$get_value=$memcache->get('username');
echo $get_value;
?>

firefox http://192.168.4.1/test.php

nginx负载均衡调度
1、配置nginx将用户请求发往后端的tomcat服务器
vim /usr/local/nginx/conf/nginx.conf
upstream tomcatgrp {
server 192.168.4.2:8080;
server 192.168.4.3:8080;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://tomcatgrp;
root html;
index index.php index.html index.htm;
}
}

启动Nginx服务
/usr/local/nginx/sbin/nginx -t
systemctl restart nginx.service

2、在vh02和vh03上安装tomcat
tar xzf apache-tomcat-8.0.30.tar.gz
mv apache-tomcat-8.0.30 /usr/local/tomcat
/usr/local/tomcat/bin/startup.sh 
netstat -utnalp | grep :8080

3、设置测试页可以显示Session ID信息
vim /usr/local/tomcat/webapps/ROOT/test.jsp
<html>
<body bgcolor="yellow"> 指定网页背景颜色
<%String s = session.getId();%> 获取SessionID
<%=s%>
<h1>tomcat web server A</h1> 固定字串信息
</body>
</html>

4、客户端(物理主机作为客户端)访问调度器
firefox http://192.168.4.1/test.jsp

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

实现session共享

在两台后端Web服务器vh02和vh03上安装msm
一、安装msm

1、拷贝msm相关的jar包
cp session/*.jar /usr/local/tomcat/lib/

2、修改tomcat,使其可以连接到memcache服务器
vim /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>

3、重启tomcat
/usr/local/tomcat/bin/shutdown.sh 
netstat -taulnp | grep :8080
/usr/local/tomcat/bin/startup.sh

4、客户端测试
客户端使用浏览器访问调度服务器。


     本文转自夜流璃雨 51CTO博客,原文链接:http://blog.51cto.com/13399294/2061824,如需转载请自行联系原作者


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1,在tomcat中找到文件apache-tomcat-6.0.37\conf\context.xml 加入内部 <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:192.168.1.65:11211" requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$" sessionBackupAsync="false" sessionBackupTimeout="100" transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory" copyCollectionsForSerialization="false" /> 加入之后的content.xml的内容为 <?xml version='1.0' encoding='utf-8'?> <!-- Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. --> <!-- The contents of this file will be loaded for each web application --> <Context> <!-- Default set of monitored resources --> <WatchedResource>WEB-INF/web.xml</WatchedResource> <!-- Uncomment this to disable session persistence across Tomcat restarts --> <!-- <Manager pathname="" /> --> <!-- Uncomment this to enable Comet connection tacking (provides events on session expiration as well as webapp lifecycle) --> <!-- <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" /> <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager" memcachedNodes="n1:192.16

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值