V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
Bowens
V2EX  ›  Go 编程语言

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

  •  
  •   Bowens · 2022-11-13 11:48:19 +08:00 · 1539 次点击
    这是一个创建于 502 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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!

    2 条回复    2022-11-14 09:47:47 +08:00
    totoro52
        1
    totoro52  
       2022-11-13 11:53:22 +08:00
    真牛逼,给 star 了
    tikazyq
        2
    tikazyq  
       2022-11-14 09:47:47 +08:00
    好像不错哦,先 star 再看
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5459 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 07:42 · PVG 15:42 · LAX 00:42 · JFK 03:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.