Load Balancer - Polaris
Integration Example
Code Source: https://github.com/gogf/examples/tree/main/balancer/polaris
Description
This example demonstrates how to implement HTTP service load balancing with GoFrame
using Polaris
. It shows:
- Service registration using
Polaris
- Client-side load balancing
- Round-robin load balancing strategy
- HTTP service communication
- Local cache and logging configuration
Requirements
- Go 1.22 or higher
- Git
GoFrame
GoFrame Polaris Registry
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
- Running
Polaris
server:# Using docker
docker run -d --name polaris \
-p 8090:8090 -p 8091:8091 -p 8093:8093 -p 9090:9090 -p 9091:9091 \
polarismesh/polaris-standalone:v1.17.2
Configuration
The example uses the following Polaris
configurations:
- Server address:
127.0.0.1:8091
- Local cache directory:
<TempDir>/polaris/backup
- Log directory:
<TempDir>/polaris/log
- Service TTL: 10 seconds
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.go -
Run 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
Polaris
- Simple HTTP endpoint that returns "Hello world"
- Automatic service discovery registration
- Configurable TTL for service registration
- HTTP server setup using
-
Client Implementation (
client/client.go
):- Service discovery using
Polaris
- Round-robin load balancing strategy
- Multiple request demonstration with timing information
- Automatic service discovery and load balancing
- Local cache and logging configuration
- Service discovery using
Notes
- The example uses
Polaris
for service registration and discovery - Round-robin load balancing is implemented for demonstration
- The client automatically handles service discovery and load balancing
- Local cache is used to improve performance
- Logging is configured for better debugging