构建memcached服务&使用Tomcat设置Session

案例1:构建memcached服务
本案例要求先快速搭建好一台memcached服务器,并对memcached进行简单的添、删、改、查操作:
安装memcached软件,并启动服务
使用telnet测试memcached服务
对memcached进行增、删、改、查等操作
步骤一:构建memcached服务
1)使用yum安装软件包memcached
[root@svr5 ~]# yum -y install memcached
[root@svr5 ~]# rpm -qa memcached
memcached-1.4.15-9.el7.x86_64
2)启动服务并查看网络连接状态验证是否开启成功:
[root@svr5 ~]# systemctl start memcached
[root@svr5 ~]# systemctl status memcached
[root@svr5 ~]# netstat -anptu | grep memcached
步骤二:使用telnet访问memcached服务器
1)使用yum安装telnet
[root@svr5 ~]# yum –y install telnet
2)使用telnet连接服务器测试memcached服务器功能,包括增、删、改、查等操作。
[root@svr5 ~]# telnet 127.0.0.1 11211
set name 0 180 3 //定义变量,变量名称为name,0表示不压缩,180表示缓存时间,3表示占用3字节空间
plj //输入变量的值,值为plj
STORED
get name //获取变量的值
VALUE name 0 3 //输出结果
plj
END
add myname 0 180 10 //新建,myname不存在则添加,存在则报错
set myname 0 180 10 //添加或替换变量
replace myname 0 180 2 //替换,如果myname不存在则报错(可改后面参数)
get myname //读取变量
append myname 0 180 10 //向变量中追加数据
delete myname //删除变量
stats //查看状态
flush_all //清空所有
quit //退出登录
######################################################
date -d '@1513046563' 时间戳

案例2:LNMP+memcached
沿用练习一,部署LNMP+memcached网站平台,通过PHP页面实现对memcached服务器的数据操作,实现以下目标:
部署LNMP实现PHP动态网站架构
为PHP安装memcache扩展
创建PHP页面,并编写PHP代码,实现对memcached的数据操作
步骤一:部署LNMP环境

1)使用yum安装基础依赖包
[root@svr5 ~]# yum -y install gcc openssl-devel pcre-devel zlib-devel
2)源码安装Nginx
从真机#scp nginx-1.8.0.tar.gz memcached服务器IP:/root
[root@svr5 ~]# useradd -s /sbin/nologin www
[root@svr5 ~]# tar -zxvf nginx-1.8.0.tar.gz
[root@svr5 ~]# cd nginx-1.8.0
[root@svr5 nginx-1.8.0]# ./configure \

--user=www --group=www \
--with-http_ssl_module
[root@svr5 ~]# make && make install
3)安装MariaDB数据库
[root@svr5 ~]# yum –y install mariadb mariadb-server mariadb-devel
4)安装PHP
[root@svr5 ~]# yum –y install php php-mysql
[root@svr5 ~]# yum –y localinstall php-fpm-5.4.16-36.el7_1.x86_64.rpm
5)为PHP添加memcache扩展
[root@svr5 ~]# yum –y install php-pecl-memcache
6)修改Nginx配置文件
[root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf
location / {
root html;
index index.php index.html index.htm;
}
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;
}
取消注释符号,修改最后一行即可。

步骤二:启动服务

1)启动Nginx服务
这里需要注意的是,如果服务器上已经启动了其他监听80端口的服务软件(如httpd),则需要先关闭该服务,否则会出现冲突。
[root@svr5 ~]# systemctl stop httpd //如果该服务存在,则关闭该服务
[root@svr5 ~]# /usr/local/nginx/sbin/nginx

2)启动MySQL服务
[root@svr5 ~]# systemctl start mariadb
[root@svr5 ~]# systemctl status mariadb
3)启动PHP-FPM服务
[root@svr5 ~]# systemctl start php-fpm
[root@svr5 ~]# systemctl status php-fpm

步骤三:创建PHP页面,使用PHP语言测试memcached服务

创建PHP首页文档/usr/local/nginx/html/index.php:
[root@svr5 ~]# 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;
?>
步骤四:客户端测试

客户端使用浏览器访问服务器PHP首页文档,检验对memcached的操作是否成功:
[root@client ~]# firefox http://192.168.4.5/test.php

#可能错误。主机名错误,端口错误,防火墙,selinux阻止

#############################################################

3 案例3:使用Tomcat设置Session
使用4台RHEL7虚拟机,其中一台作为Nginx前端调度器服务器(eth0:192.168.4.5,eth1:172.16.2.5)、两台虚拟机部署为Tomcat服务器,分别为Web1服务器(192.168.2.100)和Web2服务器(192.168.2.200),另外一台作为测试用的Linux客户机(172.16.4.100)
步骤一:部署前端Nginx调度服务器

