int DiscoveryClient::DoRegister() const { Controller cntl; cntl.http_request().set_method(HTTP_METHOD_POST); cntl.http_request().uri() = "/discovery/register"; cntl.http_request().set_content_type("application/x-www-form-urlencoded"); butil::IOBufBuilder os; os << "appid=" << _appid << "&hostname=" << _hostname << "&addrs=" << _addrs << "&env=" << _env << "&zone=" << _zone << "®ion=" << _region << "&status=" << _status << "&version=" << _version << "&metadata=" << _metadata; os.move_to(cntl.request_attachment()); s_discovery_channel.CallMethod(NULL, &cntl, NULL, NULL, NULL); if (cntl.Failed()) { LOG(ERROR) << "Fail to register " << _appid << ": " << cntl.ErrorText(); return -1; } std::string error_text; if (ParseCommonResult(cntl.response_attachment(), &error_text) != 0) { LOG(ERROR) << "Fail to register " << _hostname << " to " << _appid << ": " << error_text; return -1; } return 0; }
int DiscoveryClient::DoCancel() const { pthread_once(&s_init_channel_once, InitChannel); Controller cntl; cntl.http_request().set_method(HTTP_METHOD_POST); cntl.http_request().uri() = "/discovery/cancel"; butil::IOBufBuilder os; os << "appid=" << _appid << "&hostname=" << _hostname << "&env=" << _env << "®ion=" << _region << "&zone=" << _zone; os.move_to(cntl.request_attachment()); s_discovery_channel.CallMethod(NULL, &cntl, NULL, NULL, NULL); if (cntl.Failed()) { LOG(ERROR) << "Fail to post /discovery/cancel: " << cntl.ErrorText(); return -1; } std::string error_text; if (ParseCommonResult(cntl.response_attachment(), &error_text) != 0) { LOG(ERROR) << "Fail to cancel " << _hostname << " in " << _appid << ": " << error_text; return -1; } return 0; }
static void InitChannel() { Channel api_channel; ChannelOptions channel_options; channel_options.protocol = PROTOCOL_HTTP; channel_options.timeout_ms = FLAGS_discovery_timeout_ms; channel_options.connect_timeout_ms = FLAGS_discovery_timeout_ms / 3; if (api_channel.Init(FLAGS_discovery_api_addr.c_str(), "", &channel_options) != 0) { LOG(FATAL) << "Fail to init channel to " << FLAGS_discovery_api_addr; return; } Controller cntl; cntl.http_request().uri() = FLAGS_discovery_api_addr; api_channel.CallMethod(NULL, &cntl, NULL, NULL, NULL); if (cntl.Failed()) { LOG(FATAL) << "Fail to access " << cntl.http_request().uri() << ": " << cntl.ErrorText(); return; } std::string discovery_addr; if (ParseNodesResult(cntl.response_attachment(), &discovery_addr) != 0) { LOG(FATAL) << "Fail to parse nodes result from discovery api server"; return; } if (s_discovery_channel.Init(discovery_addr.c_str(), "", &channel_options) != 0) { LOG(FATAL) << "Fail to init channel to " << discovery_addr; return; } }
int DiscoveryClient::Fetchs(const DiscoveryFetchsParam& req, std::vector<ServerNode>* servers) const { if (!req.IsValid()) { return false; } pthread_once(&s_init_channel_once, InitChannel); servers->clear(); Controller cntl; cntl.http_request().uri() = butil::string_printf( "/discovery/fetchs?appid=%s&env=%s&status=%s", req.appid.c_str(), req.env.c_str(), req.status.c_str()); s_discovery_channel.CallMethod(NULL, &cntl, NULL, NULL, NULL); if (cntl.Failed()) { LOG(ERROR) << "Fail to get /discovery/fetchs: " << cntl.ErrorText(); return -1; } return ParseFetchsResult(cntl.response_attachment(), req.appid.c_str(), servers); }