【发布时间】:2019-03-07 14:15:20
【问题描述】:
我目前正在试验基于 Spring Boot 的微服务并开始使用 docker,但我遇到了障碍。
基本上我要做的是将 2 个小服务容器化:一个 spring 云配置服务和一个 spring 云 eureka 服务(发现服务)。 eureka 服务从 config 服务中获取其配置。
这两个服务都是独立的项目,都有自己的 Dockerfile:
Dockerfile-cloud-config-service:
FROM openjdk:10.0.2-13-jre-sid
ENV APP_FILE cloud-config-service.jar
ENV APP_HOME /usr/apps
EXPOSE 8888
COPY target/$APP_FILE $APP_HOME/
WORKDIR $APP_HOME
ENTRYPOINT ["sh", "-c"]
CMD ["exec java -jar $APP_FILE"]
Dockerfile-discovery-service:
FROM openjdk:10.0.2-13-jre-sid
ENV APP_FILE discovery-service.jar
ENV APP_HOME /usr/apps
EXPOSE 8761
COPY target/$APP_FILE $APP_HOME/
WORKDIR $APP_HOME
ENTRYPOINT ["sh", "-c"]
CMD ["exec java -jar $APP_FILE"]
使用 docker-compose 我正在尝试使用以下 docker-compose.yml 将它们联系在一起:
version: '3.7'
services:
cloud-config-service:
container_name: cloud-config-service
build:
context: cloud-config-service
dockerfile: Dockerfile-cloud-config-service
image: cloud-config-service:latest
ports:
- 8888:8888
networks:
- emp-network
discovery-service:
container_name: discovery-service
build:
context: discovery-service
dockerfile: Dockerfile-discovery-service
image: discovery-service:latest
ports:
- 8761:8761
networks:
- emp-network
links:
- cloud-config-service
networks:
emp-network:
driver: bridge
起初我将发现服务配置为从 http://localhost:8888 获取其配置,但经过一番挖掘后,我发现容器中的 localhost 指的是容器本身,并且在 Docker 文档中发现服务可以使用它们的名称相互引用。因此,我更改了发现服务的属性以从 http://cloud-config-service:8888 获取其配置。这不起作用,因此这篇文章。
两个 Dockerfile 的构建和运行都很好,除了发现服务无法在 http://cloud-config-service:8888 上获取配置服务这一事实。
如果我使用 host 网络驱动程序和 http://localhost:8888 端点,它确实有效,但是这“感觉”很老套,而不是应该如何完成。
我可能遗漏了一些微不足道的东西,但我恐怕找不到什么。
编辑: 发现服务控制台日志的小sn-p:
discovery-service | 2018-10-02 13:14:26.798 INFO 1 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://cloud-config-service:8888
cloud-config-service | 2018-10-02 13:14:26.836 INFO 1 --- [ main] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration' of type [org.springframework.cloud.autoconfigure.ConfigurationPropertiesRebinderAutoConfiguration$$EnhancerBySpringCGLIB$$8a18e3b3] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
discovery-service | 2018-10-02 13:14:27.129 INFO 1 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url - http://cloud-config-service:8888. Will be trying the next url if available
discovery-service | 2018-10-02 13:14:27.129 WARN 1 --- [ main] c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://cloud-config-service:8888/discovery-service/default": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
【问题讨论】:
标签: docker spring-boot docker-compose