Go でシンプルなロードバランサーを構築する

WBOY
リリース: 2024-09-07 22:30:37
オリジナル
296 人が閲覧しました

ロード バランサーは、最新のソフトウェア開発において非常に重要です。リクエストがどのように複数のサーバーに分散されるのか、またはトラフィックが多いときでも特定の Web サイトがなぜ速く感じるのか疑問に思ったことがある場合、その答えは効率的な負荷分散にあることがよくあります。

Building a simple load balancer in Go

この投稿では、Go の ラウンド ロビン アルゴリズム を使用して、シンプルなアプリケーション ロード バランサーを構築します。この投稿の目的は、ロード バランサーが内部でどのように機能するかを段階的に理解することです。

ロードバランサーとは何ですか?

ロード バランサーは、受信ネットワーク トラフィックを複数のサーバーに分散するシステムです。これにより、単一のサーバーに過大な負荷がかかることがなくなり、ボトルネックが防止され、全体的なユーザー エクスペリエンスが向上します。また、負荷分散アプローチにより、1 つのサーバーに障害が発生した場合、トラフィックが別の利用可能なサーバーに自動的に再ルーティングされるため、障害の影響が軽減され、可用性が向上します。

ロードバランサーを使用する理由は何ですか?

  • 高可用性: ロード バランサーはトラフィックを分散することで、1 台のサーバーに障害が発生した場合でもトラフィックを他の正常なサーバーにルーティングできるようにし、アプリケーションの回復力を高めます。
  • スケーラビリティ: ロード バランサーを使用すると、トラフィックの増加に応じてサーバーを追加し、システムを水平方向に拡張できます。
  • 効率: すべてのサーバーがワークロードを均等に共有することにより、リソースの使用率を最大化します。

負荷分散アルゴリズム

トラフィックを分散するには、さまざまなアルゴリズムと戦略があります:

  • ラウンドロビン: 利用可能な最も単純な方法の 1 つ。リクエストは利用可能なサーバー間で順番に分散されます。最後のサーバーに到達すると、最初から再度開始されます。
  • 加重ラウンドロビン: 各サーバーに固定の数値重みが割り当てられる点を除き、ラウンド ロビン アルゴリズムに似ています。この指定された重みは、トラフィックをルーティングするサーバーを決定するために使用されます。
  • 最小接続: アクティブな接続が最も少ないサーバーにトラフィックをルーティングします。
  • IP ハッシュ: クライアントの IP アドレスに基づいてサーバーを選択します。

この投稿では、ラウンド ロビン ロード バランサの実装に焦点を当てます。

ラウンドロビンアルゴリズムとは何ですか?

ラウンド ロビン アルゴリズムは、受信した各リクエストを次に利用可能なサーバーに循環的に送信します。サーバー A が最初のリクエストを処理する場合、サーバー B が 2 番目のリクエストを処理し、サーバー C が 3 番目のリクエストを処理します。すべてのサーバーがリクエストを受信すると、サーバー A から再度リクエストが開始されます。

それでは、コードに進んでロード バランサーを構築しましょう!

ステップ 1: ロードバランサーとサーバーを定義する

type LoadBalancer struct {
    Current int
    Mutex   sync.Mutex
}
ログイン後にコピー

最初に、どのサーバーが次のリクエストを処理する必要があるかを追跡するために、Current フィールドを持つ単純な LoadBalancer 構造体を定義します。 Mutex により、コードを同時に安全に使用できることが保証されます。

負荷分散する各サーバーは、サーバー構造体によって定義されます。

type Server struct {
    URL       *url.URL
    IsHealthy bool
    Mutex     sync.Mutex
}
ログイン後にコピー

ここで、各サーバーには URL と、サーバーがリクエストを処理できるかどうかを示す IsHealthy フラグがあります。

ステップ 2: ラウンドロビンアルゴリズム

ロード バランサの中心となるのはラウンド ロビン アルゴリズムです。仕組みは次のとおりです:

func (lb *LoadBalancer) getNextServer(servers []*Server) *Server {
    lb.Mutex.Lock()
    defer lb.Mutex.Unlock()

    for i := 0; i < len(servers); i++ {
        idx := lb.Current % len(servers)
        nextServer := servers[idx]
        lb.Current++

        nextServer.Mutex.Lock()
        isHealthy := nextServer.IsHealthy
        nextServer.Mutex.Unlock()

        if isHealthy {
            return nextServer
        }
    }

    return nil
}
ログイン後にコピー
  • このメソッドは、サーバーのリストをラウンドロビン方式でループします。選択したサーバーが正常な場合は、そのサーバーを返して受信リクエストを処理します。
  • Mutex を使用して、一度に 1 つの goroutine だけがロード バランサーの Current フィールドにアクセスして変更できるようにしています。これにより、複数のリクエストが同時に処理されるときに、ラウンド ロビン アルゴリズムが正しく動作することが保証されます。
  • 各サーバーには独自のミューテックスもあります。 IsHealthy フィールドをチェックすると、サーバーの Mutex がロックされ、複数の goroutine からの同時アクセスが防止されます。
  • Mutex をロックしないと、別の goroutine が値を変更する可能性があり、その結果、不正なデータや一貫性のないデータが読み取られる可能性があります。
  • クリティカル セクションを可能な限り小さく保つために、Current フィールドを更新するか、IsHealthy フィールド値を読み取るとすぐに Mutex のロックを解除します。このようにして、競合状態を回避するために Mutex を使用しています。

ステップ 3: ロードバランサーの構成

設定は config.json ファイルに保存されており、このファイルにはサーバー URL とヘルスチェック間隔が含まれています (詳細については、以下のセクションで説明します)。

type Config struct {
    Port                string   `json:"port"`
    HealthCheckInterval string   `json:"healthCheckInterval"`
    Servers             []string `json:"servers"`
}
ログイン後にコピー

設定ファイルは次のようになります:

{
  "port": ":8080",
  "healthCheckInterval": "2s",
  "servers": [
    "http://localhost:5001",
    "http://localhost:5002",
    "http://localhost:5003",
    "http://localhost:5004",
    "http://localhost:5005"
  ]
}
ログイン後にコピー

Step 4: Health Checks

We want to make sure that the servers are healthy before routing any incoming traffic to them. This is done by sending periodic health checks to each server:

func healthCheck(s *Server, healthCheckInterval time.Duration) {
    for range time.Tick(healthCheckInterval) {
        res, err := http.Head(s.URL.String())
        s.Mutex.Lock()
        if err != nil || res.StatusCode != http.StatusOK {
            fmt.Printf("%s is down\n", s.URL)
            s.IsHealthy = false
        } else {
            s.IsHealthy = true
        }
        s.Mutex.Unlock()
    }
}
ログイン後にコピー

Every few seconds (as specified in the config), the load balancer sends a HEAD request to each server to check if it is healthy. If a server is down, the IsHealthy flag is set to false, preventing future traffic from being routed to it.

Step 5: Reverse Proxy

When the load balancer receives a request, it forwards the request to the next available server using a reverse proxy. In Golang, the httputil package provides a built-in way to handle reverse proxying, and we will use it in our code through the ReverseProxy function:

func (s *Server) ReverseProxy() *httputil.ReverseProxy {
    return httputil.NewSingleHostReverseProxy(s.URL)
}
ログイン後にコピー
What is a Reverse Proxy?

A reverse proxy is a server that sits between a client and one or more backend severs. It receives the client's request, forwards it to one of the backend servers, and then returns the server's response to the client. The client interacts with the proxy, unaware of which specific backend server is handling the request.

In our case, the load balancer acts as a reverse proxy, sitting in front of multiple servers and distributing incoming HTTP requests across them.

Step 6: Handling Requests

When a client makes a request to the load balancer, it selects the next available healthy server using the round robin algorithm implementation in getNextServer function and proxies the client request to that server. If no healthy server is available then we send service unavailable error to the client.

http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
        server := lb.getNextServer(servers)
        if server == nil {
            http.Error(w, "No healthy server available", http.StatusServiceUnavailable)
            return
        }
        w.Header().Add("X-Forwarded-Server", server.URL.String())
        server.ReverseProxy().ServeHTTP(w, r)
    })
ログイン後にコピー

The ReverseProxy method proxies the request to the actual server, and we also add a custom header X-Forwarded-Server for debugging purposes (though in production, we should avoid exposing internal server details like this).

Step 7: Starting the Load Balancer

Finally, we start the load balancer on the specified port:

log.Println("Starting load balancer on port", config.Port)
err = http.ListenAndServe(config.Port, nil)
if err != nil {
        log.Fatalf("Error starting load balancer: %s\n", err.Error())
}
ログイン後にコピー

Working Demo

TL;DR

In this post, we built a basic load balancer from scratch in Golang using a round robin algorithm. This is a simple yet effective way to distribute traffic across multiple servers and ensure that your system can handle higher loads efficiently.

There's a lot more to explore, such as adding sophisticated health checks, implementing different load balancing algorithms, or improving fault tolerance. But this basic example can be a solid foundation to build upon.

You can find the source code in this GitHub repo.

以上がGo でシンプルなロードバランサーを構築するの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ソース:dev.to
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!