配置信息

eureka-server

spring.application.name=eureka-server
server.port=1111

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false

eureka-client

spring.application.name=eureka-client
server.port=8002
eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

错误分析

如上配置,配置信息非常简单.在启动eureka-server的时候并未发现任何异常,但是在启动eureka-client的时候,控制台却输出如下信息:

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.7.0.jar:1.7.0]
    at com.netflix.discovery.EurekaIdentityHeaderFilter.handle(EurekaIdentityHeaderFilter.java:27) ~[eureka-client-1.7.0.jar:1.7.0]
    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.7.0.jar:1.7.0]
    at com.netflix.eureka.cluster.ReplicationTaskProcessor.process(ReplicationTaskProcessor.java:71) ~[eureka-core-1.7.0.jar:1.7.0]
    at com.netflix.eureka.util.batcher.TaskExecutors$BatchWorkerRunnable.run(TaskExecutors.java:187) [eureka-core-1.7.0.jar:1.7.0]

看着这段揪心的提示后反复检查了配置和代码,实在想不明白哪个地方有配置8761端口号.
在这个时候打开注册中心localhost:1111后也看到eureka-client服务也注册上来了,那为什么会报这个错了,莫非eureka有自己的默认配置?

在一番搜索后得到了答案(问题传送门),因为我并未指定eureka-serverservice-url属性,所以在服务注册过来的时候eureka-server会尝试将注册信息复制到默认的service-url,即 localhost:8761上面.所以才会报出这样的错误!

解决方案

既然上面分析出来了问题,那么解决就很容易 ,即覆盖eureka-server的默认的属性service-url就可以了,在eureka-server添加如下代码:

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/

默认配置

  在得知上面的解决方案后,不禁思考eureka的这些默认配置在哪里呢?他们的默认属性值又是多少?
eureka的这些默认配置是存在org.springframework.cloud.netflix.eureka.EurekaClientConfigBean类里面.

@Data
@ConfigurationProperties(EurekaClientConfigBean.PREFIX)
public class EurekaClientConfigBean implements EurekaClientConfig, EurekaConstants {

    public static final String PREFIX = "eureka.client";

    @Autowired(required = false)
    PropertyResolver propertyResolver;

    public static final String DEFAULT_URL = "http://localhost:8761" + DEFAULT_PREFIX
            + "/";
    public static final String DEFAULT_ZONE = "defaultZone";

    private static final int MINUTES = 60;

    private boolean enabled = true;

    @NestedConfigurationProperty
    private EurekaTransportConfig transport = new CloudEurekaTransportConfig();

    private int registryFetchIntervalSeconds = 30;


    private int instanceInfoReplicationIntervalSeconds = 30;


    private int initialInstanceInfoReplicationIntervalSeconds = 40;


    private int eurekaServiceUrlPollIntervalSeconds = 5 * MINUTES;


    private String proxyPort;

    private String proxyHost;


    private String proxyUserName;

    private String proxyPassword;


    private int eurekaServerReadTimeoutSeconds = 8;

    private int eurekaServerConnectTimeoutSeconds = 5;

    private String backupRegistryImpl;

    private int eurekaServerTotalConnections = 200;

    private int eurekaServerTotalConnectionsPerHost = 50;

    private String eurekaServerURLContext;

    private String eurekaServerPort;

    private String eurekaServerDNSName;

    private String region = "us-east-1";

  //......略

看到这里一切都豁然开朗了


时光沉旧了少年
95 声望23 粉丝

有些人不离开你,你永远都长不大