Skip to main content
Version: 2.8.x(Latest)

Protocol files refer to *.proto files. Proto is the standard communication protocol for gRPC, similar to how JSON relates to HTTP. However, it's essential to note that proto and JSON have distinct differences: proto defines both "interface" information and response/request parameters, while JSON simply stores data.

Proto files are stored uniformly under manifest/protobuf, and like regular HTTP services, interface versions are managed through directory hierarchies.

User Registration


Create a directory named account to manage user account-related business logic.

app/user/manifest/protobuf/account/v1/account.proto

syntax = "proto3";  

package account.v1;

option go_package = "proxima/app/user/api/account/v1";

service Account{
rpc UserRegister(UserRegisterReq) returns (UserRegisterRes) {}
}

message UserRegisterReq {
string username = 1; // v:required|min-length:2
string password = 2; // v:required|min-length:6
string email = 3; // v:required|email
}

message UserRegisterRes {
int32 id = 1;
}

Let's briefly explain the proto syntax:

  • syntax: Specifies the file's syntax version
  • package: Defines the service namespace, similar to a package name
  • option: Sets compilation options; go_package specifies the package name for generated Go code. In GoFrame, the fixed format is project_name + app + microservice_name + api + module_name + v1
  • service: Defines remote call methods, typically RPC, specifying request and response parameters
  • message: Defines data structures, where string is the data type, username is the field name, and the incremental numbers after the equals sign are field numbers. The trailing comments are framework-provided parameter validations, used similarly to regular HTTP interfaces

Our file defines:

  • Uses proto3 syntax version
  • Defines package name as account.v1
  • Sets the Go code generation package path option go_package to proxima/app/user/api/account/v1
  • Defines an Account service with one RPC method UserRegister that accepts UserRegisterReq message and returns UserRegisterRes message
  • Defines a message type UserRegisterReq with three fields:
    • username (string type, number 1)
    • password (string type, number 2)
    • email (string type, number 3)
  • Defines a message type UserRegisterRes with one field:
    • id (integer type, number 1)

User Login/Query


Following the same pattern, let's define the user login and query interfaces. Here's the complete file content:

app/user/manifest/protobuf/account/v1/account.proto

syntax = "proto3";  

package account.v1;

option go_package = "proxima/app/user/api/account/v1";

import "pbentity/users.proto";

service Account{
rpc UserRegister(UserRegisterReq) returns (UserRegisterRes) {}
rpc UserLogin(UserLoginReq) returns (UserLoginRes) {}
rpc UserInfo(UserInfoReq) returns (UserInfoRes) {}
}

message UserRegisterReq {
string username = 1; // v:required|min-length:2
string password = 2; // v:required|min-length:6
string email = 3; // v:required|email
}

message UserRegisterRes {
int32 id = 1;
}

message UserLoginReq {
string username = 1; // v:required|min-length:2
string password = 2; // v:required|min-length:6
}

message UserLoginRes {
string token = 1;
}

message UserInfoReq {
string token = 1; // v:required
}

message UserInfoRes {
pbentity.Users user = 1;
}

This introduces two new syntax elements:

  • import "pbentity/users.proto": Imports another proto file. This file was generated by gf gen pbentity
  • pbentity.Users user: Uses the imported data model, which is almost identical to Go structs