柚子生活

www.pomelolive.com

用Docker容器在不同宿主机部署SpringCloud微服务遇到的问题

问题描述

我在192.168.0.23那台机器用Docker容器部署了Eureka注册中心服务器和zuul网关服务器,在地址为192.168.0.183的win10上用IDEA运行了zuul网关服务器,又在192.168.0.26那台机器用Docker容器部署了一台APP服务器。所有服务器都注册到了注册中心Eureka服务器。我现在通过两个网关服务器访问APP服务都报错。

本机访问

浏览器输入http://localhost:9000/app/hello?name=123

以下是浏览器访问结果:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun May 10 22:48:41 CST 2020
There was an unexpected error (type=Internal Server Error, status=500).
GENERAL

以下是服务器报错

Caused by: java.lang.RuntimeException: org.apache.http.conn.ConnectTimeoutException: Connect to 172.17.0.4:28000 [/172.17.0.4] failed: connect timed out
	at rx.exceptions.Exceptions.propagate(Exceptions.java:57) ~[rxjava-1.3.8.jar:1.3.8]
	at rx.observables.BlockingObservable.blockForSingle(BlockingObservable.java:463) ~[rxjava-1.3.8.jar:1.3.8]
	at rx.observables.BlockingObservable.single(BlockingObservable.java:340) ~[rxjava-1.3.8.jar:1.3.8]
	at com.netflix.client.AbstractLoadBalancerAwareClient.executeWithLoadBalancer(AbstractLoadBalancerAwareClient.java:112) ~[ribbon-loadbalancer-2.3.0.jar:2.3.0]
	... 117 common frames omitted
Caused by: org.apache.http.conn.ConnectTimeoutException: Connect to 172.17.0.4:28000 [/172.17.0.4] failed: connect timed out

容器访问

浏览器输入:http://192.168.0.23:9000/app/hello?name=123

以下是浏览器访问结果:

Whitelabel Error Page

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Sun May 10 14:55:20 GMT 2020
There was an unexpected error (type=Internal Server Error, status=500).
GENERAL

以下是服务器报错

Caused by: java.lang.RuntimeException: org.apache.http.conn.HttpHostConnectException: Connect to 172.17.0.4:28000 [/172.17.0.4] failed: Connection refused (Connection refused)
	at rx.exceptions.Exceptions.propagate(Exceptions.java:57) ~[rxjava-1.3.8.jar!/:1.3.8]
	at rx.observables.BlockingObservable.blockForSingle(BlockingObservable.java:463) ~[rxjava-1.3.8.jar!/:1.3.8]
	at rx.observables.BlockingObservable.single(BlockingObservable.java:340) ~[rxjava-1.3.8.jar!/:1.3.8]
	at com.netflix.client.AbstractLoadBalancerAwareClient.executeWithLoadBalancer(AbstractLoadBalancerAwareClient.java:112) ~[ribbon-loadbalancer-2.3.0.jar!/:2.3.0]
	... 117 common frames omitted
Caused by: org.apache.http.conn.HttpHostConnectException: Connect to 172.17.0.4:28000 [/172.17.0.4] failed: Connection refused (Connection refused)

Eureka注册中心的实例

注册中心.png

通过错误日志大概知道导致错误的原因。用zuul网关路由转发的时候,从Eureka注册中心获得IP地址和端口是容器的IP地址和端口172.17.0.4:28000,通过分析,只要能获得容器所在宿主机的IP地址和端口就可以解决问题了。那么,要怎么获取容器所在宿主机的IP地址和端口呢?

发表评论:

Powered By Z-BlogPHP

© CopyRight 2019-2020 pomelolive.com, 京ICP备19052672号