gpt4 book ai didi

spring - Eureka 没有通过 docker deploy 在 ec2 上注册各种服务/zuul,但允许在本地机器上正常注册服务

转载 作者:行者123 更新时间:2023-12-02 20:57:56 25 4
gpt4 key购买 nike

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

堆栈跟踪问题:

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

右侧:在本地主机上注册 ZUUL 工作出色
enter image description here

Eureka主应用类/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


网关主/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

这似乎是一个具有挑战性的空间,因为类似问题的大多数答案都没有在堆栈溢出期间得到满足,所以我很感激这个尝试!

最佳答案

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

  • 您没有明确指定 Eureka 服务器地址。因此,它采用默认地址 http://localhost:8761 (默认行为)
  • 由于您可以查看 eureka Web 控制台,因此您将容器端口与主机端口(均为 8761)绑定(bind)并使用 http://server_ip_or_domain:8761/ 访问 eureka 控制台(其他容器也一样)
  • 您没有使用任何 docker 编排工具,而是在 EC2 VM
  • 中单独运行 docker

    此问题的原因

    基于这些假设,我可以发现在容器中运行时,您的发现客户端应用程序(服务/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。您可以使用虚拟机的 IP 或域,或用于访问 eureka Web 控制台的相同地址。

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

    如果你真的不喜欢看到你的请求离开你的机器然后回来,你可以考虑 docker DNS。即当使用 docker 时,您可以使用友好的 DNS 而不是 IP。为了实现这一点,在同一个 docker 网络中运行所有容器,然后您可以通过容器名称或主机名相互引用容器。

    关于spring - Eureka 没有通过 docker deploy 在 ec2 上注册各种服务/zuul,但允许在本地机器上正常注册服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61978562/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com