SpringCloud整合nacos啓動異常: Application failed to connect to Nacos server: "" Action: Please check you

一. 背景

在搭建SpringCloud整合nacos環境,服務有springcloud-product提供者和springcloud-order消費者,啓動服務報異常以下:html

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的,疑惑了半天,下面貼一下項目環境。java

在這裏插入圖片描述

二. 項目配置

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的配置git

<?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的功能,就把這個依賴去掉。github

  • 注意:
    SpringCloud項目中若是引用了nacos-discovery或者nacos-config,都無需再像以前使用Eureka那樣加入@EnableDiscoveryClient註解啓用其服務的註冊與發現相關功能,能夠省略掉。web

在這裏插入圖片描述

四. 解決方式

1. 第一種方式

在pom.xml中去掉下面這個依賴spring

在這裏插入圖片描述

2. 第二種方式

新建bootstrap.yml,在bootstrap.yml中配置nacos-config的相關屬性,這個屬性是禁用Nacos Config自動配置,設置spring.cloud.nacos.config.enabled = false禁用Spring Cloud Nacos配置自動配置。apache

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

在這裏插入圖片描述

五. 參考文檔阿里巴巴文檔

Spring Cloud Alibaba參考文檔: Spring Cloud Alibaba參考文檔bootstrap

在這裏插入圖片描述

本文同步分享在 博客「Thinkingcao」(CSDN)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。app

相關文章
相關標籤/搜索