我目前正在尝试使用基于Spring Boot的微服务,并且正在研究docker,但遇到了一些问题。
基本上我正在尝试将2个小型服务放到容器中:一个Spring Cloud Config服务和一个Spring Cloud Eureka服务(发现服务)。Eureka服务从配置服务中获取其配置。
这两个服务都是具有自己的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
起初我将discovery-service配置为从 http://localhost:8888 获取其配置,但经过一番探索,我发现容器中的 localhost 指的是容器本身,并在Docker文档中发现服务可以使用它们的名称相互引用。因此,我将discovery-service的属性更改为从 http://cloud-config-service:8888 获取其配置。这样做并不起作用,因此我写了这篇文章。
两个 Dockerfile 都能够很好地构建和运行,只是 discovery-service 无法获取位于 http://cloud-config-service:8888 上的 config-service。
如果使用 host 网络驱动程序和 http://localhost:8888 终端点,它确实可行,但这种方法感觉上有些投机取巧,不是应该这样做的方式。
我可能错过了一些微不足道的东西,但恐怕我找不到它。 编辑: discovery-service 控制台日志的小片段:
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 exec curl
实际上返回了正确的配置。我开始认为问题可能出在Spring Cloud查询属性文件中指定的地址的方式上。 - Ash