当前位置: 面试刷题>> 负载均衡器 (经典算法题500道)


### 题目描述补充 **题目:负载均衡器设计与实现** 设计一个基本的负载均衡器,该负载均衡器能够将进入的请求按照一定的策略分配到多个服务器上。在本题中,我们需要实现一个轮询(Round Robin)策略的负载均衡器。轮询策略意味着请求将按顺序逐一分配到各个服务器上,直到最后一个服务器,然后重新开始循环。 **功能要求**: 1. 支持动态添加和删除服务器。 2. 能够处理请求,并按照轮询策略分配到各个服务器。 3. 返回处理请求的服务器信息。 ### PHP 示例代码 ```php class LoadBalancer { private $servers = []; private $currentIndex = 0; public function addServer($server) { $this->servers[] = $server; } public function removeServer($server) { $key = array_search($server, $this->servers); if ($key !== false) { unset($this->servers[$key]); // 重新索引数组 $this->servers = array_values($this->servers); // 如果当前索引指向被删除的元素,则调整索引 if ($this->currentIndex >= count($this->servers)) { $this->currentIndex = 0; } } } public function dispatchRequest() { if (empty($this->servers)) { return 'No servers available'; } $server = $this->servers[$this->currentIndex]; $this->currentIndex = ($this->currentIndex + 1) % count($this->servers); return "Request dispatched to: " . $server; } } // 使用示例 $loadBalancer = new LoadBalancer(); $loadBalancer->addServer('Server1'); $loadBalancer->addServer('Server2'); $loadBalancer->addServer('Server3'); echo $loadBalancer->dispatchRequest(); // Request dispatched to: Server1 echo "\n"; echo $loadBalancer->dispatchRequest(); // Request dispatched to: Server2 echo "\n"; echo $loadBalancer->dispatchRequest(); // Request dispatched to: Server3 echo "\n"; $loadBalancer->removeServer('Server2'); echo $loadBalancer->dispatchRequest(); // Request dispatched to: Server1 ``` ### Python 示例代码 ```python class LoadBalancer: def __init__(self): self.servers = [] self.current_index = 0 def add_server(self, server): self.servers.append(server) def remove_server(self, server): if server in self.servers: self.servers.remove(server) # 如果当前索引指向被删除的元素,则调整索引 if self.current_index >= len(self.servers): self.current_index = 0 def dispatch_request(self): if not self.servers: return 'No servers available' server = self.servers[self.current_index] self.current_index = (self.current_index + 1) % len(self.servers) return f"Request dispatched to: {server}" # 使用示例 load_balancer = LoadBalancer() load_balancer.add_server('Server1') load_balancer.add_server('Server2') load_balancer.add_server('Server3') print(load_balancer.dispatch_request()) # Request dispatched to: Server1 print(load_balancer.dispatch_request()) # Request dispatched to: Server2 print(load_balancer.dispatch_request()) # Request dispatched to: Server3 load_balancer.remove_server('Server2') print(load_balancer.dispatch_request()) # Request dispatched to: Server1 ``` ### JavaScript 示例代码 ```javascript class LoadBalancer { constructor() { this.servers = []; this.currentIndex = 0; } addServer(server) { this.servers.push(server); } removeServer(server) { const index = this.servers.indexOf(server); if (index !== -1) { this.servers.splice(index, 1); // 如果当前索引指向被删除的元素,则调整索引 if (this.currentIndex >= this.servers.length) { this.currentIndex = 0; } } } dispatchRequest() { if (this.servers.length === 0) { return 'No servers available'; } const server = this.servers[this.currentIndex]; this.currentIndex = (this.currentIndex + 1) % this.servers.length; return `Request dispatched to: ${server}`; } } // 使用示例 const loadBalancer = new LoadBalancer(); loadBalancer.addServer('Server1'); loadBalancer.addServer('Server2'); loadBalancer.addServer('Server3'); console.log(loadBalancer.dispatchRequest()); // Request dispatched to: Server1 console.log(loadBalancer.dispatchRequest()); // Request dispatched to: Server2 console.log(loadBalancer.dispatchRequest()); // Request dispatched to: Server3 loadBalancer.removeServer('Server2'); console.log(loadBalancer.dispatchRequest()); // Request dispatched to: Server1 ``` **码小课网站中有更多相关内容分享给大家学习**,包括但不限于分布式系统、负载均衡算法、服务器集群管理等进阶话题。
推荐面试题