淺談Go-zero構(gòu)建RPC與API服務(wù)全流程
更新時(shí)間:2025年09月15日 09:30:39 作者:數(shù)擎AI
Go Zero 是一個(gè)流行的框架,它提供了構(gòu)建微服務(wù)的基礎(chǔ)設(shè)施,包括 RPC 服務(wù)和 RESTful API,本文主要介紹了Go-zero構(gòu)建RPC與API服務(wù)全流程,感興趣的可以了解一下
rpc服務(wù)與api服務(wù)的創(chuàng)建
1. rpc的protobuf文件創(chuàng)建
syntax = "proto3";
package user;
option go_package = "./user";
message UserReq {
string id = 1;
}
message UserResp {
string id = 1;
string name = 2;
string phone = 3;
}
service User{
rpc GetUser(UserReq) returns(UserResp);
}
2. rpc服務(wù)的創(chuàng)建
goctl rpc protoc user.proto --go_out=. --go-grpc_out=. --zrpc_out=. #通過(guò)proto生成服務(wù)
3. api的protobuf文件創(chuàng)建
api服務(wù)的創(chuàng)建
goctl api new api # 生成 goctl api go -api user.api -dir . style=gozero
api 服務(wù)調(diào)用rpc服務(wù)
1. api服務(wù)etc的添加
Name: User
Host: 0.0.0.0
Port: 8888
# userRpc服務(wù)調(diào)用
UserRpc:
Etcd:
Hosts:
- 127.0.0.1:2379
Key: user.rpc2. api服務(wù)配置的添加
package config
import (
"github.com/zeromicro/go-zero/rest"
"github.com/zeromicro/go-zero/zrpc"
)
type Config struct {
rest.RestConf
UserRpc zrpc.RpcClientConf // 添加調(diào)用rpc服務(wù)
}3. api服務(wù)svc配置請(qǐng)求上下文
package svc
import (
"easy-chat/examples/user/api/internal/config"
"easy-chat/examples/user/rpc/userclient"
"github.com/zeromicro/go-zero/zrpc"
)
type ServiceContext struct {
Config config.Config
userclient.User
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
User: userclient.NewUser(zrpc.MustNewClient(c.UserRpc)),
}
}
4. api服務(wù)邏輯層調(diào)用rpc服務(wù)
// GetUser 獲取用戶信息
func (l *GetUserLogic) GetUser(req *types.UserReq) (resp *types.UserResp, err error) {
user, err := l.svcCtx.User.GetUser(l.ctx, &userclient.UserReq{Id: req.Id})
if err != nil {
return nil, err
}
return &types.UserResp{
Id: user.Id,
Name: user.Name,
Phone: user.Phone,
}, nil
}
rpc 服務(wù)響應(yīng)api服務(wù)
1. 邏輯層響應(yīng)api服務(wù)
// GetUser rpc 服務(wù)處理邏輯
func (l *GetUserLogic) GetUser(in *user.UserReq) (*user.UserResp, error) {
return &user.UserResp{
Id: "123456",
Name: "tom",
Phone: "159924****8",
}, nil
}
數(shù)據(jù)庫(kù)的操作
1. 編寫操作的sql文件并生成對(duì)應(yīng)的操作數(shù)據(jù)庫(kù)接口
CREATE TABLE `users`
(
`id` varchar(24) COLLATE utf8mb4_unicode_ci NOT NULL,
`avatar` varchar(191) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`name` varchar(24) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`phone` varchar(20) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '',
`password` varchar(191) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`status` int(10) DEFAULT NULL,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
# -c是增加緩存 goctl model mysql ddl -src user.sql -dir . -c
2. proto文件增加添加用戶接口
syntax = "proto3";
package user;
option go_package = "./user";
message CreateReq {
string id = 1;
string name = 2;
string phone = 3;
}
message CreateResp {
string msg = 1;
}
message UserReq {
string id = 1;
}
message UserResp {
string id = 1;
string name = 2;
string phone = 3;
}
service User{
// CreateUser 創(chuàng)建用戶信息
rpc CreateUser(CreateReq) returns(CreateResp);
//GetUser 獲取用戶信息
rpc GetUser(UserReq) returns(UserResp);
}
3. 生成對(duì)應(yīng)的rpc接口
goctl rpc protoc user.proto --go_out=. --go-grpc_out=. --zrpc_out=. #通過(guò)proto生成服務(wù)
4. 添加etc請(qǐng)求配置
Name: user.rpc
ListenOn: 0.0.0.0:8080
Etcd:
Hosts:
- 127.0.0.1:2379
Key: user.rpc
Mysql:
DataSource: root:V4Nn9fa#Xf!@tpc(127.0.0.1:3306)/user?charset=utf8mb4
Cache:
- Host: 127.0.0.1:6379
Type: node
Pass:
5. 添加配置結(jié)構(gòu)體
package config
import (
"github.com/zeromicro/go-zero/core/stores/cache"
"github.com/zeromicro/go-zero/zrpc"
)
type Config struct {
zrpc.RpcServerConf
// mysql配置
Mysql struct {
DataSource string
}
// redis配置
Cache cache.CacheConf
}
6. 添加svc 上下文
package svc
import (
"easy-chat/examples/user/model"
"easy-chat/examples/user/rpc/internal/config"
"github.com/zeromicro/go-zero/core/stores/sqlx"
)
type ServiceContext struct {
Config config.Config
// mysql 連接信息
UserModel model.UsersModel
}
func NewServiceContext(c config.Config) *ServiceContext {
conn := sqlx.NewMysql(c.Mysql.DataSource)
return &ServiceContext{
Config: c,
// mysql和redis配置
UserModel: model.NewUsersModel(conn, c.Cache),
}
}
7. logic增加用戶邏輯
// CreateUser 創(chuàng)建用戶信息
func (l *CreateUserLogic) CreateUser(in *user.CreateReq) (*user.CreateResp, error) {
_, err := l.svcCtx.UserModel.Insert(l.ctx, &model.Users{
Id: in.Id,
Name: in.Name,
Phone: in.Phone,
})
if err != nil {
return nil, err
}
return &user.CreateResp{Msg: "ok"}, nil
}
api 服務(wù)調(diào)用rpc服務(wù)
1. 增加添加用戶api
syntax = "v1"
type CreateReq {
Id string `json:"id"`
Name string `json:"name"`
Phone string `json:"phone"`
}
type CreateResp {
Msg string `json:"msg"`
}
type UserReq {
Id string `json:"id"`
}
type UserResp {
Id string `json:"id"`
Name string `json:"name"`
Phone string `json:"phone"`
}
service User {
// createUser 添加用戶
@handler createUser
post /user (CreateReq) returns (CreateResp)
// getUser 獲取用戶
@handler getUser
get /user (UserReq) returns (UserResp)
}
2. 生成對(duì)應(yīng)的api服務(wù)接口
goctl api go -api user.api -dir . style = gozero
3. 編寫對(duì)應(yīng)的邏輯
func (l *CreateUserLogic) CreateUser(req *types.CreateReq) (resp *types.CreateResp, err error) {
user, err := l.svcCtx.User.CreateUser(l.ctx, &userclient.CreateReq{
Id: req.Id,
Name: req.Name,
Phone: req.Phone,
})
if err != nil {
return nil, err
}
return &types.CreateResp{Msg: user.Msg}, nil
}
到此這篇關(guān)于淺談Go-zero構(gòu)建RPC與API服務(wù)全流程的文章就介紹到這了,更多相關(guān)Go-zero構(gòu)建RPC與API服務(wù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Go語(yǔ)言對(duì)接微信支付與退款指南(示例詳解)
在互聯(lián)網(wǎng)技術(shù)日益發(fā)展的背景下,Go語(yǔ)言憑借并發(fā)處理能力,在后端開(kāi)發(fā)中大放異彩,本文詳細(xì)介紹如何使用Go語(yǔ)言對(duì)接微信支付,完成支付和退款功能,包括準(zhǔn)備工作、初始化微信支付客戶端、實(shí)現(xiàn)支付功能,以及處理支付回調(diào)和退款等2024-10-10
使用Go語(yǔ)言編寫一個(gè)毫秒級(jí)生成組件庫(kù)文檔工具
在開(kāi)發(fā)組件庫(kù)的過(guò)程中,文檔無(wú)疑是不可或缺的一環(huán),在本文中將嘗試將Go語(yǔ)言與前端技術(shù)巧妙融合,以創(chuàng)建一款能在毫秒級(jí)別完成文檔生成的工具,需要的可以參考下2024-03-03
go語(yǔ)言-在mac下brew升級(jí)golang
這篇文章主要介紹了go語(yǔ)言-在mac下brew升級(jí)golang,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04

