本机调试OK,但是部署DOCKER之后报错:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
一直以为是数据驱动JAR问题,后来验证了两点:
1:spring boot 2.0之后,数据库连接SQL server用:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>4.0</version>
</dependency>
2:如果有外部JAR打包需要配置:
2.1:拷贝JAR到src-resources-lib文件夹下
2.2:补充POM.XML下配置
<dependency>
<groupId>com</groupId><!--随便填的-->
<artifactId>xcwebapi</artifactId><!--jar包名字-->
<version>1.1</version><!--版本号-->
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/JDK_1.8_corp.openapicalls.lib.webapi.jar</systemPath><!--路径-->
</dependency>
2.3:补充加载所有JAR
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<webResources>
<resource>
<directory>src/main/resources/lib/</directory>
<targetPath>WEB-INF/lib/</targetPath>
<includes>
<include>**/*.jar</include>
</includes>
</resource>
</webResources>
</configuration>
</plugin>
这两点验证没问题之后,部署K8 DOCKER还是报错。
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled. (ConditionEvaluationReportLoggingListener.java:136)
[2020-07-29 01:21:28,697][ERROR][main]
***************************
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).
(LoggingFailureAnalysisReporter.java:40)
[2020-07-29 01:21:28,700][ERROR][main] ERR-运行发生错误:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through 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/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 (XiechengApplication.java:30)
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'data

博客讲述了作者在将Spring Boot应用部署到Docker及Kubernetes环境后,遇到数据库连接DataSource配置问题。经过排查,发现并非JDBC驱动问题,而是应用未能正确读取环境特定的配置文件(如`application-prd2.yml`)。解决方案是确保Docker环境中应用使用`application.properties`文件进行配置,从而成功解决问题。
最低0.47元/天 解锁文章
779





