Eureka配置问题

在使用Spring Cloud做微服务开发中,经常会使用Eureka Server作为注册中心,如果配置不当可能会导致一些不可预期的异常信息。以下是我最近遇到的因为忽略了配置eureka.client.service-url.defaultZone而导致的异常,乍一看很蒙圈。

com.sun.jersey.api.client.ClientHandlerException: org.apache.http.conn.ConnectTimeoutException: Connect to localhost:8761 timed out
    at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:187) ~[jersey-apache-client4-1.19.1.jar:1.19.1]
    at com.netflix.eureka.cluster.DynamicGZIPContentEncodingFilter.handle(DynamicGZIPContentEncodingFilter.java:48) ~[eureka-core-1.8.7.jar:1.8.7]
    at com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFilter.java:27) ~[eureka-client-1.8.7.jar:1.8.7]
    at com.sun.jersey.api.client.Client.handle(Client.java:652) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.sun.jersey.api.client.WebResource.handle(WebResource.java:682) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.sun.jersey.api.client.WebResource.access$200(WebResource.java:74) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.sun.jersey.api.client.WebResource$Builder.post(WebResource.java:570) ~[jersey-client-1.19.1.jar:1.19.1]
    at com.netflix.eureka.transport.JerseyReplicationClient.submitBatchUpdates(JerseyReplicationClient.java:116) ~[eureka-core-1.8.7.jar:1.8.7]
    at com.netflix.eureka.cluster.ReplicationTaskProcessor.process(ReplicationTaskProcessor.java:80) ~[eureka-core-1.8.7.jar:1.8.7]
    at com.netflix.eureka.util.batcher.TaskExecutors$BatchWorkerRunnable.run(TaskExecutors.java:187) [eureka-core-1.8.7.jar:1.8.7]
    at java.lang.Thread.run(Thread.java:748) [na:1.8.0_144]
Caused by: org.apache.http.conn.ConnectTimeoutException: Connect to localhost:8761 timed out
    at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:123) ~[httpclient-4.5.5.jar:4.5.5]
    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:180) ~[httpclient-4.5.5.jar:4.5.5]
    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:144) ~[httpclient-4.5.5.jar:4.5.5]
    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:134) ~[httpclient-4.5.5.jar:4.5.5]
    at org.apache.http.impl.client.DefaultRequestDirector.tryConnect(DefaultRequestDirector.java:610) ~[httpclient-4.5.5.jar:4.5.5]
    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:445) ~[httpclient-4.5.5.jar:4.5.5]
    at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:835) ~[httpclient-4.5.5.jar:4.5.5]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:118) ~[httpclient-4.5.5.jar:4.5.5]
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:56) ~[httpclient-4.5.5.jar:4.5.5]
    at com.sun.jersey.client.apache4.ApacheHttpClient4Handler.handle(ApacheHttpClient4Handler.java:173) ~[jersey-apache-client4-1.19.1.jar:1.19.1]
    ... 10 common frames omitted

从以上异常可以看到,服务一直在请求localhost:8761,而且始终超时,我检查了所有模块的注册中心地址配置,都没有发现8761这个端口,通过IDE全局搜索也没有找到这个端口,我启动的注册中心地址的端口为1000。注册中心服务application.yaml配置如下

spring:
  application:
    name: eureka-server
server:
  port: 1000
eureka:
  instance:
    # 服务刷新时间配置,每隔这个时间会主动心跳一次
    # 默认30s
    lease-renewal-interval-in-seconds: 5
    # 服务过期时间配置,超过这个时间没有接收到心跳EurekaServer就会将这个实例剔除
    # 注意,EurekaServer一定要设置eureka.server.eviction-interval-timer-in-ms否则这个配置无效,这个配置一般为服务刷新时间配置的三倍
    # 默认90s
    lease-expiration-duration-in-seconds: 15
  server:
    ####关闭自我保护机制
    enable-self-preservation: false
    # 默认180s
    # eureka server缓存readWriteCacheMap失效时间,这个只有在这个时间过去后缓存才会失效,失效前不会更新,过期后从registry重新读取注册服务信息,registry是一个ConcurrentHashMap。
    # 由于启用了evict其实就用不太上改这个配置了
    response-cache-auto-expiration-in-seconds: 60
    # 启用主动失效,并且每次主动失效检测间隔为3s
    eviction-interval-timer-in-ms: 10000
  client:
    register-with-eureka: false
    fetch-registry: false

问题到这里已经无法继续查找下去了,于是我开始怀疑是不是Eureka自己配置了默认的注册中心地址。从网上Google了一番,发现了EurekaClientConfigBean这个类,浏览了一下源码,豁然开朗。先贴出这个类的部分代码

package org.springframework.cloud.netflix.eureka;
import ...
@ConfigurationProperties("eureka.client")
public class EurekaClientConfigBean implements EurekaClientConfig {
    public static final String PREFIX = "eureka.client";
    public static final String DEFAULT_URL = "http://localhost:8761/eureka/";
    public static final String DEFAULT_ZONE = "defaultZone";
    private static final int MINUTES = 60;
    @Autowired(
        required = false
    )
    .....
    public EurekaClientConfigBean() {
        this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/");
        this.gZipContent = true;
        this.useDnsForFetchingServiceUrls = false;
        this.registerWithEureka = true;
        this.preferSameZoneEureka = true;
        this.availabilityZones = new HashMap();
        this.filterOnlyUpInstances = true;
        this.fetchRegistry = true;
        this.dollarReplacement = "_-";
        this.escapeCharReplacement = "__";
        this.allowRedirects = false;
        this.onDemandUpdateStatusChange = true;
        this.clientDataAccept = EurekaAccept.full.name();
        this.shouldUnregisterOnShutdown = true;
        this.shouldEnforceRegistrationAtInit = false;
    }
    ...
}

问题得解,就是因为下面这行代码

public EurekaClientConfigBean() {
  this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/");
  ...
}

而我们的application.yaml中并没有配置defaultZone 。因此我们需要配置defaultZone 覆盖其默认值。完整application.yaml配置如下

spring:
  application:
    name: eureka-server
server:
  port: 1000
eureka:
  instance:
    # 服务刷新时间配置,每隔这个时间会主动心跳一次
    # 默认30s
    lease-renewal-interval-in-seconds: 5
    # 服务过期时间配置,超过这个时间没有接收到心跳EurekaServer就会将这个实例剔除
    # 注意,EurekaServer一定要设置eureka.server.eviction-interval-timer-in-ms否则这个配置无效,这个配置一般为服务刷新时间配置的三倍
    # 默认90s
    lease-expiration-duration-in-seconds: 15
  server:
    ####关闭自我保护机制
    enable-self-preservation: false
    # 默认180s
    # eureka server缓存readWriteCacheMap失效时间,这个只有在这个时间过去后缓存才会失效,失效前不会更新,过期后从registry重新读取注册服务信息,registry是一个ConcurrentHashMap。
    # 由于启用了evict其实就用不太上改这个配置了
    response-cache-auto-expiration-in-seconds: 60
    # 启用主动失效,并且每次主动失效检测间隔为3s
    eviction-interval-timer-in-ms: 10000
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://localhost:1000/eureka/
原文地址:https://www.jianshu.com/p/788745f7dc7d
posted @ 2019-08-07 14:31  星朝  阅读(697)  评论(0编辑  收藏  举报