问题描述
我以这个示例为例我从中创建了3个Docker映像,并带有以下Dockerfile:
springdocker-registration:
FROM openjdk:8-jdk-alpine
VOLUME / tmp
添加target / microservice-demo-1.1.0.RELEASE.jar app.jar
EXPOSE 1111
ENTRYPOINT exec java -jar /app.jar注册
springdocker帐户:
从openjdk:8-jdk-alpine
VOLUME / tmp
添加目标/microservice-demo-1.1.0。 RELEASE.jar app.jar
EXPOSE 2222
ENTRYPOINT exec java -jar /app.jar帐户
springdocker-web:
FROM openjdk:8-jdk-alpine
VOLUME / tmp
添加target / microservice-demo-1.1.0.RELEASE.jar app.jar
EXPOSE 3333
ENTRYPOINT exec java -jar /app.jar web
I f我分别运行三个图像,一切正常,网络
和帐户
服务注册到 registration
服务(这是eureka注册表的实现),我可以使用我的应用程序。但是,在将 docker-compose
与以下 docker-compose.yml
文件
$一起使用时b $ b
版本: 3.4
服务:
注册:
图片:springdocker-registration
端口:
- 1111:1111
帐户:
图片:springdocker-accounts
端口:
- 2222:2222
链接:
-注册
取决于:
-注册
网址:
图片:springdocker-web
端口:
- 3333:3333
取决于:
-注册
-帐户
链接:
-注册
服务网络
和帐户
无法注册注册
服务。以下是应用程序的配置文件:
registration-server.yml:
eureka:
实例:
主机名:localhost
客户端:
registerWithEureka:false
fetchRegistry:false
serviceUrl:
defaultZone :http:// localhost:1111 / eureka /
服务器:
端口:1111
春季:
百里香:
已启用: false
accounts-server.yml:
春天:
应用程序:
名称:帐户服务
免费标记:
已启用:false
百里香:
缓存:错误的
前缀:类路径:/ accounts-server / templates /
错误:
路径:/ error
服务器:
端口:2222
eureka:
客户端:
serviceUrl:
defaultZone:http:// localhost:1111 / eureka
实例:
leaseRenewalIntervalInSeconds:5
preferredIpAddress:真
web-server.yml
春天:
应用程序:
名称:网络服务
免费标记:
已启用:false
百里香:
缓存:错误的
前缀:类路径:/ web-server / templates /
错误:
路径:/ error
eureka:
客户:
serviceUrl:
defaultZone:http:// localhost:1111 / eureka
实例:
leaseRenewalIntervalInSeconds:5
preferredIpAddress:true
服务器:
端口:3333
我可以发布<$的完整控制台日志c $ c> docker-compose up ,但我认为这很有趣:
1:错误RedirectingEurekaHttpClient-请求执行错误
com.sun.jersey.api.client.ClientHandlerException:java.net.ConnectException:连接被拒绝(连接被拒绝)
1:错误DiscoveryClient-DiscoveryClient_WEB-SERVI CE / e3b5e6b3396c:web-service:3333-无法刷新其缓存! status =无法在任何已知服务器
上执行请求com.netflix.discovery.shared.transport.TransportException:无法在任何已知服务器
上执行请求
由于它在docker中运行,请勿使用 localhost
。 Docker compose允许您引用容器名称。
eureka:
客户端:
serviceUrl:
defaultZone:http:// registration:1111 / eureka
侧面注释: defaultZone
必须准确无误,我花了两天的时间想知道为什么它不起作用,因为intellij auto完成了无法正常工作的 default-zone
的工作。 / p>
I took this example https://github.com/paulc4/microservices-demo and I created 3 docker images from it, with the following Dockerfiles:
springdocker-registration:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/microservice-demo-1.1.0.RELEASE.jar app.jar
EXPOSE 1111
ENTRYPOINT exec java -jar /app.jar registration
springdocker-accounts:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/microservice-demo-1.1.0.RELEASE.jar app.jar
EXPOSE 2222
ENTRYPOINT exec java -jar /app.jar accounts
springdocker-web:
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ADD target/microservice-demo-1.1.0.RELEASE.jar app.jar
EXPOSE 3333
ENTRYPOINT exec java -jar /app.jar web
If I run the three images separately everything works ok, the web
and accounts
services register to the registration
service (which is an implementation of the eureka registry) and I can use my application. However when using docker-compose
with the following docker-compose.yml
file
version: "3.4"
services:
registration:
image: springdocker-registration
ports:
- "1111:1111"
accounts:
image: springdocker-accounts
ports:
- "2222:2222"
links:
- registration
depends_on:
- registration
web:
image: springdocker-web
ports:
- "3333:3333"
depends_on:
- registration
- accounts
links:
- registration
the services web
and accounts
are not able to register to the registration
service. Here are the configuration files for the applications:
registration-server.yml:
eureka:
instance:
hostname: localhost
client:
registerWithEureka: false
fetchRegistry: false
serviceUrl:
defaultZone: http://localhost:1111/eureka/
server:
port: 1111
spring:
thymeleaf:
enabled: false
accounts-server.yml:
spring:
application:
name: accounts-service
freemarker:
enabled: false
thymeleaf:
cache: false
prefix: classpath:/accounts-server/templates/
error:
path: /error
server:
port: 2222
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1111/eureka
instance:
leaseRenewalIntervalInSeconds: 5
preferIpAddress: true
web-server.yml
spring:
application:
name: web-service
freemarker:
enabled: false
thymeleaf:
cache: false
prefix: classpath:/web-server/templates/
error:
path: /error
eureka:
client:
serviceUrl:
defaultZone: http://localhost:1111/eureka
instance:
leaseRenewalIntervalInSeconds: 5
preferIpAddress: true
server:
port: 3333
I can post the full console log of docker-compose up
but I think this is the interesting point:
1: ERROR RedirectingEurekaHttpClient - Request execution error
com.sun.jersey.api.client.ClientHandlerException: java.net.ConnectException: Connection refused (Connection refused)
1: ERROR DiscoveryClient - DiscoveryClient_WEB-SERVICE/e3b5e6b3396c:web-service:3333 - was unable to refresh its cache! status = Cannot execute request on any known server
com.netflix.discovery.shared.transport.TransportException: Cannot execute request on any known server
Since its running in docker, don't use localhost
. Docker compose lets you refer to container names.
eureka:
client:
serviceUrl:
defaultZone: http://registration:1111/eureka
Side note: defaultZone
must be exact, I spent 2 days wondering why it wouldn't work since intellij auto completes to do default-zone
which wont work.
这篇关于使用docker-compose时应用程序未注册到eureka的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!