我已经在centos服务器上安装了Couchbase,现在尝试使用Java SDK访问它,但它会抛出非法状态异常。 我可以从8091端口的浏览器访问Couchbase控制台。 是否还有其他步骤可以通过Java SDK访问它? 我也可以通过浏览器访问Couchbase Web控制台。
以下是我用过的代码:
公共类CacheInitialize {
public static CouchbaseClient client = null;
public void Initialize() {
final ArrayList<URI> nodes = new ArrayList<URI>();
// Add one or more nodes of your cluster (exchange the IP with yours)
nodes.add(URI.create("http://IP-ADDRESS/pools"));
try {
client = new CouchbaseClient(nodes, "default", "");
} catch (final Exception e) {
System.err.println("Error connecting to Couchbase: "
+ e.getMessage());
System.exit(1);
}
}
public static void main(final String[] args) {
new CacheInitialize().Initialize();
final CouchbaseClient client = CacheInitialize.client;
// Store a Document
client.set("my-first-document", "Hello Couchbase!");
// Retreive the Document and print it
System.out.println(client.get("my-first-document"));
// Shutting down properly
client.shutdown();
}
}
这是我得到的例外:
java.io.IOException: Connection reset by peer
at sun.nio.ch.FileDispatcherImpl.read0(Native Method)
at sun.nio.ch.SocketDispatcher.read(SocketDispatcher.java:39)
at sun.nio.ch.IOUtil.readIntoNativeBuffer(IOUtil.java:225)
at sun.nio.ch.IOUtil.read(IOUtil.java:193)
at sun.nio.ch.SocketChannelImpl.read(SocketChannelImpl.java:375)
at net.spy.memcached.MemcachedConnection.handleReads(MemcachedConnection.java:772)
at net.spy.memcached.MemcachedConnection.handleReadsAndWrites(MemcachedConnection.java:672)
at net.spy.memcached.MemcachedConnection.handleIO(MemcachedConnection.java:635)
at net.spy.memcached.MemcachedConnection.handleIO(MemcachedConnection.java:409)
at com.couchbase.client.CouchbaseConnection.run(CouchbaseConnection.java:288)