繁体   English   中英

如何为 Spring 云配置服务器的客户端服务构建 Docker 映像

[英]How to build a Docker image for client service of Spring Cloud Config Server

我为云配置服务构建了一个 Docker 映像。 我有另一个服务employee ,我想为其构建 Docker 映像。 此客户端服务将从配置服务器读取数据源配置属性

我能够在没有 Docker 的情况下成功地将employee服务与本地主机上的配置服务连接起来。 问题只是我无法为employee服务构建 Docker 映像。

在为员工服务运行mvn package dockerfile:build命令时,我收到以下错误

***************************
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

下面是位于src/main/resources目录下的bootstrap.yml文件

spring:
  application:
    name: employee
  profiles:
    active: dev
  cloud:
    config:
      uri: http://configserver:8071

配置服务的 bootstrap.yml 文件

spring:
  application:
    name: config-server
  profiles:
    active: native,dev
    
  cloud:
    config:
      server:
        #Local configuration: This locations can either of classpath or locations in the filesystem.
        native:
          #Reads from a specific filesystem folder 
          search-locations: classpath:/config
    
server:
  port: 8071

配置目录中的employee.properties 文件

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

员工-dev.properties 文件

spring.datasource.url=jdbc:mysql://db:3306/employee?allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root

这可能是因为主机名为configserver的配置服务器容器未启动。 我不确定我应该提供什么主机名。 即使我想通过启动配置服务器的容器来构建employee服务的图像,它也不会在configserver主机上,而是在localhost上。 这可能是构建失败的原因?

我想构建员工服务的 Docker 映像,以便可以使用docker-compose up命令启动其容器

编辑:这个问题很相似,但我已经在我的客户端服务中添加了spring-cloud-starter-config依赖项,它仍然给我同样的构建错误

编辑 2

您根本不必为客户端服务单独构建 Docker 映像!

当我运行docker-compose up命令时,我注意到 output 中的以下行

c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://configserver:8071

但我仍然不确定它是否真的从配置服务器读取数据。 所以我故意在配置服务器属性文件中为我的服务添加了不正确的数据库凭据,并且服务启动失败! 现在我确定我的employee服务正在从配置服务器读取数据

我仍然不确定如何为我的employee服务创建一个新的 Docker 图像。

您是否尝试将 URL 参数数据库更改为虚拟主机名的实际主机?

db =将其更改为 IP 地址或可访问负载均衡器 URL

spring.datasource.url=jdbc:mysql://db:3306/employee?allowPublicKeyRetrieval=true&useSSL=false

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM