Why Register to Nacos Error In Cloud Server while Success In Local PC

171 Views Asked by At

In my application, I start spring cloud moudules based on spring cloud alibaba. Browse official document, I know I should use nacos version 2.2.0. But when I start this nacos server in my local PC, my APP could register and run success. But when start this server in my cloud server, It start error. I deduce it may be nacos only internal network could access, so I start a nginx app to proxy my IP address. In a result, I could access nacos dashboard on browser, but my application register failed yet.Then I package my app by maven and transport it to my cloud server, start it by java -jar. Howerer it start failed too because of register error. Besides, When I perform official register shell directly in cloud server, it returns ok;

// directly link nacos
[root@VM-4-16-centos ~]# curl -X POST 'http://127.0.0.1:9038/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'
ok
// link nacos by nginx proxy
[root@VM-4-16-centos ~]# curl -X POST 'http://localhost:9001/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'
ok

This is error message;

2023-10-22 13:29:46.802 ERROR 21464 --- [           main] c.a.c.n.registry.NacosServiceRegistry    : 
nacos registry, freshwater-gateway register failed...
NacosRegistration{nacosDiscoveryProperties=NacosDiscoveryProperties
{
   serverAddr='http://MY_NACOS_LOCATION:9001', 
   username='', 
   password='', 
   endpoint='', 
   namespace='', 
   watchDelay=30000, 
   logName='', 
   service='freshwater-gateway', 
   weight=1.0, 
   clusterName='DEFAULT', 
   group='DEFAULT_GROUP', 
   namingLoadCacheAtStart='false', 
   metadata = {
          IPv6=null, 
          preserved.register.source=SPRING_CLOUD
   }, 
   registerEnabled=true, 
   ip='192.168.217.205', 
   networkInterface='', 
   port=8080, 
   secure=false, 
   accessKey='', 
   secretKey='', 
   heartBeatInterval=null, 
   heartBeatTimeout=null, 
   ipDeleteTimeout=null, 
   instanceEnabled=true, 
   ephemeral=true, 
   failureToleranceEnabled=false
}, ipDeleteTimeout=null, 
   failFast=true}},

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

This is my APP parent pom.xml.

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <spring-boot.version>2.6.13</spring-boot.version>
    <spring-cloud.version>2021.0.5</spring-cloud.version>
    <spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
</properties>
<dependencyManagement>
    <dependencies>
        <!-- SpringCloud -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!-- SpringCloud Alibaba -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${spring-cloud-alibaba.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!-- SpringBoot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
</dependencyManagement>

Start nacos and nginx by docker-compose.

services:
  freshwater-nacos:
    container_name: freshwater-nacos
    image: nacos/nacos-server:v2.2.0
    build:
      context: ./nacos
    env_file:
      - nacos/env/nacos.env
    volumes:
      - /home/data/nacos/logs/:/home/nacos/logs
    ports:
      - "9038:8848"
      - "9048:9848"
      - "9049:9849"
    depends_on:
      - freshwater-mysql
    restart: always
  freshwater-mysql:
    container_name: mysql
    build:
      context: ./mysql
    image: example/mysql:8.0.30
    env_file:
      - ./mysql/env/mysql.env
    volumes:
      - /home/data/mysql:/var/lib/mysql
    ports:
      - "3306:3306"
    healthcheck:
      test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ]
      interval: 5s
      timeout: 10s
      retries: 10
  freshwater-nginx:
    container_name: freshwater-nginx
    image: nginx
    build:
      context: ./nginx
    ports:
      - "9001:9001"
    volumes:
      - ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf

nginx.conf

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen 9001;
        server_name MY_NACOS_LOCATION;

        location / {
            proxy_pass http://freshwater-nacos:8848;
        }
    }
}

nacos envs:

PREFER_HOST_MODE=hostname
MODE=standalone
SPRING_DATASOURCE_PLATFORM=mysql
MYSQL_SERVICE_HOST=mysql
MYSQL_SERVICE_DB_NAME=nacos_devtest
MYSQL_SERVICE_PORT=3306
MYSQL_SERVICE_USER=nacos
MYSQL_SERVICE_PASSWORD=nacos
MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
NACOS_AUTH_IDENTITY_KEY=2222
NACOS_AUTH_IDENTITY_VALUE=2xxx
NACOS_AUTH_TOKEN=SecretKey012345678901234567890123456789012345678901234567890123456789
NACOS_AUTH_ENABLE=false

My app bootstrap.yml. Due to my app run in physical machine,so use 127.0.0.1

# Tomcat
server: 
  port: 8080

# Spring
spring: 
  application:
    name: freshwater-gateway
  profiles:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:9038
      config:
        server-addr: 127.0.0.1:9038
        file-extension: yml

In my application, I start spring cloud moudules based on spring cloud alibaba. Browse official document, I know I should use nacos version 2.2.0. But when I start this nacos server in my local PC, my APP could register and run success. But when start this server in my cloud server, It start error. I deduce it may be nacos only internal network could access, so I start a nginx app to proxy my IP address. In a result, I could access nacos dashboard on browser, but my application register failed yet.Then I package my app by maven and transport it to my cloud server, start it by java -jar. Howerer it start failed too because of register error. Besides, When I perform official register shell directly in cloud server, it returns ok;

