java 负载均衡服务器,我该如何实现?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31684156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Load balancing server, how can I implement it?
提问by PedroD
I googled for load balancing but the only thing I can find is the working theory, which at the moment, is the "easy" part for me. But zero examples of how to implement one.
我用谷歌搜索负载平衡,但我唯一能找到的是工作理论,目前,对我来说是“简单”的部分。但是如何实现一个的零示例。
I have several questions pertaining load balancing:
我有几个关于负载平衡的问题:
I have a domain (example.com) and I behind it I have a load balancing server (lets call it A) which, according to the theory, will ask the client to close the connection with A, and connect to B, a sub-server and carry on the request with B. Will the client, in a web browser stop seeing "example.com/page.html" in the address bar and start seeing "B_ip_address/page.html" instead?
How can I implement a simple load balancer from scratch? My doubt targets the HTTP part. Is there some specific message or set of messages I need to send the client which will make him disconnect from me and connect to a sub-server?
What about lower level protocols than HTTP, such as TCP/IP, are there any standard packets to tell the client he just connected to a load balancer server and now he needs to connect to xxx.xxx.xxx.xxx to carry on the request?
What method is the most used? (1) client connects to load balancing server, and it asks the client to directly connect to one of the sub-servers, or (2) the load balancing server starts bridging all traffic from the client to the sub-server and vice-versa in a transparent fashion?
我有一个域(example.com),在它后面我有一个负载平衡服务器(我们称之为A),根据理论,它会要求客户端关闭与 A 的连接,并连接到 B,一个子-server 并与 B 进行请求。客户端是否会在 Web 浏览器中停止在地址栏中看到“example.com/page.html”并开始看到“B_ip_address/page.html”?
如何从头开始实现一个简单的负载均衡器?我的怀疑针对 HTTP 部分。是否有一些特定的消息或一组消息我需要发送给客户端,这将使他与我断开连接并连接到子服务器?
比HTTP低的协议,比如TCP/IP,有没有什么标准的包告诉客户端他刚刚连接到负载均衡服务器,现在他需要连接到xxx.xxx.xxx.xxx来进行请求?
什么方法用得最多?(1) 客户端连接到负载均衡服务器,它要求客户端直接连接到其中一个子服务器,或者 (2) 负载均衡服务器开始桥接从客户端到子服务器的所有流量,反之亦然以透明的方式?
So question 2, 3 and 4 regard the load balancing protocol, and the 1st one the way a domain name can be connected to a load balancer and what are the underlying consequences.
所以问题 2、3 和 4 是关于负载平衡协议的,第一个问题是域名可以连接到负载平衡器的方式以及潜在的后果是什么。
采纳答案by Konrad
Your approach is a kind of static load balancing by redirect the calls to another server. All following calls may use this other server or are send to the load balancer again for redirect.
您的方法是通过将调用重定向到另一台服务器来实现静态负载平衡。随后的所有调用都可能使用此其他服务器或再次发送到负载平衡器以进行重定向。
An implementation depends on the implementation of your system. A load balancer works best for independent requests with no session state. You need to sync the session state otherwise between the "end" servers. Or use a shared session store to provide the session state to all servers.
实现取决于您的系统的实现。负载均衡器最适合没有会话状态的独立请求。您需要在“结束”服务器之间同步会话状态。或者使用共享会话存储向所有服务器提供会话状态。
There exists a simple and transparent solution for HTTP server load balancing. You can use the load balancing module of an nginx server (http://nginx.org/en/docs/http/load_balancing.html). This can be used for HTTP and HTTPS requests. And it may be extended with extra servers dynamically if the load increases. You need to edit the nginx configuration and restart the server. This can be transparent to existing connections. And nginx does not cause problems with changing domain or host names.
有一个简单透明的 HTTP 服务器负载平衡解决方案。您可以使用 nginx 服务器的负载平衡模块 ( http://nginx.org/en/docs/http/load_balancing.html)。这可用于 HTTP 和 HTTPS 请求。如果负载增加,它可能会动态扩展额外的服务器。您需要编辑 nginx 配置并重新启动服务器。这对现有连接来说是透明的。并且 nginx 不会导致更改域或主机名的问题。
Other protocols need some support by the client and the server. Load balancing may be transparent if a specialized device is between the client and server. Or the communication protocol needs to support connection redirects.
其他协议需要客户端和服务器的一些支持。如果专用设备位于客户端和服务器之间,则负载平衡可能是透明的。或者通信协议需要支持连接重定向。
Edit: Load balancing can be implemented by DNS round robin too. Each DNS lookup call returns another IP address for the same domain name. The client choose an IP and connects to this server. Another client can use the next IP. The address bar name is the same all the time.
编辑:负载平衡也可以通过 DNS 循环实现。每个 DNS 查找调用都会返回同一个域名的另一个 IP 地址。客户端选择一个 IP 并连接到该服务器。另一个客户端可以使用下一个 IP。地址栏名称始终相同。
Example:
例子:
Non-authoritative answer:
Name: www.google.com
Addresses: 2a00:1450:4001:80f::1010
173.194.116.209
173.194.116.210
173.194.116.212
173.194.116.211
173.194.116.208
Non-authoritative answer:
Name: www.google.com
Addresses: 2a00:1450:4001:80f::1010
173.194.116.210
173.194.116.212
173.194.116.211
173.194.116.208
173.194.116.209
Non-authoritative answer:
Name: www.google.com
Addresses: 2a00:1450:4001:80f::1010
173.194.116.212
173.194.116.211
173.194.116.208
173.194.116.209
173.194.116.210
The IP address range rotates. Most HTTP load balancers work as transparent load balancer like nginx or other reverse proxy implementations. A redirecting load balancer is more a low tech implementation I think.
IP 地址范围轮换。大多数 HTTP 负载均衡器作为透明负载均衡器工作,如 nginx 或其他反向代理实现。我认为重定向负载平衡器更像是一种低技术实现。
TCP/IP is not a protocol. It's the transport layer used to transfer data implementing a specific communication protocol. While TCP/IP itself is a protocol for the network components. But not the applications. You may check https://en.wikipedia.org/wiki/OSI_model.
TCP/IP 不是协议。它是用于传输实现特定通信协议的数据的传输层。而 TCP/IP 本身是网络组件的协议。但不是应用程序。您可以查看https://en.wikipedia.org/wiki/OSI_model。