无法配置数据源:未指定 'url' 属性,无法配置嵌入式数据源。原因:发

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured. Reason: Fa

我正在使用 Spring 引导、Spring 云配置和 Docker 开发 Web 应用程序。有3个项目。一个是 springboot web,Spring Cloud Config Server 和 MySql Database。我使用了 Spring Boot Web with Themeleaf。它基本上是执行 CRUD 操作。在开发阶段它工作正常。但是当我将它部署在 Docker Spring-启动 webapp 容器 (springboot-thymeleaf-web-crud) 给我以下错误--


应用程序启动失败


描述:

无法配置数据源:'url'未指定属性,无法配置嵌入式数据源。

原因:无法确定合适的驱动程序class

操作:

考虑以下几点: 如果您需要嵌入式数据库(H2、HSQL 或 Derby),请将其放在 class 路径中。 如果您有要从特定配置文件加载的数据库设置,您可能需要激活它(当前没有任何配置文件处于活动状态)。

我在 Docker Compose 文件上创建了 3 个容器。

version: '3'

services: 
   springboot-thymeleaf-web-crud:
      image: numery/springboot-thymeleaf-web-crud:latest
      networks: 
         - employee-network
      ports:
         - 8080:8080 
      depends_on:
         - employee-database   
         - spring-cloud-config-server-employee

   spring-cloud-config-server-employee:
      image: numery/spring-cloud-config-server-employee:latest
      networks: 
         - employee-network
      ports:
         - 8888:8888

   employee-database:
      image: mysql:5
      networks:
         - employee-network
      environment:
         - MYSQL_ROOT_PASSWORD=rootpassword
         - MYSQL_DATABASE=employee_directory
      volumes:
         - /home/numery/Docker-Database/employee_directory:/var/lib/mysql

networks:
   employee-network:

SpringBoot Web Application.properties如下

spring.application.name=employee-service-mysql
server.port=8080
spring.cloud.config.uri=http://spring-cloud-config-server- 
employee:8888
spring.profiles.active=dev

Spring启动配置服务器提供以下属性--

spring.datasource.url: jdbc:mysql://employee- 
database:3306/employee_directory? 
useSSL=false&serverTimezone=UTC&useLegacyDatetimeCode=false
spring.datasource.username: root
spring.datasource.password: rootpassword
spring.datasource.validationQuery: SELECT 1
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.tomcat.max-wait: 20000
spring.tomcat.max-active: 50
spring.tomcat.max-idle: 20
spring.tomcat.min-idle: 15

spring.jpa.properties.hibernate.dialect: 
org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql: true
spring.jpa.format-sql: true
spring.jpa.database: MYSQL
spring.jpa.hibernate.ddl-auto: create

在这 3 个容器中 Mysql 和 Spring Cloud Config 容器工作正常。但是对于 Spring Boot Webapp(springboot-thymeleaf-web-crud) 正在通过给出上述错误退出。

注意:我在其他一些 SpringBoot Rest API 上使用相同的数据源(Spring Data JPA)配置。哪些工作正常。但这是我第一次使用 SpringBoot Web。我是否需要在数据源上明确定义任何配置。

请帮忙!!

数据源URL,即spring.datasource.url: jdbc:mysql://employee-可能有误。 或者应该将您的数据源 url 添加到您的 Application.properties 文件

url 的格式应为 jdbc:oracle:thin:@hostname:portNumber/schemaName

有关详细信息和说明,Not using the hostname and port in jdbc url

我执行以下任务来解决问题--

1) 我们应该 运行ning mysql 容器。

2) 检查 mysql ip 运行ning 的容器-- docker 检查 mysql 容器 | grep IP

3) 将 mysql 容器的 IP 添加到我的 springboot 网络应用程序--

"spring.datasource.url: jdbc:mysql://172.20.0.2:3306/employee_directory? 使用 SSL=false&serverTimezone =UTC&useLegacyDatetimeCode=假

4) 运行 mvn clean, mvn install 并构建 SpringBoot Web 镜像.

5) 如果我们关闭容器,我们需要执行 再次执行相同的任务。因为每次Mysql 容器获取不同的ip。