欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

SpringCloud整合nacos启动异常: Application failed to connect to Nacos server: "" Action: Please check you

程序员文章站 2024-03-15 23:30:18
...

一. 背景

在搭建SpringCloud整合nacos环境,服务有springcloud-product提供者和springcloud-order消费者,启动服务报异常如下:

2020-04-29 18:30:32.183  INFO 8228 --- [           main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$266b225b] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.6.RELEASE)

2020-04-29 18:30:33.078 ERROR 8228 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Application failed to connect to Nacos server: ""

Action:

Please check your Nacos server config


Process finished with exit code 1

检查项目配置,发现配置都是OK的,疑惑了半天,下面贴一下项目环境。

SpringCloud整合nacos启动异常: Application failed to connect to Nacos server: "" Action: Please check you

二. 项目配置

1. 父工程依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>springcloud-ribbon-hystrix</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>springcloud-product</module>
        <module>springcloud-order</module>
    </modules>

    <properties>
        <project.build.sourceEncoding> UTF-8 </project.build.sourceEncoding>
        <project.reporting.outputEncoding> UTF-8 </project.reporting.outputEncoding>
        <java.version> 1.8 </java.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <!--cloud -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.SR2</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- SpringBoot的依赖配置 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>2.1.6.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>

            <!-- Alibaba-Cloud -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

2. springcloud-product工程

生产者springcloud-product和消费者springcloud-order依赖信息和配置基本一样,这里就贴springcloud-product的配置

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>springcloud-ribbon-hystrix</artifactId>
        <groupId>org.example</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springcloud-product</artifactId>

    <properties>
        <project.build.sourceEncoding> UTF-8 </project.build.sourceEncoding>
        <project.reporting.outputEncoding> UTF-8 </project.reporting.outputEncoding>
        <java.version> 1.8 </java.version>
    </properties>

    <dependencies>
        <!-- web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- actuator -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!-- Alibaba-nacos服务发现-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!-- Alibaba-nacos配置中心-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

        <!-- nacos-client -->
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>1.2.0</version>
        </dependency>

        <!-- hystrix断路器 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>

        <!-- openfeign客户端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

        <!-- lombok -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
</project>

3. springcloud-product工程

server:
  port: 8013

spring:
  application:
    name: nacos-product
  cloud:
    nacos:
      discovery:
        enabled: true
        server-addr: 130.252.102.241:8848
        register-enabled: true

4. 启动类ProductApp

package com.thinkingcao.product;

import org.springframework.boot.SpringApplication;
import org.springframework.cloud.client.SpringCloudApplication;

/**
 * @desc:  商品服务-提供者
 * @author: cao_wencao
 * @date: 2020-04-29 17:48
 */
@SpringCloudApplication
public class ProductApp {
    public static void main(String[] args) {
        SpringApplication.run(ProductApp.class, args);
    }
}

三. 原因总结

  • application.yml文件中我只配置了服务的注册与发现,并没有使用nacos的配置中心功能,但是我引用了spring-cloud-starter-alibaba-nacos-config这个包,是用来做配置中心的,这里要知道nacos为我们做了许多工作,在程序启动时,会动态的根据引入的依赖去加载初始对应的配置,无论是注册中心还是配置中心,所以我们既然没用到config的功能,就把这个依赖去掉。

  • 注意:
    SpringCloud项目中如果引用了nacos-discovery或者nacos-config,都无需再像之前使用Eureka那样加入@EnableDiscoveryClient注解启用其服务的注册与发现相关功能,可以省略掉。

SpringCloud整合nacos启动异常: Application failed to connect to Nacos server: "" Action: Please check you

四. 解决方式

1. 第一种方式

在pom.xml中去掉下面这个依赖

SpringCloud整合nacos启动异常: Application failed to connect to Nacos server: "" Action: Please check you

2. 第二种方式

新建bootstrap.yml,在bootstrap.yml中配置nacos-config的相关属性,这个属性是禁用Nacos Config自动配置,设置spring.cloud.nacos.config.enabled = false禁用Spring Cloud Nacos配置自动配置。

spring:
  cloud:
    nacos:
      config:
        enabled: false #禁用nacos-config自动配置功能

SpringCloud整合nacos启动异常: Application failed to connect to Nacos server: "" Action: Please check you

五. 参考文档阿里巴巴文档

Spring Cloud Alibaba参考文档: Spring Cloud Alibaba参考文档

SpringCloud整合nacos启动异常: Application failed to connect to Nacos server: "" Action: Please check you