The Go Programming Language
http://golang.org/
Go Playground
Go Projects
Revel Web Framework
Bowens

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

  •  
  •   Bowens · Nov 13, 2022 · 2354 views
    This topic created in 1279 days ago, the information mentioned may be changed or developed.

    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 replies    2022-11-14 09:47:47 +08:00
    MIUIOS
        1
    MIUIOS  
       Nov 13, 2022
    真牛逼,给 star 了
    tikazyq
        2
    tikazyq  
       Nov 14, 2022
    好像不错哦,先 star 再看
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5590 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 08:43 · PVG 16:43 · LAX 01:43 · JFK 04:43
    ♥ Do have faith in what you're doing.