0
点赞
收藏
分享

微信扫一扫

Go + cousul 治理微服务(服务注册)【四】

眼君 2023-02-15 阅读 91


Go + cousul 治理微服务(服务注册)【四】_Go

上节把服务端讲完了,现在我们需要把服务注册到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 是否在服务列表中

Go + cousul 治理微服务(服务注册)【四】_Go_02


嗯,果然,非常好!

Go + cousul 治理微服务(服务注册)【四】_微服务_03


接下来就是调用注册再consul 中的服务了

git commit -m "consul 服务注册"

源码

​​https://github.com/liaoshengping/Go-Consul-Demo​​


举报

相关推荐

0 条评论