Zuul网关无法连接到Docker-Compose

时间:2018-04-11 15:51:45

标签: docker spring-boot docker-compose netflix-zuul netflix-eureka

当我通过Spring-Boot可运行的jar文件独立启动Eureka和Zuul时,一切似乎都运行正常。但是当我尝试在Docker容器中运行每个并使用Docker-Compose连接它们时,Zuul网关无法找到Eureka服务器。

搬运工-compose.yml:

version: '3.6'

services:

 eureka-serviceregistry:
  container_name: serviceregistry
  build: ./ServiceRegistry
  ports:
   - "8761:8761"
  hostname: serviceregistry
  environment:
    eureka.client.serviceUrl.defaultZone: http://serviceregistry:8761/eureka/

 zuul-edgeservice:
  container_name: gateway
  build: ./ServiceGateway
  ports:
   - "8080:8080"
  hostname: gateway
  environment:
      eureka.client.serviceUrl.defaultZone: http://serviceregistry:8761/eureka/

Eureka Dockerfile:

FROM openjdk:8-jdk-alpine

# Add Spring Boot app.jar to Container
COPY /build/libs/ServiceRegistry-0.0.1-SNAPSHOT.jar /usr/local/ServiceRegistry.jar

EXPOSE 8761

# Fire up our Spring Boot app by default
ENTRYPOINT [ "sh", "-c", "java -Djava.security.egd=file:/dev/./urandom -jar /usr/local/ServiceRegistry.jar" ]

Eureka application.properties:

server.port=8761

spring.application.name=EurekaServiceRegistry

eureka.client.register-with-eureka=true
eureka.client.fetch-registry=false
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka
eureka.instance.hostname=EurekaServiceRegistry

logging.level.com.netflix.eureka=INFO
logging.level.com.netflix.discovery=INFO

Eureka Spring-Boot课程:

