Registry - Etcd
Integration
Code Source: https://github.com/gogf/examples/tree/main/registry/etcd
Description
This example demonstrates how to integrate Etcd
service registry with GoFrame
applications. It shows how to:
- Register services with
Etcd
- Discover services using
Etcd
- Implement service health checks
- Build distributed systems
Structure
.
├── grpc/ # gRPC service examples
│ ├── client/ # gRPC client implementation
│ ├── controller/ # gRPC service controllers
│ ├── protobuf/ # Protocol buffer definitions
│ └── server/ # gRPC server implementation
│ ├── main.go # Server startup code
│ └── config.yaml # Server configuration
├── http/ # HTTP service examples
│ ├── client/ # HTTP client implementation
│ └── server/ # HTTP server implementation
├── go.mod # Go module file
└── go.sum # Go module checksums
Features
The example showcases the following features:
-
Service Registration
- Automatic service registration
- TTL-based health check
- Metadata management
- Lease management
-
Service Discovery
- Dynamic service discovery
- Load balancing
- Failover support
- Watch mechanism
-
Protocol Support
HTTP
servicesgRPC
services- Custom protocols
Requirements
- Go 1.22 or higher
- Git
- GoFrame
- GoFrame Etcd Registry
- Etcd
Prerequisites
- Running
etcd
server:# Using docker
docker run -d --name etcd -p 2379:2379 -e ALLOW_NONE_AUTHENTICATION=yes bitnami/etcd:3.4.24
Usage
HTTP
Example
-
Start the
HTTP
server:cd http/server
go run server.go -
Run the
HTTP
client:cd http/client
go run client.go
gRPC
Example
-
Start the
gRPC
server:cd grpc/server
go run server.go -
Run the
gRPC
client:cd grpc/client
go run client.go
Implementation Details
The example demonstrates:
- etcd client configuration
- Service registration process
- Service discovery mechanism
- Health check implementation
- Load balancing strategies
- Watch mechanism usage
- Lease management
Notes
- etcd server must be running
- Default etcd address: 127.0.0.1:2379
- Services are registered automatically
- TTL-based health checks
- Automatic lease renewal
- Load balancing is handled automatically
- Consider security in production
- Handle failover scenarios
- Monitor service health
- Implement proper error handling
- Watch for service changes
- Manage leases properly