gRPC
- Service Resolver
Code Source: https://github.com/gogf/examples/tree/main/grpc/resolver
Description
This example demonstrates how to use service resolver with etcd
in gRPC
services using GoFrame
. It shows how to:
- Configure service resolver with
etcd
- Register services to
etcd
- Discover services using resolver
- Handle service updates
Structure
.
├── client/ # Client example
│ └── client.go # Client with service resolver
├── controller/ # Service controllers
│ └── helloworld.go # Hello service implementation
├── protobuf/ # Protocol buffer definitions
│ └── helloworld.proto # Service and message definitions
├── server/ # Server example
│ └── server.go # Server with service registration
├── go.mod # Go module file
└── go.sum # Go module checksums
Features
The example showcases the following features:
-
Service Resolution
etcd
integration- Service registration
- Service discovery
- Update handling
-
Client Usage
- Resolver configuration
- Service discovery
- Connection management
- Error handling
-
Server Features
- Service registration
etcd
integration- Health checking
- Metadata management
Requirements
Prerequisites
-
Running
etcd
instance:# Using Docker
docker run -d --name etcd -p 2379:2379 -e ALLOW_NONE_AUTHENTICATION=yes bitnami/etcd:3.4.24 -
Protocol buffer compiler:
# For macOS
brew install protobuf
# Install protoc-gen-go and protoc-gen-go-grpc
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
Usage
-
Generate protocol buffer code:
cd protobuf
protoc --go_out=paths=source_relative:. --go-grpc_out=paths=source_relative:. *.proto -
Start the server:
cd server
go run server.go -
Run the client:
cd client
go run client.go
Implementation Details
The example demonstrates:
- Service registration with
etcd
- Service discovery using resolver