添加配置文件:
app/word/manifest/config/etcd.yaml
etcd:
address: "srv.com:2379"
在入口文件添加注册逻辑:
app/word/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/word/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)
}
重新运行项目,再次进入etcd
容器,执行命令查看:
$ etcdctl get "" --prefix --keys-only
/service/default/default/user/latest/{IP}:32001
/service/default/default/word/latest/{IP}:32002
可以看见,我们的两个微服务都已经成功注册。接下来,运行网关,即可调用它们。