首页 > 解决方案 > Netflix-zuul 无法在 docker 中路由 Spring Boot 微服务 API

问题描述

当我在 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):这里

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

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

标签: spring-bootdockermicroservicesnetflix-zuulproxy-server

解决方案


我不是 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。


推荐阅读