Skip to content

Commit d83e242

Browse files
committed
feat(examples): add gRPC interop example
Add examples/grpc-interop showing that any standard gRPC client can call a go-micro service — no go-micro SDK required on the client side. Includes: - proto/greeter.proto with generated Go, gRPC, and micro stubs - server/ using go-micro gRPC transport - client/ using stock google.golang.org/grpc (no go-micro imports) - README with Python example and explanation of how routing works Addresses the confusion from issue #2818 where users didn't know that go-micro gRPC services are callable by any gRPC client.
1 parent e4d7801 commit d83e242

10 files changed

Lines changed: 1012 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ Every service gets `Client()`, `Server()`, and `Model()` — call services, hand
275275

276276
Check out [/examples](examples/) for runnable code:
277277
- [hello-world](examples/hello-world/) - Basic RPC service
278+
- [grpc-interop](examples/grpc-interop/) - Call go-micro from any gRPC client
278279
- [web-service](examples/web-service/) - HTTP REST API
279280
- [multi-service](examples/multi-service/) - Multiple services in one binary
280281
- [mcp](examples/mcp/) - MCP integration with AI agents

examples/grpc-interop/README.md

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# gRPC Interop Example
2+
3+
This example shows that **any standard gRPC client** can call a go-micro service — no go-micro SDK required on the client side.
4+
5+
The server is a normal go-micro service using the gRPC transport. The client is a plain `google.golang.org/grpc` client with no go-micro imports.
6+
7+
## How it works
8+
9+
go-micro's gRPC server uses a [`grpc.UnknownServiceHandler`](https://pkg.go.dev/google.golang.org/grpc#UnknownServiceHandler) that catches all incoming requests and routes them by parsing the standard gRPC method path (`/package.Service/Method`). This means any language with gRPC support (Python, Java, Rust, etc.) can call go-micro services using generated protobuf stubs.
10+
11+
## Running
12+
13+
Start the server:
14+
15+
```bash
16+
cd examples/grpc-interop
17+
go run ./server/
18+
```
19+
20+
In another terminal, call it with the standard gRPC client:
21+
22+
```bash
23+
cd examples/grpc-interop
24+
go run ./client/ --name Alice
25+
# Response: Hello Alice
26+
```
27+
28+
## Calling from other languages
29+
30+
Generate stubs from `proto/greeter.proto` in your language of choice and point them at `localhost:50051`. For example, with Python:
31+
32+
```bash
33+
pip install grpcio-tools
34+
python -m grpc_tools.protoc -Iproto --python_out=. --grpc_python_out=. proto/greeter.proto
35+
```
36+
37+
```python
38+
import grpc
39+
import greeter_pb2
40+
import greeter_pb2_grpc
41+
42+
channel = grpc.insecure_channel("localhost:50051")
43+
stub = greeter_pb2_grpc.GreeterStub(channel)
44+
45+
response = stub.Hello(greeter_pb2.HelloRequest(name="Alice"))
46+
print(response.message) # Hello Alice
47+
```
48+
49+
## Key points
50+
51+
- The go-micro server registers handlers via `pb.RegisterGreeterHandler()`
52+
- The standard gRPC client uses stubs generated by `protoc-gen-go-grpc`
53+
- Both share the same `.proto` file — that's the contract
54+
- The server uses protobuf encoding on the wire, same as any gRPC service
55+
- Service discovery (mDNS, consul, etc.) is only needed for go-micro-to-go-micro calls; direct gRPC clients connect by address
56+
57+
## Regenerating proto stubs
58+
59+
```bash
60+
protoc --go_out=. --go_opt=paths=source_relative \
61+
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
62+
--micro_out=. --micro_opt=paths=source_relative \
63+
proto/greeter.proto
64+
```
65+
66+
Requires `protoc-gen-go`, `protoc-gen-go-grpc`, and `protoc-gen-micro`.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// client calls the go-micro Greeter service using a standard gRPC
2+
// client — no go-micro SDK. This proves any language with gRPC support
3+
// can call go-micro services.
4+
package main
5+
6+
import (
7+
"context"
8+
"flag"
9+
"fmt"
10+
"log"
11+
"time"
12+
13+
pb "example/proto"
14+
15+
"google.golang.org/grpc"
16+
"google.golang.org/grpc/credentials/insecure"
17+
)
18+
19+
func main() {
20+
addr := flag.String("addr", "localhost:50051", "server address")
21+
name := flag.String("name", "World", "name to greet")
22+
flag.Parse()
23+
24+
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
25+
defer cancel()
26+
27+
conn, err := grpc.NewClient(*addr,
28+
grpc.WithTransportCredentials(insecure.NewCredentials()),
29+
)
30+
if err != nil {
31+
log.Fatalf("connect: %v", err)
32+
}
33+
defer conn.Close()
34+
35+
client := pb.NewGreeterClient(conn)
36+
37+
resp, err := client.Hello(ctx, &pb.HelloRequest{Name: *name})
38+
if err != nil {
39+
log.Fatalf("Hello: %v", err)
40+
}
41+
42+
fmt.Printf("Response: %s\n", resp.Message)
43+
}

