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

注册服务报“缺少 serviceName 参数 #134

Open
USMQ opened this issue Jul 25, 2023 · 1 comment
Open

注册服务报“缺少 serviceName 参数 #134

USMQ opened this issue Jul 25, 2023 · 1 comment

Comments

@USMQ
Copy link

USMQ commented Jul 25, 2023

系统 ubuntu 20 nacos-sdk-cpp 版本:1.0.8(C++ 版本)
nacos-server 版本:1.3.0(Java 版本)

修改 :examples/registerInstances.cpp
#include
#include <unistd.h>
#include "factory/NacosServiceFactory.h"
#include "ResourceGuard.h"
#include "naming/Instance.h"
#include "NacosString.h"
#include "Properties.h"
#include "constant/PropertyKeyConst.h"

using namespace std;
using namespace nacos;

int main() {
Properties configProps;
configProps[PropertyKeyConst::SERVER_ADDR] = "10.1.27.20:8848";
NacosServiceFactory *factory = new NacosServiceFactory(configProps);
ResourceGuard _guardFactory(factory);
NamingService *namingSvc = factory->CreateNamingService();
ResourceGuard _serviceFactory(namingSvc);
Instance instance;
instance.clusterName = "DefaultCluster";
instance.ip = "192.168.64.253";
instance.port = 8888;
instance.instanceId = "12345";
instance.ephemeral = true;

//Registers 5 services named TestNamingService-1...5
try {
    for (int i = 0; i < 5; i++) {
        NacosString serviceName = "TestNamingService-" + NacosStringOps::valueOf(i);
        instance.port = 1987 + i;
        std::cout << "IP:" << instance.ip << std::endl;
        std::cout << "Port: " << instance.port << std::endl;
        std::cout << "InstanceId: " << instance.instanceId << std::endl;
        std::cout << "Ephemeral: " << instance.ephemeral << std::endl;
        std::cout << "ServiceName: "<< serviceName << std::endl;
        std::cout << "clusterName: " << instance.clusterName << std::endl;
        std::cout << "Weight: "<< instance.weight << std::endl;
        namingSvc->registerInstance(serviceName, instance);
    }
}
catch (NacosException &e) {
    cout << "encounter exception while registering service instance, raison:" << e.what() << endl;
    return -1;
}
sleep(300);
try {
    for (int i = 0; i < 5; i++) {
        NacosString serviceName = "TestNamingService" + NacosStringOps::valueOf(i);

        namingSvc->deregisterInstance(serviceName, "192.168.64.253", 1987 + i);
        sleep(1);
    }
}
catch (NacosException &e) {
    cout << "encounter exception while registering service instance, raison:" << e.what() << endl;
    return -1;
}
sleep(30);

return 0;

}

运行过程及运行结果如下:
g++ registerInstances.cpp -L. -lnacos-cli -I/usr/local/include/nacossdkcpp -o registerInstances.out
./registerInstances.out

IP:192.168.64.253
Port: 1987
InstanceId: 12345
Ephemeral: 1
ServiceName: TestNamingService-0
clusterName: DefaultCluster
Weight: 1
encounter exception while registering service instance, raison:failed to req API:/nacos/v1/ns/instance after all servers(http://10.1.27.20:8848) tried: failed to req API:http://10.1.27.20:8848/nacos/v1/ns/instance code:400 errormsg:caused: Param 'serviceName' is required.;

本机只运行了一个nacos server,是否还需要其他服务作为配置中心?

@USMQ
Copy link
Author

USMQ commented Jul 25, 2023

增加了configProps[PropertyKeyConst::LOG_LEVEL] = "DEBUG"; 输出的log
nacos-sdk-cpp.log

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