@EnableEurekaServer
@SpringBootApplication
public class QQServiceRegistry {

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

Zuul Dockerfile:

FROM openjdk:8-jdk-alpine

# Add Spring Boot app.jar to Container
COPY /build/libs/ServiceGateway-0.0.1-SNAPSHOT.jar /usr/local/ServiceGateway.jar

EXPOSE 8080

# Fire up our Spring Boot app by default
ENTRYPOINT [ "sh", "-c", "java -Djava.security.egd=file:/dev/./urandom -jar /usr/local/ServiceGateway.jar" ]

Zuul application.properties:

zuul.routes.auth.path=/enterprise_service/**
zuul.routes.auth.service-id=ENTERPRISE_SERVICE
eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka

Zuul Spring-Boot课程:

@EnableZuulProxy
@SpringBootApplication
public class ServiceGateway {

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

Root build.gradle文件:

buildscript {
    ext {
        springBootVersion = '2.0.1.RELEASE'
    }
    repositories {
        jcenter()
    }
    dependencies {
        classpath "org.jfrog.buildinfo:build-info-extractor-gradle:4+"
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
}

subprojects {
    apply plugin: 'java'
    apply plugin: 'eclipse'
    apply plugin: 'idea'
    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'
    apply plugin: 'com.jfrog.artifactory'
    apply plugin: 'maven-publish'

    sourceCompatibility = 1.8
    targetCompatibility = 1.8

    artifactory {
        contextUrl = "${artifactory_contextUrl}"
        publish {
            repository {
                repoKey = 'gradle-dev-local'
                username = "${artifactory_user}"
                password = "${artifactory_password}"
                maven = true
            }
            defaults {
                publications ('mavenJava')
            }
        }
        resolve {
            repository {
                repoKey = 'gradle-dev'
                username = "${artifactory_user}"
                password = "${artifactory_password}"
                maven = true
            }
        }
    }

    publishing {
        publications {
            mavenJava(MavenPublication) {
                from components.java
            }
        }
    }

    artifactoryPublish {
        dependsOn assemble
    }

    dependencyManagement {
        imports {
            mavenBom 'org.springframework.cloud:spring-cloud-dependencies:Finchley.M8'
        }
    }

    repositories {
        maven {
            url 'https://repo.spring.io/libs-milestone'
        }
    }

    dependencies {
        compile('org.springframework.boot:spring-boot-starter-actuator')
        compile('org.springframework.boot:spring-boot-starter-web')
        testCompile('org.springframework.boot:spring-boot-starter-test')
    }

   bootJar {
        launchScript()
    }
}

Eureka子项目gradle文件:

group = 'my.company.service.infrastructure.registry'
version = '0.0.1-SNAPSHOT'

dependencies {
    compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')
}

Zuul子项目gradle文件:

group = 'my.company.service.infrastructure.gateway'
version = '0.0.1-SNAPSHOT'

dependencies {
    compile('org.springframework.cloud:spring-cloud-starter-netflix-zuul')
    compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
}

在Zuul日志中,当它试图向Eureka注册时,这种错误模式一遍又一遍:

  

2018-04-11 15:38:20.136 INFO 6 --- [nfoReplicator-0]> com.netflix.discovery.DiscoveryClient:DiscoveryClient_UNKNOWN / gateway:>注册服务......   2018-04-11 15:38:20.139 ERROR 6 --- [nfoReplicator-0]> c.n.d.s.t.d.RedirectingEurekaHttpClient:请求执行错误

     

com.sun.jersey.api.client.ClientHandlerException:java.net.ConnectException:>连接被拒绝(连接被拒绝)     在> com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4> Handler.java:187)〜[jersey-apache-client4-1.19.1.jar!/:1.19.1]     at> com.sun.jersey.api.client.filter.GZIPContentEncodingFilter.handle(GZIPContentEn> codingFilter.java:123)〜[jersey-client-1.19.1.jar!/:1.19.1]     at> com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFil> ter.java:27)〜[eureka-client-1.8.6.jar!/:1.8.6]     在com.sun.jersey.api.client.Client.handle(Client.java:652)〜[jersey-client-> 1.19.1.jar!/:1.19.1]     在com.sun.jersey.api.client.WebResource.handle(WebResource.java:682)〜>> [jersey-client-1.19.1.jar!/:1.19.1]     在com.sun.jersey.api.client.WebResource.access $ 200(WebResource.java:74)〜> [jersey-client-1.19.1.jar!/:1.19.1]     在com.sun.jersey.api.client.WebResource $ Builder.post(WebResource.java:570)>〜[jersey-client-1.19.1.jar!/:1.19.1]     at> com.netflix.discovery.shared.transport.jersey.AbstractJerseyEurekaHttpClient.re> gister(AbstractJerseyEurekaHttpClient.java:56)〜[eureka-client-> 1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator $ 1.ex> ecute(EurekaHttpClientDecorator.java:59)[eureka-client-1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.shared.transport.decorator.MetricsCollectingEurekaHttpCli> ent.execute(MetricsCollectingEurekaHttpClient.java:73)〜[eureka-client-> 1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.regi> ster(EurekaHttpClientDecorator.java:56)[eureka-client-1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator $ 1.ex> ecute(EurekaHttpClientDecorator.java:59)[eureka-client-1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.shared.transport.decorator.RedirectingEurekaHttpClient.ex> ecuteOnNewServer(RedirectingEurekaHttpClient.java:118)~ [eureka-client-> 1.8.6.jar!/:1.8.6]     在> com.netflix.discovery.shared.transport.decorator.RedirectingEurekaHttpClient.ex> ecute(RedirectingEurekaHttpClient.java:79)〜[eureka-client-1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.regi> ster(EurekaHttpClientDecorator.java:56)[eureka-client-1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator $ 1.ex> ecute(EurekaHttpClientDecorator.java:59)[eureka-client-1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.exec> ute(RetryableEurekaHttpClient.java:119)[eureka-client-1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.regi> ster(EurekaHttpClientDecorator.java:56)[eureka-client-1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator $ 1.ex> ecute(EurekaHttpClientDecorator.java:59)[eureka-client-1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.exec> ute(SessionedEurekaHttpClient.java:77)[eureka-client-1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.regi> ster(EurekaHttpClientDecorator.java:56)[eureka-client-1.8.6.jar!/:1.8.6]     在com.netflix.discovery.DiscoveryClient.register(DiscoveryClient.java:829)> [eureka-client-1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.InstanceInfoReplicator.run(InstanceInfoReplicator.java:10>4)[eureka-client-1.8.6.jar!/:1.8.6]     at java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:511)> [na:1.8.0_151]     at java.util.concurrent.FutureTask.run(FutureTask.java:266)[na:1.8.0_151]     at> java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.access $ 201>(ScheduledThreadPoolExecutor.java:180)[na:1.8.0_151]     at> java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.run(Schedu> ledThreadPoolExecutor.java:293)[na:1.8.0_151]     at> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)> [na:1.8.0_151]     at> java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)> [na:1.8.0_151]     在java.lang.Thread.run(Thread.java:748)[na:1.8.0_151]   引起:java.net.ConnectException:连接被拒绝(连接被拒绝)     at java.net.PlainSocketImpl.socketConnect(Native Method)〜[na:1.8.0_151]     at> java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)〜> [na:1.8.0_151]     at> java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:>206)〜[na:1.8.0_151]     在> java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)〜> [na:1.8.0_151]     在java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)〜> [na:1.8.0_151]     在java.net.Socket.connect(Socket.java:589)〜[na:1.8.0_151]     at> org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory> .java:121)~ [httpclient-4.5.5.jar!/:4.5.5]     at> org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(Defaul> tClientConnectionOperator.java:180)〜[httpclient-4.5.5.jar!/:4.5.5]     at> org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:144)〜> [httpclient-4.5.5.jar!/:4.5.5]     在> org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdap> ter.java:134)〜[httpclient-4.5.5.jar!/:4.5.5]     at> org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDir> ector.java:610)~ [httpclient-4.5.5.jar!/:4.5.5]     at> org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirect> or.java:445)~ [httpclient-4.5.5.jar!/:4.5.5]     at> org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.jav> a:835)〜[httpclient-4.5.5.jar!/:4.5.5]     at> org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.jav> a:118)〜[httpclient-4.5.5.jar!/:4.5.5]     at> org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.jav> a:56)〜[httpclient-4.5.5.jar!/:4.5.5]     at> com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4> Handler.java:173)〜[jersey-apache-client4-1.19.1.jar!/:1.19.1]     ...省略了29个常见帧

     

2018-04-11 15:38:20.140 WARN 6 --- [nfoReplicator-0]> c.n.d.s.t.d.RetryableEurekaHttpClient:请求执行失败,使用>消息:java.net.ConnectException:连接被拒绝(连接被拒绝)   2018-04-11 15:38:20.140 WARN 6 --- [nfoReplicator-0]> com.netflix.discovery.DiscoveryClient:DiscoveryClient_UNKNOWN / gateway - >注册失败无法在任何已知服务器上执行请求

     

com.netflix.discovery.shared.transport.TransportException:无法在任何已知服务器上执行>请求     at> com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.exec> ute(RetryableEurekaHttpClient.java:111)〜[eureka-client-1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.regi> ster(EurekaHttpClientDecorator.java:56)〜[eureka-client-1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator $ 1.ex> ecute(EurekaHttpClientDecorator.java:59)〜[eureka-client-1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.exec> ute(SessionedEurekaHttpClient.java:77)〜[eureka-client-1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.regi> ster(EurekaHttpClientDecorator.java:56)〜[eureka-client-1.8.6.jar!/:1.8.6]     在com.netflix.discovery.DiscoveryClient.register(DiscoveryClient.java:829)>〜[eureka-client-1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.InstanceInfoReplicator.run(InstanceInfoReplicator.java:10>4)[eureka-client-1.8.6.jar!/:1.8.6]     at java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:511)> [na:1.8.0_151]     at java.util.concurrent.FutureTask.run(FutureTask.java:266)[na:1.8.0_151]     at> java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.access $ 201>(ScheduledThreadPoolExecutor.java:180)[na:1.8.0_151]     at> java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.run(Schedu> ledThreadPoolExecutor.java:293)[na:1.8.0_151]     at> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)> [na:1.8.0_151]     at> java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)> [na:1.8.0_151]     在java.lang.Thread.run(Thread.java:748)[na:1.8.0_151]

     

2018-04-11 15:38:20.141 WARN 6 --- [nfoReplicator-0]> c.n.discovery.InstanceInfoReplicator:>实例信息复制器出现问题

     

com.netflix.discovery.shared.transport.TransportException:无法在任何已知服务器上执行>请求     at> com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.exec> ute(RetryableEurekaHttpClient.java:111)〜[eureka-client-1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.regi> ster(EurekaHttpClientDecorator.java:56)〜[eureka-client-1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator $ 1.ex> ecute(EurekaHttpClientDecorator.java:59)〜[eureka-client-1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.exec> ute(SessionedEurekaHttpClient.java:77)〜[eureka-client-1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.regi> ster(EurekaHttpClientDecorator.java:56)〜[eureka-client-1.8.6.jar!/:1.8.6]     在com.netflix.discovery.DiscoveryClient.register(DiscoveryClient.java:829)>〜[eureka-client-1.8.6.jar!/:1.8.6]     at> com.netflix.discovery.InstanceInfoReplicator.run(InstanceInfoReplicator.java:10>4)〜[eureka-client-1.8.6.jar!/:1.8.6]     at java.util.concurrent.Executors $ RunnableAdapter.call(Executors.java:511)> [na:1.8.0_151]     at java.util.concurrent.FutureTask.run(FutureTask.java:266)[na:1.8.0_151]     at> java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.access $ 201>(ScheduledThreadPoolExecutor.java:180)[na:1.8.0_151]     at> java.util.concurrent.ScheduledThreadPoolExecutor $ ScheduledFutureTask.run(Schedu> ledThreadPoolExecutor.java:293)[na:1.8.0_151]     at> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)> [na:1.8.0_151]     at> java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624)> [na:1.8.0_151]     在java.lang.Thread.run(Thread.java:748)[na:1.8.0_151]

     

2018-04-11 15:38:50.043 ERROR 6 --- [tbeatExecutor-0]> c.n.d.s.t.d.RedirectingEurekaHttpClient:请求执行错误

2 个答案:

答案 0 :(得分:1)

问题是YAML语法错误。

以下docker-compose.yml有效。请注意环境部分。

version: '3.6'

services:

  eureka-serviceregistry:
    container_name: serviceregistry
    build: ./ServiceRegistry
    ports:
      - 8761:8761
    hostname: serviceregistry
    environment:
      - EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://serviceregistry:8761/eureka

  zuul-edgeservice:
    container_name: gateway
    build: ./ServiceGateway
    ports:
      - 8080:8080
    hostname: gateway
    environment:
      - EUREKA_CLIENT_SERVICEURL_DEFAULTZONE=http://serviceregistry:8761/eureka

答案 1 :(得分:0)

尝试替换为spring.application.name = serviceregistry
eureka服务器下的application.properties中的eureka.instance.hostname = serviceregistry