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

多线程崩溃问题,使用多个端口多个线程,模拟多节点部署服务注册和心跳发送 #130

Open
linlongtao opened this issue Apr 7, 2023 · 2 comments

Comments

@linlongtao
Copy link

问题描述:
想使用多个线程多个端口模拟多节点部署注册服务和心跳发送。
使用临时实例并且保持 NamingService *namingSvc实例对象不被析构(使用while(true)),从而实现自动发送心跳。
第一个线程能够正常启动并注册服务,但是第二个线程跑到NamingService *namingSvc = factory->CreateNamingService();处就会崩溃。
报错信息:
信号: SIGABRT (Aborted)
Nacos SDK异常: Login failed after all servers are tried
请大佬们帮忙看一下:
1 .具体是什么原因导致资源竞争的出现。
2. 是否还有其他方案实现多节点部署注册服务和心跳发送。

代码主要部分:

/ 注册到Nacos
void registerToNacos(int port)
{
    try
    {
        // 创建Nacos SDK客户端
        Properties configProps;
        configProps[PropertyKeyConst::SERVER_ADDR] = SERVER_ADDR;
        configProps[PropertyKeyConst::AUTH_USERNAME] = "nacos";
        configProps[PropertyKeyConst::AUTH_PASSWORD] = "nacos";

        INacosServiceFactory *factory = NacosFactoryFactory::getNacosFactory(configProps);

        ResourceGuard<INacosServiceFactory> _guardFactory(factory);
        NamingService *namingSvc = factory->CreateNamingService();  //断点调试,第二个线程到这里就崩溃了
        ResourceGuard<NamingService> _serviceFactory(namingSvc);


        // 构造服务实例对象
        Instance instance;
        instance.ip = "192.168.116.130";
        instance.port = port;
        instance.weight = 1;
        instance.enabled = true;
        instance.healthy = true;
        instance.instanceId = "exampleInstanceId";
        instance.ephemeral = true;

        namingSvc->registerInstance(SERVICE_NAME, GROUP_NAME, instance);

        while (true)
        {
            cout << port << " Send heartbeat success" << endl;
            sleep(5);
        }
    }
    catch (NacosException &e)
    {
        cerr << "Nacos SDK异常: " << e.what() << endl;
    }

}

int main(int argc, char **argv)
{

    thread tHeartbeat1(registerToNacos, 2001);
    sleep(5);
    thread tHeartbeat2(registerToNacos, 2002);

    tHeartbeat1.join();
    tHeartbeat2.join();

    while (true)
    {
        // 主线程不退出
        sleep(3);
    }

    return 0;
}

调试报错显示:
信号: SIGABRT (Aborted)
Nacos SDK异常: Login failed after all servers are tried

@ieatkids
Copy link

每个线程设一个不同的nacos::PropertyKeyConst::UDP_RECEIVER_PORT试试呢,我这里能work

@huangxinze
Copy link

问题描述: 想使用多个线程多个端口模拟多节点部署注册服务和心跳发送。 使用临时实例并且保持 NamingService *namingSvc实例对象不被析构(使用while(true)),从而实现自动发送心跳。 第一个线程能够正常启动并注册服务,但是第二个线程跑到NamingService *namingSvc = factory->CreateNamingService();处就会崩溃。 报错信息: 信号: SIGABRT (Aborted) Nacos SDK异常: Login failed after all servers are tried 请大佬们帮忙看一下: 1 .具体是什么原因导致资源竞争的出现。 2. 是否还有其他方案实现多节点部署注册服务和心跳发送。

代码主要部分:

/ 注册到Nacos
void registerToNacos(int port)
{
    try
    {
        // 创建Nacos SDK客户端
        Properties configProps;
        configProps[PropertyKeyConst::SERVER_ADDR] = SERVER_ADDR;
        configProps[PropertyKeyConst::AUTH_USERNAME] = "nacos";
        configProps[PropertyKeyConst::AUTH_PASSWORD] = "nacos";

        INacosServiceFactory *factory = NacosFactoryFactory::getNacosFactory(configProps);

        ResourceGuard<INacosServiceFactory> _guardFactory(factory);
        NamingService *namingSvc = factory->CreateNamingService();  //断点调试,第二个线程到这里就崩溃了
        ResourceGuard<NamingService> _serviceFactory(namingSvc);


        // 构造服务实例对象
        Instance instance;
        instance.ip = "192.168.116.130";
        instance.port = port;
        instance.weight = 1;
        instance.enabled = true;
        instance.healthy = true;
        instance.instanceId = "exampleInstanceId";
        instance.ephemeral = true;

        namingSvc->registerInstance(SERVICE_NAME, GROUP_NAME, instance);

        while (true)
        {
            cout << port << " Send heartbeat success" << endl;
            sleep(5);
        }
    }
    catch (NacosException &e)
    {
        cerr << "Nacos SDK异常: " << e.what() << endl;
    }

}

int main(int argc, char **argv)
{

    thread tHeartbeat1(registerToNacos, 2001);
    sleep(5);
    thread tHeartbeat2(registerToNacos, 2002);

    tHeartbeat1.join();
    tHeartbeat2.join();

    while (true)
    {
        // 主线程不退出
        sleep(3);
    }

    return 0;
}

调试报错显示: 信号: SIGABRT (Aborted) Nacos SDK异常: Login failed after all servers are tried

碰到相同的问题,也是第二个线程跑到 NamingService *namingSvc = factory->CreateNamingService(); 的时候 就崩溃了; 请问楼主解决了吗?

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

3 participants