服务注册
上节把服务端讲完了,现在我们需要把服务注册到 consul 中去。
go get github.com/micro/go-micro/v2/registry
go get github.com/micro/go-plugins/registry/consul/v2
引入微服务注册包和 consul 注册包
我们在 server.go 中修改代码:
consulRegister := consul.NewRegistry(func(options *registry.Options) {
options.Addrs = []string{
"192.168.205.22:8500",
}
})
service := micro.NewService(
micro.Name("order.service"),
micro.Registry(consulRegister),
)
这边的 192.168.205.22
是我本地虚拟机的 ip,同学们可以根据自己实际情况修改,如果是本地的一般是 172.0.0.1
8500
是第二节中的的 consul 的端口,根据实际情况修改哈
在 NewService 服务中,把 consulRegister 信息注册到服务当中去。
这样就完成了服务的注册了,我们打开 cousul 查看一下,这个 order.service 是否在服务列表中
嗯,果然,非常好!
接下来就是调用注册再 consul 中的服务了
git add .
git commit -m "consul 服务注册"
推荐文章: