Vertx.io 集群和服务发现
Vertx.io cluster and service discovery
我正在玩vertx.io
,看起来很棒。现在我建立了一个由三个 Verticle 组成的集群(三个简单的 java 主胖罐)。一个 Verticle 公开了一个 Web 界面(休息不佳 api),另外两个只是通过 vertx.io
的服务发现机制知道 Web Verticle 它们是启动还是关闭。
这是我的(相关部分)简单的 "non web" verticle:
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());
}
});
}
}
下面是我如何部署两个 "non web" verticles 之一:
public class FileReaderApp {
private static Logger log = LogManager.getLogger(FileReaderApp.class);
private static String id;
public static void main(String[] args) {
ClusterManager cMgr = new HazelcastClusterManager();
VertxOptions vOpt = new VertxOptions(new JsonObject());
vOpt.setClusterManager(cMgr);
Vertx.clusteredVertx(vOpt, ch -> {
if (ch.succeeded()) {
log.info("Deploying file reader.");
Vertx vertx = ch.result();
vertx.deployVerticle(new FileReader(), h -> {
if (h.succeeded()) {
id = h.result();
} else {
log.error(h.cause());
}
});
} else {
log.error(ch.cause());
}
});
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
log.info("Undeploying " + id);
Vertx.vertx().undeploy(id, h -> {
if (h.succeeded()) {
log.info("undeployed.");
} else {
log.error(h.cause());
}
});
}
});
}
}
当 "non-web" 个 Verticle 开始时,"web" 个 Verticle 会被正确通知。但是当 "non-web" verticle 关闭时,我按下键盘 Ctrl-C
,我得到了这个错误并且 "web" verticle 仍然认为每个人都在:
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.run(FileReaderApp.java:32)
我不完全明白发生了什么。应用程序在取消部署 Verticle 时关闭?如何解决这个问题? vertx.io 方法是什么?
有两个问题
- 您应该使用集群 Vert.x 实例取消部署 Verticle,而不仅仅是任何实例
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.io
,看起来很棒。现在我建立了一个由三个 Verticle 组成的集群(三个简单的 java 主胖罐)。一个 Verticle 公开了一个 Web 界面(休息不佳 api),另外两个只是通过 vertx.io
的服务发现机制知道 Web Verticle 它们是启动还是关闭。
这是我的(相关部分)简单的 "non web" verticle:
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());
}
});
}
}
下面是我如何部署两个 "non web" verticles 之一:
public class FileReaderApp {
private static Logger log = LogManager.getLogger(FileReaderApp.class);
private static String id;
public static void main(String[] args) {
ClusterManager cMgr = new HazelcastClusterManager();
VertxOptions vOpt = new VertxOptions(new JsonObject());
vOpt.setClusterManager(cMgr);
Vertx.clusteredVertx(vOpt, ch -> {
if (ch.succeeded()) {
log.info("Deploying file reader.");
Vertx vertx = ch.result();
vertx.deployVerticle(new FileReader(), h -> {
if (h.succeeded()) {
id = h.result();
} else {
log.error(h.cause());
}
});
} else {
log.error(ch.cause());
}
});
Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
log.info("Undeploying " + id);
Vertx.vertx().undeploy(id, h -> {
if (h.succeeded()) {
log.info("undeployed.");
} else {
log.error(h.cause());
}
});
}
});
}
}
当 "non-web" 个 Verticle 开始时,"web" 个 Verticle 会被正确通知。但是当 "non-web" verticle 关闭时,我按下键盘 Ctrl-C
,我得到了这个错误并且 "web" verticle 仍然认为每个人都在:
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.run(FileReaderApp.java:32)
我不完全明白发生了什么。应用程序在取消部署 Verticle 时关闭?如何解决这个问题? vertx.io 方法是什么?
有两个问题
- 您应该使用集群 Vert.x 实例取消部署 Verticle,而不仅仅是任何实例
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) {
}
}
});