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:
 [email protected]/jdk.internal.misc.Unsafe.park(Native Method)
 [email protected]/java.util.concurrent.locks.LockSupport.parkNanos(LockSupport.java:234)
 [email protected]/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:
 [email protected]/jdk.internal.misc.Unsafe.park(Native Method)
 [email protected]/java.util.concurrent.locks.LockSupport.park(LockSupport.java:194)
 [email protected]/java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2081)
 [email protected]/java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:433)
 [email protected]/java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1054)
 [email protected]/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1114)
 [email protected]/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
 [email protected]/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 错误情况重现

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

  • Nacos依赖
<dependency>
    <groupId>com.alibaba.cloudgroupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
    <groupId>com.alibaba.cloudgroupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>
<dependency>
    <groupId>com.alibaba.nacosgroupId>
    <artifactId>nacos-clientartifactId>
dependency>
  • 配置文件内容(需要修改为 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 # 启用自动更新
  • Nacos的web页面配置
    SpringBoot项目将Nacos作为配置中心与注册中心,微服务启动失败_第1张图片
    SpringBoot项目将Nacos作为配置中心与注册中心,微服务启动失败_第2张图片

  • 第一个配置文件
    SpringBoot项目将Nacos作为配置中心与注册中心,微服务启动失败_第3张图片

  • 第二个配置文件
    SpringBoot项目将Nacos作为配置中心与注册中心,微服务启动失败_第4张图片

  • 第三个配置文件
    SpringBoot项目将Nacos作为配置中心与注册中心,微服务启动失败_第5张图片

  • 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;
    }
}

2.解决问题

2.1 修改依赖,配置文件

  • 依赖
<dependency>
    <groupId>com.alibaba.cloudgroupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>

<dependency>
    <groupId>com.alibaba.cloudgroupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-configartifactId>
dependency>


<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloudgroupId>
            <artifactId>spring-cloud-dependenciesartifactId>
            <version>Greenwich.RELEASEversion>
            <type>pomtype>
            <scope>importscope>
        dependency>
        
        <dependency>
            <groupId>com.alibaba.cloudgroupId>
            <artifactId>spring-cloud-alibaba-dependenciesartifactId>
            <version>2.1.0.RELEASEversion>
            <type>pomtype>
            <scope>importscope>
        dependency>
    dependencies>
dependencyManagement>
  • 配置文件
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 # 启用自动更新
  • 再次启动为服务,发现可以正常启动
  • 访问地址:http://127.0.0.1/page/pageTest/dev1
    SpringBoot项目将Nacos作为配置中心与注册中心,微服务启动失败_第6张图片
  • 访问地址:http://127.0.0.1/page/pageTest/dev2

SpringBoot项目将Nacos作为配置中心与注册中心,微服务启动失败_第7张图片

  • 访问地址:http://127.0.0.1/page/pageTest/dev3

SpringBoot项目将Nacos作为配置中心与注册中心,微服务启动失败_第8张图片

3.在线修改配置文件

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

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

4.总结

  • 项目依赖是用我上面的即可,不需要配置其他依赖内容
  • 本地微服务配置文件内容只配置 nacos 即可,如果要制定多个配置文件,需要分别指定 data-id,refresh,type,group等信息,否则也启动不起来,本人亲测
  • Nacos的web页面在线修改完配置文件,无需重启任何项目及服务,配置会自动刷新到微服务中,立即生效

你可能感兴趣的:(开发过程报错集锦,spring,boot,微服务,java,nacos,spring,cloud)