本文介绍了使用 docker-compose 时应用程序未注册到 eureka的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拿了这个例子https://github.com/paulc4/microservices-demo并且我从中创建了 3 个 docker 镜像,其中包含以下 Dockerfile:

I took this example https://github.com/paulc4/microservices-demo and I created 3 docker images from it, with the following Dockerfiles:

springdocker-注册:

springdocker-registration:

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-accounts:

springdocker-accounts:

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:

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

如果我分别运行三个图像,一切正常,webaccounts 服务注册到 registration 服务(这是一个实现尤里卡注册表),我可以使用我的应用程序.但是,当使用 docker-compose 和以下 docker-compose.yml 文件

If I run the three images separately everything works ok, the web and accounts services register to the registration service (which is an implementation of the eureka registry) and I can use my application. However when using docker-compose with the following docker-compose.yml file

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

服务webaccounts 无法注册到registration 服务.以下是应用程序的配置文件:

the services web and accounts are not able to register to the registration service. Here are the configuration files for the applications:

注册服务器.yml:

eureka:
  instance:
    hostname: localhost
  client:
    registerWithEureka: false
    fetchRegistry: false
    serviceUrl:
     defaultZone: http://localhost:1111/eureka/

server:
  port: 1111

spring:
  thymeleaf:
    enabled: false

accounts-server.yml:

accounts-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

web-server.yml

web-server.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 的完整控制台日志,但我认为这是有趣的一点:

I can post the full console log of docker-compose up but I think this is the interesting point:

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 允许您引用容器名称.

Since its running in docker, don't use localhost. Docker compose lets you refer to container names.

eureka:
  client:
    serviceUrl:
      defaultZone: http://registration:1111/eureka

旁注:defaultZone 必须是准确的,我花了 2 天时间想知道为什么它不起作用,因为 intellij 自动完成执行 default-zone 不起作用.

Side note: defaultZone must be exact, I spent 2 days wondering why it wouldn't work since intellij auto completes to do default-zone which wont work.

这篇关于使用 docker-compose 时应用程序未注册到 eureka的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-26 11:51