Importing Controllers in CMD
Like monolithic services, microservices also need to be imported in the cmd
. The difference is that the service startup changes from HTTP
to gRPC
.
app/user/internal/cmd/cmd.go
package cmd
import (
"context"
"github.com/gogf/gf/contrib/rpc/grpcx/v2"
"github.com/gogf/gf/v2/os/gcmd"
"google.golang.org/grpc"
"proxima/app/user/internal/controller/account"
)
var (
Main = gcmd.Command{
Name: "main",
Usage: "main",
Brief: "user grpc service",
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
c := grpcx.Server.NewConfig()
c.Options = append(c.Options, []grpc.ServerOption{
grpcx.Server.ChainUnary(
grpcx.Server.UnaryValidate,
)}...,
)
s := grpcx.Server.New(c)
account.Register(s)
s.Run()
return nil
},
}
)
Main Entry File
Import the database driver and cmd
in the main entry file.
app/user/main.go
package main
import (
_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
"github.com/gogf/gf/v2/os/gctx"
"proxima/app/user/internal/cmd"
)
func main() {
cmd.Main.Run(gctx.GetInitCtx())
}
Configuration File
app/user/manifest/config/config.yaml
grpc:
name: "user"
address: ":32001"
database:
default:
link: "mysql:root:12345678@tcp(srv.com:3306)/user"
debug: true
The grpc
field defines two essential parameters: the microservice name and the listening port. The service name is used for service registration, and the listening port is self-explanatory. These two are mandatory; for other configuration options, refer to the configuration template.
Starting the Service
Switch to the root directory and ensure all dependencies are properly installed.
$ cd ../../
go mod tidy
Return to the microservice directory and start the user microservice.
$ cd app/user
gf run .\main.go
build: .\main.go
go build -o .\main.exe .\main.go
.\main.exe
build running pid: 15480
2024-12-06 15:02:01.246 [DEBU] {d8e6fef56e840e1815d0325bc73eda8f} set default registry using file registry as no custom registry set, path: C:\Users\half\AppData\Local\Temp\gsvc
2024-12-06 15:02:01.269 [DEBU] {d8e6fef56e840e1815d0325bc73eda8f} service register: &{Head: Deployment: Namespace: Name:user Version: Endpoints:192.168.10.91:32001 Metadata:map[protocol:grpc]}
2024-12-06 15:02:01.270 [INFO] {d8e6fef56e840e1815d0325bc73eda8f} pid[15480]: grpc server started listening on [:32001]
With this, we've completed the development of the first microservice for Proxima Notebook, which isn't much different from developing a monolithic service.
Testing Results
When testing gRPC in your testing tool, you'll need to use the proto protocol file. Make sure to specify the correct dependency paths.
grpc 127.0.0.1:32001.account.v1.Account.UserRegister
{
"username": "oldme",
"password": "123456",
"email": "tyyn1022@gmail.com"
}
{
"id": 1
}
grpc 127.0.0.1:32001.account.v1.Account.UserLogin
{
"username": "oldme",
"password": "123456"
}
{
"token": "I am token"
}
grpc 127.0.0.1:32001.account.v1.Account.UserInfo
{
"token": "I am token"
}
{
"user": {
"Id": 1,
"Username": "oldme",
"Password": "123456",
"Email": "tyyn1022@gmail.com",
"CreatedAt": {
"seconds": "1733407200",
"nanos": 0
},
"UpdatedAt": {
"seconds": "1733407200",
"nanos": 0
}
}
}