Skip to main content
Version: 2.8.x(Latest)

Load Balancer - HTTP Service Example

Code Source: https://github.com/gogf/examples/tree/main/balancer/http

Description

This example demonstrates how to implement HTTP service load balancing with GoFrame. It shows:

  • Service registration using etcd
  • Client-side load balancing
  • Round-robin load balancing strategy
  • HTTP service communication

Requirements

Structure

.
├── client/ # HTTP client implementation with load balancing
│ └── client.go # Client code with round-robin balancer
├── server/ # HTTP server implementation
│ └── server.go # Server code with service registration
├── go.mod # Go module file
└── go.sum # Go module checksums

Prerequisites

  1. Running etcd server:
    # Using docker
    docker run -d --name etcd-server \
    --publish 2379:2379 \
    --publish 2380:2380 \
    --env ALLOW_NONE_AUTHENTICATION=yes \
    bitnami/etcd:latest

Usage

  1. Start multiple server instances (use random different ports):

    # Terminal 1
    cd server
    go run server.go

    # Terminal 2
    cd server
    go run server.go

    # Terminal 3
    cd server
    go run server.go
  2. Run the client to test load balancing:

    cd client
    go run client.go

Implementation Details

  1. Server Implementation (server/server.go):

    • HTTP server setup using GoFrame
    • Service registration with etcd
    • Simple HTTP endpoint that returns "Hello world"
    • Automatic service discovery registration
  2. Client Implementation (client/client.go):

    • Service discovery using etcd
    • Round-robin load balancing strategy
    • Multiple request demonstration
    • Automatic service discovery and load balancing

Notes

  • The example uses etcd for service registration and discovery
  • Round-robin load balancing is implemented for demonstration
  • The client automatically handles service discovery and load balancing
  • Multiple server instances can be started to demonstrate load distribution