Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem that the service cannot be registered in Nacos cluster mode

2025-05-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to solve the problem that services cannot register in Nacos cluster mode". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The service cannot be registered in Nacos cluster mode 1. Environment

1. Server: three Tencent Cloud CentOS 7.264-bit machines

2. JDK1.8.0_151 (because nacos must be above 1.8). There will be startup problems below 1.8, so be sure to upgrade.

3. Nacos version 0.8.0

2. Build the nacos server

It can be built basically according to the official website documents, and modified according to the official website documents.

Cluster.conf, add the IP of three machines (172.XX.XX.14:8848, 172.XX.XX.7:8848, 172.XX.XX.14:8848), start the discovery error report

Java.lang.IllegalStateException: unable to find local peer: 118.XX.XX.52:8848, all peers: [172.XX.XX.14:8848, 172.XX.XX.7:8848, 172.XX.XX.14:8848]

At com.alibaba.nacos.naming.raft.PeerSet.local (PeerSet.java:191)

At com.alibaba.nacos.naming.monitor.PerformanceLoggerThread.collectmetrics (PerformanceLoggerThread.java:114)

At sun.reflect.GeneratedMethodAccessor74.invoke (Unknown Source)

At sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)

At java.lang.reflect.Method.invoke (Method.java:497)

Ignore it, add the wrong IP first, and start successfully.

III. Service registration

At this point, the service application is launched to register the service, and the following error is reported:

Java.lang.IllegalStateException: failed to req API:/nacos/v1/ns/instance after all servers ([172.XX.XX.23:80]) tried

At com.alibaba.nacos.client.naming.net.NamingProxy.reqAPI (NamingProxy.java:335)

At com.alibaba.nacos.client.naming.net.NamingProxy.reqAPI (NamingProxy.java:267)

At com.alibaba.nacos.client.naming.net.NamingProxy.registerService (NamingProxy.java:167)

At com.alibaba.nacos.client.naming.NacosNamingService.registerInstance (NacosNamingService.java:170)

At org.springframework.cloud.alibaba.nacos.registry.NacosServiceRegistry.register (NacosServiceRegistry.java:56)

At org.springframework.cloud.alibaba.nacos.registry.NacosServiceRegistry.register (NacosServiceRegistry.java:29)

At org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.register (AbstractAutoServiceRegistration.java:209)

At org.springframework.cloud.alibaba.nacos.registry.NacosAutoServiceRegistration.register (NacosAutoServiceRegistration.java:75)

At org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.start (AbstractAutoServiceRegistration.java:108)

At org.springframework.cloud.client.serviceregistry.AbstractAutoServiceRegistration.bind (AbstractAutoServiceRegistration.java:73)

At sun.reflect.NativeMethodAccessorImpl.invoke0 (NativeMethod)

At sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)

At sun.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)

At java.lang.reflect.Method.invoke (Method.java:498)

The server application failed to start the call service registration, but the three machines in the nacos service backend all started successfully, and the backend can enter normally, and access / nacos/v1/ns/instance through URL can also be accessed normally. There is no solution. Debug the code.

IV. Problem tracking

According to the debugging findings, when we came to no leader now, we suspected that the nacos registry did not start successfully, and the 118.XX.XX.52:8848 added in cluster.conf did not solve the real problem, so don't be lazy and find the real cause of the problem so as not to cover it up.

Loggers.RAFT.info ("I'm not leader, will proxy to leader."); if (RaftCore.getLeader () = = null) {throw new IllegalArgumentException ("no leader now.");}

This warning is indeed printed in the back-end log, and the log is in naming-raft.log.

Then start debugging the nacos service code. Through debugging, it is found that the nacos registry will obtain the local IP address through InetAddress.getLocalHost () in the NetUtils class at startup, but this IP (118.XX.XX.52:8848) is not the native IP, which leads to the incorrect native IP and the registry does not load successfully.

Flip through the document:

Underlying reason: in most Linux operating systems, the hostname is looked up with the configuration in / etc/hosts, while the InetAddress.java of Java calls InetAddressImpl.java 's public native String getLocalHostName () throws UnknownHostException To get the local host name, this method of Java is native, which is an implementation of the local system. At this time, the local IP is obtained according to the machine name in the local / etc/hostname file. However, this IP is not the intranet IP of this machine, so where did this IP come from?

When the relevant configuration of Tencent Cloud was found, the IP was not found, and it was not found on the machine. Later, the IP reported above was obtained through the native name of PING. It is speculated that the IP is uniformly assigned by Tencent Cloud, and the result of the PING of the produced machine is the same.

V. settlement

1. Set the local name: / etc/hostname mName preferably not in the form of an IP address. If it is written, it must be in the full IP form of the local machine (not only half of it). If there is, there is no need to set it.

two。 Add a line of native IP mName to / etc/hosts

3. Restart the registry, and the registry starts successfully and finds that the error of java.lang.IllegalStateException: unable to find local peer: 118.XX.XX.52:8848 is not reported, and through debugging, it is found that IP is indeed correct.

4. Modify the cluster.conf to remove 118.XX.XX.52:8848, restart the service application to register, and the service registration is successful

Analysis of the reasons why the service is not registered with nacos

Unable to register because there is no version number added.

This is the end of org.springframework.cloud spring-cloud-starter-alibaba-nacos-discovery 0.2.1.RELEASE 's "how to solve the problem that services cannot register in Nacos cluster mode". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report