vertx 上的最佳实践设计。存储库的 Eventbus 或 Singleton

Best practice design on vertx. Eventbus or Singleton for repositories

我正在使用 vertx3

我需要用到redis来设置和获取值。(Redis以后可能会换成其他东西)

我正在为我的实施寻找最佳实践设计。

我正在处理 vertx 集群,我需要通过事件总线检索消息,提取消息并插入 Redis。

另一方面,我可以通过网络获取请求,也可以提取消息并将它们插入到 redis

两个选项:

  1. 我是否应该有一个"redis-verticle"通过总线获取消息并写入它们。

  2. 我是否应该创建一个 "Listener verticle" 来保存 DAO,DAO 将保存 RedisRepo 对象来写入它们。

    我也将能够处理网络调用并持有这个 DAO 对象

如果我在 spring-app 上,我会创建一个包含 RedisRepo 的 DAO 并将其注入我的服务层,但这里我们有事件总线,所以我不确定。

(顺便说一下,我要将 redis 数据源更改为其他内容,所以我必须考虑通用包装器)

1. 

public class RedisRepoVerticle extends AbstractVerticle {

...
 public void start() {
 client = new RedisClient("localhost", 6379);
                 connection = client.connect();
...

vertx.eventBus().consumer("redis-operation", (handler) -> {
            {
                JsonObject msg = new JsonObject(handler.body().toString());
               //write straight to Redis 

            }
        });

}






2. 

     public class RedisMessageListener extends AbstractVerticle {
        DatasourceDAO datasource
        ...
         public void start() {
         client = new RedisClient("localhost", 6379);
                         connection = client.connect();
    ...

    vertx.eventBus().consumer("redis-operation", (handler) -> {
                {
                    JsonObject msg = new JsonObject(handler.body().toString());
                   datasourceDAO.writeToRedis(..); 

                }
            });

    }

//datasourceDAO will hold RedisRepo object

如果我选择第二个选项,我应该开始维护单例吗?我不想复制我的 daos,它会复制我的 redisrepo 类

我认为实施第二种情况会更好,创建一个单独的 Verticle 来容纳 Redis 客户端和一个单独的 Verticle 来接受、处理请求并将请求传递给 Redis。每个 Verticle 都应该有单独的连接池,将来在创建从每个 Verticle 到 Redis 的单独连接时,您可能会遇到维护多个连接池的问题。

DAO-s 复制不应该是这里的情况。我建议你看看 https://github.com/vert-x3/vertx-service-proxy. It's a vert.x sub-project which can help you understand what a verticle itself is. You should tread a verticle as a service (or in particular, DAO) that provides a set of methods. It behaves like a singleton, that is you don't create new instances of a verticle on-demand, but there can be more than one instance of it depending on the configuration (http://vertx.io/docs/vertx-core/groovy/#_specifying_number_of_verticle_instances).