High availability with Vert.x
Vert.x
同样支持对module
的高可用(high availability (HA)
)运行.
Automatic failover
假设一个module
与HA
一起运行,如果module
所在的Vert.x
实例运行失败了,那么该module
会自动在集群中其他的节点上重新启动,那我们称该module
为fail-over
想要module
与HA
一起运行,那么只需要在命令行上加上-ha
参数
vertx runmod com.acme~my-mod~2.1 -ha
但是想要HA
正常工作,你就需要在集群中添加多个Vert.x
实例,也就是说必须存在一个已经启动的Vert.x
实例
(该实例是否也需要加-ha参数呢?应该是不需要的,要不然就自我矛盾了)
vertx runmod com.acme~my-other-mod~1.1 -ha
如果现在运行着com.acme~my-mod~2.1
的module
的Vert.x
实例挂掉了(你可以通过kill -9
这个命令进行测试),那么运行着com.acme~my-mod~1.1
的Vert.x
实例会自动开始部署com.acme~my-mod~2.1``module
,因此,运行着com.acme~my-mod~1.1
的Vert.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 实例
vertx -ha
当你使用ha
选项时,你就不需要指定-cluster
选项了,因为当Vert.x
实例与HA
一起运行时,cluster就默认开启了。
HA groups
当你与HA
运行一个Vert.x
实例时,你也可以选择指定一个HA group
。 HA group
是在逻辑上将集群中一组节点进行分组。只有同一个HA group
组内的节点才能相互failover
,也就是说failover
是不同横跨HA group
的. 如果你不显式指定HA group
,那么就会将其分配进默认的__DEFAULT__
组里.
当运行module
时,通过-hgroup
选项指定HA group
vertx runmod com.acme~my-mod~2.1 -ha -hagroup somegroup
下面展示了一些示例:
In console 1:
vertx runmod com.mycompany~my-mod1~1.0 -ha -hagroup g1
In console 2:
vertx runmod com.mycompany~my-mod2~1.0 -ha -hagroup g1
In console 3:
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
部署之前,引导它进行quorum
。quorum
指的是集群中某个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:
vertx runmod com.mycompany~my-mod1~1.0 -ha -quorum 3
在console 1中Vert.x
实例会启动成功但是,它并不会部署module
,因为在集群中只有一个节点
In console 2:
vertx runmod com.mycompany~my-mod2~1.0 -ha -quorum 3
在console 1中Vert.x
实例会启动成功但是,它并不会部署module
,因为在集群中只有俩个节点
In console 3:
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
的时候,通过系统参数的方式进行指定。
-Dorg.vertx.logger-delegate-factory-class-name=org.vertx.java.core.logging.impl.Log4jLogDelegateFactory
or
-Dorg.vertx.logger-delegate-factory-class-name=org.vertx.java.core.logging.impl.SLF4JLogDelegateFactory
如果你不想要使用Vert.x
提供的日志功能也可以,你只需要像平常那样使用你喜欢的日志框架,同时引用相关日志jar包,同时你还要在module
里进行配置。