我拿了这个例子 https://github.com/paulc4/microservices-demo 并从中创建了 3 个 docker 镜像,其中包含以下 Dockerfile:
springdocker-注册:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/microservice-demo-1.1.0.RELEASE.jar app.jar
EXPOSE 1111
ENTRYPOINT exec java -jar /app.jar registration
springdocker-帐户:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/microservice-demo-1.1.0.RELEASE.jar app.jar
EXPOSE 2222
ENTRYPOINT exec java -jar /app.jar accounts
springdocker-web:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/microservice-demo-1.1.0.RELEASE.jar app.jar
EXPOSE 3333
ENTRYPOINT exec java -jar /app.jar web
如果我分别运行三个图像,一切正常,
web
和 accounts
服务会注册到 registration
服务(这是 eureka 注册表的实现),我可以使用我的应用程序。但是,当将 docker-compose
与以下 docker-compose.yml
文件一起使用时version: "3.4"
services:
registration:
image: springdocker-registration
ports:
- "1111:1111"
accounts:
image: springdocker-accounts
ports:
- "2222:2222"
links:
- registration
depends_on:
- registration
web:
image: springdocker-web
ports:
- "3333:3333"
depends_on:
- registration
- accounts
links:
- registration
web
和 accounts
服务无法注册到 registration
服务。以下是应用程序的配置文件:注册-server.yml:
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:1111/eureka/
server:
port: 1111
spring:
thymeleaf:
enabled: false
帐户-server.yml:
spring:
application:
name: accounts-service
freemarker:
enabled: false
thymeleaf:
cache: false
prefix: classpath:/accounts-server/templates/
error:
path: /error
server:
port: 2222
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1111/eureka
instance:
leaseRenewalIntervalInSeconds: 5
preferIpAddress: true
网络服务器.yml
spring:
application:
name: web-service
freemarker:
enabled: false
thymeleaf:
cache: false
prefix: classpath:/web-server/templates/
error:
path: /error
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1111/eureka
instance:
leaseRenewalIntervalInSeconds: 5
preferIpAddress: true
server:
port: 3333
我可以发布
docker-compose up
的完整控制台日志,但我认为这是有趣的一点:1: ERROR RedirectingEurekaHttpClient - Request execution error
com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused (Connection refused)
1: ERROR DiscoveryClient - DiscoveryClient_WEB-SERVICE/e3b5e6b3396c:web-service:3333 - was unable to refresh its cache! status = Cannot execute request on any known server
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
最佳答案
由于它在 docker 中运行,所以不要使用 localhost
。 Docker compose 允许您引用容器名称。
eureka:
client:
serviceUrl:
defaultZone: http://registration:1111/eureka
旁注:
defaultZone
必须准确,我花了 2 天时间想知道为什么它不起作用,因为 intellij auto 完成了无法运行的 default-zone
。关于docker - 使用 docker-compose 时应用程序未注册到 eureka,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47266795/