微服务 - go-zero api请求调用rpc服务

开发环境

本地使用etcd注册rpc服务,这个部分文档主要编写api使用、api调用rpc服务,下次文档更新使用mysql和redis缓存部分。

go version : go1.20.1 
goctl : version 1.4.4 

文件目录说明:

├── README.md
├── apps
│   ├── app
│   │   └── api
│   └── rpc
│       └── drama
├── common
│   └── midfleware
├── go.mod
├── go.sum
└── pkg

Api文件编写 && 调用Rpc服务

项目开始前,先编写Api接口,使用goctl去生成代码,生成代码命令:

type DramaListRequest {
	Page   int16  `json:"page,default=1"`
	Limit  int8   `json:"limit,default=20"`
	Cookie string `header:"Cookie"`
}

type DramaInfoRequest {
	DramaId int `json:"dramaId,default=1"`
}

type dramaSeniorEditRequest {
}

type Request {
	Code uint32      `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

@server(
	group: admin
	prefix: api
	middleware :AuthMiddleware
)

service admin-api {
	@doc "有声作品详情"
	@handler dramaInfoHandler
	get /audio/dramaInfo(DramaInfoRequest) returns (Request)
	
	@doc "有声作品列表"
	@handler dramaListHandler
	get /audio/dramaList(DramaListRequest) returns (Request)
	
	@doc "作品高级设置"
	@handler dramaSeniorEdit
	post /audio/dramaSeniorEdit(dramaSeniorEditRequest) returns (Request)
}

生成api文件命令:

goctl api go  -api *.api -dir ./ --style=goZero

服务之间的调用,启动,配置文件是灵魂操作:

1.配置api文件 , audio/apps/app/api/etc/admin-api.yaml

Name: admin-api
Host: 0.0.0.0
Port: 8888

Log:
  ServiceName: admin-api
  KeepDays: 7
  Stat: false

DramaRpcConf:
  Etcd:
    Hosts:
      - 0.0.0.0:2379
    key: drama.rpc

2.引入config , audio/apps/app/api/internal/config/config.go

type Config struct {
	rest.RestConf
	DramaRpcConf zrpc.RpcClientConf
}

3.加载svc文件,audio/apps/app/api/internal/svc/servicecontext.go

type ServiceContext struct {
    
    
	Config         config.Config
	AuthMiddleware rest.Middleware
	DramaRpcClient dramaclient.Drama
}

func NewServiceContext(c config.Config) *ServiceContext {
    
    
	return &ServiceContext{
    
    
		Config:         c,
		AuthMiddleware: middleware.NewAuthMiddleware().Handle,
		DramaRpcClient: dramaclient.NewDrama(zrpc.MustNewClient(c.DramaRpcConf)),
	}
}

4.logic调用Rpc ,audio/apps/app/api/internal/logic/admin/dramaInfoLogic.go

func (l *DramaInfoLogic) DramaInfo(req *types.DramaInfoRequest) (resp *types.Request, err error) {
	// todo: add your logic here and delete this line
	dramaResp, err := l.svcCtx.DramaRpcClient.GetDramaInfo(l.ctx, &dramaclient.GetDramaInfoRequest{
		Id: int64(req.DramaId),
	})
	fmt.Println(dramaResp)
	return &types.Request{Data: dramaResp.Id}, nil
}

Rpc服务

先写一个pb文件,使用goctl命令生成Rpc服务。

syntax = "proto3";

package drama;
option go_package="./drama";

message GetDramaInfoRequest {
  int64 id = 1;
}

message GetDramaInfoResponse {
  int64 id = 1;
}

service Drama {
  rpc GetDramaInfo(GetDramaInfoRequest) returns(GetDramaInfoResponse);
}

使用命令生成Rpc目录:

goctl rpc protoc *.proto --go_out=../ --go-grpc_out=../ --zrpc_out=../ --style=goZero

测试Rpc服务

安装grpcui

go get github.com/fullstorydev/grpcui/cmd/grpcui
go install github.com/fullstorydev/grpcui/cmd/grpcui

启动服务,ip和端口是配置文件的端口。

grpcui -plaintext 127.0.0.1:8899
gRPC Web UI available at http://127.0.0.1:57044/

猜你喜欢

转载自blog.csdn.net/xuezhiwu001/article/details/129261312