Let's start by running the service before we call the microservices.
Configuration Files
app/gateway/manifest/config/config.yaml
server:
address: ":8000"
openapiPath: "/api.json"
swaggerPath: "/swagger"
logger:
path: "./log"
file: "{Y-m-d}.log"
level: "all"
stdout: true
app/gateway/manifest/config/etcd.yaml
etcd:
address: "srv.com:2379"
Command File
app/gateway/internal/cmd/cmd.go
package cmd
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gcmd"
"proxima/app/gateway/internal/controller/user"
"proxima/app/gateway/internal/controller/words"
)
var (
Main = gcmd.Command{
Name: "main",
Usage: "main",
Brief: "start http gateway server",
Func: func(ctx context.Context, parser *gcmd.Parser) (err error) {
s := g.Server()
s.Group("/", func(group *ghttp.RouterGroup) {
group.Middleware(ghttp.MiddlewareHandlerResponse)
group.Group("/v1", func(group *ghttp.RouterGroup) {
group.Group("/", func(group *ghttp.RouterGroup) {
group.Bind(user.NewV1())
group.Bind(words.NewV1())
})
})
})
s.Run()
return nil
},
}
)
Starting the Service
app/gateway/main.go
package main
import (
"github.com/gogf/gf/contrib/registry/etcd/v2"
"github.com/gogf/gf/contrib/rpc/grpcx/v2"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gctx"
"proxima/app/gateway/internal/cmd"
)
func main() {
var ctx = gctx.New()
conf, err := g.Cfg("etcd").Get(ctx, "etcd.address")
if err != nil {
panic(err)
}
var address = conf.String()
grpcx.Resolver.Register(etcd.New(address))
cmd.Main.Run(ctx)
}
$ gf run .\main.go
build running pid: 16144
2024-12-10 15:30:30.788 [INFO] pid[16144]: http server started listening on [:8000]
2024-12-10 15:30:30.788 [INFO] {f0f10d9d4ec00f181b7a6f615f39d54b} swagger ui is serving at address: http://127.0.0.1:8000/swagger/
2024-12-10 15:30:30.789 [INFO] {f0f10d9d4ec00f181b7a6f615f39d54b} openapi specification is serving at address: http://127.0.0.1:8000/api.json
2024-12-10 15:30:30.817 [DEBU] {f0f10d9d4ec00f181b7a6f615f39d54b} service register: &{Head: Deployment: Namespace: Name:default Version: Endpoints:192.168.10.98:8000 Metadata:map[insecure:true protocol:http]}
2024-12-10 15:30:30.900 [DEBU] {f0f10d9d4ec00f181b7a6f615f39d54b} etcd put success with key "/service/default/default/default/latest/192.168.10.98:8000", value "{"insecure":true,"protocol":"http"}", lease "7587883327293376805"
ADDRESS | METHOD | ROUTE | HANDLER | MIDDLEWARE
----------|--------|-----------------|----------------------------------------------------------------------|--------------------
:8000 | ALL | /api.json | github.com/gogf/gf/v2/net/ghttp.(*Server).openapiSpec |
----------|--------|-----------------|----------------------------------------------------------------------|--------------------
:8000 | ALL | /swagger/* | github.com/gogf/gf/v2/net/ghttp.(*Server).swaggerUI | HOOK_BEFORE_SERVE
----------|--------|-----------------|----------------------------------------------------------------------|--------------------
:8000 | POST | /v1/users/login | proxima/app/gateway/internal/controller/user.(*ControllerV1).Login |
----------|--------|-----------------|----------------------------------------------------------------------|--------------------
:8000 | POST | /v1/words | proxima/app/gateway/internal/controller/words.(*ControllerV1).Create |
----------|--------|-----------------|----------------------------------------------------------------------|--------------------
:8000 | GET | /v1/words/{id} | proxima/app/gateway/internal/controller/words.(*ControllerV1).Detail |
----------|--------|-----------------|----------------------------------------------------------------------|--------------------
Entering the etcd
container and executing the command to view:
$ etcdctl get "" --prefix --keys-only
/service/default/default/default/latest/{IP}:8000
/service/default/default/word/latest/{IP}:32001
/service/default/default/word/latest/{IP}:32002
As we can see, all three of our services have been successfully registered.