【问题标题】:Netflix-zuul can't route spring boot microservice api in dockerNetflix-zuul 无法在 docker 中路由 Spring Boot 微服务 API
【发布时间】:2019-09-11 06:44:24
【问题描述】:

当我在 docker 容器中部署我的 zuul-gateway-service 并对其进行测试时,我收到“There was an unexpected error (type=Internal Server Error, status=500) GENERAL”错误。但是在 Windows 中,当我在 Eclipse 中运行应用程序时,一切正常,我可以通过 zuul 网关端口访问服务,我还可以通过网关使用邮递员的每个映射。但它在 docker 容器中不起作用。

ZuulGatewayServerApplication.java ;

@EnableEurekaClient
@EnableZuulProxy
@SpringBootApplication
@EnableDiscoveryClient
public class ZuulGatewayServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ZuulGatewayServerApplication.class, args);
    }
}

zuul-gateway-service 的application.properties 文件;

server.port=8762
spring.application.name=t-zuul-server
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka

zuul.ignored-services=*

zuul.routes.t-author-bookstore.path=/author/**
zuul.routes.t-author-bookstore.service-ıd=t-author-bookstore
#zuul.routes.t-author-bookstore.strip-prefix=false

zuul.routes.t-book-bookstore.path=/book/**
zuul.routes.t-book-bookstore.service-ıd=t-book-bookstore
#zuul.routes.t-book-bookstore.strip-prefix=false
#... there is also 4 more services

我还尝试将这些代码添加到 zuul-gateway-service 的 application.properties 文件中;

eureka.client.registerWithEureka = true
eureka.client.register-with-eureka=true
ribbon.eureka.enabled=true
zuul.routes.${service_name}.strip-prefix=false

在docker中,对于zuul-gateway-service,我的Dockerfile是这样的

FROM openjdk:8-alpine
VOLUME /tmp
COPY t-zuul-gateway-server-1.0.jar t-zuul-app.jar
EXPOSE 8762
ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/t-zuul-app.jar"]

这就是我启动 docker 图像的方式

docker run -d --network=bookstore-mongodb -p 8761:8761 --name t-eureka-server t-eureka-server-1.0
docker run -d --network=bookstore-mongodb -p 8762:8762 --name t-zuul-servicee --link=mongo --rm -e EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://localhost:8761/eureka t-zuul-gateway-server-1.0
docker run -d --network=bookstore-mongodb -p 8052:8052 --name t-book-bookstore --link=mongo --rm -e EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://localhost:8761/eureka t-book-bookstore-1.0

8052 端口按预期工作。

这是我的 docker 容器进程的外观(docker ps):here

我还尝试将 zuul-gateway-service 与其他容器链接--link。但它没有用。

相同的代码在 Windows 中运行良好,但在 docker 容器中却不行。我希望将网关与 docker 容器中的服务连接起来。感谢您的点击。

【问题讨论】:

  • 你需要将你的应用程序中的localhost替换为zuul-gateway-service的属性为你的eureka服务器镜像的名称。

标签: spring-boot docker microservices netflix-zuul proxy-server


【解决方案1】:

除此代码外,帖子中编写的所有代码和其他代码都是正确的;

zuul.routes.xyz.service-ıd=xyz

应该是这样的;

zuul.routes.xyz.serviceId=xyz

而且此服务 ID 必须与每个服务的 spring.application.name 值相同。

其实我不知道为什么 eclipse 建议 this 但正确的代码是我提到的第二个。

【讨论】:

  • 不知道这怎么会是公认的答案。
【解决方案2】:

我不是 100% 确定,但我认为这是关于您的 Zuul 无法连接到 Eureka。 我猜原因是您使用 localhost 作为 Eureka 的地址,但是 localhost 也在容器中定义并指向它自己而不是您的主机。

您是否尝试过使用 docker-compose?在您的撰写文件中,您可以执行以下操作:

version: '3.3'
services:
    eureka:
        image: t-eureka-server-1.0
        ports:
            - "8761:8761"

    zuul:
        image: t-zuul-gateway-server-1.0
        ports:
            - "8762:8762"
        depends_on:
            - "eureka"
        links:
            - "eureka:eureka"

    bookstore:
        image: t-book-bookstore-1.0
        ports:
            - "8052:8052"
        links:
            - "eureka:eureka"

当然,您需要添加您的 mongo DB 才能使其工作,但您可以检查 Zuul 是否可以连接到 Eureka。

【讨论】:

  • 在这方面做得很好,我在上面添加了一条评论,让它在不使用 docker-compose 的情况下工作,尽管使用 compose 让生活更轻松。
猜你喜欢
  • 2019-01-27
  • 2019-07-18
  • 2018-08-21
  • 2021-02-01
  • 2019-02-13
  • 2020-10-25
  • 2018-09-21
  • 2020-01-11
  • 2017-07-07
相关资源
最近更新 更多