Eureka 未通过 docker 部署在 ec2 上注册各种 services/zuul,但允许在本地机器上正常注册服务

Eureka not registering various services/zuul on ec2 via docker deploy, but allowing registration of services fine on local machine

我看了其他几篇文章,无济于事,我只是想通过 docker 部署一个 ec2 实例,并向其注册各种 services/zuul。重要的是,我是 运行 通过 docker 在单独容器上的服务/eureka 客户端。 是的,在使用 docker 部署后,我可以通过互联网查看 Eureka 客户端页面,但它没有显示任何实例。我将通过 docker 显示本地日志与我的 docker 文件的并排比较。我有其他朋友使用类似的 application.yml 和 pom.xml configurations/main 应用程序 class 配置没有问题。

堆栈跟踪问题:

左侧:在 EC2 上注册 ZUUL 不工作

右侧:在本地主机上注册 ZUUL 效果很好

尤里卡主应用class/application.yml

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

spring:
  application:
    name: discovery-service-fm
server:
  port: 8761  
eureka:
  client:
    register-with-eureka: false
    fetch-registry: false

网关main/application.yml

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@EnableZuulProxy
@SpringBootApplication
public class Application {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}
spring:
  application:
    name: gateway-service-fm
ribbon:
  ReadTimeout: 60000

这似乎是一个挑战space,因为在整个堆栈溢出过程中,类似问题的大部分答案都没有得到满足,所以我很感激你的尝试!

根据您提供的片段和解释的场景,以下是我的假设。

  1. 您没有明确指定尤里卡服务器地址。因此,它采用默认地址 http://localhost:8761(默认行为)
  2. 因为您可以查看 eureka web 控制台,所以您将容器端口与主机端口(均为 8761)绑定,并使用 http://server_ip_or_domain:8761/ 访问 eureka 控制台(您对其他容器也是如此)
  3. 您没有使用任何 docker 编排工具,您只是 运行在 EC2 VM
  4. 中单独 docker

这个问题的原因

基于这些假设,我可以发现当 运行在容器中时,您的发现客户端应用程序 (services/zuul) 正在尝试访问该容器的本地主机,而不是您的主机,因为您是没有明确指定任何其他 eureka URL,因此,它无法在同一个容器中找到 eureka 服务器。

The point is, containers have their own localhost and when you're referring to localhost from a container this doesn't mean that you are referring to the localhost of the host machine. Instead, your request is still trying to access the localhost of the container. (Imagine this as same as your application running on a VM)

解决方案

您可以在 application.yml 中指定发现服务器 URL。不是本地主机 URL。您可以使用 VM 的 IP 或域或您用于访问 eureka web 控制台的相同地址。

要管理这两种环境,您可以使用 spring 配置文件或使用环境变量来设置这些特定于环境的属性。

如果您真的不希望看到您的请求从您的计算机发出并返回,您可以考虑 docker DNS。即当使用 docker 时,您可以使用友好的 DNS 而不是 IP。为了实现这个 运行 同一个 docker 网络中的所有容器,然后您可以通过容器名称或主机名将容器相互引用。