Load Balancer - HTTP Service Example
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 checksumsPrerequisites
- Running
etcdserver:# Using docker docker run -d --name etcd-server \ --publish 2379:2379 \ --publish 2380:2380 \ --env ALLOW_NONE_AUTHENTICATION=yes \ bitnami/etcd:latest
Usage
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.goRun the client to test load balancing:
cd client go run client.go
Implementation Details
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
- HTTP server setup using
Client Implementation (
client/client.go):- Service discovery using
etcd - Round-robin load balancing strategy
- Multiple request demonstration
- Automatic service discovery and load balancing
- Service discovery using
Notes
- The example uses
etcdfor 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