使用go语言根据微服务名称获取consul的微服务的时候,为什么是services啊?然后metainfo.LastIndex?

var lastIndex uint64
    config := consulapi.DefaultConfig()
    config.Address = "127.0.0.1:8500"
    client, err := consulapi.NewClient(config)
    if err != nil {
        fmt.Println("api new client is failed, err:", err)
        return
    }
    services, metainfo, err := client.Health().Service("serverNode", "v1", true, &consulapi.QueryOptions{WaitIndex: lastIndex, // 同步点,这个调用将一直阻塞,直到有新的更新
    })
    if err != nil {
        log.Fatalf("error retrieving instances from Consul: %v", err)
    }
    lastIndex = metainfo.LastIndex
    addrs := map[string]struct{}{}
    for _, service := range services {
        fmt.Println("service.Service.Address:", service.Service.Address, "service.Service.Port:", service.Service.Port)
        addrs[net.JoinHostPort(service.Service.Address, strconv.Itoa(service.Service.Port))] = struct{}{}
    }
回复
阅读 407
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