goZero微服務開發(fā)小結
一、go-zero微服務環(huán)境安裝
1、go-zero腳手架的安裝
go install github.com/zeromicro/go-zero/tools/goctl@latest
2、etcd的安裝根據自己電腦操作系統(tǒng)下載對應的版本,具體的使用自己查閱文章
二、創(chuàng)建一個user-rpc服務
1、定義user.proto文件
syntax = "proto3";
package user;
option go_package="./user";
service User {
rpc FindById(FindByIdReq) returns (FindByIdResp);
}
message FindByIdReq{
int64 id = 1;
}
message FindByIdResp {
int64 id = 1;
string username = 2;
}
2、使用命令生成對應的項目文件
goctl rpc protoc ./user.proto --go_out=. --go-grpc_out=. --zrpc_out=./ goctl rpc protoc ./user.proto --go_out=./types --go-grpc_out=./types --zrpc_out=. --style goZero -m
3、安裝對應的依賴包
go mod tidy
4、運行user服務
go run user.go
5、在etcd中查看服務是否已經注冊成功
etcdctl get --prefix user.rpc
6、模擬業(yè)務代碼返回數(shù)據
func (l *FindByIdLogic) FindById(in *user.FindByIdReq) (*user.FindByIdResp, error) {
return &user.FindByIdResp{
Id: in.Id,
Username: "哈哈哈",
}, nil
}7、使用apifox可以直接調用rpc的服務,引入文件

三、在提供restful api接口端調用rpc服務返回數(shù)據給前端
1、創(chuàng)建一個user-api的項目
2、創(chuàng)建描述文件
syntax = "v1"
type GetUserReq {
Id int64 `path:"id"` // 主鍵id
}
type GetUserResp {
Id int64 `json:"id"` // 用戶id
Username string `json:"username"` // 用戶名
}
@server(
prefix: api/v1/user
group: user
)
service user-api {
@doc "根據用戶id獲取用戶新"
@handler GetUserByIdApi
get /:id (GetUserReq) returns (GetUserResp)
}
3、使用腳本生成對應的項目文件
goctl api go -api *.api -dir . --style=gozero
4、在user-api的配置文件中引入rpc服務的配置
Name: user-api
Host: 0.0.0.0
Port: 8888
UserRpc:
Etcd:
Hosts:
- 127.0.0.1:2379
Key: user.rpc5、在apps/user-api/internal/config/config.go創(chuàng)建服務的配置
type Config struct {
rest.RestConf
UserRpc zrpc.RpcClientConf
}
6、在apps/user-api/internal/svc/servicecontext.go依賴注入rpc服務
type ServiceContext struct {
Config config.Config
UserRpc userclient.User
}
func NewServiceContext(c config.Config) *ServiceContext {
return &ServiceContext{
Config: c,
UserRpc: userclient.NewUser(zrpc.MustNewClient(c.UserRpc)),
}
}7、模擬實現(xiàn)業(yè)務代碼
func (l *GetUserByIdApiLogic) GetUserByIdApi(req *types.GetUserReq) (resp *types.GetUserResp, err error) {
// 模擬業(yè)務開發(fā)
findByIdResp, err := l.svcCtx.UserRpc.FindById(l.ctx, &user.FindByIdReq{
Id: req.Id,
})
if err != nil {
return &types.GetUserResp{}, errors.New("查詢失敗")
}
return &types.GetUserResp{
Id: findByIdResp.Id,
Username: findByIdResp.Username,
}, nil
}
8、直接瀏覽模擬請求http://localhost:8888/api/v1/user/1
四、實際項目中可能是多個微服務的引入
1、在go-zero項目的svc文件夾下創(chuàng)建一個rpcClient.go的文件
type RpcClient struct {
config config.Config
// 服務1
systemRpc zrpc.Client
systemMutex sync.Mutex
// 服務2
usersMutex sync.Mutex
usersRpc zrpc.Client
}
func NewRpcClient(c config.Config) *RpcClient {
return &RpcClient{
config: c,
}
}
func (r *RpcClient) GetSystemRpc() system.ConfigServiceClient {
r.systemMutex.Lock()
defer r.systemMutex.Unlock()
if r.systemRpc == nil {
systemRpc, err := zrpc.NewClient(r.config.SystemRpc)
if err != nil {
logx.Errorf("new system rpc failed: %+v", err)
return nil
}
r.systemRpc = systemRpc
}
return system.NewConfigServiceClient(r.systemRpc.Conn())
}
func (r *RpcClient) GetUsersRpc() users.UsersClient {
r.usersMutex.Lock()
defer r.usersMutex.Unlock()
if r.usersRpc == nil {
usersRpc, err := zrpc.NewClient(r.config.UsersRpc)
if err != nil {
logx.Errorf("new user rpc failed: %+v", err)
return nil
}
r.usersRpc = usersRpc
}
return users.NewUsersClient(r.usersRpc.Conn())
}2、在serviceContext.go文件中注入上面的rpcClient.go
type ServiceContext struct {
Config config.Config
RpcClient *RpcClient
}
func NewServiceContext(c config.Config) *ServiceContext {
rpcClient := NewRpcClient(c)
if rpcClient == nil {
logx.Errorf("new rpc client failed")
panic(0)
}
return &ServiceContext{
Config: c,
RpcClient: rpcClient,
}
}3、正常使用user服務的方法
到此這篇關于goZero微服務開發(fā)小結的文章就介紹到這了,更多相關goZero微服務內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Go語言同步與異步執(zhí)行多個任務封裝詳解(Runner和RunnerAsync)
這篇文章主要給大家介紹了關于Go語言同步與異步執(zhí)行多個任務封裝(Runner和RunnerAsync)的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-01-01
Go Excelize API源碼解讀GetSheetViewOptions與SetPageLayo
這篇文章主要為大家介紹了Go Excelize API源碼解讀GetSheetViewOptions與SetPageLayout方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08

