High availability with Vert.x

Vert.x同样支持对module的高可用(high availability (HA))运行.

Automatic failover

假设一个moduleHA一起运行,如果module所在的Vert.x实例运行失败了,那么该module会自动在集群中其他的节点上重新启动,那我们称该modulefail-over

想要moduleHA一起运行,那么只需要在命令行上加上-ha参数

  1. vertx runmod com.acme~my-mod~2.1 -ha

但是想要HA正常工作,你就需要在集群中添加多个Vert.x实例,也就是说必须存在一个已经启动的Vert.x实例

(该实例是否也需要加-ha参数呢?应该是不需要的,要不然就自我矛盾了)

  1. vertx runmod com.acme~my-other-mod~1.1 -ha

如果现在运行着com.acme~my-mod~2.1moduleVert.x实例挂掉了(你可以通过kill -9这个命令进行测试),那么运行着com.acme~my-mod~1.1Vert.x实例会自动开始部署com.acme~my-mod~2.1``module,因此,运行着com.acme~my-mod~1.1Vert.x实例接下来会运行那俩个module

注意:干净地关闭一个Vert.x实例并不会引起failover,例如使用CTRL-C或者kill -SIGINT

你也可以以bare模式运行Vert.x实例。这种模式下运行的Vert.x实例启动时并不会自动运行任何module,they will also failover for nodes in the cluster. 可以通过下面的命令开启一个bare 实例

  1. vertx -ha

当你使用ha选项时,你就不需要指定-cluster选项了,因为当Vert.x实例与HA一起运行时,cluster就默认开启了。

HA groups

当你与HA运行一个Vert.x实例时,你也可以选择指定一个HA groupHA group是在逻辑上将集群中一组节点进行分组。只有同一个HA group组内的节点才能相互failover,也就是说failover是不同横跨HA group的. 如果你不显式指定HA group,那么就会将其分配进默认的__DEFAULT__组里.

当运行module时,通过-hgroup选项指定HA group

  1. vertx runmod com.acme~my-mod~2.1 -ha -hagroup somegroup

下面展示了一些示例:

In console 1:

  1. vertx runmod com.mycompany~my-mod1~1.0 -ha -hagroup g1

In console 2:

  1. vertx runmod com.mycompany~my-mod2~1.0 -ha -hagroup g1

In console 3:

  1. vertx runmod com.mycompany~my-mod3~1.0 -ha -hagroup g2

如果我们把console 1中的实例kill掉,那么该实例会向console 2中的实例进行fail over,但不会向console 3中的实例进行fail over,因为他们没有在一个共同的HA group

如果我们将console 3中的实例kill掉之后,就不会发生fail over了,因为该组中只有一个Vert.x实例

Dealing with network partitions - Quora

HA实现还支持quora

When starting a Vert.x instance you can instruct it that it requires a “quorum” before any HA deployments will be deployed. A quorum is a minimum number of nodes for a particular group in the cluster. Typically you chose your quorum size to Q = 1 + N/2 where N is the number of nodes in the group.

当你开启一个Vert.x实例时,你可以在它进行HA部署之前,引导它进行quorumquorum指的是集群中某个HA group中的最小节点数。一般你可将qurom设定为Q = 1 + N / 2,其中N是HA group中节点的数量。

If there are less than Q nodes in the cluster the HA deployments will undeploy. They will redeploy again if/when a quorum is re-attained. By doing this you can prevent against network partitions, a.k.a. split brain.

如果集群中的节点数小于Q,那么HA部署会undeploy。 如果quorum重新获得后,那么HA会进行重新部署。By doing this you can prevent against network partitions, a.k.a. split brain.

更多信息参考quora

如果想要使用quorum运行Vert.x实例,你只需要在命令行中指定-quorum选项就好:

In console 1:

  1. vertx runmod com.mycompany~my-mod1~1.0 -ha -quorum 3

在console 1中Vert.x实例会启动成功但是,它并不会部署module,因为在集群中只有一个节点

In console 2:

  1. vertx runmod com.mycompany~my-mod2~1.0 -ha -quorum 3

在console 1中Vert.x实例会启动成功但是,它并不会部署module,因为在集群中只有俩个节点

In console 3:

  1. vertx runmod com.mycompany~my-mod3~1.0 -ha -quorum 3

现在,我们有三个节点了,quorum条件达到了。现在module就会自动地部署到他们所在的Vert.x实例上

如果我们close或者kill掉三个节点中的一个,其他节点上所有的module都会自动的解除部署,因为现在quorum不再满足条件。

Quora也可以和HA group一起结合使用。

Logging

每一个verticle实例都可以从它内部检索出属于它自己的logger,至于如何检索就要参考具体语言实现的API了。

默认的log日志是存储在系统临时目录的vertx.log文件中,在Linux中是\tmp.

默认实现使用JUL纪录日志。但是我们可以通过$VERTX_HOME\conf\logging.properties这个属性文件进行修改。

如果你想要使用其他的日志框架,例如log4j,你可以在启动Vert.x的时候,通过系统参数的方式进行指定。

  1. -Dorg.vertx.logger-delegate-factory-class-name=org.vertx.java.core.logging.impl.Log4jLogDelegateFactory

or

  1. -Dorg.vertx.logger-delegate-factory-class-name=org.vertx.java.core.logging.impl.SLF4JLogDelegateFactory

如果你不想要使用Vert.x提供的日志功能也可以,你只需要像平常那样使用你喜欢的日志框架,同时引用相关日志jar包,同时你还要在module里进行配置。