1
我嘗試使用館長和Zookeeper進行服務發現。動物園管理員服務發現連接強制關閉
當我嘗試註冊我的服務時發生以下情況。
2014-09-23 16:36:09,755 [myid:] - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:[email protected]] - Accepted socket connection from /127.0.0.1:52026
2014-09-23 16:36:09,759 [myid:] - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:[email protected]] - Client attempting to establish new session at /127.0.0.1:52026
2014-09-23 16:36:09,790 [myid:] - INFO [SyncThread:0:[email protected]] - Established session 0x148a17bad590007 with negotiated timeout 40000 for client /127.0.0.1:52026
2014-09-23 16:36:11,735 [myid:] - WARN [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:[email protected]] - Exception causing close of session 0x148a17bad590007 due to java.io.IOException: An existing con
nection was forcibly closed by the remote host
2014-09-23 16:36:11,736 [myid:] - INFO [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:[email protected]] - Closed socket connection for client /127.0.0.1:52026 which had sessionid 0x148a17bad590007
2014-09-23 16:36:32,001 [myid:] - INFO [SessionTracker:[email protected]] - Expiring session 0x148a17bad590006, timeout of 40000ms exceeded
2014-09-23 16:36:32,001 [myid:] - INFO [ProcessThread(sid:0 cport:-1)::[email protected]] - Processed session termination for sessionid: 0x148a17bad590006
2014-09-23 16:36:52,000 [myid:] - INFO [SessionTracker:[email protected]] - Expiring session 0x148a17bad590007, timeout of 40000ms exceeded
2014-09-23 16:36:52,001 [myid:] - INFO [ProcessThread(sid:0 cport:-1)::[email protected]] - Processed session termination for sessionid: 0x148a17bad590007
相關的代碼如下:(在getDiscovery方法是,這導致上述錯誤的那個)
private ServiceDiscovery<InstanceDetails> getDiscovery() {
return ServiceDiscoveryBuilder.builder(InstanceDetails.class)
.basePath(Config.basePath)
.client(curatorFramework)
.serializer(jacksonInstanceSerializer)
.build();
}
我用這在以下:
public void advertiseAvailability() {
try {
ServiceDiscovery<InstanceDetails> discovery = getDiscovery();
discovery.start();
discovery.registerService(getInstance()); // getInstance returns a ServiceInstance<InstanceDetails>
discovery.close();
System.out.println("Advertised");
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
以下是配置
public class Config{
public static final String basePath = "/";
public static final String serviceName = "myTestService1";
public static final String address = "127.0.0.1";
public static final int port = 2181;
}
下面是需要curatorFramework
CuratorFramework curatorFramework;
curatorFramework = CuratorFrameworkFactory.builder()
.connectionTimeoutMs(1000)
.retryPolicy(new RetryNTimes(10, 500))
.connectString(Config.address+":"+Config.port)
.build();
curatorFramework.start();
new EnsurePath(Config.basePath).ensure(curatorFramework.getZookeeperClient());
的初始化和jacksonInstanceSerializer如下:
this.jacksonInstanceSerializer = instanceSerializerFactory.getInstanceSerializer(
new TypeReference<ServiceInstance<InstanceDetails>>() {}
);
其中instanceSerializerFactory是:
public class InstanceSerializerFactory {
private final ObjectReader objectReader;
private final ObjectWriter objectWriter;
public InstanceSerializerFactory(ObjectReader objectReader, ObjectWriter objectWriter) {
this.objectReader = objectReader;
this.objectWriter = objectWriter;
}
public <T> InstanceSerializer<T> getInstanceSerializer(
TypeReference<ServiceInstance<T>> typeReference) {
return new JacksonInstanceSerializer<T>(objectReader, objectWriter, typeReference);
}
}
final class JacksonInstanceSerializer<T> implements InstanceSerializer<T> {
private final TypeReference<ServiceInstance<T>> typeRef;
private final ObjectWriter objectWriter;
private final ObjectReader objectReader;
JacksonInstanceSerializer(ObjectReader objectReader, ObjectWriter objectWriter,
TypeReference<ServiceInstance<T>> typeRef) {
this.objectReader = objectReader;
this.objectWriter = objectWriter;
this.typeRef = typeRef;
}
注:實例詳細內容吧我正在註冊(服務名稱和地址等)
看起來您正在使用它後立即關閉Discovery實例。這沒有意義。 – Randgalt 2014-09-23 18:49:29