• SpringBoot项目将Nacos作为配置中心与注册中心,微服务启动失败


    SpringBoot项目将Nacos作为配置中心与注册中心,微服务启动失败

    1. 错误介绍

    1.1 错误内容

    022-06-28 12:00:40.819 ERROR [-,,,] 18920 --- [  restartedMain] o.s.b.web.embedded.tomcat.TomcatStarter  : Error starting Tomcat context. Exception: org.springframework.beans.factory.BeanCreationException. Message: Error creating bean with name 'servletEndpointRegistrar' defined in class path resource [org/springframework/boot/actuate/autoconfigure/endpoint/web/ServletEndpointManagementContextConfiguration$WebMvcServletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.endpoint.web.ServletEndpointRegistrar]: Factory method 'servletEndpointRegistrar' threw exception; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'healthEndpoint' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Unsatisfied dependency expressed through method 'healthEndpoint' parameter 1; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthIndicatorRegistry' defined in class path resource [org/springframework/boot/actuate/autoconfigure/health/HealthIndicatorAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthIndicatorRegistry]: Factory method 'healthIndicatorRegistry' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dbHealthIndicator' defined in class path resource [org/springframework/boot/actuate/autoconfigure/jdbc/DataSourceHealthIndicatorAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.boot.actuate.health.HealthIndicator]: Factory method 'dbHealthIndicator' threw exception; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
    2022-06-28 12:00:40.838  INFO [-,,,] 18920 --- [  restartedMain] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
    2022-06-28 12:00:40.848  WARN [-,,,] 18920 --- [  restartedMain] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] appears to have started a thread named [AsyncReporter{RabbitMQSender{addresses=[localhost:5672], queue=zipkin}}] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
     java.base@11.0.5/jdk.internal.misc.Unsafe.park(Native Method)
     java.base@11.0.5/java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:234)
     java.base@11.0.5/java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.awaitNanos(AbstractQueuedSynchronizer.java:2123)
     app//zipkin2.reporter.ByteBoundedQueue.drainTo(ByteBoundedQueue.java:81)
     app//zipkin2.reporter.AsyncReporter$BoundedAsyncReporter.flush(AsyncReporter.java:257)
     app//zipkin2.reporter.AsyncReporter$Builder$1.run(AsyncReporter.java:190)
    2022-06-28 12:00:40.849  WARN [-,,,] 18920 --- [  restartedMain] o.a.c.loader.WebappClassLoaderBase       : The web application [ROOT] appears to have started a thread named [spring.cloud.inetutils] but has failed to stop it. This is very likely to create a memory leak. Stack trace of thread:
     java.base@11.0.5/jdk.internal.misc.Unsafe.park(Native Method)
     java.base@11.0.5/java.util.concurrent.locks.LockSupport.park(LockSupport.java:194)
     java.base@11.0.5/java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2081)
     java.base@11.0.5/java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:433)
     java.base@11.0.5/java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1054)
     java.base@11.0.5/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1114)
     java.base@11.0.5/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
     java.base@11.0.5/java.lang.Thread.run(Thread.java:834)
    2022-06-28 12:00:40.851  WARN [-,,,] 18920 --- [  restartedMain] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
    2022-06-28 12:00:41.853  WARN [-,,,] 18920 --- [  restartedMain] z.r.AsyncReporter$BoundedAsyncReporter   : Timed out waiting for in-flight spans to send
    2022-06-28 12:00:45.842  INFO [-,,,] 18920 --- [  restartedMain] ConditionEvaluationReportLoggingListener : 
    
    Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
    2022-06-28 12:00:45.848 ERROR [-,,,] 18920 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 
    
    ***************************
    APPLICATION FAILED TO START
    ***************************
    
    Description:
    
    Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
    
    Reason: Failed to determine a suitable driver class
    
    
    Action:
    
    Consider the following:
    	If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
    	If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41

    1.2 错误情况重现

    当时我已经配置了Nacos的依赖,编写了配置文件,然后在 Linux 服务器上启动了 Nacos服务,当启动 SpringBoot 项目的时候,发生了报错

    • Nacos依赖
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>
    <dependency>
        <groupId>com.alibaba.nacos</groupId>
        <artifactId>nacos-client</artifactId>
    </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 配置文件内容(需要修改为 bootstrap.yml)
    spring:
      cloud:
        nacos:
          discovery:
            server-addr: 192.168.159.133:8848
          config:
            server-addr: 192.168.159.133:8848
            namespace: 4558f032-32ff-4e7b-b090-8dbfe9fc6a39
            group: PAGE_GROUP
            type: yaml
            ext-config[0]:
              data-id: ProductPageDemo-dev.yaml
              refresh: true # 启用自动更新
            ext-config[1]:
              data-id: ProductPageDemo-1.yaml
              refresh: true # 启用自动更新
            ext-config[2]:
              data-id: ProductPageDemo-2.yaml
              refresh: true # 启用自动更新
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • Nacos的web页面配置
      在这里插入图片描述
      在这里插入图片描述

    • 第一个配置文件
      在这里插入图片描述

    • 第二个配置文件
      在这里插入图片描述

    • 第三个配置文件
      在这里插入图片描述

    • controller 内容

    /**
     * @author 云梦归遥
     * @date 2022/6/25 11:57
     * @description
     */
    @RequestMapping("/pageTest")
    @RestController
    @RefreshScope // 手动刷新 Config 远程配置文件到本地缓存
    public class PageTestController {
    // 测试 Nacos 配置中心
        @Value("${dev1.dataId:no}")
        private String dev1_dataId;
        @Value("${dev2.dataId:no}")
        private String dev2_dataId;
        @Value("${dev3.dataId:no}")
        private String dev3_dataId;
    
        @RequestMapping("/dev1")
        public String getDev1(){
            return "/dev1 对应配置文件:" + dev1_dataId;
        }
    
        @RequestMapping("/dev2")
        public String getDev2(){
            return "/dev2 对应配置文件:" + dev2_dataId;
        }
    
        @RequestMapping("/dev3")
        public String getDev3(){
            return "/dev3 对应配置文件:" + dev3_dataId;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    2.解决问题

    2.1 修改依赖,配置文件

    • 依赖
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>
    <!-- Nacos 配置中心 -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
    </dependency>
    
    <!-- SpringCloud 的一整套依赖 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!--SCA -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>2.1.0.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 配置文件
    spring:
      cloud:
        nacos:
          discovery:
            server-addr: 192.168.159.133:8848
          config:
            server-addr: 192.168.159.133:8848
            namespace: 4558f032-32ff-4e7b-b090-8dbfe9fc6a39
            ext-config[0]:
              group: PAGE_GROUP
              type: yaml
              data-id: ProductPageDemo-dev.yaml
              refresh: true # 启用自动更新
            ext-config[1]:
              type: yaml
              group: PAGE_GROUP
              data-id: ProductPageDemo-1.yaml
              refresh: true # 启用自动更新
            ext-config[2]:
              type: yaml
              group: PAGE_GROUP
              data-id: ProductPageDemo-2.yaml
              refresh: true # 启用自动更新
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    在这里插入图片描述

    在这里插入图片描述

    3.在线修改配置文件

    3.1 在线修改配置文件并测试,配置是否可以自动刷新

    • 依赖如上,没有变化
    • 在线在Nacos的web页面修改第二个配置文件,进行测试,不需要重新启动任何服务
      在这里插入图片描述
    • 访问地址:http://127.0.0.1/page/pageTest/dev2
      在这里插入图片描述
    • 进过测试,Nacos可以保证配置在线修改完成之后,配置会自动刷新到微服务之中,不需要重新启动任何服务及项目,因为我们在配置文件中写好了 refresh: true 这个配置项

    4.总结

    • 项目依赖是用我上面的即可,不需要配置其他依赖内容
    • 本地微服务配置文件内容只配置 nacos 即可,如果要制定多个配置文件,需要分别指定 data-id,refresh,type,group等信息,否则也启动不起来,本人亲测
    • Nacos的web页面在线修改完配置文件,无需重启任何项目及服务,配置会自动刷新到微服务中,立即生效
  • 相关阅读:
    2084. 为订单类型为 0 的客户删除类型为 1 的订单
    谷粒商城 (四) --------- 项目结构创建 & 初始化数据库
    CRM软件系统价格不同的原因
    MySQL索引优化策略
    NX二次开发-NX+VS写代码设断点调试技巧
    route命令小结
    python---if语句
    RL 基础 | Value Iteration 的收敛性证明
    基于鹈鹕算法优化概率神经网络PNN的分类预测 - 附代码
    JavaWeb&JavaEE重要基础知识
  • 原文地址:https://blog.csdn.net/qq_45834685/article/details/125499822