Java Vertx.io群集和服务发现
我正在玩Java Vertx.io群集和服务发现,java,vert.x,vertx-verticle,Java,Vert.x,Vertx Verticle,我正在玩vertx.io,它看起来很棒。现在,我构建了一个由三个Verticle(三个简单的java主fat jar)组成的集群。一个垂直链接公开了一个web界面(一个糟糕的rest api),另外两个垂直链接通过vertx.io的服务发现机制简单地提醒web垂直链接它们处于上升或下降状态。 以下是我的(相关部分)简单的“非网络”垂直图: public class FileReader extends AbstractVerticle { private ServiceDiscovery
vertx.io
,它看起来很棒。现在,我构建了一个由三个Verticle(三个简单的java主fat jar)组成的集群。一个垂直链接公开了一个web界面(一个糟糕的rest api),另外两个垂直链接通过vertx.io
的服务发现机制简单地提醒web垂直链接它们处于上升或下降状态。
以下是我的(相关部分)简单的“非网络”垂直图:
public class FileReader extends AbstractVerticle {
private ServiceDiscovery discovery;
private Logger log = LogManager.getLogger(getClass());
private Record record;
@Override
public void start(Future<Void> startFuture) throws Exception {
record = EventBusService.createRecord(getServiceName(), getServiceAddress(), getClass());
setUpRecord(record);
discovery = ServiceDiscovery.create(vertx);
discovery.publish(record, h -> {
if (h.succeeded()) {
log.info("Record published.");
} else {
log.info("Record not published.", h.cause());
}
});
startFuture.complete();
}
...
@Override
public void stop(Future<Void> stopFuture) throws Exception {
log.info("Stopping verticle.");
discovery.unpublish(record.getRegistration(), h -> {
if (h.succeeded()) {
log.info("Service unpublished.");
stopFuture.complete();
} else {
log.error(h.cause());
stopFuture.fail(h.cause());
}
});
}
}
当“非web”垂直链接启动时,“web”垂直链接将得到正确的通知。但是当“非网络”垂直链接关闭时,我按了一个键盘Ctrl-C
,我得到了这个错误,“网络”垂直链接仍然认为每个人都在上:
2017-12-01 09:08:27 INFO FileReader:31 - Undeploying 82a8f5c2-e6a2-4fc3-84ff-4bb095b5dc43
Exception in thread "Thread-3" java.lang.IllegalStateException: Shutdown in progress
at java.lang.ApplicationShutdownHooks.add(ApplicationShutdownHooks.java:66)
at java.lang.Runtime.addShutdownHook(Runtime.java:211)
at io.vertx.core.impl.FileResolver.setupCacheDir(FileResolver.java:310)
at io.vertx.core.impl.FileResolver.<init>(FileResolver.java:92)
at io.vertx.core.impl.VertxImpl.<init>(VertxImpl.java:185)
at io.vertx.core.impl.VertxImpl.<init>(VertxImpl.java:144)
at io.vertx.core.impl.VertxImpl.<init>(VertxImpl.java:140)
at io.vertx.core.impl.VertxFactoryImpl.vertx(VertxFactoryImpl.java:34)
at io.vertx.core.Vertx.vertx(Vertx.java:82)
at edu.foo.app.FileReaderApp$1.run(FileReaderApp.java:32)
2017-12-01 09:08:27信息文件读取器:31-取消部署82a8f5c2-e6a2-4fc3-84ff-4bb095b5dc43
线程“thread-3”java.lang.IllegalStateException中的异常:正在关闭
在java.lang.ApplicationShutdownHooks.add(ApplicationShutdownHooks.java:66)中
在java.lang.Runtime.addShutdownHook(Runtime.java:211)中
位于io.vertx.core.impl.FileResolver.setupCacheDir(FileResolver.java:310)
位于io.vertx.core.impl.FileResolver。(FileResolver.java:92)
位于io.vertx.core.impl.VertxImpl.(VertxImpl.java:185)
位于io.vertx.core.impl.VertxImpl.(VertxImpl.java:144)
位于io.vertx.core.impl.VertxImpl.(VertxImpl.java:140)
位于io.vertx.core.impl.VertxFactoryImpl.vertx(VertxFactoryImpl.java:34)
位于io.vertx.core.vertx.vertx(vertx.java:82)
位于edu.foo.app.FileReaderApp$1.run(FileReaderApp.java:32)
我不完全明白发生了什么。应用程序在取消部署verticle时关闭?如何解决这个问题?什么是vertx.io方法?有两个问题
undeploy
是一个非阻塞操作,因此关闭挂钩线程必须等待完成Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
log.info("Undeploying " + id);
CountDownLatch latch = new CountDownLatch(1);
theClusteredVertxInstance.undeploy(id, h -> {
if (h.succeeded()) {
log.info("undeployed.");
} else {
log.error(h.cause());
}
latch.countDown();
});
try {
latch.await(5, TimeUnit.SECONDS);
} catch(Exception ignored) {
}
}
});
我不确定这是否是一个问题,但我认为您应该使用群集vertx实例而不是
vertx.vertx()
@Xargos取消部署。使用相同实例不会引发异常,但不会调用verticle的stop方法。调用stop()告诉发现服务verticle已经消失,这一点很重要。如果您更改了代码,请添加更新的版本。如果你的意思是在竖线上的stop()
,那么就不是了。文档中明确说明不要自己调用它。
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
log.info("Undeploying " + id);
CountDownLatch latch = new CountDownLatch(1);
theClusteredVertxInstance.undeploy(id, h -> {
if (h.succeeded()) {
log.info("undeployed.");
} else {
log.error(h.cause());
}
latch.countDown();
});
try {
latch.await(5, TimeUnit.SECONDS);
} catch(Exception ignored) {
}
}
});