一.php+memcache的部署

      memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用来减轻数据库负载。它通过在内存中缓存数据去减少读取数据库的次数,从而提高动态网站的访问速度。目前被许多网站使用以提升网站的访问速度,尤其对于一些大型的、需要频繁访问数据库的网站访问速度提升效果十分明显。Memcache通过在内存里维护一个统一的巨大的hash表,它能够用来存储各种格式的数据,包括视频,文件以及数据库检索的结果等,将数据调用到内存中,然后从内存中读取,从而大大提高读取速度。memcached是一种无阻塞的socket通信方式服务,基于libevent库,由于无阻塞通信,对内存读写速度非常之快。

1.memcache的安装部署(php在上节已经部署成功)

(1)把php的命令路径添加到环境变量的配置文件中

rhel6.5-memcache

rhel6.5-memcache

(2)创建在php中memcache的预编译环境

rhel6.5-memcache

(3)源码包的编译安装(memcache的源码包:memcache-2.2.5.tgz )

rhel6.5-memcache

rhel6.5-memcache

/usr/local/lnmp/php/lib/php/extensions/no-debug-non-zts-20131226/        ##memcache模块的路径

rhel6.5-memcache

(4)在php的配置文件中添加memcache.so的模块

vim /usr/local/lamp/php/etc/php.ini  ##php的配置文件

rhel6.5-memcache

php -m     ###查看php支持的模块

rhel6.5-memcache

rhel6.5-memcache

(5)memcache由socket进行通信,memcached提供后台服务(端口11211)

rhel6.5-memcache

/etc/init.d/memcached    start         ##开启memcached服务

rhel6.5-memcache

2.设置memcache的测试页

(1)源码包提供了测试页面,复制到nginx的发布目录下

rhel6.5-memcache

(2)memcache的可视化页面

vim memcache.php        ##memcache可视化页面的配置文件

memcache有用户名和密码的设定,更安全,可以直观的查看缓存命中情况

rhel6.5-memcache

rhel6.5-memcache

rhel6.5-memcache

rhel6.5-memcache

不断刷新memcache的测试页面example.php

rhel6.5-memcache

memcache的缓存命中率在上升

rhel6.5-memcache

3.memcache的压力测试

物理机测试

ab -c 10 -n 5000 http://172.25.78.2index.html   ###静态页面最快,每一秒达到5982的请求

rhel6.5-memcache

ab -c 10 -n 5000 http://172.25.78.2/example.php  ###memcache的测试页面走的memcache,比较快

rhel6.5-memcache

ab -c 10 -n 5000 http://172.25.78.2/index.php    ###普通的测试页面没有走memcache,慢

-c     ##每秒10个并发访问,共访问5000个

rhel6.5-memcache

显然memcache的测试页面要快太多,是普通测试页面速度的3倍左右

php-memcache的流程:

client--nginx:80--fastcgi_pass--php-fpm:9000:--php-memcache--memcache:11211

二.nginx+memcache的部署、

客户端访问的请求,不通过php的传接,直接访问memcache:11211,速度会更快

client--nginx:80--memcache:11211

因为memc和srcache模块是第三方模块,openresty提供了这些模块,要进行openresty的编译安装

OpenResty: 是一个基于nginx 与 Lua 的高性能 Web 平台,其内部集成了大量精良的 Lua 库、第三方模块以及大多数的依赖项。用于方便地搭建能够处理超高并发、扩展性极高的动态 Web 应用、Web 服务和动态网关。通过汇聚各种设计精良的 nginx 模块,从而将nginx有效地变成一个强大的通用 Web 应用平台。这样,Web 开发人员和系统工程师可以使用 Lua 脚本语言调动nginx支持的各种 C 以及 Lua模块,快速构造出足以胜任 10K 乃至 1000K 以上单机并发连接的高性能 Web 应用系统。目标是让你的Web服务直接跑在nginx服务内部,充分利用 nginx的非阻塞 I/O 模型,不仅仅对 HTTP 客户端请求,甚至于对远程后端诸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都进行一致的高性能响应。

