我正在使用spring,spring boot,eureka(用于服务发现)和功能区开发微服务应用程序。
我的应用程序由三个服务客户端,服务器和eureka-server组成
客户端和服务器都在eureka-server注册,后来客户端使用eureka服务发现调用服务器。
我能够在本地运行应用程序,事情很好。
但是当在aws上部署时,事情会变得混乱。
遵循的步骤
结果
使用ECS时
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
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时
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
休息客户端配置
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();
}
我能够解决这个问题。这里的问题是系统试图使用docker容器id作为主机系统无法解析来ping Eureka客户端。在微服务配置中,为所有服务设置eureka.instance.preferIpAddress=true
。它肯定会解决问题。