// directly link nacos
[root@VM-4-16-centos ~]# curl -X POST 'http://127.0.0.1:9038/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'
ok
// link nacos by nginx proxy
[root@VM-4-16-centos ~]# curl -X POST 'http://localhost:9001/nacos/v1/ns/instance?serviceName=nacos.naming.serviceName&ip=20.18.7.10&port=8080'
ok

This is error message;

2023-10-22 13:29:46.802 ERROR 21464 --- [           main] c.a.c.n.registry.NacosServiceRegistry    : 
nacos registry, freshwater-gateway register failed...
NacosRegistration{nacosDiscoveryProperties=NacosDiscoveryProperties
{
   serverAddr='http://MY_NACOS_LOCATION:9001', 
   username='', 
   password='', 
   endpoint='', 
   namespace='', 
   watchDelay=30000, 
   logName='', 
   service='freshwater-gateway', 
   weight=1.0, 
   clusterName='DEFAULT', 
   group='DEFAULT_GROUP', 
   namingLoadCacheAtStart='false', 
   metadata = {
          IPv6=null, 
          preserved.register.source=SPRING_CLOUD
   }, 
   registerEnabled=true, 
   ip='192.168.217.205', 
   networkInterface='', 
   port=8080, 
   secure=false, 
   accessKey='', 
   secretKey='', 
   heartBeatInterval=null, 
   heartBeatTimeout=null, 
   ipDeleteTimeout=null, 
   instanceEnabled=true, 
   ephemeral=true, 
   failureToleranceEnabled=false
}, ipDeleteTimeout=null, 
   failFast=true}},

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

This is my APP parent pom.xml.

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <spring-boot.version>2.6.13</spring-boot.version>
    <spring-cloud.version>2021.0.5</spring-cloud.version>
    <spring-cloud-alibaba.version>2021.0.5.0</spring-cloud-alibaba.version>
</properties>
<dependencyManagement>
    <dependencies>
        <!-- SpringCloud -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!-- SpringCloud Alibaba -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-alibaba-dependencies</artifactId>
            <version>${spring-cloud-alibaba.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

        <!-- SpringBoot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>${spring-boot.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
</dependencyManagement>

Start nacos and nginx by docker-compose.

services:
  freshwater-nacos:
    container_name: freshwater-nacos
    image: nacos/nacos-server:v2.2.0
    build:
      context: ./nacos
    env_file:
      - nacos/env/nacos.env
    volumes:
      - /home/data/nacos/logs/:/home/nacos/logs
    ports:
      - "9038:8848"
      - "9048:9848"
      - "9049:9849"
    depends_on:
      - freshwater-mysql
    restart: always
  freshwater-mysql:
    container_name: mysql
    build:
      context: ./mysql
    image: example/mysql:8.0.30
    env_file:
      - ./mysql/env/mysql.env
    volumes:
      - /home/data/mysql:/var/lib/mysql
    ports:
      - "3306:3306"
    healthcheck:
      test: [ "CMD", "mysqladmin" ,"ping", "-h", "localhost" ]
      interval: 5s
      timeout: 10s
      retries: 10
  freshwater-nginx:
    container_name: freshwater-nginx
    image: nginx
    build:
      context: ./nginx
    ports:
      - "9001:9001"
    volumes:
      - ./nginx/conf/nginx.conf:/etc/nginx/nginx.conf

nginx.conf

worker_processes  1;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen 9001;
        server_name MY_NACOS_LOCATION;

        location / {
            proxy_pass http://freshwater-nacos:8848;
        }
    }
}

nacos envs:

PREFER_HOST_MODE=hostname
MODE=standalone
SPRING_DATASOURCE_PLATFORM=mysql
MYSQL_SERVICE_HOST=mysql
MYSQL_SERVICE_DB_NAME=nacos_devtest
MYSQL_SERVICE_PORT=3306
MYSQL_SERVICE_USER=nacos
MYSQL_SERVICE_PASSWORD=nacos
MYSQL_SERVICE_DB_PARAM=characterEncoding=utf8&connectTimeout=1000&socketTimeout=3000&autoReconnect=true&useSSL=false&allowPublicKeyRetrieval=true
NACOS_AUTH_IDENTITY_KEY=2222
NACOS_AUTH_IDENTITY_VALUE=2xxx
NACOS_AUTH_TOKEN=SecretKey012345678901234567890123456789012345678901234567890123456789
NACOS_AUTH_ENABLE=false

My app bootstrap.yml. Due to my app run in physical machine,so use 127.0.0.1

# Tomcat
server: 
  port: 8080

# Spring
spring: 
  application:
    name: freshwater-gateway
  profiles:
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:9038
      config:
        server-addr: 127.0.0.1:9038
        file-extension: yml
0

There are 0 best solutions below