[英]How to build a Docker image for client service of Spring Cloud Config Server
I have built a Docker image for cloud config service.我为云配置服务构建了一个 Docker 映像。 I have another service
employee
for which I want to build a Docker image.我有另一个服务
employee
,我想为其构建 Docker 映像。 This client service will read data source configuration properties from the config server此客户端服务将从配置服务器读取数据源配置属性
I am able to connect the employee
service successfully with config service on localhost without Docker.我能够在没有 Docker 的情况下成功地将
employee
服务与本地主机上的配置服务连接起来。 The problem is only that I am not able to build a Docker image for the employee
service.问题只是我无法为
employee
服务构建 Docker 映像。
On running mvn package dockerfile:build
command for the employee service I get the following error在为员工服务运行
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
Below is the bootstrap.yml
file located at src/main/resources
directory下面是位于
src/main/resources
目录下的bootstrap.yml
文件
spring:
application:
name: employee
profiles:
active: dev
cloud:
config:
uri: http://configserver:8071
Config service's bootstrap.yml file配置服务的 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 file in config directory配置目录中的employee.properties 文件
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
employee-dev.properties file员工-dev.properties 文件
spring.datasource.url=jdbc:mysql://db:3306/employee?allowPublicKeyRetrieval=true&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
This is probably because the container of config server with hostname configserver
is not up.这可能是因为主机名为
configserver
的配置服务器容器未启动。 I am not sure what hostname I should provide.我不确定我应该提供什么主机名。 Even if I want to build the image of
employee
service by starting the container of config server, it won't be on configserver
host but on localhost
.即使我想通过启动配置服务器的容器来构建
employee
服务的图像,它也不会在configserver
主机上,而是在localhost
上。 This is probably why the build is failing?这可能是构建失败的原因?
I want to build the Docker image of employee service so that its container can be fired up using the docker-compose up
command我想构建员工服务的 Docker 映像,以便可以使用
docker-compose up
命令启动其容器
Edit: This question is similar but I already have the spring-cloud-starter-config
dependency added in my client service and it still gives me the same error on build编辑:这个问题很相似,但我已经在我的客户端服务中添加了
spring-cloud-starter-config
依赖项,它仍然给我同样的构建错误
Edit 2编辑 2
You don't have to separately build a Docker image for the client service at all!您根本不必为客户端服务单独构建 Docker 映像!
When I ran docker-compose up
command I noticed the following line in output当我运行
docker-compose up
命令时,我注意到 output 中的以下行
c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://configserver:8071
But I was still not sure if it was actually reading the data from the config server.但我仍然不确定它是否真的从配置服务器读取数据。 So I purposely added incorrect database credentials in config server properties file for my service and the service startup failed!
所以我故意在配置服务器属性文件中为我的服务添加了不正确的数据库凭据,并且服务启动失败! Now I am sure that my
employee
service is reading the data from the config server现在我确定我的
employee
服务正在从配置服务器读取数据
I am still not sure how can I achieve creating a new Docker image for my employee
service.我仍然不确定如何为我的
employee
服务创建一个新的 Docker 图像。
Did you try changing the URL param db to actual host of vhostname?您是否尝试将 URL 参数数据库更改为虚拟主机名的实际主机?
db =change it to IP addess or accessable load balancer URL db =将其更改为 IP 地址或可访问负载均衡器 URL
spring.datasource.url=jdbc:mysql://db:3306/employee?allowPublicKeyRetrieval=true&useSSL=false spring.datasource.url=jdbc:mysql://db:3306/employee?allowPublicKeyRetrieval=true&useSSL=false
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.