examples/grpc-interop/go.mod

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
module example
2+
3+
go 1.24
4+
5+
require (
6+
go-micro.dev/v5 v5.16.0
7+
google.golang.org/grpc v1.71.1
8+
google.golang.org/protobuf v1.36.6
9+
)
10+
11+
require (
12+
dario.cat/mergo v1.0.2 // indirect
13+
github.com/armon/go-metrics v0.4.1 // indirect
14+
github.com/bitly/go-simplejson v0.5.0 // indirect
15+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
16+
github.com/coreos/go-semver v0.3.0 // indirect
17+
github.com/coreos/go-systemd/v22 v22.3.2 // indirect
18+
github.com/cornelk/hashmap v1.0.8 // indirect
19+
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
20+
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
21+
github.com/fatih/color v1.16.0 // indirect
22+
github.com/fsnotify/fsnotify v1.6.0 // indirect
23+
github.com/go-redis/redis/v8 v8.11.5 // indirect
24+
github.com/gogo/protobuf v1.3.2 // indirect
25+
github.com/golang/protobuf v1.5.4 // indirect
26+
github.com/google/uuid v1.6.0 // indirect
27+
github.com/hashicorp/consul/api v1.32.1 // indirect
28+
github.com/hashicorp/errwrap v1.1.0 // indirect
29+
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
30+
github.com/hashicorp/go-hclog v1.5.0 // indirect
31+
github.com/hashicorp/go-immutable-radix v1.3.1 // indirect
32+
github.com/hashicorp/go-multierror v1.1.1 // indirect
33+
github.com/hashicorp/go-rootcerts v1.0.2 // indirect
34+
github.com/hashicorp/golang-lru v1.0.2 // indirect
35+
github.com/hashicorp/serf v0.10.1 // indirect
36+
github.com/klauspost/compress v1.18.0 // indirect
37+
github.com/lib/pq v1.10.9 // indirect
38+
github.com/mattn/go-colorable v0.1.13 // indirect
39+
github.com/mattn/go-isatty v0.0.20 // indirect
40+
github.com/miekg/dns v1.1.50 // indirect
41+
github.com/mitchellh/go-homedir v1.1.0 // indirect
42+
github.com/mitchellh/hashstructure v1.1.0 // indirect
43+
github.com/mitchellh/mapstructure v1.5.0 // indirect
44+
github.com/nats-io/nats.go v1.42.0 // indirect
45+
github.com/nats-io/nkeys v0.4.11 // indirect
46+
github.com/nats-io/nuid v1.0.1 // indirect
47+
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
48+
github.com/patrickmn/go-cache v2.1.0+incompatible // indirect
49+
github.com/pkg/errors v0.9.1 // indirect
50+
github.com/rabbitmq/amqp091-go v1.10.0 // indirect
51+
github.com/russross/blackfriday/v2 v2.1.0 // indirect
52+
github.com/urfave/cli/v2 v2.27.6 // indirect
53+
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
54+
go.etcd.io/bbolt v1.4.0 // indirect
55+
go.etcd.io/etcd/api/v3 v3.5.21 // indirect
56+
go.etcd.io/etcd/client/pkg/v3 v3.5.21 // indirect
57+
go.etcd.io/etcd/client/v3 v3.5.21 // indirect
58+
go.uber.org/multierr v1.10.0 // indirect
59+
go.uber.org/zap v1.27.0 // indirect
60+
golang.org/x/crypto v0.37.0 // indirect
61+
golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 // indirect
62+
golang.org/x/mod v0.24.0 // indirect
63+
golang.org/x/net v0.38.0 // indirect
64+
golang.org/x/sync v0.13.0 // indirect
65+
golang.org/x/sys v0.32.0 // indirect
66+
golang.org/x/text v0.24.0 // indirect
67+
golang.org/x/tools v0.31.0 // indirect
68+
google.golang.org/genproto/googleapis/api v0.0.0-20250324211829-b45e905df463 // indirect
69+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250324211829-b45e905df463 // indirect
70+
)
71+
72+
replace go-micro.dev/v5 => ../..

0 commit comments

Comments
 (0)