如何实施“循环法” Java 中的负载平衡 Web 服务器
我希望在一个非常简单的 HTTP Web 服务器上用 Java 实现“循环”负载平衡。
在这种情况下,循环意味着每个端口一个 HTTP 请求。
假设我有一个网络服务器,为 3 个不同的端口运行 3 个线程,每个线程侦听该端口上的连接。 (这种方法可能是错误的。)
因此,一个 HTTP 连接请求来自某个端口,并假设这需要 3 个 HTTP 请求(例如,一个包含 2 个图像的网页)。例如,如果此请求来自端口 1234,而我的其他 2 个线程位于另外 2 个端口上什么都不做,我将如何对其进行负载平衡?因此,第一个线程获取第一个请求,第二个线程获取下一个请求,第三个线程获取下一个请求,然后再次返回。 (如果这是我对循环赛的正确理解。)
I'm looking to implement "Round Robin" Load Balancing in Java on a very simple HTTP web server.
Round Robin in this case would mean one HTTP request per port.
Say I have a webserver running 3 threads for 3 different ports each thread listening for a connection on that port. (This approach may be wrong.)
So a HTTP connection request comes in on a port, and say this needs 3 HTTP requests (a web page with 2 images for example). If this request came in on port 1234, for example, and my other 2 threads are sitting on 2 other ports doing nothing, how would I load balance this? So the first thread gets the first request, 2nd thread the next, 3rd the next, and back again. (If this is my correct understanding of round robin.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要两件事:
1)您需要一个可以在端口之间按顺序永远轮换的数据结构。这被称为“循环数据结构”。
2) 您需要确保数据结构在线程之间可共享,也就是说,当一个线程开始使用端口执行某项操作时,数据结构被通知该端口正忙的事实,然后特定端口被锁定。
一种简单的方法可能是在此处使用同步循环队列,并在两个槽中的每一个槽中使用锁。
将每个端口的代表性锁添加到队列后,您可以让每个线程在使用时锁定该特定端口。一旦锁被释放,工作就可以重新开始。
因此,首先,我们将创建一个具有 2 个锁的队列,并且服务器将在队列中拥有一个迭代器。
当请求到来时 - 线程将向服务器询问端口。服务器将检查当前(第一个)插槽是否已锁定。如果是这样,它将等待 --- 当解锁时,它将异步地将锁提供给线程,并增加队列中的位置。然后,线程将锁定该资源、处理请求并解锁。一旦线程完成处理,它将解锁资源。同时,如果第二个线程到来,服务器可以自由分配第二个端口(如果该端口空闲)。
当然,您的设计中存在潜在的死锁:如果一个线程从未完成,服务器将被卡住,等待锁解锁,然后再递增到下一个端口。
http://www.koders.com/java/fid13E588928D0C01917AC9C30E35D802BDBA546368.aspx?s=Queue#L23
You need two things :
1) You need a data structure which will rotate between the ports, in order, forever. This is known as a "circular data structure".
2) You will need to ensure that the data structure is shareable between threads, that is, that when one thread starts using a port for doing something, the data structure is notified of the fact that that port is busy, and then the particular port is locked.
A bare bones approach could be to use a synchronized circular queue here with locks in each of two slots.
Once you add each port's representative lock to the queue, you can have each thread lock that particular port while in use. Once the lock is released, the work can begin anew.
So first, we will create the queue to have 2 locks init, and the server will have an iterator in the queue.
When the request comes in - the thread will ask the server for a port. The server will check if the current (1st) slot is locked. If so , it will wait --- when unlocked, it will asynchronously give the lock to the thread, and increment position in the queue. The thread will then lock this resource, process the request, and unlock it. Once the thread finishes processing, it will unlock the resource. Meanwhile, if the 2nd thread comes, the server is free to assign the 2nd port, if that port is free.
Of course - there is a potential deadlock in your design here : if one thread never completes, the server will be stuck waiting for the lock to unlock before it increments to the next port.
http://www.koders.com/java/fid13E588928D0C01917AC9C30E35D802BDBA546368.aspx?s=Queue#L23