In doing micro using Spring Cloud service development, Eureka Server is often used as a registration center, if configured incorrectly, it may cause some unexpected exception information. The following is an exception because ignoring the configuration eureka.client.service-url.defaultZone a result of my recent encounter, at first glance very Mongolian circle.
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
Abnormalities can be seen from the above, the service has been requested localhost:8761
, and always time-out, I checked the registry addresses of all modules configured, did not find 8761
this port, IDE by global search did not find this port, I started a registry of address port 1000
. Registry Services application.yaml
configuration is as follows
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
The problem here has been unable to continue to look down, I began to suspect that is not the default configuration Eureka own registry address. Google a lot from the Internet and found EurekaClientConfigBean
this class, glanced source, suddenly. To put part of the code of the class
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;
}
...
}
Problems have solutions, because this line of code
public EurekaClientConfigBean() {
this.serviceUrl.put("defaultZone", "http://localhost:8761/eureka/");
...
}
And we application.yaml
are not configured defaultZone
. So we need to configure defaultZone
override its default value. Complete application.yaml
configuration is as follows
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/
Original Address: https: //www.jianshu.com/p/788745f7dc7d