【问题标题】:membase server to couchbase server migration : Java clientmembase 服务器到 couchbase 服务器迁移:Java 客户端
【发布时间】:2017-03-21 05:11:36
【问题描述】:

我使用 aspymemcached 客户端连接到我的 membase 服务器。 代码如下:

public static MemcachedClient MemcachedClient(String bucketName){
        URI server1 = new URI("http://192.168.100.111:8091/pools");
        URI server2 = new URI("http://127.0.0.1:8091/pools");
        ArrayList<URI> serverList = new ArrayList<URI>();
        serverList.add(server1);
        serverList.add(server2);
        return  new MemcachedClient(serverList, bucketName, "");
}

用于将对象放入缓存:

public static void makeMembaseCacheEntry(final String key, final int expiryTime, final Object value, final String bucketName) {
    MemcachedClient client = getMembaseClient(bucketName);
    if (client != null) {
        client.set(key, expiryTime, value);       
}

从缓存中获取对象:

   public static Object getMembaseCacheEntry(String key) {
        Object value = null;
         try {
            MemcachedClient client = getMembaseClient(bucketName);
            if (client != null) {
                value = client.get(key);
            }
        } catch (Exception e) {
        }
        return value;
}

现在我计划将 membase 服务器升级到 couchbase 服务器,因此我必须使用 couchbase 客户端 java API(参考:http://docs.couchbase.com/developer/java-2.1/java-intro.html)。 在 cousebase 客户端中,所有操作都在 JsonObject ref 上执行:

http://docs.couchbase.com/developer/java-2.0/documents-basics.html

那么如何将以上两种方法迁移到couchbase客户端

【问题讨论】:

标签: java couchbase spymemcached membase couchbase-java-api


【解决方案1】:

如果您存储的是序列化的Object,Java SDK 提供SerializableDocument 类型(请参阅https://developer.couchbase.com/documentation/server/4.6/sdk/java/document-operations.html#story-h2-10)。

它与构建在 spymemcached 之上的 1.x 客户端存储的对象兼容,但它使用标志元数据,我不确定从 Memcached 迁移到 Couchbase 会如何影响这些(因此您可能必须编写一些迁移代码)。

与 Spymemcached 相比,Couchbase SDK 2.x 有一个更接近 Couchbase 集群组织的 API:您开始连接到 Cluster,在其上打开 Bucket,您可以在其上执行 key/像getupdateinsertupsert这样的值操作。

在您的第一个 sn-p 中,您只需要至少一个 couchbase 节点的 IP。从中你会得到一个Cluster(使用CouchbaseCluster.create(...))。 然后使用cluster.openBucket(bucketName) 打开Bucket。这应该很像:

//TODO make both of these singletons
Cluster cluster = CouchbaseCluster.create("192.168.100.111", "127.0.0.1:8091"); 
Bucket bucket = cluster.openBucket(bucketName, bucketPassword);

注意ClusterBucket 是线程安全的,应该用作单例,而不是像在makeMembaseCacheEntrygetMembaseCacheEntry 中那样在每次调用时重新打开... p>

为了让你需要包装你的value

Document doc = SerializableDocument.create(key, expiry, value);
bucket.upsert(doc);

(如果您想创建或替换,请使用upsert,请参阅文档了解其他类型的 kv 操作)

对于获取,您需要告诉存储桶它反序列化一个对象:

SerializableDocument doc = bucket.get(key, SerializableDocument.class);
Object value = doc.content();

【讨论】:

  • 我尝试使用SerializableDocument,但它不起作用,因为我将Object 存储在服务器中。你可以看到makeMembaseCacheEntry 的方法签名,它Object 不是Serializable。例如:public static void makeMembaseCacheEntry(final String key, final int expiryTime, final Object value, final String bucketName) 它给出了编译错误:The method create(String, int, ByteBuf) in the type BinaryDocument is not applicable for the arguments (String, int, Object)
  • 您没有可以使用的更具体的基本类型Serializable?此外,编译错误似乎表明您使用了BinaryDocument.create,而不是SerializableDocument
  • 您没有可以使用的可序列化的更具体的基本类型吗? > 我可以将方法限制为 Serializable 类型,因为我的 API 用户可能想要存储任何类型的对象,这就是我将 Object 类型放在参数中的原因。这适用于 spymemcached java clinet
  • 实际上,如果对象无法通过 Java 序列化机制进行序列化,则 spymemcached 也会失败(这就是我们在 Couchbase SDK 中更加明确的原因),请参阅 github.com/couchbase/spymemcached/blob/master/src/main/java/net/…
  • 是的,你是对的。现在我正在处理这个问题:forums.couchbase.com/t/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-03
相关资源
最近更新 更多