openresty的中文官网:http://openresty.org/cn/

因为Nginx并不支持模块动态加载,所以要安装新的模块,必须重新编译Nginx。

1.openresty的源码编译安装(源码包:openresty-1.13.6.1.tar.gz)

(1)编译安装

rhel6.5-memcache

rhel6.5-memcache

rhel6.5-memcache

(2)openresty的路径在/usr/local/下(关闭原来的nginx的端口,在openresty的目录下会有新的nginx)

./nginx -s reload       ##否则当openresty下的nginx配置完更新时,会报错,端口冲突了
nginx: [error] invalid PID number "" in "/usr/local/openresty/nginx/logs/nginx.pid"         ###原来的nginx的端口冲突

rhel6.5-memcache

把example.php的memcache的测试页面,复制到新的nginx的目录下,方便测试

rhel6.5-memcache

(3)nginx的配置文件的设置

vim /usr/local/openresty/nginx/conf/nginx.conf

先需要定义memcache的upstream,定义一个缓存器memcache,端口为本机的11211

rhel6.5-memcache

给memc-nginx-module配置location,下面配置的location是/memc,所有请求都通过请求这个location请求memcache

rhel6.5-memcache

##nginx直接去了memcache的11211端口

rhel6.5-memcache

(4)更新配置文件

rhel6.5-memcache

2.在物理机上进行压力测试

ab -c 10 -n 5000 http://172.25.15.1/example.php       ##比前面的php的memcache更快一点,直接去访问memcache的11211端口

三.tomcat+memcache的部署

Tomcat:技术先进、性能稳定,而且免费,因而深受Java爱好者的喜爱并得到了部分软件开发商的认可,是目前比较流行的Web应用服务器,Tomcat很受广大程序员的喜欢,因为它运行时占用的系统资源小,扩展性好,支持负载均衡与邮件服务等开发应用系统常用的功能,是一个小型的轻量级应用服务器,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP程序的首选。

1.tomcat的安装部署

(1)java压缩包和和tomcat压缩包解压就直接能使用

 java包:jdk-7u79-linux-x64.tar.gz 

tomcat包:apache-tomcat-7.0.37.tar.gz

做一个链接方便调用

rhel6.5-memcache

rhel6.5-memcache

(2)添加java的命令的环境路径

rhel6.5-memcache

rhel6.5-memcache

(3)测试环境

写一个java代码测试以下

rhel6.5-memcache

环境正常

rhel6.5-memcache

测试:http://172.25.78.2:8080

rhel6.5-memcache

开启tomcat,tomcat的端口是8080

rhel6.5-memcache

(4)测试

/usr/local/tomcat/webapps/ROOT/      ##tomcat的发布目录,把要发布的文件放在此目录下即可

rhel6.5-memcache

rhel6.5-memcache

2.nginx的tomcat的负载均衡

server2:172.25.78.2:8080

server3:172.25.78.3:8080

定义一个tomcat负载均衡器,当以.jsp结尾的访问,去匹配jsp的location的模块

rhel6.5-memcache

rhel6.5-memcache

更新nginx的配置信息

rhel6.5-memcache

定义server3的发布文件

rhel6.5-memcache

rhel6.5-memcache

测试:curl 172.25.78.2/test.jsp

rhel6.5-memcache

3.nginx的扩展模块nginx-sticky-module

nginx-sticky-module 为 nginx 的第三方模块,使 nginx 支持 sticky 模式,所谓 sticky 模式就是指同一个用户的访问请求都被发送到同一个 tomcat 实例上处理,nginx以前对session保持支持不太好,主要采用ip_hash把同一来源的客户(同一C段的IP)固定指向后端的同一台机器,ip_hash有个缺点是不能实现很好的负载均衡;直到nginx的扩展模块nginx-sticky-module的出现,解决了session sticky的问题。从同一个客户端来的访问一直指向刚开始分配的后端服务器,不然负载均衡之后,客户端请求又被分配到另一个后端服务器,客户又要再次登陆。

(1)重新编译nginx来添加nginx-sticky-module(nginx包:nginx-1.10.1.tar.gz)

(nginx-sticky-module源码包:nginx-sticky-module-ng.tar.gz)

rhel6.5-memcache

rhel6.5-memcache

把之前openresty下的nginx直接复制过来使用

rhel6.5-memcache

(2)nginx对sticky模块的配置(因为1.10版本不支持memcache,要删除之前对memcache的配置)

直接把sticky添加到负载均衡部分,即可配合使用

rhel6.5-memcache

(3)设置session会话的测试页面(server2和server3都设置)

vim  /usr/local/tomcat/webapps/ROOT/test.jsp         ##session的测试文件

rhel6.5-memcache

开启server2和server3的tomcat,nginx  -s  reload 更新nginx的配置文件

测试:

rhel6.5-memcache

分配到的后端服务器为server2,在/usr/local/tomcat/catalina.out,可以看见session的记录,一旦分配到server2,进行多次操作依旧在server2中,

rhel6.5-memcache

server3中/usr/local/tomcat/catalina.out没有session的记录

rhel6.5-memcache

如果关掉server2的tomcat,访问指向server3作为后端服务器,

rhel6.5-memcache

session的记录在server3中

rhel6.5-memcache

4.session的共享存储

memcached-session-manager的工作原理

     所有的tomcat节点需要安装memcached-session-manager,每一个tomcat会有自己的本地session,当一个请求执行完毕之后,如果对应的session之前不存在(也就是说这是某个用户的第一次请求),则将该session拷贝一份副本至memcached缓存,当该session的下一个请求到达时,会使用tomcat的本地session,请求处理结束之后,session的变化会同步更新到memcached缓存中对应的session里,从而确保本地session和缓存中的session始终保持一致。如果当前结点失效,下一个请求会被路由给另外一个tomcat处理,这个tomcat发现请求所属的session并不存在,于是它将查询memcached缓存,并将查询到的session恢复到本地,这样就完成了容错处理,memcached-session-manager也支持non-sticky session。

tomcat采用交叉共享存储session

rhel6.5-memcache

Tomcat1 将 session 存储在 memcached2 上。只有当 M2 不可用时,T1 才将 session 存
储在 memcached1 上(M1 是 T1 failoverNode)。使用这种配置的好处是,当 T1 和 M1 同时崩

溃时也不会丢失 session 会话,避免单点故障。

(1)session共享存储的部署

在server2和server3都安装memcached服务,并启动(server2之前已经配置过了)

rhel6.5-memcache

rhel6.5-memcache

(2)配置session共享存储所需的jar包

rhel6.5-memcache

把jar包复制到/sur/local/tomcat/lib

rhel6.5-memcache

(3)设置tomcat的配置文件,使其支持session共享存储

vim /usr/local/tomcat/conf/context.xml             ##tomcat的配置文件

server2:

rhel6.5-memcache

server3:

失效节点:failoverNodes=n2   ##当server3的session交叉存储点srever2上的memcache挂掉,就把session存储在本地server3上

rhel6.5-memcache

(4)开启server2和server3的tomcat

rhel6.5-memcacherhel6.5-memcache

(5)测试:

开启tomcat后,可见memcachedsessionservice开始初始化,读取设置的memcache的两个节点

rhel6.5-memcache

可见172.25.78.2的session存储在server3,实现了交叉存储

n2:172.25.78.3

n1:172.25.78.2

rhel6.5-memcache

关闭server3的memcache,也就是关闭n2节点,server2的session就存储在n1

rhel6.5-memcache

rhel6.5-memcache

在server2创建信息

rhel6.5-memcache

当后端服务指定到server3上,session也会同步过来

rhel6.5-memcache




相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-01-16
  • 2021-11-13
  • 2021-05-02
  • 2022-12-23
  • 2022-12-23
  • 2021-09-21
猜你喜欢
  • 2021-05-31
  • 2022-12-23
  • 2021-11-07
  • 2021-06-04
  • 2021-09-01
  • 2021-08-08
相关资源
相似解决方案