nacos icon indicating copy to clipboard operation
nacos copied to clipboard

nacos-client 2.1.0 erorr use endpoint configuration method

Open tobeng opened this issue 2 years ago • 6 comments

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");
        }
    }

tobeng avatar Aug 05 '22 03:08 tobeng

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

YunWZ avatar Aug 05 '22 04:08 YunWZ

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

tobeng avatar Aug 05 '22 04:08 tobeng

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

YunWZ avatar Aug 05 '22 07:08 YunWZ

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

KomachiSion avatar Aug 08 '22 11:08 KomachiSion

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

KomachiSion avatar Aug 08 '22 11:08 KomachiSion

i will fix it

hujun-w-2 avatar Aug 09 '22 09:08 hujun-w-2

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

tobeng avatar Aug 12 '22 09:08 tobeng

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

我看看

hujun-w-2 avatar Aug 12 '22 09:08 hujun-w-2

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

hujun-w-2 avatar Aug 12 '22 11:08 hujun-w-2

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

好的,感谢处理。

tobeng avatar Aug 15 '22 07:08 tobeng