3

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

 1 year ago
source link: https://studygolang.com/articles/35957
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

hwbrzzl · 1天之前 · 366 次点击 · 预计阅读时间 1 分钟 · 大约8小时之前 开始浏览    

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
    }
    
  2. 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"},
             },
         },
    })
    
  2. 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!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK