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-client 2.1.0 erorr use endpoint configuration method #8882

Closed
tobeng opened this issue Aug 5, 2022 · 10 comments
Closed

nacos-client 2.1.0 erorr use endpoint configuration method #8882

tobeng opened this issue Aug 5, 2022 · 10 comments
Assignees
Labels
area/Client Related to Nacos Client SDK kind/bug Category issues or prs related to bug.
Milestone

Comments

@tobeng
Copy link

tobeng commented Aug 5, 2022

nacos-client 2.1.0 版本使用 endpoint 配置方式报错
com.alibaba.nacos.client.NamingTest 测试代码:

    @Test
    public void testServiceList() throws Exception {
        
        Properties properties = new Properties();
        properties.put(PropertyKeyConst.ENDPOINT, "127.0.0.1");
        properties.put(PropertyKeyConst.ENDPOINT_PORT, "8010");
        properties.put(PropertyKeyConst.USERNAME, "nacos");
        properties.put(PropertyKeyConst.PASSWORD, "nacos");
        
        Instance instance = new Instance();
        instance.setIp("1.1.1.1");
        instance.setPort(800);
        instance.setWeight(2);
        instance.setEphemeral(false);
        Map<String, String> map = new HashMap<String, String>();
        map.put("netType", "external");
        map.put("version", "2.0");
        map.put("ephemeral", "false");
        instance.setMetadata(map);
    
        NamingService namingService = NacosFactory.createNamingService(properties);
        namingService.registerInstance("nacos.test.1", instance);
        
        ThreadUtils.sleep(5000L);
        
        List<Instance> list = namingService.getAllInstances("nacos.test.1");
        
        System.out.println(list);
        
        ThreadUtils.sleep(30000L);
        //        ExpressionSelector expressionSelector = new ExpressionSelector();
        //        expressionSelector.setExpression("INSTANCE.metadata.registerSource = 'dubbo'");
        //        ListView<String> serviceList = namingService.getServicesOfServer(1, 10, expressionSelector);
        
    }

提示错误:

ErrCode:-400, ErrMsg:serverList is empty,please check configuration
	at com.alibaba.nacos.api.naming.NamingFactory.createNamingService(NamingFactory.java:61)
	at com.alibaba.nacos.api.NacosFactory.createNamingService(NacosFactory.java:77)
	at com.alibaba.nacos.client.NamingTest.testServiceList(NamingTest.java:55)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
	at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
	at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
	at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
	at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
	at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
	at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: java.lang.reflect.InvocationTargetException

	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:490)
	at com.alibaba.nacos.api.naming.NamingFactory.createNamingService(NamingFactory.java:59)
	... 24 more
Caused by: com.alibaba.nacos.api.exception.runtime.NacosLoadException: serverList is empty,please check configuration
	at com.alibaba.nacos.client.naming.core.ServerListManager.<init>(ServerListManager.java:91)
	at com.alibaba.nacos.client.naming.remote.NamingClientProxyDelegate.<init>(NamingClientProxyDelegate.java:70)
	at com.alibaba.nacos.client.naming.NacosNamingService.init(NacosNamingService.java:95)
	at com.alibaba.nacos.client.naming.NacosNamingService.<init>(NacosNamingService.java:81)
	... 29 more

查看 nacos-client 源码是在解决 [fix by zero error(#7724)] 问题在 com.alibaba.nacos.client.naming.core.ServerListManager 增加了代码:
image

但是使用 endpoint 方式是不会赋值给 serverList ,所以会报错,ServerListManager 对外提供获取 serverList 的方式是 getServerList() ,所以这里判断也应该通过 getServerList() 获取判断:

    public ServerListManager(Properties properties, String namespace) {
        this.namespace = namespace;
        initServerAddr(properties);
        if (!getServerList().isEmpty()) {
            currentIndex.set(new Random().nextInt(getServerList().size()));
        } else {
            throw new NacosLoadException("serverList is empty,please check configuration");
        }
    }
@YunWZ
Copy link
Contributor

YunWZ commented Aug 5, 2022

你的地址服务(endpoint server)返回nacos服务地址列表了吗?
使用命令查看返回值 curl http://{endpoint}:{endpoin-port}/nacos/serverlist

@tobeng
Copy link
Author

tobeng commented Aug 5, 2022

用的是 nacos-address 服务,有返回 nacos 服务列表:
image

@YunWZ
Copy link
Contributor

YunWZ commented Aug 5, 2022

@KomachiSion
我认为这是一个bug。 当使用endpoint时,initServerAddr方法中并没有更新成员变量serverList,而此时会抛出NacosLoadException异常。

@KomachiSion
Copy link
Collaborator

KomachiSion commented Aug 8, 2022

@YunWZ 仔细读代码,客户端是根据是否有设置endpoint来读取不同列表的,有endpoint的时候只会使用serversFromEndpointserverList本来就不需要初始化

@KomachiSion
Copy link
Collaborator

可以问下 @hujun-w-2 为什么要添加空列表判断

@hujun-w-2
Copy link
Collaborator

i will fix it

YunWZ added a commit to YunWZ/nacos that referenced this issue Aug 11, 2022
@KomachiSion KomachiSion added kind/bug Category issues or prs related to bug. area/Client Related to Nacos Client SDK and removed status/need feedback labels Aug 11, 2022
@KomachiSion KomachiSion added this to the 2.1.2 milestone Aug 12, 2022
@tobeng
Copy link
Author

tobeng commented Aug 12, 2022

@hujun-w-2 @KomachiSion 我认为这个[#8933]提交的代码有问题,并没有修复这个问题,提交的描述和代码逻辑不一致。

@hujun-w-2
Copy link
Collaborator

@hujun-w-2 @KomachiSion 我认为这个[#8933]提交的代码有问题,并没有修复这个问题,提交的描述和代码逻辑不一致。

我看看

@hujun-w-2
Copy link
Collaborator

@tobeng 抱歉,已重新提交修复

@tobeng
Copy link
Author

tobeng commented Aug 15, 2022

@tobeng 抱歉,已重新提交修复

好的,感谢处理。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/Client Related to Nacos Client SDK kind/bug Category issues or prs related to bug.
Projects
None yet
Development

No branches or pull requests

4 participants