foreword

The source of the material for this article is that friends learn the pit that nacos2.1.1 stepped on. Serve directly

Pit point 1: the port is occupied

Because it is for learning and use, my friend built a nacos pseudo-cluster on the physical machine, that is, the IPs are the same, and the ports are 8848, 8847, and 8849. However, after starting the nacos server, one starts normally, and the other two report that the port is occupied

The reason for this situation is explained on the official website


From the official website, we can easily know that this port is occupied mainly because of grpc, because the generation method of other ports is generated by the main port +1000 and the main port +1001.

Solution

Do not use adjacent numbers for the ports of the cluster, and try to increase the step size as much as possible. For example, set it to 7777, 8888, 9999, etc.

Pit 2: com.alibaba.nacos.api.exception.NacosException: Client not connected, current status: STARTING exception occurs when the microservice project starts

This problem occurs when the nacos address configured by a friend in the project is the nginx address. The configuration example is as follows

 spring.cloud.nacos.discovery.server-addr=nginx ip

At the beginning, the configuration example of friend nginx is as follows

 upstream nacos-cluster { 
        server 127.0.0.1:7777;
        server 127.0.0.1:8888;
        server 127.0.0.1:9999;
   }
    server {
        listen       80;
        server_name  localhost;
        location / {
            proxy_pass http://nacos-cluster;
        }
        }

It is no problem for the browser to access through nginx, but the nacos service address is configured as nginx ip in the project and reported

 com.alibaba.nacos.api.exception.NacosException: Client not connected, current status:STARTING

This abnormal information, later friends check the information, it is written on the official website


So he changed the forwarding method to TCP. His nginx version is 1.9+ and above, which supports TCP proxy by default, and there is no need to install additional stream modules. An example of configuring TCP with nginx is as follows

 stream {
    upstream nacos-cluster-grpc{
        # nacos2版本,grpc端口与要比主端口多1000,主端口为7777、8888、9999
        server 127.0.0.1:8777;
        server 127.0.0.1:9888;
        server 127.0.0.1:10999; 
    }
    server{
       listen 9848;
        proxy_pass nacos-cluster-grpc;
    }
}

When a friend configures nginx tcp proxy forwarding, use the telnet command

 telnet 127.0.0.1 9848

See if it can be forwarded to the nacos server normally. After verification, the network can be connected. Then the friend fills in the following address in the nacos configuration of the microservice project

 spring.cloud.nacos.discovery.server-addr=127.0.0.1:9848 #nginx代理tcp的地址

I thought everything was going well, but as soon as the project started, I still reported

 com.alibaba.nacos.api.exception.NacosException: Client not connected, current status:STARTING

So my friend was stunned, what happened? Come and talk to me

In fact, the FAQ on the nacos official website has mentioned the corresponding problem-solving ideas.


Because the proxy tcp port we configured in nginx is 9848, this port can be regarded as the port of grpc, because the port of grpc = nacos main port + 1000, so we can get this formula, the main port of nacos is = 9848 - 1000 = 8848, and our microservice project configures the nacos port, in fact, the configuration is the main port, so in fact our configuration should be written as

 spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848 #nginx代理tcp端口 - 1000

After configuring this, the registration was successful. This idea is brought to us by the official website, but as a programmer who is a bit pursuing, we should not just satisfy this, we can get the answer directly according to the exception reported by the console

The abnormal troubleshooting process is omitted, and the key problem-solving code is directly posted

1. First parse the core code of the main port number

location at

 com.alibaba.nacos.common.remote.client.RpcClient#resolveServerInfo
 ServerInfo private RpcClient.ServerInfo resolveServerInfo(String serverAddress) {
        Matcher matcher = EXCLUDE_PROTOCOL_PATTERN.matcher(serverAddress);
        if (matcher.find()) {
            serverAddress = matcher.group(1);
        }

        String[] ipPortTuple = serverAddress.split(":", 2);
        int defaultPort = Integer.parseInt(System.getProperty("nacos.server.port", "8848"));
        String serverPort = (String)CollectionUtils.getOrDefault(ipPortTuple, 1, Integer.toString(defaultPort));
        return new RpcClient.ServerInfo(ipPortTuple[0], NumberUtils.toInt(serverPort, defaultPort));
    }
2. Second, set the core code of the grpc port

The location is:

 com.alibaba.nacos.common.remote.client.grpc.GrpcClient#connectToServer


The port setting is in the red part of the screenshot, and then from this.rpcPortOffset() we can find

 public int rpcPortOffset() {
        return Integer.parseInt(System.getProperty("nacos.server.grpc.port.offset", String.valueOf(Constants.SDK_GRPC_PORT_DEFAULT_OFFSET)));
    }

This offset can be modified through nacos.server.grpc.port.offset, and the default is 1000 if it is not modified. So following the source code, we can come up with another solution. In the nacos configuration of the microservice, still fill in the tcp address of the proxy nginx, example

 spring.cloud.nacos.discovery.server-addr=127.0.0.1:9848 #nginx代理tcp的地址

When starting at the same time, add

 -Dnacos.server.grpc.port.offset=0

Or hardcoded in the main startup class

 System.setProperty("nacos.server.grpc.port.offset","0");

Note: The important details here are: GRPC port = main port + grpc port offset, the calculated port value should be equal to the nginx proxy tcp port value.

Summarize

Because my friend is using the latest version of nacos2, the search engine for some problems is not so easy to find answers. Therefore, when encountering such a problem, the best way to solve the problem is the official website and the corresponding github, as well as the source code tracking.

the back one
Client not connected, current status: STARTING, in fact, there is another solution, which is to lower the client version to version 1.x, because this problem is essentially the problem of not being able to connect to grpc, so we can use http directly without grpc. Well, the 2.x server version supports both http and grpc, so the client version is adjusted to 1.x, and he interacts with the server in the form of http. But it is not recommended to do this, because you have upgraded 2.x, on the one hand, it is for high performance. If you lower the version, it is better to use 1.x directly.

There are also the two solutions in the text, I personally recommend not to change the offset, just calculate it directly through the main port + 1000

appendix

Many of the nacos2 problem-solving ideas in this article are from this article on the official website

https://nacos.io/en-us/docs/2.0.0-compatibility.html


linyb极客之路
336 声望193 粉丝