简体   繁体   English

您认为 Vert.x 中的垂直设计指南是什么?

[英]What do you consider as verticle design guideline in Vert.x?

While the core manual (and other documentation) of Vert.x shows several use cases and gives good explanations of Vert.x in general, I am curious what are do and donts when designing verticle classes.虽然 Vert.x 的核心手册(和其他文档)展示了几个用例并对 Vert.x 给出了很好的解释,但我很好奇在设计 Verticle 类时应该做什么和不应该做什么。

Preword : I am aware of that the design of Vert.x is in general AGAINST giving strict design guidelines.前言:我知道 Vert.x 的设计通常是反对给出严格的设计指南的。 So, no need to mention this in answers.因此,无需在答案中提及这一点。

An example that led me to this question is as following.导致我提出这个问题的一个例子如下。 I created a verticle named ServiceDiscoveryVerticle.java which has the following responsibilities:我创建了一个名为ServiceDiscoveryVerticle.java的 Verticle,它具有以下职责:

  • read in a configuration file of services and then publish them via the Vert.x ServiceDiscovery读入服务的配置文件,然后通过 Vert.x ServiceDiscovery 发布它们
  • manage services additionaly in lists(published/unpublished) to keep track of unpublished ones在列表中额外管理服务(已发布/未发布)以跟踪未发布的服务
  • receive messages via the event bus, for either publishing or unpublishing a certain service通过事件总线接收消息,用于发布或取消发布某个服务

All this is code is manifested in the overriden start method.所有这些代码都体现在重写的 start 方法中。

So the core questions I ask in this questions are:所以我在这个问题中提出的核心问题是:

  1. What are do and donts when designing verticle classes?设计 Verticle 类时做什么和不做什么? (by your personal prefence/opinion) (根据您的个人喜好/意见)
  2. Are there any general guidelines of what belong to a verticle and what not?是否有关于什么属于 Verticle 什么不属于什么的一般准则? (officialy or community-wise) (官方或社区)
  3. Is it recommandable to split the start method up into private methods (if so, should it be in the same class or better put in a seperate one like OwnServiceDiscovery.java )?是否可以将启动方法拆分为私有方法(如果是这样,它应该在同一个 class 中还是更好地放在一个单独的方法中,例如OwnServiceDiscovery.java )?
  4. Any other ideas/remarks on my given example( ServiceDiscoveryVerticle.java )?关于我给定的示例( ServiceDiscoveryVerticle.java )的任何其他想法/评论?

One can do a lot of philosophy here, but I will try to keep it simple.在这里可以做很多哲学,但我会尽量保持简单。

The fact is that a verticle and its start() is and will be the main way that you init your system, mount handlers, trigger things like loading config and co.事实上,verticle 及其start()是并且将是您初始化系统、挂载处理程序、触发加载配置和 co 等事情的主要方式。 So don't be too hard on yourself, this part is correct.所以不要对自己太苛刻,这部分是正确的。

If you are using Web Service API or Service Proxy then handlers are mounted automatically for you.如果您使用的是 Web 服务 API 或服务代理,则会自动为您安装处理程序。 The actual code of these handlers are in external classes that you can decide on how to structure them.这些处理程序的实际代码位于外部类中,您可以决定如何构建它们。

If you are mounting your handlers on your own, then you can use a lot of inline code, or you can decide to extract them into classes.如果您自己安装处理程序,那么您可以使用大量内联代码,或者您可以决定将它们提取到类中。 In a lager application however you will probably split and extract code as much as you can.然而,在更大的应用程序中,您可能会尽可能多地拆分和提取代码。

I personally extract code out of verticle as much as I can and make it a rather coordination and setup place.我个人尽可能多地从 Verticle 中提取代码,并使其成为一个相当协调和设置的地方。 Also my start() method (or rather rxStart() ) is a bunch of calls to other methods who's names give me an overview of what is going on in the start of system rather than having a lot of code that I can't read.另外,我的start()方法(或者更确切地说rxStart() )是对其他方法的一堆调用,这些方法的名字让我对系统启动时发生的事情有一个概述,而不是有很多我看不懂的代码. But these are all personal preferences as you said.但正如你所说,这些都是个人喜好。 Vert.x does not imply any of it on you! Vert.x 并不暗示你!

  1. Don't block the Event Loop不要阻塞事件循环
  2. Don't call verticle from another verticle, use EventBus不要从另一个verticle调用verticle,使用EventBus
  3. If you're using executeBlocking, you're probably doing something wrong如果你使用 executeBlocking,你可能做错了什么
  4. If you're constantly deploying/undeploying verticles, you're probably doing something wrong如果您不断部署/取消部署 Verticle,您可能做错了什么
  5. Don't share state using verticles不要使用 verticles 共享 state
  6. Keep your verticles small, but not too small保持你的verticles小,但不要太小

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM