memcache同步session的步骤(文中用的PHP的例子,但是和java原理是一样的)
memcache同步session的方法(文中用的PHP的例子,但是和java原理是一样的)
前面讲了如何配置Windows Server 2003网络负载平衡系统,那么在web架构中配置好Server群集后,我们肯定会考虑session共享和同步的问题。试想下,同一个IP访问负载平衡系统中的同一个网页会被分配到不同的服务器上,而如果session不同步,那么对于同一个登录用户,就会出现一会是登录状态一会又不是登录状态的问题。
下面就这个问题给出了三种解决思路,供大家参考:
一、利用数据库同步session
在做多服务器session同步时我没有用这种方法,如果非要用这种方法的话,我想过二种方法:
1、用一个低端电脑建个数据库专门存放web服务器的session,或者,把这个专门的数据库建在文件服务器上,用户访问web服务器时,会去这个专门的数据库check一下session的情况,以达到session同步的目的。
2、这种方法是把存放session的表和其他数据库表放在一起,如果mysql也做了集群了话,每个mysql节点都要有这张表,并且这张session表的数据表要实时同步。
说明:用数据库来同步session,会加大数据库的负担,数据库本来就是容易产生瓶颈的地方,如果把session还放到数据库里面,无疑是雪上加霜。上面的二种方法,第一点方法较好,把放session的表独立开来,减轻了真正数据库的负担
二、利用cookie同步session
session是文件的形势存放在服务器端的,cookie是文件的形势存在客户端的,怎么实现同步呢?方法很简单,就是把用户访问页面产生的 session放到cookie里面,就是以cookie为中转站。你访问web服务器A,产生了session把它放到cookie里面了,你访问被分配到web服务器B,这个时候,web服务器B先判断服务器有没有这个session,如果没有,在去看看客户端的cookie里面有没有这个 session,如果也没有,说明session真的不存,如果cookie里面有,就把cookie里面的sessoin同步到web服务器B,这样就可以实现session的同步了。
说明:这种方法实现起来简单,方便,也不会加大数据库的负担,但是如果客户端把cookie禁掉了的话,那么session就无从同步了,这样会给网站带来损失;cookie的安全性不高,虽然它已经加了密,但是还是可以伪造的。
三、利用memcache同步session
memcache可以做分布式,如果没有这功能,他也不能用来做session同步。他可以把web服务器中的内存组合起来,成为一个”内存池”,不管是哪个服务器产生的sessoin都可以放到这个”内存池”中,其他的都可以使用。
优点:以这种方式来同步session,不会加大数据库的负担,并且安全性比用cookie大大的提高,把session放到内存里面,比从文件中读取要快很多。
缺点:memcache把内存分成很多种规格的存储块,有块就有大小,这种方式也就决定了,memcache不能完全利用内存,会产生内存碎片,如果存储块不足,还会产生内存溢出。
四、总结
上面三种方法都是可行的
第一种方法,最影响系统速度的那种,不推荐使用;
第二种方法,效果不错,不过安全隐患一样的存在;
第三种方法,个人觉得第三种方法是最好的,推荐大家使用。
五、web集群时利用memcache来同步session
觉得用memcache来同步session是最好的,下面是我的设置过程
1、模拟web集群
我启动了二个memcached进程,分别模拟二台服务器
/usr/local/bin/memcached-d-m1024-c3000-uuenucom-p12000-P./memcached.pid
/usr/local/bin/memcached-d-m1024-c3000-uuenucom-p13000-P./mem.pid
2、修改php的配置
vi/usr/local/php/lib/php.ini
session.save_handler=“memcache”
memcache.hash_strategy=“consistent”
session.save_path=“tcp://127.0.0.1:13000?weight=10,tcp://127.0.0.1:12000″
说明:第一行,session的储存方式是memcache;第二行,memcache的hash算法是consistent;第三行,session储存的位置;
3、重启apache
查看phpinfo
session
SessionSupportenabled
Registeredsavehandlersfilesusersqlitememcache
Registeredserializerhandlersphpphp_binary
紧接着下面是
session.save_pathtcp://127.0.0.1:13000,tcp://127.0.0.1:12000
4、做个简单测试
a)准备文件session.php
PHP代码
session_start();
$_SESSION['username']=“abcabc”;echosession_id();
?>
b)显示session内容文件
PHP代码
$mem=newMemcache;
$mem->addServer(“127.0.0.1″,12000)ordie(“Couldnotaddserver12000″);
$mem->addServer(“127.0.0.1″,13000)ordie(“Couldnotaddserver13000″);
$val=$mem->get(‘qp0mrob2ovcqle3u4lbr4obsa5′);
//echosession_id();
echo$val;
?>
c)结果是username|s:6:”abcabc”;
?
转载:
?http://blog.itpub.net/27042095/viewspace-1218269/

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

In iOS 17, Apple introduced several new privacy and security features to its mobile operating system, one of which is the ability to require two-step authentication for private browsing tabs in Safari. Here's how it works and how to turn it off. On an iPhone or iPad running iOS 17 or iPadOS 17, Apple's browser now requires Face ID/Touch ID authentication or a passcode if you have any Private Browsing tab open in Safari and then exit the session or app to access them again. In other words, if someone gets their hands on your iPhone or iPad while it's unlocked, they still won't be able to view your privacy without knowing your passcode

Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

Cookies on the mobile phone are stored in the browser application of the mobile device: 1. On iOS devices, Cookies are stored in Settings -> Safari -> Advanced -> Website Data of the Safari browser; 2. On Android devices, Cookies Stored in Settings -> Site settings -> Cookies of Chrome browser, etc.

With the popularity of the Internet, we use browsers to surf the Internet have become a way of life. In the daily use of browsers, we often encounter situations where we need to enter account passwords, such as online shopping, social networking, emails, etc. This information needs to be recorded by the browser so that it does not need to be entered again the next time you visit. This is when cookies come in handy. What are cookies? Cookie refers to a small data file sent by the server to the user's browser and stored locally. It contains user behavior of some websites.

Cockpit is a web-based graphical interface for Linux servers. It is mainly intended to make managing Linux servers easier for new/expert users. In this article, we will discuss Cockpit access modes and how to switch administrative access to Cockpit from CockpitWebUI. Content Topics: Cockpit Entry Modes Finding the Current Cockpit Access Mode Enable Administrative Access for Cockpit from CockpitWebUI Disabling Administrative Access for Cockpit from CockpitWebUI Conclusion Cockpit Entry Modes The cockpit has two access modes: Restricted Access: This is the default for the cockpit access mode. In this access mode you cannot access the web user from the cockpit

The latest feature update for Windows 11 is here, and it brings various app updates and improvements, however, many people are reporting that Windows 1123H2 does not have Copilot. This is unfortunate, especially if you're looking forward to trying out new AI assistants. We at Windows Report have encountered the same problem, and in today's guide we will show you some potential solutions that may help if Windows 11 Copilot is missing. How to enable Copilot on 23H2? How do we test, review and score? Over the past 6 months, we have been working hard to establish a new review system for how we produce content. use it
