gpt4 book ai didi

docker - Zuul网关无法在Docker-Compose下连接到Eureka服务注册表

转载 作者:行者123 更新时间:2023-12-02 19:32:33 25 4
gpt4 key购买 nike

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

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/

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 靴类:
@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 引导类(class):
@EnableZuulProxy
@SpringBootApplication
public class ServiceGateway {

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

根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: >registering service... 2018-04-11 15:38:20.139 ERROR 6 --- [nfoReplicator-0] >c.n.d.s.t.d.RedirectingEurekaHttpClient : Request execution error

com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: >Connection refused (Connection refused) at >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] at com.sun.jersey.api.client.Client.handle(Client.java:652) ~[jersey-client->1.19.1.jar!/:1.19.1] at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682) ~>>[jersey-client-1.19.1.jar!/:1.19.1] at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) ~>[jersey-client-1.19.1.jar!/:1.19.1] at 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] at >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] at 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] at java.lang.Thread.run(Thread.java:748) [na:1.8.0_151] Caused by: java.net.ConnectException: Connection refused (Connection refused) 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] at >java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~>[na:1.8.0_151] at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~>[na:1.8.0_151] at 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] at >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 common frames omitted

2018-04-11 15:38:20.140 WARN 6 --- [nfoReplicator-0] >c.n.d.s.t.d.RetryableEurekaHttpClient : Request execution failed with >message: java.net.ConnectException: Connection refused (Connection refused) 2018-04-11 15:38:20.140 WARN 6 --- [nfoReplicator-0] >com.netflix.discovery.DiscoveryClient : DiscoveryClient_UNKNOWN/gateway - >registration failed Cannot execute request on any known server

com.netflix.discovery.shared.transport.TransportException: Cannot execute >request on any known server 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] at 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] at 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 : There was a problem with the >instance info replicator

com.netflix.discovery.shared.transport.TransportException: Cannot execute >request on any known server 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] at 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] at 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 : Request execution error

最佳答案

问题是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

关于docker - Zuul网关无法在Docker-Compose下连接到Eureka服务注册表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49779491/

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