Spring RestTemplate无法解析尤里卡名称

时间:2018-11-30 13:54:22

标签: spring spring-cloud resttemplate

我有1个eureka服务器和2个微服务。它们称为oauth和身份验证。我启动了所有内容,然后2个微服务注册到了eureka。我可以通过在浏览器中键入eureka服务器的网址来验证这一点。

我现在想使用resttemplate,以便oauth微服务可以与身份验证微服务通信。

在我的课堂上,我已经添加了自动接线

@Autowired
private RestTemplate restTemplate;

然后我使用下面的代码从oauth微服务与身份验证微服务进行通信。

ResponseEntity<ResponseDto<?>> responseEntity = restTemplate.exchange("http://authentication/api/authentication/verify",
            HttpMethod.POST, request, typeRef);`

执行此代码后,我会在日志中看到以下内容:

oauth    | 2018-11-30 13:41:58.535  INFO 1 --- [nio-9001-exec-1] s.c.a.AnnotationConfigApplicationContext : Refreshing SpringClientFactory-authentication: startup date [Fri Nov 30 13:41:58 GMT 2018]; parent: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6743e411
oauth    | 2018-11-30 13:41:58.641  INFO 1 --- [nio-9001-exec-1] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
oauth    | 2018-11-30 13:41:59.115  INFO 1 --- [nio-9001-exec-1] c.netflix.config.ChainedDynamicProperty  : Flipping property: authentication.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
oauth    | 2018-11-30 13:41:59.150  INFO 1 --- [nio-9001-exec-1] c.n.u.concurrent.ShutdownEnabledTimer    : Shutdown hook installed for: NFLoadBalancer-PingTimer-authentication
oauth    | 2018-11-30 13:41:59.221  INFO 1 --- [nio-9001-exec-1] c.netflix.loadbalancer.BaseLoadBalancer  : Client: authentication instantiated a LoadBalancer: DynamicServerListLoadBalancer:{NFLoadBalancer:name=authentication,current list of Servers=[],Load balancer stats=Zone stats: {},Server stats: []}ServerList:null
oauth    | 2018-11-30 13:41:59.237  INFO 1 --- [nio-9001-exec-1] c.n.l.DynamicServerListLoadBalancer      : Using serverListUpdater PollingServerListUpdater
oauth    | 2018-11-30 13:41:59.303  INFO 1 --- [nio-9001-exec-1] c.netflix.config.ChainedDynamicProperty  : Flipping property: authentication.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
oauth    | 2018-11-30 13:41:59.310  INFO 1 --- [nio-9001-exec-1] c.n.l.DynamicServerListLoadBalancer      : DynamicServerListLoadBalancer for client authentication initialized: DynamicServerListLoadBalancer:{NFLoadBalancer:name=authentication,current list of Servers=[{MYSERVERSIP}:9003],Load balancer stats=Zone stats: {defaultzone=[Zone:defaultzone; Instance count:1;   Active connections count: 0;    Circuit breaker tripped count: 0;   Active connections per server: 0.0;]
oauth    | },Server stats: [[Server:{MYSERVERSIP}:9003; Zone:defaultZone;   Total Requests:0;   Successive connection failure:0;    Total blackout seconds:0;   Last connection made:Thu Jan 01 00:00:00 GMT 1970;  First connection made: Thu Jan 01 00:00:00 GMT 1970;    Active Connections:0;   total failure count in last (1000) msecs:0; average resp time:0.0;  90 percentile resp time:0.0;    95 percentile resp time:0.0;    min resp time:0.0;  max resp time:0.0;  stddev resp time:0.0]
oauth    | ]}ServerList:org.springframework.cloud.netflix.ribbon.eureka.DomainExtractingServerList@1c33a40d
oauth    | 2018-11-30 13:42:00.250  INFO 1 --- [erListUpdater-0] c.netflix.config.ChainedDynamicProperty  : Flipping property: authentication.ribbon.ActiveConnectionsLimit to use NEXT property: niws.loadbalancer.availabilityFilteringRule.activeConnectionsLimit = 2147483647
oauth    | 2018-11-30 13:44:06.826  INFO 1 --- [nio-9001-exec-1] o.s.s.o.provider.endpoint.TokenEndpoint  : Handling error: InternalAuthenticationServiceException, I/O error on POST request for "http://authentication/api/authentication/verify": Operation timed out (Connection timed out); nested exception is java.net.ConnectException: Operation timed out (Connection timed out)

我可以从家用计算机访问身份验证微服务,这意味着该端口已打开。我尝试了@LoadBalanced批注,但是不起作用。

这是怎么回事????

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。原来我有一个iptables规则阻止了该请求。因此,如果发生这种情况,请检查iptables规则。