我面临一个奇怪的问题,我为我的每个服务(spring-cloud,eureka,my-service)创建了一个DockerFile,所以当我启动docker compose时,spring cloud服务器启动,eureka启动,但我的应用程序不是能够从Spring云中检索配置,但是当我访问日志中出现的url时,我对如何解决此问题没有任何想法。
关注我的DockerFiles:Spring云Docker文件
FROM openjdk:11.0.2-jre-stretch
RUN ["mkdir", "/root/remote-config-service"]
COPY target/remote-config-service-0.0.1-SNAPSHOT.jar /root/remote-config-service/
ENTRYPOINT java -jar /root/remote-config-service/remote-config-service-0.0.1-SNAPSHOT.jar
EXPOSE 9000 9001
eureka Docker文件:
FROM openjdk:11.0.2-jre-stretch
RUN ["mkdir", "/root/eureka-service-discovery"]
COPY target/eureka-service-discovery-0.0.1-SNAPSHOT.jar /root/eureka-service-discovery/
ENTRYPOINT java -jar /root/eureka-service-discovery/eureka-service-discovery-0.0.1-SNAPSHOT.jar
EXPOSE 8761
我的应用程序Docker文件:
FROM openjdk:11.0.2-jre-stretch
ENV PROFILE="dev"
ADD ["target/product-api-0.0.1-SNAPSHOT.jar", "product-api-0.0.1-SNAPSHOT.jar"]
EXPOSE 18100 8091
RUN sh -c 'touch /product-api-0.0.1-SNAPSHOT.jar'
ENTRYPOINT [ "sh", "-c", "java -Djava.security.egd=file:/dev/./urandom -Dspring.profiles.active=$PROFILE -jar /product-api-0.0.1-SNAPSHOT.jar" ]
HEALTHCHECK --interval=5m --timeout=3s \
CMD curl -f http://localhost:8091/actuator/health || exit 1
Docker撰写
remote-config-service:
container_name: remote-config-service
build: ./remote-config-service
ports:
- 9000:9000
- 9001:9001
depends_on:
- mysqldb
eureka-service-discovery:
build: ./eureka-service-discovery
container_name: eureka-service-discovery
hostname: localhost
restart: on-failure
ports:
- 8761:8761
product-api:
container_name: product-api
build: ./product-api
ports:
- 18000:18000
- 8091:8091
depends_on:
- eureka-service-discovery
- remote-config-service
- mysqldb
restart: on-failure
当我尝试启动我的应用程序(product-api)时,我遇到了这个错误:
c.c.c.ConfigServicePropertySourceLocator : Fetching config from server at : http://localhost:9000
c.c.c.ConfigServicePropertySourceLocator : Connect Timeout Exception on Url - http://localhost:9000. Will be trying the next url if available
c.c.c.ConfigServicePropertySourceLocator : Could not locate PropertySource: I/O error on GET request for "http://localhost:9000/product-api/dev": Connection refused (Connection refused); nested exception is java.net.ConnectException: Connection refused (Connection refused)
但是当我在浏览器中访问此网址时:
http://localhost:9000/product-api/dev
它工作正常。
有人有想法帮我解决这个问题吗?
默认情况下,Docker容器无法通过localhost相互通信,它们在网络方面彼此隔离。这就是为什么你可以在你的浏览器中访问localhost:9000但容器无法访问它。容器中的localhost只是该容器的环回地址,而不是主机。有几种不同的网络类型(请参阅https://docs.docker.com/network/),但默认类型是bridge
。 Bridge网络将您的容器与主机上的其他容器网络隔离,然后将您的一个主机端口上的流量转发到容器端口(由port: <host>:<container>
条目定义)有两个选项允许容器相互通信:
network: host
运行(不推荐)。这将直接在您的主机网络上运行您的容器,以便它们可以通过localhost相互通信。如果你使用它,你可以摆脱port:
设置。容器将绑定服务直接使用的任何端口到主机(如果您想运行多个使用相同端口的容器,这也可能导致端口冲突。)version: '3'
services:
service1:
container_name: service1
build: .
networks:
- servicenet
ports:
- 8888:9000
service2:
build: .
container_name: service2
restart: on-failure
networks:
- servicenet
ports:
- 8887:8761
networks:
servicenet:
这将允许service1通过http://service1
与service2通信,反之亦然