1、下载对应consul的api

 go get github.com/hashicorp/consul/api

2、初始化consul配置

consulConfig := api.DefaultConfig()
3、创建consul对象
consulClient, err := api.NewClient(consulConfig)
if err != nil {
   fmt.Println("api.NewClient err:", err)
   return
}
4、告诉consul 要注册服务的配置信息
registerService := api.AgentServiceRegistration{
   ID: "xf01",
   Tags: []string{"grpc", "consul"},
   Name: "xf01 grpc and consul",
   Address: "192.168.132.133",
   Port: 8800,
   Check: &api.AgentServiceCheck{
      CheckID: "xf01 consul grpc check",
      TCP: "192.168.132.133:8800",
      Timeout: "1s",
      Interval: "5s",
   },
}
5、注册grpc对象到consul上
err = consulClient.Agent().ServiceRegister(&registerService)
if err != nil {
   fmt.Println("consulClient.Agent().ServiceRegister err:", err)
   return
}

6、启动consul

consul agent -dev

7、调用http://localhost:8500/查看服务状态

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