Next, we'll register the user microservice with etcd
to make it available for other services to call.
Add a configuration file with the etcd
access address.
app/user/manifest/config/etcd.yaml
etcd:
address: "srv.com:2379"
Add the registration logic in the entry file:
app/user/main.go
package main
import (
_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
"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/user/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)
}
In fact, the key code for service registration is just one line, while the rest is code for reading the configuration file:
grpcx.Resolver.Register(etcd.New(address))
Restart the project to apply the changes. Then enter the etcd
container and execute the following command to verify the registration:
$ etcdctl get "" --prefix --keys-only
This command shows all existing keys
in etcd
, where we should see our registered service:
/service/default/default/user/latest/{IP}:32001
Service registration can be understood as similar to DNS name resolution. The service name
grpc.name
in the configuration file can be thought of as analogous to a domain name.