微服务无法访问Docker Compose上的Config Server [英] Microservice can not reach to Config Server on Docker Compose
问题描述
我有一个Eureka服务器,配置服务器和Spring-Boot服务.我已经创建了一个docker compose文件,如下所示:
I have a Eureka Server, Config Server and Spring-Boot Service. I have created a docker compose file like this:
version: '3'
services:
user-service:
container_name: user-service-container
image: user-service:latest
build:
context: ./user-service
dockerfile: DockerFile
hostname: user-service
ports:
- "8082:8082"
depends_on:
- postgresdb
- eureka-service
- config-server
environment:
management.context-path : /userservice
hostName : user-service
EUREKA_HOST: eureka-service
EUREKA_PORT: 8761
config-server:
container_name: config-server-container
image: config-server:latest
build:
context: ./config-server
dockerfile: DockerFile
hostname: config-server
ports:
- "8081:8081"
depends_on:
- eureka-service
environment:
management.context-path : /config
hostName : config-server
SPRING_PROFILES_ACTIVE : registration-first
EUREKA_HOST: eureka-service
EUREKA_PORT: 8761
eureka-service:
container_name: eureka-service-container
image: eureka-service:latest
build:
context: ./eureka-service
dockerfile: DockerFile
hostname: eureka-service
ports:
- "8761:8761"
postgresdb:
container_name: postgres-db-container
image: postgres:10.5
hostname: postgres
ports:
- "5432:5432"
使用此配置,Config Server可以将自身注册到Eureka Server,但是User Service的配置部分抛出以下异常:
With this configuration Config Server can register itself to Eureka Server, however configuration part of the User Service throws below exception:
2018-09-07T18:53:17.869040200Z 2018-09-07 18:53:17.868 INFO 6 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:8888
2018-09-07T18:53:18.331651100Z 2018-09-07 18:53:18.331 INFO 6 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url - http://localhost:8888. Will be trying the next url if available
2018-09-07T18:53:18.332055700Z 2018-09-07 18:53:18.331 WARN 6 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://localhost:8888/user-service/registered-config-server,dev": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
2018-09-07T18:53:18.334406300Z 2018-09-07 18:53:18.334 INFO 6 --- [ main] c.k.d.u.UserServiceApplication : The following profiles are active: registered-config-server,dev
2018-09-07T18:53:18.371334800Z 2018-09-07 18:53:18.370 INFO 6 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@4d1c00d0: startup date [Fri Sep 07 18:53:18 UTC 2018]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@13969fbe
从此异常中,我了解到用户服务已连接到Euroka Server,但它尝试通过错误的URL连接到Config Server(即 http://localhost:8888 )
From this exception, I understand that User Service connects to Euroka Server but it tries to connect to Config Server via a wrong URL (i.e. http://localhost:8888 )
Euroka Server应用程序属性:
Euroka Server App Properties:
spring.application.name=eureka-server
server.port=8761
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
配置服务器应用程序属性:
Config Server App Properties:
spring:
cloud:
config:
server:
git :
uri: https://bitbucket.org/blabla
username: blabla
password: ***
discovery:
enabled: true
service-id: config-server
server:
port: 8081
---
spring:
application:
name: config-server
profiles: registration-first
eureka:
client:
registerWithEureka: true
fetchRegistry: true
serviceUrl:
defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/
instance:
appname: config-server
hostname: ${hostName}
statusPageUrlPath: ${management.context-path}/info
healthCheckUrlPath: ${management.context-path}/health
preferIpAddress: true
metadataMap:
instanceId: ${spring.application.name}:${server.port}
用户服务应用程序属性:
User Service Application Properties:
endpoints:
enabled: false
info:
enabled: true
health:
enabled: true
metrics:
enabled: true
refresh:
enabled: true
eureka:
client:
fetchRegistry: true
registerWithEureka: true
serviceUrl:
defaultZone: http://${eureka.host:localhost}:${eureka.port:8761}/eureka/
instance:
hostname: ${hostName}
statusPageUrlPath: ${management.context-path}/info
healthCheckUrlPath: ${management.context-path}/health
preferIpAddress: true
metadataMap:
instanceId: ${spring.application.name}:${server.port}
spring.jpa.database: POSTGRESQL
spring.datasource.platform: postgres
spring.jpa.show-sql: true
spring.jpa.hibernate.ddl-auto: update
spring.database.driverClassName: org.postgresql.Driver
spring.datasource.url: jdbc:postgresql://postgresdb:5432/postgres
spring.datasource.username: postgres
spring.datasource.password: postgres
用户服务Bootstrap.yml:
User Service Bootstrap.yml:
spring:
profiles:
active: registered-config-server,dev
application:
name: user-service
server.port: 8082
---
spring:
profiles: known-config-server
cloud:
config:
uri: http://localhost:8080
---
spring:
profiles: registered-config-server
cloud:
config:
discovery:
enabled: true
serviceId: config-server
但是当我在没有docker的情况下在本地PC上运行这些应用程序时,它们可以正常工作.
But when I run these applications on my local PC without docker they work properly.
有人有什么建议吗?
UserService Docker文件:
UserService Docker File:
FROM java:8
EXPOSE 8082
ADD /target/user-service-0.0.1-SNAPSHOT.jar user-service.jar
CMD java -jar user-service.jar
config-server docker文件
config-server docker file
FROM java:8
EXPOSE 8081
ADD /target/config-server-0.0.1-SNAPSHOT.jar config-server.jar
CMD java -jar config-server.jar
eureka服务器docker文件
eureka-server docker file
FROM java:8
EXPOSE 8761
ADD /target/eureka-service-0.0.1-SNAPSHOT.jar eureka-service.jar
CMD java -jar eureka-service.jar
推荐答案
在尝试从运行在Docker容器中的Spring Cloud Config Server中获取配置时,我也面临着同样的问题.我的Spring Boot应用程序尝试在 http://localhost:8888 上访问它,即使我在其中指定了Docker容器名称该应用程序的application.proprties
.
I am too facing the same issue when trying to fetch configurations from Spring Cloud Config Server running in a Docker container. My Spring Boot app tries to access it on http://localhost:8888 even though I have specified the docker container name in the app's application.proprties
.
下面是我的应用程序的 application.properties :
Below is my app's application.properties:
spring.application.name=stock-app
spring.profiles.active=default
spring.cloud.config.uri=http://configserver:8888
server.port=8301
作为一种解决方法,我在docker compose中将配置服务器URL作为参数传递.
As a workaround, I am passing config server URL as a parameter in my docker compose.
这是我的 docker-compose.yml :
version: '2'
services:
configserver:
image: himarg/config-server:0.0.1-SNAPSHOT
ports:
- "8888:8888"
environment:
ENCRYPT_KEY: "IMSYMMETRIC"
database:
image: himarg/stock-db:latest
ports:
- "3307:3306"
environment:
MYSQL_ROOT_PASSWORD: "Welcome1"
MYSQL_DATABASE: "inventory"
stockapp:
image: himarg/stock-app:0.0.1-SNAPSHOT
ports:
- "8301:8301"
environment:
PROFILE: "default"
CONFIGSERVER_URI: "http://configserver:8888"
CONFIGSERVER_PORT: "8888"
DATABASESERVER_PORT: "3306"
ENCRYPT_KEY: "IMSYMMETRIC"
然后,在我用于启动应用程序的shell脚本中使用此参数.
Then I use this parameter in my shell script that I use to start my app.
下面是 Dockerfile 和 script :
Dockerfile
FROM openjdk:8-jdk-alpine
RUN apk update && apk upgrade && apk add netcat-openbsd
RUN mkdir -p /usr/local/stock-app
ADD @project.build.finalName@.jar /usr/local/stock-app/
ADD run.sh run.sh
RUN chmod +x run.sh
CMD ./run.sh
run.sh
#!/bin/sh
echo "********************************************************"
echo "Waiting for the database server to start on port $DATABASESERVER_PORT"
echo "********************************************************"
while ! `nc -z database $DATABASESERVER_PORT`; do sleep 3; done
echo "******** Database Server has started "
echo "********************************************************"
echo "Waiting for the configuration server to start on port $CONFIGSERVER_PORT"
echo "********************************************************"
while ! `nc -z configserver $CONFIGSERVER_PORT`; do sleep 3; done
echo "******* Configuration Server has started"
echo "********************************************************"
echo "Starting stock-app "
echo "********************************************************"
java -Dspring.cloud.config.uri=$CONFIGSERVER_URI -jar /usr/local/stock-app/@project.build.finalName@.jar
进行了此更改之后,尽管我不确定这种解决方法是否符合您的要求,但我能够成功运行我的应用程序.
After making this change, I am able to successfully run my app, though I am not sure if this kind of workaround suits your requirement.
这篇关于微服务无法访问Docker Compose上的Config Server的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!