go mod init 初始化项目并按服务边界划分目录,每个服务独立 go.mod;gRPC 接口统一放 api/ 下,用 protoc 生成代码;server 必须注册 reflection 和 health;client 调用需带超时 context 和拦截器。

用 go mod init 初始化项目并规划服务目录结构
微服务不是把代码拆成多个 main.go 就完事。初始化阶段就要明确服务边界,避免后期模块耦合。先在根目录运行:
go mod init example.com/micro然后按职责划分子目录,比如:
auth-svc、order-svc、user-svc。每个子目录下各自执行 go mod init example.com/micro/auth-svc —— 不要共用一个 go.mod,否则版本冲突和依赖污染会非常难排查。
用 gRPC 定义服务接口并生成 Go 代码
服务间调用走 gRPC 是当前最稳妥的选择,比 REST 更适合内部高频通信。把 .proto 文件统一放在 api/ 目录下(如 api/user/v1/user.proto),再用 protoc 生成 Go 代码:
protoc --go_out=. --go-grpc_out=. api/user/v1/user.proto注意两个关键参数:
--go_out=. 生成普通 Go 类型,--go-grpc_out=. 生成 server/client 接口。如果漏掉后者,会发现生成的 UserClient 根本没有 CreateUser 这类方法,只有一堆空接口。
mallcloud商城基于SpringBoot2.x、SpringCloud和SpringCloudAlibaba并采用前后端分离vue的企业级微服务敏捷开发系统架构。并引入组件化的思想实现高内聚低耦合,项目代码简洁注释丰富上手容易,适合学习和企业中使用。真正实现了基于RBAC、jwt和oauth2的无状态统一权限认证的解决方案,面向互联网设计同时适合B端和C端用户,支持CI/CD多环境部署,并提
启动 gRPC Server 并注册健康检查与反射服务
刚写完 server 代码却连不上?大概率是没开反射或健康检查,导致客户端无法发现服务或调试失败。在 main.go 中注册这两项:
import (
"google.golang.org/grpc/reflection"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
)
func main() {
lis, _ := net.Listen("tcp", ":8081")
s := grpc.NewServer()
pb.RegisterUserServiceServer(s, &userService{})
// 必加:让 grpcurl、evans 等工具能自动发现服务
reflection.Register(s)
// 必加:支持 /healthz 检查,K8s readiness probe 依赖它
healthServer := health.NewServer()
grpc_health_v1.RegisterHealthServer(s, healthServer)
s.Serve(lis)
}不加 reflection,你连 grpcurl -plaintext localhost:8081 list 都会报错;不加 health,K8s 可能直接把 Pod 当成不可用给干掉。
从 client 调用其他服务时传入正确 context 和拦截器
服务间调用不是简单 new 个 client 就发请求。必须带超时、链路追踪上下文,否则一次慢请求可能拖垮整条调用链。
conn, _ := grpc.Dial("user-svc:8081",
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithUnaryInterceptor(otelgrpc.UnaryClientInterceptor()), // OpenTelemetry 链路
)
client := pb.NewUserServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
resp, err := client.GetUser(ctx, &pb.GetUserRequest{Id: "u123"})常见错误:用 context.Background() 直接发请求 → 无法被父级 cancel 中断;不设 timeout → 故障时无限等待;忘记关 conn → fd 耗尽。生产环境务必用连接池(如 grpc.WithBlock() + 连接复用),别每次调用都 Dial。
微服务环境初始化最难的不是写代码,而是让每个服务在启动那一刻就“知道自己是谁、能找谁、怎么被找到”。目录结构、proto 位置、health/reflection 注册、context 生命周期——这些地方出问题,不会报编译错误,但会让你花三天时间查“为什么调不通”。









