【发布时间】:2018-02-18 17:01:38
【问题描述】:
我正在使用 spring、spring boot、eureka(用于服务发现)和功能区开发微服务应用程序。
我的应用程序由三个服务客户端、服务器和 eureka-server 组成
客户端和服务器都注册到 eureka-server,然后客户端使用 eureka 服务发现调用服务器。
我能够在本地运行应用程序并且一切正常。
但是在 aws 上部署时,事情就变得一团糟了。
遵循的步骤
- 同一安全组中的三个 ec2 实例。
- 每个微服务都作为具有正确暴露端口的 docker 容器运行。
- 一个 ec2 实例具有弹性 IP。 Eureka-server 容器正在其上运行。
- 客户端和服务器容器部署在其他两个 ec2 实例上。
结果
使用 ECS 时
-
客户端和服务器能够向 Eureka-server 注册自己。
Application AMIs Availability Zones Status HELLO-CLIENT n/a (1) (1) UP (1) - 3fcd1c92386d:hello-client:8071 HELLO-SERVER n/a (1) (1) UP (1) - 6a5d643b32e1:hello-server:8072 -
当点击客户端端点获取 java.net.UnknownHostException。
Whitelabel Error Page This application has no explicit mapping for /error,so you are seeing this as a fallback. Sun Sep 10 08:38:17 GMT 2017 There was an unexpected error (type=Internal Server Error, status=500). I/O error on GET request for "http://hello-server/hello/server/":6a5d643b32e1; nested exception is java.net.UnknownHostException: 6a5d643b32e1
使用 Docker Swarm 时
-
客户端和服务器能够向 Eureka-server 注册自己。
Application AMIs Availability Zones Status HELLO-CLIENT n/a (1) (1) UP (1) - ip-10-255-0-5.ap-south-1.compute.internal:hello-client:8071 HELLO-SERVER n/a (1) (1) UP (1) - ip-10-255-0-6.ap-south-1.compute.internal:hello-server:8072 -
当点击客户端端点时,连接超时。
白标错误页面
This application has no explicit mapping for /error, so you are seeing this as a fallback. Sun Sep 10 11:22:20 GMT 2017 There was an unexpected error (type=Internal Server Error, status=500). I/O error on GET request for "http://hello-server/hello/server/": Operation timed out (Connection timed out); nested exception is java.net.ConnectException: Operation timed out (Connection timed out)
尤里卡配置
application.properties
--------
spring.application.name=eureka-server
server.port=8070
bootstrap.yml
--------
eureka:
client:
registerWithEureka: false
fetchRegistry: false
server:
waitTimeInMsWhenSyncEmpty: 0
service-url:
defaultZone: http://<Elastic IP of eureka server>:8070/eureka
服务器配置
application.properties
--------
spring.application.name=hello-server
server.port=8072
bootstrap.yml
--------
eureka:
client:
service-url:
defaultZone: http://<Elastic IP of eureka server>:8070/eureka
客户端配置
application.properties
--------
spring.application.name=hello-client
server.port=8071
bootstrap.yml
--------
eureka:
client:
service-url:
defaultZone: http://<Elastic IP of eureka server>:8070/eureka
Rest 客户端配置
Resource.java
---------------------------------
@Autowired
RestTemplate restTemplate;
@GetMapping
@RequestMapping("/hello/client")
public String hello(){
String uri = "http://hello-server/hello/server/";
return restTemplate.getForObject(uri, String.class);
}
Config.java
----------------------------
@LoadBalanced
@Bean
public RestTemplate restTemplate(){
return new RestTemplate();
}
【问题讨论】:
-
如何注册到 eureka。你能分享你的 application.properties 文件吗?你的码头工人是如何设置的?您使用的是 docker-swarm 还是 compose?
-
嗨 Barbakini,我已经添加了上面的配置细节。
-
您在客户端的
RestTemplateconfiguration 怎么样?你是直接写ip或者docker容器名还是服务器的eureka注册名?很可能,您的问题出在客户服务中。我认为客户端不会向 eureka 询问服务器服务,而是尝试使用无法解析的书面名称或 IP 地址直接连接。 -
码头工人是否在同一个网络中?如果不是,你需要让两者在同一个地方,否则他们看不到对方
-
@juanlumm 显然容器可以互相看到,因为服务器和客户端都可以将自己注册到尤里卡。
标签: amazon-web-services docker spring-boot netflix-eureka service-discovery