Netflix-Zuul无法在Docker中路由Spring Boot微服务API

时间:2019-04-22 01:34:53

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

当我在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容器中的服务连接。感谢您的点击。

2 个答案:

答案 0 :(得分:0)

我不确定100%,但是我认为这与您的Zuul无法连接到Eureka有关。 我想原因是您使用本地主机作为Eureka的地址,但是本地主机也是在容器内定义的,它指向自身而不是您的主机。

您是否尝试过使用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数据库以使其正常运行,但是您可以检查Zuul是否可以连接到Eureka。

答案 1 :(得分:0)

除此代码外,其他所有写在邮政中的代码都是正确的;

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

应该是这样;

zuul.routes.xyz.serviceId=xyz

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

实际上,我不知道为什么Eclipse会建议this,但是正确的代码是我提到的第二个。