The driver library contains a number of different ways to connect to a Couchbase server. First, I will begin with a discussion of these methods and then we will use one of those methods to make the chat room client application connect with your Couchbase installation.
There are more than one way of connecting with one or more Couchbase servers from the driver:
A direct connection can be made by creating an instance of the CouchbaseClient object and passing in one or more addresses. For example:
URI server = new URI(addresses); ArrayList<URI> serverList = new ArrayList<URI>(); serverList.add(server); CouchbaseClient client = new CouchbaseClient( serverList, "default", "");
It's recommended to provide more than one server address of all the servers participating in the Couchbase cluster since the client can recover easily if the original server goes down.
Use the connection factory
CouchbaseConnectionFactoryconstructors to
establish a connection with your server:
URI base = new URI(String.format( "http://%s:8091/pools", serverAddress)); ArrayList<URI> serverList = new ArrayList<URI>(); serverList.add(base); CouchbaseConnectionFactory cf = new CouchbaseConnectionFactory(serverList, "default", "");
Create a connection that is authenticated using SASL by using
a CouchbaseConnectionFactory. Merely
specifying the authenticated bucket will establish an
authenticated connection. In the case of Couchbase, the
username and password you use here are based on the buckets
you have defined on the server. The username is the bucket
name, and the password is the password used when creating the
bucket. I will talk more about this later, in the meantime
here is the code you will need to authenticate and start using
a bucket with SASL authentication.
CouchbaseConnectionFactory cf = new CouchbaseConnectionFactory(baseURIs, "rags", "password"); client = new CouchbaseClient((CouchbaseConnectionFactory) cf);
Let's start making modifications to the tutorial
Main.java class in order to make our first
connection. Here we will be making an unauthenticated ASCII
protocol connection to the server. After you have the tutorial
code working, you can easily go back and change the
connect() method to use authentication.
First you modify your main() method as
follows:
public static void main(String[] args) { if (args.length != 1) { System.err.println("usage: serveraddress"); System.exit(1); } String serverAddress = args[0]; System.out.println(String.format("Connecting to %s",serverAddress)); try { connect(serverAddress); client.shutdown(1, TimeUnit.MINUTES); } catch (IOException ex) { ex.printStackTrace(); } }
Next, add the connect() method.
private void connect(String serverAddress) throws Exception { URI base = new URI(String.format("http://%s:8091/pools",serverAddress)); List<URI> baseURIs = new ArrayList<URI>(); baseURIs.add(base); CouchbaseConnectionFactory cf = new CouchbaseConnectionFactory(baseURIs, "default", ""); client = new CouchbaseClient((CouchbaseConnectionFactory) cf);
You'll recognize this constructor as a single server connection. What if you want to know more about the current connection state such as when the connection has been gained, or lost? You can add a connection observer by modifying the connect method and adding the following lines:
public void connectionLost(SocketAddress sa) { System.out.println("Connection lost to " + sa.toString()); } public void connectionEstablished(SocketAddress sa, int reconnectCount) { System.out.println("Connection established with " + sa.toString()); System.out.println("Reconnected count: " + reconnectCount); } }); }
You've only connected with one server, what if it goes offline? This can easily be fixed by changing the first three lines of the connect method:
URI fallback = new URI( String.format("http://%s:8091/pools",fallbackAddress)); baseURIs.add(fallback);
This class will even work with colon-delimited IPv6 addresses.
Finally, you need to create the static member variable to store the client instance at the top of the class:
private static CouchbaseClient client;Now you can try compiling and running the application.
You can see that the driver outputs some logging statements
indicating that it is making two connections. You can also see
that the connection observer you added to the
connect() method is being called with the
addresses of the two servers as they are connected and the
reconnection count is 1 indicating that the network is in good
shape. It's ready to get some work done.