当我通过Intellij启动zuul和eureka时,一切都很好,zuul已在eureka服务器上注册。在eureka服务器中,我看到
2019-03-15 18:00:20.727 INFO 31713 --- [nio-8761-exec-2] c.n.e.registry.AbstractInstanceRegistry : Registered instance ZUUL-SERVER/192.168.0.11:zuul-server:8762 with status UP (replication=false)
2019-03-15 18:00:21.309 INFO 31713 --- [nio-8761-exec-3] c.n.e.registry.AbstractInstanceRegistry : Registered instance ZUUL-SERVER/192.168.0.11:zuul-server:8762 with status UP (replication=true)
但是当我尝试在zuul容器中使用docker-compose up -d
与docker启动这两项服务时,我却遇到了例外:
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:112) ~[eureka-client-1.9.3.jar!/:1.9.3]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134) ~[eureka-client-1.9.3.jar!/:1.9.3]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.java:137) ~[eureka-client-1.9.3.jar!/:1.9.3]
at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) ~[eureka-client-1.9.3.jar!/:1.9.3]
at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134) ~[eureka-client-1.9.3.jar!/:1.9.3]
at com.netflix.discovery.DiscoveryClient.getAndStoreFullRegistry(DiscoveryClient.java:1051) ~[eureka-client-1.9.3.jar!/:1.9.3]
at com.netflix.discovery.DiscoveryClient.fetchRegistry(DiscoveryClient.java:965) ~[eureka-client-1.9.3.jar!/:1.9.3]
at com.netflix.discovery.DiscoveryClient.<init>(DiscoveryClient.java:414) ~[eureka-client-1.9.3.jar!/:1.9.3]
at com.netflix.discovery.DiscoveryClient.<init>(DiscoveryClient.java:269) ~[eureka-client-1.9.3.jar!/:1.9.3]
at org.springframework.cloud.netflix.eureka.CloudEurekaClient.<init>(CloudEurekaClient.java:63) ~[spring-cloud-netflix-eureka-client-2.1.0.M1.jar!/:2.1.0.M1]
at org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration.eurekaClient(EurekaClientAutoConfiguration.java:290) ~[spring-cloud-netflix-eureka-client-2.1.0.M1.jar!/:2.1.0.M1]
at org.springframework.cloud.netflix.eureka.EurekaClientAutoConfiguration$RefreshableEurekaClientConfiguration$$EnhancerBySpringCGLIB$$8fcb0d88.CGLIB$eurekaClient$2(<generated>) ~[spring-cloud-netflix-eureka-client-2.1.0.M1.jar!/:2.1.0.M1]
eureka和zuul的spring-boot-starter-parent
的版本为2.1.2.RELEASE
,spring-cloud
的版本为Greenwich.M1
eureka服务器属性文件:
# Give a name to the eureka server
spring.application.name=eureka-server
# default port for eureka server
server.port=8761
# eureka by default will register itself as a client. So, we need to set it to false.
# What's a client server? See other microservices (image, gallery, auth, etc).
eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
zuul-properties文件包含以下行:
eureka.client.service-url.default-zone=http://eureka:8761/eureka/
docker-compose.yml文件(相关服务)
version: "2"
services:
eureka:
container_name: eureka
build:
context: .
dockerfile: eureka.Dockerfile
image: eureka-service
ports:
- "8761:8761"
networks:
- event-network
zuul:
container_name: zuul
build:
context: .
dockerfile: zuul.Dockerfile
image: zuul-service
ports:
- "8762:8762"
networks:
- event-network
networks:
event-network:
driver: bridge
在两个Dockerfile中,我都只是将jar添加到容器中,并使用java -jar
命令启动jar。我用@EnableEurekaClient
注释了zuul主类,并用@EnableEurekaServer
注释了eureka主类。我不认为异常与zuul有关,因为我的其余微服务都存在相同的问题。它们的其余部分也无法连接到尤里卡。
当我通过容器或通过intellij启动尤里卡时,我可以访问尤里卡仪表板。我什至尝试放置this图像而不是我的图像,但是我遇到了同样的异常。我还尝试了docker exec "zuul" -it bash
,并在zuul容器中尝试了curl eureka:8761
,并且可以正常访问仪表板。
答案 0 :(得分:1)
不确定Eureka,但nginx代理服务器也有类似问题。 Nginx无法识别docker的主机解析器,我使用的一种可能的解决方案是获取特定容器服务的静态IP。
在这种情况下,可以为Eureka容器设置静态IP,并在Zuul的配置文件中使用它。
如果您有一个Eureca容器,当然可以使用。对于很多容器,应该看看zuul如何阅读和理解docker的主机解析器。