请问老师加上Druid为什么启动的时候报错呢?
来源:9-4 Spring Cloud Eureka服务注册和发现-2

慕虎8754231
2020-04-01
2020-04-01 22:04:07.976 ERROR 1832 --- [ main] 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$WebMvcSer
vletEndpointManagementContextConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springfr
amework.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 thro
ugh method 'healthEndpoint' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'healthContributorRegistry' defined in class path resource
[org/springframework/boot/actuate/autoconfigure/health/HealthEndpointConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationExcep
tion: Failed to instantiate [org.springframework.boot.actuate.health.HealthContributorRegistry]: Factory method 'healthContributorRegistry' threw exception; nested exception is org.springframework.beans.fac
tory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.actuate.autoconfigure.jdbc.DataSourceHealthContributorAutoConfiguration': Unsatisfied dependency expressed throug
h constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/au
toconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zax
xer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to det
ermine a suitable driver class
2020-04-01 22:04:07.996 INFO 1832 --- [ main] o.apache.catalina.core.StandardService : Stopping service [Tomcat]
2020-04-01 22:04:08.049 WARN 1832 --- [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.cont
ext.ApplicationContextException: Unable to start web server; nested exception is org.springframework.boot.web.server.WebServerException: Unable to start embedded Tomcat
2020-04-01 22:04:08.061 INFO 1832 --- [ main] ConditionEvaluationReportLoggingListener :
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-04-01 22:04:08.069 ERROR 1832 --- [ main] 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).
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 13.153 s
[INFO] Finished at: 2020-04-01T22:04:08+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:2.2.6.RELEASE:run (default-cli) on project user: Application finished with exit code: 1 -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
配置如下:
#配置druid連接池(注意理解JDBC与druid的关系
spring.datasource.druid.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.druid.url = jdbc:mysql://127.0.0.1:3306/houses?useUnicode=true&characterEncoding=utf-8&autoReconnect=true&allowMultiQueries=true&serverTimezone=GMT%2b8
spring.datasource.druid.username = root
spring.datasource.druid.password = root
#最大連接數
spring.datasource.druid.maxActive=30
#最小連接數
spring.datasource.druid.minIdle=5
#最大連接等大時間
spring.datasource.druid.maxWait=10000
#解決mysql8小時問題
spring.datasource.druid.validationQuery=SELECT 'x'
#空閒連接檢查時間間隔
spring.datasource.druid.timeBetweenLogStatsMillis=60000
#空閒連接的最小空閒連接時間
spring.datasource.druid.minEvictableIdleTimeMillis=300000
依赖如下:
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
Druid的配置类如下:
public class DruidConfig {
@Bean(initMethod = "init",destroyMethod = "close")
@ConfigurationProperties(prefix = "spring.datasource.druid")
public DruidDataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setProxyFilters(Lists.newArrayList(statFilter()));
return dataSource;
}
@Bean
public Filter statFilter(){
StatFilter filter = new StatFilter();
//定义慢日志的时间,即1毫秒为慢日志
filter.setSlowSqlMillis(1);
//定义是否打印慢日志
filter.setLogSlowSql(true);
//定义是否需要将慢日志合并起来
filter.setMergeSql(true);
return filter;
}
@Bean
public ServletRegistrationBean servletRegistrationBean(){
return new ServletRegistrationBean(new StatViewServlet(),"/druid/*");
}
}
写回答
1回答
-
格鲁
2020-04-06
是不是mysql驱动没有配
00
相似问题