1)使用源码安装nginx软件
[root@svr5 ~]# yum -y install pcre-devel openssl-devel
[root@svr5 ~]# tar -zxvf nginx-1.8.0.tar.gz
[root@svr5 ~]# cd nginx-1.8.0
[root@svr5 nginx-1.8.0]# ./configure
[root@svr5 nginx-1.8.0]# make && make install
2)修改Nginx配置文件
Nginx配置文件中,通过upstream定义后端服务器地址池,默认调度策略为轮询,使用proxy_pass调用upstream定义的服务器地址池:
[root@svr5 ~]# vim /usr/local/nginx/conf/nginx.conf
.. ..
upstream tomcatgrp {
server 192.168.2.100:8080;
server 192.168.2.200:8080;
}
server {
listen 80;
server_name localhost;
location / {
proxy_pass http://tomcatgrp;
root html;
index index.html index.htm;
}
}
3)启动Nginx服务

[root@svr5 ~]# /usr/local/nginx/sbin/nginx
步骤二:部署Tomcat

注意:以下部署Tomcat的操作,需要在两台后端服务器做相同的操作,下面我们以一台Web1服务器(192.168.2.100)为例,对Web2服务器执行相同操作即可。
1)安装jdk环境
[root@svr100 ~]# yum –y install java-1.8.0-openjdk
2)安装tomcat软件
[root@svr100 ~]# tar -zxvf apache-tomcat-8.0.30.tar.gz
[root@svr100 ~]# mv apache-tomcat-8.0.30 /usr/local/tomcat
3)启动tomcat服务,并查看端口信息
[root@svr100 ~]# /usr/local/tomcat/bin/startup.sh //启动tomcat
[root@svr100 ~]# netstat -utnalp | grep :8080
tcp 0 0 :::8080 :::* LISTEN 3181/java
4)创建JSP测试页面,要求页面可以显示Session ID信息
通过在页面代码中添加JSP语句<%String s = session.getId();%>即可:
[root@svr100 ~]# ~]# vim /usr/local/tomcat/webapps/ROOT/test.jsp
<html>
<body bgcolor="red"> //指定网页背景颜色
<center>
<%String s = session.getId();%> //获取SessionID
<%=s%>
<h1>tomcatA</h1> //固定字串信息,Web2的信息应该为tomcatB
</center>
</body>
</html>
步骤三:客户端访问测试效果

1)客户端使用浏览器访问测试页面
[root@pc01 ~]# firefox http://192.168.4.5/test.jsp
客户端两次访问的页面,应该对应的是Web1和Web2服务器返回的不同页面,并且会分别显示不同的Session ID信息
#########################################################
案例4:Tomcat实现session共享
步骤一:构建memcached服务

1)安装Memcached服务
[root@svr5 ~]# yum –y install memcached
2)启动服务并查看网络连接状态验证是否开启成功:
[root@svr5 ~]# systemctl start memcached
[root@svr5 ~]# netstat -anptu | grep memcached
步骤二:在Tomcat服务器上部署msm

注意:这些操作在两台后端Web服务器上均需要执行,以下操作以Web1(192.168.2.100)服务器为例。
1)安装msm
[root@svr100 ~]# cd lnmp-soft/session //从真机中scp lnmp-solf包
[root@svr100 session]# ls .jar
asm-5.1.jar minlog-1.3.0.jar
kryo-3.0.3.jar msm-kryo-serializer-1.9.2.jar
kryo-serializers-0.34.jar reflectasm-1.11.1.jar
memcached-session-manager-1.9.2.jar spymemcached-2.11.1.jar
memcached-session-manager-tc8-1.9.2.jar
[root@svr100 session]# cp
.jar /usr/local/tomcat/lib/
2)修改Tomcat配置文件,连接memcached服务器
[root@svr100 ~]# ~]# vim /usr/local/tomcat/conf/context.xml
<Context>
<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
memcachedNodes="mem1:192.168.2.5:11211"
requestUriIgnorePattern=".*.(ico|png|gif|jpg|css|js)$"
transcoderFactoryClass="de.javakaffee.web.msm.JavaSerializationTranscoderFactory"/>
</Context>
[root@localhost ~]# /usr/local/tomcat/bin/shutdown.sh
[root@localhost ~]# /usr/local/tomcat/bin/startup.sh

以上步骤在web2上类似实施

步骤三:客户端测试
[root@pc01 ~]# firefox http://192.168.4.5/test.jsp
客户端两次访问的页面,应该对应的是Web1和Web2服务器返回的不同页面,但是Session ID信息不会变化。

转载于:https://blog.51cto.com/13402239/2049920

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值