Goravel 让微服务间通讯如此简单,省出来的时间想干点啥干点啥吧

2022-11-13 11:48:19 +08:00
 Bowens

Goravel 升级 v1.4 ,使微服务间请求更加简单一体化。No bb, show the code.

Goravel 是一个功能完备、具有良好扩展能力的 Golang Web 应用程序框架。风格与 Laravel 保持一致,让 PHPer 不用学习新的框架,也可以愉快的玩转 Golang !

服务端

  1. 定义接口
func (r *UserController) GetUser(ctx context.Context, req *grpcuser.UserRequest) (*grpcuser.UserResponse, error) {
  return &grpcuser.UserResponse{Name: "Goravel"}, nil
}
  1. 运行服务
facades.Grpc.Run(facades.Config.GetString("grpc.host"))

客户端

  1. 配置服务
facades.Config.Add("grpc", map[string]interface{}{
  "clients": map[string]any{
    "user": map[string]any{
      "host":         config.Env("GRPC_USER_HOST", ""),
      "interceptors": []string{"trace"},
    },
  },
})
  1. 发起请求
func (r *ServiceImpl) GetUser(ctx context.Context, userID uint64) (*grpcuser.User, error) {
  // 创建客户端实例
  client, _ := facades.Grpc.Client(ctx, "user")
  // 注册服务
  userServiceClient := grpcuser.NewUserServiceClient(client)
  // 调用接口
  resp, _ := userServiceClient.GetUser(ctx, &grpcuser.UserRequest{
    UserID: userID,
  })
  return resp.Data, nil
}

Over, 欢迎 Star 、PR 、Issues!

1573 次点击
所在节点    Go 编程语言
2 条回复
totoro52
2022-11-13 11:53:22 +08:00
真牛逼,给 star 了
tikazyq
2022-11-14 09:47:47 +08:00
好像不错哦,先 star 再看

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/894838

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX