【SpringBoot项目启动报错】Failed to configure a DataSource

11 篇文章 0 订阅
3 篇文章 0 订阅
这篇博客记录了在Spring Boot应用启动时遇到的错误,问题源于DataSource的配置不正确。报错信息表明无法确定合适的数据库驱动类,并提示缺少`url`属性配置。解决方案包括检查Nacos配置,添加缺失的配置信息,如数据库URL、用户名和密码,以及调整数据源Bean的名称。修复这些问题后,应用应能成功启动。
摘要由CSDN通过智能技术生成

1 报错信息

2022-03-08 11:46:30.050 [main] WARN  o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - [refresh,591] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clickHouseServiceImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'clusterClickHouseTemplate' defined in class path resource [com/idongyou/dongxin/common/datasource/config/JdbcDataSourceConfig.class]: Unsatisfied dependency expressed through method 'clusterClickHouseTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clusterClickHouseDataSource' defined in class path resource [com/idongyou/dongxin/common/datasource/config/JdbcDataSourceConfig.class]: Invocation of init method failed; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
2022-03-08 11:46:30.052 [main] INFO  c.a.d.p.DruidDataSource - [close,2071] - {dataSource-0} closing ...
2022-03-08 11:46:30.068 [main] INFO  o.a.c.c.StandardService - [log,173] - Stopping service [Tomcat]
2022-03-08 11:46:30.108 [main] INFO  r.y.c.ClickHouseDriver - [<clinit>,49] - Driver registered
2022-03-08 11:46:30.139 [main] ERROR o.s.b.d.LoggingFailureAnalysisReporter - [report,40] - 

***************************
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 (the profiles dev are currently active).

2022-03-08 11:46:30.140 [Thread-5] WARN  c.a.n.c.n.NotifyCenter - [shutdown,136] - [NotifyCenter] Start destroying Publisher
2022-03-08 11:46:30.140 [Thread-1] WARN  c.a.n.c.h.HttpClientBeanHolder - [shutdown,108] - [HttpClientBeanHolder] Start destroying common HttpClient
2022-03-08 11:46:30.140 [Thread-5] WARN  c.a.n.c.n.NotifyCenter - [shutdown,153] - [NotifyCenter] Destruction of the end
2022-03-08 11:46:30.140 [Thread-1] WARN  c.a.n.c.h.HttpClientBeanHolder - [shutdown,114] - [HttpClientBeanHolder] Destruction of the end

2 解决方法

1、检查nacos配置,添加对应的配置信息

      clusterclickhouse:
        driver-class-name: ru.yandex.clickhouse.ClickHouseDriver
        url: jdbc:clickhouse://ip:port/dx_data
        username: dev
        password: dev
        validationQuery: SELECT 1

2、检查数据源Bean名称,不能过长
修改前:

    @Bean(name = "clusterClickHouseTemplate")
    public JdbcTemplate clusterClickHouseTemplate(@Qualifier("clusterClickHouseDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "clusterClickHouseDataSource")
    @Qualifier("clusterClickHouseDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.druid.clusterclickhouse")
    public DataSource clusterClickHouseDataSource() {
        return DruidDataSourceBuilder.create().build();
    }

修改后:

    @Bean(name = "clusterClickHouseTemplate")
    public JdbcTemplate clusterClickHouseTemplate(@Qualifier("clusterChDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }

    @Bean(name = "clusterChDataSource")
    @Qualifier("clusterChDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.druid.clusterclickhouse")
    public DataSource clusterClickHouseDataSource() {
        return DruidDataSourceBuilder.create().build();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值