Skip to content

Commit 80345fe

Browse files
Copilotasim
andauthored
docs: clarify gRPC server option ordering and service name usage (#2820)
* Initial plan * docs: clarify gRPC server option ordering and service name usage - Fix all examples to show Server option before Name option - Add note explaining why option ordering matters - Add new section on "Option Ordering Issue" in Common Errors - Add new section on "Service Name vs Package Name" in Common Errors - Update transport.md to show proper ordering with comment Co-authored-by: asim <[email protected]> * docs: refine comments based on code review feedback - Clarify that Server ordering before Name is mandatory - Remove confusing comment about Client ordering being for consistency Co-authored-by: asim <[email protected]> --------- Co-authored-by: copilot-swe-agent[bot] <[email protected]> Co-authored-by: asim <[email protected]>
1 parent 00a1194 commit 80345fe

File tree

2 files changed

+52
-8
lines changed

2 files changed

+52
-8
lines changed

internal/website/docs/guides/grpc-compatibility.md

Lines changed: 50 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,14 @@ import (
4343
)
4444

4545
service := micro.NewService(
46-
micro.Name("helloworld"),
47-
micro.Server(grpcServer.NewServer()),
46+
micro.Server(grpcServer.NewServer()), // Server must come before Name
4847
micro.Client(grpcClient.NewClient()),
48+
micro.Name("helloworld"),
4949
)
5050
```
5151

52+
> **Important**: The `micro.Server()` option must be specified **before** `micro.Name()`. This is because `micro.Name()` sets the name on the current server, and if `micro.Server()` comes after, it replaces the server with a new one that has no name set.
53+
5254
## When to Use Which
5355

5456
| Use Case | Solution |
@@ -118,10 +120,11 @@ func (s *Say) Hello(ctx context.Context, req *pb.Request, rsp *pb.Response) erro
118120

119121
func main() {
120122
// Create service with gRPC server for native gRPC compatibility
123+
// Note: Server must be set before Name to ensure the name is applied to the gRPC server
121124
service := micro.NewService(
125+
micro.Server(grpcServer.NewServer()),
122126
micro.Name("helloworld"),
123127
micro.Address(":8080"),
124-
micro.Server(grpcServer.NewServer()),
125128
)
126129

127130
service.Init()
@@ -154,12 +157,13 @@ import (
154157
func main() {
155158
// Create service with gRPC client
156159
service := micro.NewService(
157-
micro.Name("helloworld.client"),
158160
micro.Client(grpcClient.NewClient()),
161+
micro.Name("helloworld.client"),
159162
)
160163
service.Init()
161164

162-
// Create client
165+
// Create client - use the service name "helloworld" (not the proto package name)
166+
// Go Micro uses this name for registry lookup, which may differ from the package name
163167
sayService := pb.NewSayService("helloworld", service.Client())
164168

165169
// Call service
@@ -202,10 +206,10 @@ import (
202206

203207
func main() {
204208
service := micro.NewService(
205-
micro.Name("helloworld"),
206-
micro.Address(":8080"),
207-
micro.Server(grpcServer.NewServer()),
209+
micro.Server(grpcServer.NewServer()), // Server first
208210
micro.Client(grpcClient.NewClient()),
211+
micro.Name("helloworld"), // Name after Server
212+
micro.Address(":8080"),
209213
)
210214

211215
service.Init()
@@ -264,6 +268,44 @@ import "go-micro.dev/v5/server/grpc"
264268
import "go-micro.dev/v5/client/grpc"
265269
```
266270

271+
### Option Ordering Issue
272+
273+
If the gRPC server is working but your service has no name or is not being found in the registry:
274+
275+
**Cause**: The `micro.Server()` option is specified **after** `micro.Name()`.
276+
277+
When options are processed, `micro.Name()` sets the name on the current server. If `micro.Server()` comes later, it replaces the server with a new one that doesn't have the name set.
278+
279+
**Solution**: Always specify `micro.Server()` **before** `micro.Name()`:
280+
281+
```go
282+
// Wrong - server replaces the one with the name set
283+
service := micro.NewService(
284+
micro.Name("helloworld"), // Sets name on default server
285+
micro.Server(grpcServer.NewServer()), // Replaces server, name is lost!
286+
)
287+
288+
// Correct - name is set on the gRPC server
289+
service := micro.NewService(
290+
micro.Server(grpcServer.NewServer()), // Set server first
291+
micro.Name("helloworld"), // Name is now applied to gRPC server
292+
)
293+
```
294+
295+
### Service Name vs Package Name
296+
297+
When creating a client to call another service, use the **service name** (set via `micro.Name()`), not the proto package name:
298+
299+
```go
300+
// If the server was started with micro.Name("helloworld")
301+
sayService := pb.NewSayService("helloworld", service.Client()) // Use service name
302+
303+
// NOT the package name from the proto file
304+
// sayService := pb.NewSayService("helloworld.Say", service.Client()) // Wrong!
305+
```
306+
307+
Go Micro uses the service name for registry lookup, which may differ from the proto package name.
308+
267309
## Environment Variable Configuration
268310

269311
You can also configure the server and client via environment variables:

internal/website/docs/transport.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ import (
2929
grpcClient "go-micro.dev/v5/client/grpc"
3030
)
3131

32+
// Important: Server must be specified before Name
3233
service := micro.NewService(
3334
micro.Server(grpcServer.NewServer()),
3435
micro.Client(grpcClient.NewClient()),
36+
micro.Name("myservice"),
3537
)
3638
```
3739

0 commit comments

Comments
 (0)