Couchbase lite - Android 同步问题

Couchbase lite - Android sync issue

我已经创建了一个 Ubuntu 虚拟机 运行 Couchbase 服务器,其中有一个 ships 数据桶,其中加载了许多文档。

我正在尝试在 Android 模拟器中测试将此数据桶简单地拉入 Couchbase lite 数据库 运行。但是我遇到了很多问题!

我创建了一个 config.json 来配置同步网关

{
    "log":["CRUD+", "REST+", "Changes+", "Attach+"],
    "verbose" : true,
    "databases": {
        "ships": {
            "server":"http://192.168.0.9:8091",
            "sync":`function (doc) { channel (doc.channels); }`,
            "users": {
                "GUEST": {
                    "disabled": false,
                    "admin_channels": ["*"]
                }
            }
        }
    }
}

当我启动同步网关时,它似乎启动正常

E:\Couchbase>sync_gateway ./data/config.json
2015-08-23T10:00:43.275+01:00 Enabling logging: [CRUD+ REST+ Changes+ Attach+]
2015-08-23T10:00:43.276+01:00 ==== Couchbase Sync Gateway/1.1.0(28;86f028c) ====

2015-08-23T10:00:43.276+01:00 Configured Go to use all 8 CPUs; setenv GOMAXPROCS
 to override this
2015-08-23T10:00:43.277+01:00 WARNING: Error setting MaxFileDescriptors to 5000:
 Unsupported on Windows -- rest.setMaxFileDescriptors() at config.go:443
2015-08-23T10:00:43.277+01:00 Opening db /ships as bucket "ships", pool "default
", server <http://192.168.0.9:8091>
2015-08-23T10:00:43.277+01:00 Opening Couchbase database ships on <http://192.16
8.0.9:8091>
2015/08/23 10:00:43  Trying with selected node 0
2015/08/23 10:00:43  Trying with selected node 0
2015-08-23T10:00:43.374+01:00     Reset guest user to config
2015-08-23T10:00:43.375+01:00 Starting admin server on 127.0.0.1:4985
2015-08-23T10:00:43.379+01:00 Starting server on :4984 ...

在我的 MainActivity class 中,我有以下创建同步的方法 URL

private URL createSyncURL(boolean isEncrypted){
    URL syncURL = null;
    String host = "https://10.0.2.2";
    String port = "4984";
    String dbName = "ships";
    try {
        syncURL = new URL(host + ":" + port + "/" + dbName);
    } catch (MalformedURLException me) {
        me.printStackTrace();
    }
    return syncURL;
}

以及调用复制过程的方法

    private void startReplications() throws CouchbaseLiteException {
        Replication pull = application.getDatabase().createPullReplication(this.createSyncURL(false));
        pull.setContinuous(true);
        pull.start();

        if(!pull.isRunning()) {
            Log.d(TAG, "Replication is not running due to " +pull.getLastError().getMessage());
            Log.d(TAG, "Replication is not running due to " +pull.getLastError().getCause());
            Log.d(TAG, "Replication is not running due to " +pull.getLastError().getStackTrace());
            Log.d(TAG, "Replication is not running due to " +pull.getLastError().toString());
        }
     }

当我调用 startReplications() 方法时,我收到以下异常

08-23 09:16:53.034  17976-18002/com.couchbase.examples.couchbaseevents E/RemoteRequest﹕ io exception.  url: https://10.0.2.2:4984/ships/_local/74fbb4649e042fdae28537a02b680ad5890703e3
    javax.net.ssl.SSLPeerUnverifiedException: No peer certificate
            at com.android.org.conscrypt.SSLNullSession.getPeerCertificates(SSLNullSession.java:104)
            at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:98)
            at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:393)
            at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:170)
            at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)
            at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)
            at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)
            at com.couchbase.lite.support.RemoteRequest.executeRequest(RemoteRequest.java:184)
            at com.couchbase.lite.support.RemoteRequest.run(RemoteRequest.java:103)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access1(ScheduledThreadPoolExecutor.java:152)
            at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)

如果我尝试将 URL 更改为 http://10.0.2.2,则会得到以下内容

08-23 09:23:33.012  20702-20702/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.couchbase.examples.couchbaseevents, PID: 20702
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.couchbase.examples.couchbaseevents/com.couchbase.examples.couchbaseevents.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Throwable.getMessage()' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access0(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Throwable.getMessage()' on a null object reference
            at com.couchbase.examples.couchbaseevents.MainActivity.startReplications(MainActivity.java:139)
            at com.couchbase.examples.couchbaseevents.MainActivity.onCreate(MainActivity.java:56)
            at android.app.Activity.performCreate(Activity.java:5990)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
            at android.app.ActivityThread.access0(ActivityThread.java:151)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5257)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

查看同步网关控制台,我可以看到以下内容

2015-08-23T10:23:38.094+01:00 HTTP:  #001: GET /ships/_local/bfdf0950d51e390e054
bb31f2895d54b286c36a1
2015-08-23T10:23:38.095+01:00 HTTP: #001:     --> 404 missing  (1.0 ms)
2015-08-23T10:23:38.128+01:00 HTTP:  #002: POST /ships/_changes
2015-08-23T10:23:38.128+01:00 Changes: MultiChangesFeed({*}, {Since:0 Limit:0 Co
nflicts:true IncludeDocs:false Wait:false Continuous:false Terminator:0xc0820a92
c0 HeartbeatMs:300000 TimeoutMs:0}) ...
2015-08-23T10:23:38.129+01:00 Changes+: MultiChangesFeed: channels expand to cha
nnels.TimedSet{"!":0x1, "*":0x1} ...
2015-08-23T10:23:38.206+01:00 Changes+: MultiChangesFeed sending &{Seq:1 ID:_use
r/GUEST Deleted:false Removed:{} Doc:map[] Changes:[] Err:<nil> branched:false}

2015-08-23T10:23:38.207+01:00 Changes: MultiChangesFeed done
2015-08-23T10:23:38.229+01:00 HTTP:  #003: POST /ships/_changes
2015-08-23T10:23:38.230+01:00 Changes: MultiChangesFeed({*}, {Since:1 Limit:50 C
onflicts:true IncludeDocs:false Wait:true Continuous:false Terminator:0xc0822c21
80 HeartbeatMs:300000 TimeoutMs:0}) ...
2015-08-23T10:23:38.230+01:00 Changes+: MultiChangesFeed: channels expand to cha
nnels.TimedSet{"*":0x1, "!":0x1} ...
2015-08-23T10:23:38.230+01:00 Changes+: MultiChangesFeed waiting...
2015-08-23T10:23:38.230+01:00 Changes+: Waiting for "ships"'s count to pass 0

我不明白为什么我会收到 404 响应?如果我使用浏览器转到 http://127.0.0.1:4984/ships/_local/ee1ac8f4b732441355817ac7e9446c27e4089efb,我会得到以下 JSON 响应

{"error":"not_found","reason":"missing"}

ships 数据桶肯定存在于 couchbase 服务器上

问题是我通过 Couchbase 服务器上的 cbdocloader 工具将文档添加到 Couchbase。

要使同步正常工作,必须通过同步网关添加它们。我通过同步网关 REST API

进行了批量加载

http://developer.couchbase.com/mobile/develop/references/sync-gateway/rest-api/database/post-bulk-docs/index.html