Goravel 升级 v1.4,使微服务间请求更加简单一体化。No bb, show the code.
Goravel 是一个功能完备、具有良好扩展能力的 Golang Web 应用程序框架。风格与 Laravel 保持一致,让 PHPer 不用学习新的框架,也可以愉快的玩转 Golang!
服务端
- 定义接口
func (r *UserController) GetUser(ctx context.Context, req *grpcuser.UserRequest) (*grpcuser.UserResponse, error) {
return &grpcuser.UserResponse{Name: "Goravel"}, nil
}
- 运行服务
facades.Grpc.Run(facades.Config.GetString("grpc.host"))
客户端
- 配置服务
facades.Config.Add("grpc", map[string]interface{}{
"clients": map[string]any{
"user": map[string]any{
"host": config.Env("GRPC_USER_HOST", ""),
"interceptors": []string{"trace"},
},
},
})
- 发起请求
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!
點(diǎn)擊查看更多內(nèi)容
為 TA 點(diǎn)贊
評(píng)論
評(píng)論
共同學(xué)習(xí),寫下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