Spring 微服务实例未向在 8761 以外的端口中运行的 Eureka 注册

Posted

技术标签:

【中文标题】Spring 微服务实例未向在 8761 以外的端口中运行的 Eureka 注册【英文标题】:Spring microservice instance is not registering with Eureka running in port other than 8761 【发布时间】:2019-08-11 05:16:53 【问题描述】:

我的微服务的application.yml(Eureka 的客户端)

spring:
  application:
    name: queue1

eureka:
  client:
    registerWithEureka: true
    fetchRegistry: true  
    serviceUrl:
      defaultZone: http://localhost:8762/

server:
  port: 7777

app:
  id: $random.uuid

尤里卡的application.yml

eureka:
  instance:
    hostname: peer1
  client:
    register-with-eureka: false
    fetch-registry: false
server:
  port: 8762

我正在端口 8762 中启动 Eureka,并在我的微服务中引用它。 微服务无法向 Eureka 注册。 以下是微服务日志中的异常。

2019-03-21 17:02:16.248 ERROR 1804 --- [freshExecutor-0] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_ZUUL_RIBBON/ICB8061.ibsplc.com:zuul_ribbon:8080 - 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
    at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:112) ~[eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134) ~[eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$6.execute(EurekaHttpClientDecorator.java:137) ~[eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) ~[eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.getApplications(EurekaHttpClientDecorator.java:134) ~[eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.DiscoveryClient.getAndStoreFullRegistry(DiscoveryClient.java:1051) [eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.DiscoveryClient.fetchRegistry(DiscoveryClient.java:965) [eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.DiscoveryClient.refreshRegistry(DiscoveryClient.java:1479) [eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.DiscoveryClient$CacheRefreshThread.run(DiscoveryClient.java:1446) [eureka-client-1.9.8.jar:1.9.8]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [na:1.8.0_202]
    at java.util.concurrent.FutureTask.run(Unknown Source) [na:1.8.0_202]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_202]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_202]
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_202]



2019-03-21 17:02:16.278 ERROR 1804 --- [tbeatExecutor-0] c.n.d.s.t.d.RedirectingEurekaHttpClient  : Request execution error. endpoint=DefaultEndpoint serviceUrl='http://localhost:8762/

javax.ws.rs.WebApplicationException: com.fasterxml.jackson.databind.exc.MismatchedInputException: Root name 'timestamp' does not match expected ('instance') for type [simple type, class com.netflix.appinfo.InstanceInfo]
 at [Source: (com.sun.jersey.client.apache4.ApacheHttpClient4Handler$HttpClientResponseInputStream); line: 1, column: 2]
    at com.netflix.discovery.provider.DiscoveryJerseyProvider.readFrom(DiscoveryJerseyProvider.java:110) ~[eureka-client-1.9.8.jar:1.9.8]
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:634) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.sun.jersey.api.client.ClientResponse.getEntity(ClientResponse.java:586) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.netflix.discovery.shared.transport.jersey.AbstractJerseyEurekaHttpClient.sendHeartBeat(AbstractJerseyEurekaHttpClient.java:105) ~[eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$3.execute(EurekaHttpClientDecorator.java:92) [eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.shared.transport.decorator.MetricsCollectingEurekaHttpClient.execute(MetricsCollectingEurekaHttpClient.java:73) ~[eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.sendHeartBeat(EurekaHttpClientDecorator.java:89) [eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$3.execute(EurekaHttpClientDecorator.java:92) [eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.shared.transport.decorator.RedirectingEurekaHttpClient.execute(RedirectingEurekaHttpClient.java:89) ~[eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.sendHeartBeat(EurekaHttpClientDecorator.java:89) [eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$3.execute(EurekaHttpClientDecorator.java:92) [eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.shared.transport.decorator.RetryableEurekaHttpClient.execute(RetryableEurekaHttpClient.java:120) [eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.sendHeartBeat(EurekaHttpClientDecorator.java:89) [eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator$3.execute(EurekaHttpClientDecorator.java:92) [eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.shared.transport.decorator.SessionedEurekaHttpClient.execute(SessionedEurekaHttpClient.java:77) [eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator.sendHeartBeat(EurekaHttpClientDecorator.java:89) [eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.DiscoveryClient.renew(DiscoveryClient.java:846) [eureka-client-1.9.8.jar:1.9.8]
    at com.netflix.discovery.DiscoveryClient$HeartbeatThread.run(DiscoveryClient.java:1405) [eureka-client-1.9.8.jar:1.9.8]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [na:1.8.0_202]
    at java.util.concurrent.FutureTask.run(Unknown Source) [na:1.8.0_202]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [na:1.8.0_202]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [na:1.8.0_202]
    at java.lang.Thread.run(Unknown Source) [na:1.8.0_202]

现在最重要的是,如果我将Eureka服务器端口和微服务的Eureka注册端口更改为8761,一切都很好!!!!本例微服务注册成功。

在发布问题之前多次执行此操作,它的行为是这样的。

这里的任何输入都会非常有帮助。

【问题讨论】:

我使用的springboot版本是2.1.3.RELEASE。和云版本:Greenwich.RELEASE 我有 defaultZone: localhost:8761/eureka 也许试试 defaultZone: localhost:8762/eureka ? 这行得通! defaultZone 属性中应该有 eureka。非常感谢马克思...... 没有让它更清楚。 defaultZone 应该是“localhost:8762/eureka”而不是“localhost:8762”。当使用端口 8761 并且 defaultZone 值为“localhost:8761”时 - 注册工作正常。在这种情况下,我猜,在内部,eureka 使用 url "localhost:8761/eureka" 而不是属性文件 "localhost:8761" 中给出的内容 /eureka 对我有用,但并非在所有情况下。我有一个 Spring Boot 模块版本 2.1.5 与 localhost:9100 工作正常,另一个需要 localhost:9100/eureka 的 Spring Boot 版本 2.1.3 否则会引发相同的 MismatchedInputException 错误 【参考方案1】:

我遇到了同样的问题,这是由于 Eureka 服务器项目中的 spring 安全依赖项。将@EnableWebSecurity 代码添加到为我修复的eureka 服务器应用程序之后。

    @EnableWebSecurity
   static class WebSecurityConfig extends WebSecurityConfigurerAdapter 
        @Override
        protected void configure(HttpSecurity http) throws Exception 
            http.csrf().disable();
        
    

Spring Boot 版本 - 2.3.2.RELEASE Spring Cloud 版本 - Hoxton.SR1 https://github.com/spring-cloud/spring-cloud-netflix/issues/2754#issuecomment-372808529

【讨论】:

以上是关于Spring 微服务实例未向在 8761 以外的端口中运行的 Eureka 注册的主要内容,如果未能解决你的问题,请参考以下文章

3Spring Cloud - Eureka(高可用Eureka Server集群)

一本专为Java/Spring开发人员准备的微服务实战指南

Spring Cloud微服务实战-服务治理(Spring Cloud Eureka)

Spring Cloud微服务实战教程|Spring Cloud教程

Spring Cloud微服务实战pdf高清版免费下载

PK1648-Spring Cloud微服务实战视频