Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

首次向Nacos服务进行注册的时候, 回到的回调较慢, 要5秒左右 #140

Open
huangxinze opened this issue Feb 22, 2024 · 0 comments

Comments

@huangxinze
Copy link

huangxinze commented Feb 22, 2024

额,解决了:配置项 SUBSCRIPTION_POLL_INTERVAL 未设置

麻烦问一下, 使用该sdk向nacos服务进行注册的时候,首次注册的回调为什么要等大概5秒左右才能收到回调,以下是注册和接收回调的代码:

    nacos::Properties conf;
    conf[nacos::PropertyKeyConst::SERVER_ADDR] = m_strNacosAddressList;
    conf[nacos::PropertyKeyConst::UDP_RECEIVER_PORT] = std::to_string(m_nLocalPort);
    try {
        auto factory = nacos::NacosFactoryFactory::getNacosFactory(conf);
        nacos::ResourceGuard<nacos::INacosServiceFactory> _guardFactory(factory);
        m_pNacosNamingService = factory->CreateNamingService();
    } catch (const nacos::NacosException &e) {
        logW("initialization of nacos naming service failed: " + std::string(e.what()));
        return false;
    }

// setup nacos instance params.
nacos::Instance instance;
instance.ip = m_strLocalIP;
instance.port = m_nLocalPort;
instance.clusterName = "test";
instance.instanceId = "xxxxxxxxxxxxxx";
instance.ephemeral = true;

// regist and subscribe.
try {
    m_pNacosNamingService->registerInstance(strServiceName, instance);
    if (!m_bIsNacosSubscribed) {
        m_bIsNacosSubscribed = true;
        m_pNacosNamingService->subscribe(strServiceName, new NacosEventListener(this));
    }
} catch (const nacos::NacosException &e) {
    logW("registration failed: " + std::string(e.what()));
    return false;
}
return true;

class NacosEventListener : public nacos::EventListener {

public:
NacosEventListener(CCallback*pCallback) {
this->m_pCallback= pCallback;
}

public:
void receiveNamingInfo(const nacos::ServiceInfo &serviceInfo) {
m_pCallback->OnNacosReceiveNamingInfo(serviceInfo);
}

private:
CCallback* m_pCallback;

};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant