简体   繁体   English

负载均衡和APC

[英]Load balancing and APC

I am interested in a scenario where webservers serving a PHP application is set up with a load balancer. 我感兴趣的是一个服务于PHP应用程序的Web服务器设置了负载均衡器的场景。

There will be multiple webservers with APC behind the load balancer. 在负载均衡器后面将有多个带有APC的Web服务器。 All requests will have to go through the load balancer, which then sends it to one of the web servers to process. 所有请求都必须通过负载均衡器,然后负载均衡器将其发送到其中一个Web服务器进行处理。

I understand that memcached should be used for distributed caching, but I think having the APC cache on each machine cache things like application configurations and other objects that will NOT be different across any of the servers would yield even better performance. 据我所知,memcached的应该用于分布式缓存,但我认为有像应用程序配置和其他对象将被跨越不同任何服务器会产生更好的性能每台机器高速缓存的东西APC缓存。

There is also an administrator area for this application. 此应用程序还有一个管理员区域。 It is also accessed via the load balancer (for example, site.com/admin). 它也可以通过负载均衡器(例如,site.com/admin)访问。 In a case like this, how can I call apc_clear_cache to clear the APC object cache on ALL servers? 在这种情况下,如何调用apc_clear_cache来清除所有服务器上的APC对象缓存?

Externally in your network you have a public IP you use to route all your requests to your load balancer that distributes load round robin so outside you cannot make a request to clear your cache on each server one at a time because you don't know which one is being used at any given time. 在您的网络外部,您有一个公共IP,用于将您的所有请求路由到负载均衡器并分配负载循环,因此您不能在每个服务器上请求清除缓存,因为您不知道哪个一个在任何给定的时间使用。 However, within your network, each machine has its own internal IP and can be called directly. 但是,在您的网络中,每台计算机都有自己的内部IP,可以直接调用。 Knowing this you can do some funny/weird things that do work externally. 知道这一点,你可以做一些外部工作的有趣/奇怪的事情。

A solution I like is to be able to hit a single URL and get everything done such as http://www.mywebsite/clearcache.php or something like that. 我喜欢的解决方案是能够点击一个URL并完成所有工作,例如http://www.mywebsite/clearcache.php或类似的东西。 If you like that as well, read on. 如果您也喜欢,请继续阅读。 Remember you can have this authenticated if you like so your admin can hit this or however you protect it. 请记住,如果您愿意,可以对此进行身份验证,以便您的管理员可以点击这个或者保护它。

You could create logic where you can externally make one request to clear your cache on all servers. 您可以创建逻辑,从外部发出一个请求以清除所有服务器上的缓存。 Whichever server receives the request to clear cache will have the same logic to talk to all servers to clear their cache. 无论哪个服务器收到清除缓存的请求,都会使用相同的逻辑与所有服务器通信以清除其缓存。 This sounds weird and a bit frankenstein but here goes the logic assuming we have 3 servers with IPs 10.232.12.1, 10.232.12.2, 10.232.12.3 internally: 这听起来很奇怪,有点弗兰肯斯坦,但这里的逻辑假设我们有3台服务器内部IP为10.232.12.1,10.232.12.2,10.232.12.3:

1) All servers would have two files called "initiate_clear_cache.php" and "clear_cache.php" that would be the same copies for all servers.

2) "initiate_clear_cache.php" would do a file_get_contents for each machine in the network calling "clear_cache.php" which would include itself
for example: 
file_get_contents('http://10.232.12.1/clear_cache.php');
file_get_contents('http://10.232.12.2/clear_cache.php');
file_get_contents('http://10.232.12.3/clear_cache.php');

3) The file called "clear_cache.php" is actually doing the cache clearing for its respective machine.

4) You only need to make a single request now such as http://www.mywebsite/initial_clear_cache.php and you are done.

Let me know if this works for you. 如果这对您有用,请告诉我。 I've done this in .NET and Node.js similar but haven't tried this in PHP yet but I'm sure the concept is the same. 我在.NET和Node.js中做过类似的但是还没有在PHP中尝试过这个,但我确信这个概念是一样的。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM