Yearly Archives: 2015

JBoss EAP 7 and NoSQL using Java EE and Docker

JBoss EAP 7 Beta is now released, many congratulations to Red Hat and particularly to the WildFly team!

There are plenty of improvements coming in this release as documented in Release Notes. One of the major themes is Java EE 7 compliance.

JBoss EAP 7 and Java EE 7

IBM and Oracle already provide commercially supported Java EE 7-compliant Application Servers. And now Red Hat will be joining this party soon as well. Although WildFly has supported Java EE 7 for 2+ years but commercial support is a critical for open source to be adopted enterprise-wide. So this is good news!

You can learn all about different Java EE 7 APIs in the DZone Refcardz that I authored along with @alrubinger.

Java EE 7 Refcardz

There are plenty of “hello world” Java EE 7 Samples that should all run with JBoss EAP. Hopefully somebody will update the pom.xml and add a new profile.

Why NoSQL?

If you are building a traditional enterprise application then you might be fine using an RDBMS. There are plenty of advantages of using RDBMS but using a NoSQL database instead has a few advantages:

  • No need to have a pre-defined schema and that makes them a schema-less database. Addition of new properties to existing objects is easy and does not require ALTER TABLE. The unstructured data gives flexibility to change the format of data any time without downtime or reduced service levels. Also there are no joins happening on the server because there is no structure and thus no relation between them.
  • Scalability, agility and performance is more important than the entire set of functionality typically provided by an RDBMS. This set of databases provide eventual consistency and/or transactions restricted to single items but more focus on CRUD.
  • NoSQL are designed to scale-out (horizontal) instead of scale-up (vertical). This is important knowing that databases, and everything else as well, is moving into the cloud. RBDMS can scale-out using sharding but requires complex management and not for the faint of heart. Queries requiring JOINs across shards is extremely inefficient.
  • RDBMS have impedance mismatch between the database structure and the domain classes. An Object Relational Mapping, such as one provided by Java Persistence API or Hibernate is needed in such case.
  • NoSQL databases are designed for less management and simpler data models lead to lower administration cost as well.

So you are all excited about NoSQL now and want to learn more:

  • Why NoSQL?
  • Why do successful enterprises rely on NoSQL?
  • Top 10 Enterprise NoSQL Usecases

In short, there are four different types of NoSQL databases:

  • Document: Couchbase, Mongo, and others
  • Key/Value: Couchbase, Redis, and others
  • Graph: Neo4J, OrientDB, and others
  • Column: Cassandra and others

Java EE 7 provides Java Persistence API that does not provide any support for NoSQL. So how do you get started with NoSQL with JBoss EAP 7?

This blog will show how to query a Couchbase database using simple Java EE application deployed on JBoss EAP 7 Beta.

What is Couchbase?

Couchbase is an open-source, NoSQL, document database. It allows to access, index, and query JSON documents while taking advantage of integrated distributed caching for high performance data access.

Developers can write applications to Couchbase using different languages (Java, Go, .NET, Node, PHP, Python, C) multiple SDKs. This blog will show how you can easily create a CRUD application using Java SDK for Couchbase.

Run JBoss EAP 7

There are two ways to start JBoss EAP 7.

Download and Run

  • Download JBoss EAP 7 Beta and unzip.
  • Start the application server as:

Docker Run

In a containerized world, you just docker run to run your JBoss EAP. However, JBoss EAP image does not exist on Docker Hub and so the image needs to be explicitly built. You still need to explicitly download JBoss EAP and then use the following Dockerfile to build the image:

The image is built as:

And then you can run the JBoss EAP 7 container as:

Notice, how application and management ports are bound to all network interfaces. This will simplify to deploy the application to this JBoss EAP instance later.

Stop the server as we will show an easier way to start it later.

Start Application Server and Database

The Java EE application will provide a HTTP CRUD interface over JSON documents stored in Couchbase. The application itself will be deployed on JBoss EAP 7 Beta. So it would require to start Couchbase and JBoss EAP.

Use the Docker Compose file from github.com/arun-gupta/docker-images/blob/master/jboss-eap7-nosql/docker-compose.yml to start Couchbase and JBoss EAP 7 container:

The application is started as:

The started containers can be seen as:

Configure Couchbase Server

Clone couchbase-javaee application. This Java EE application uses Couchbase Java SDK APIs to connect to the Couchbase server. The bootstrap code is:

and is invoked from Database abstraction.

Couchbase Server can be configured using REST API. These REST APIs are defined in a Maven profile in pom.xml of this application. And so configure Couchbase server as:

Deploy Java EE Application to JBoss

Java EE Application can be easily deployed to JBoss EAP 7 Beta using the WildFly Maven Plugin. This is also defined as a Maven profile in pom.xml as well.

Deploy the application as:

Access the Application

As mentioned earlier, the application provides HTTP CRUD API over JSON documents stored in Couchbase.

Access the application as:

CRUD operations (GET, POST, PUT, DELETE) can be performed on Airline resource in the application. Complete CRUD API is documented at github.com/arun-gupta/couchbase-javaee.

This blog explained how to access a NoSQL database from JBoss EAP 7.

Read more about Couchbase 4:

  • What’s New in Couchbase Server 4.1
  • Couchbase Server documentation
  • Talk to us on Couchbase Forums
  • Follow @couchbasedev or @couchbase

Learn more about Couchbase in this recent developer-focused webinar:

Couchbase on OpenShift 3

OpenShift is Red Hat’s open source PaaS platform. OpenShift 3 provides a holistic experience of running your applications using Docker and Kubernetes. In a classic Red Hat way, all the work is done in the open source at OpenShift Origin. This also drives the next major release of OpenShift Online and OpenShift Enterprise.

OpenShift 3 using Docker and Kubernetes for container orchestration makes it really simple to bring any products that have a Docker image to run with minimal effort. This blog explains how to get started with Couchbase on OpenShift 3.

OpenShift-logoCouchbase Logo

Getting Started with OpenShift 3

  • Download the latest Vagrant box (1.1 as of this writing) and Vagrantfile from: openshift.org/vm/. Copy them in the same directory.Vagrantfile is configured for 2GB memory and can be updated if you need to run more containers. OpenShift Master, Node, Docker Registry, and other components run inside the VM.This blog was written using Vagrant 1.7.4 and VirtualBox 5.0.10r104061.
  • Add the Vagrant Box:
  • Start the Virtual Machine:

Download and Configure OpenShift 3 Client

  • Download Mac 64-bit client tools (gem install rhc is for v2 only) from openshift.org/vm/ and extract them a in directory. The listing looks like:
  • Verify the client version:
  • Remove ~/.kube/configor rename to something else.
  • Login to OpenShift:

Create Couchbase Application in OpenShift 3

  • Create a new Couchbase instance:
    arungupta/couchbase is used as it uses Couchbase REST API to preconfigure the Couchbase server with:

    • Memory and index quota
    • Query, Data, and Index service
    • Username and password credentials
    • Install travel-sample bucket

    This sample bucket will be used later for querying data.

  • Check the status of deployment:

  • Find the list of Pods:

  • Get more details about the Couchbase pod:

Query Couchbase Sample Bucket

  • Log into the Vagrant box:

  • Find a list of all the running containers:

    Search for Couchbase container:

    Get the id for our container:

  • Get IP address of the Pod where Couchbase server is running:

  • Use the IP address shown above to start Couchbase Query CLI:

  • Query the sample bucket:

Enjoy!

This blog shows the very basics of getting started with Couchbase on OpenShift 3. Future blogs will show:

  • How to deploy an application to OpenShift and use this Couchbase
  • How to make this application accessible outside OpenShift
  • How to scale Couchbase in OpenShift
  • Possibly some other interesting items that come along

Do you have a suggestion on what you’d like to see?

Read more about Couchbase 4.1:

  • What’s New in Couchbase Server 4.1
  • Download Couchbase Server 4.1
  • Couchbase Server documentation
  • Talk to us on Couchbase Forums
  • Follow @couchbasedev or @couchbase

Couchbase 4.1 Docker Container

Couchbase Logo

Couchbase 4.1 was recently released. Highlights of this release are:

  • N1QL: Complete SQL CRUD Support with N1QL for Batch and OLTP Applications
  • Covering Indexes
  • Prepared Statements
  • Additional Supported Platforms

You can read all about it at Introducing Couchbase Server 4.1 and also watch a replay of Hands-on Introduction to What’s New in Couchbase 4.1:

Getting Started with Couchbase 4.1 and Docker

Use the following Docker Compose file:

to start a pre-configured Couchbase 4.1 Docker container:

This will:

  • Start Couchbase 4.1 Server (Dockerfile)
  • Configure it using Couchbase REST API as explained in Configure Couchbase Docker Container using REST API
    • Sets up memory and index quota
    • Sets up Query, Data, and Index service
    • Configures username and password credentials
    • Install travel-sample bucket

The logs can be seen as:

Connect to CBQ (Couchbase Query tool):

Learn more about Running N1QL Queries from Command Line.

Stop the server:

Remove the container:

Read more about Couchbase 4.1:

  • What’s New in Couchbase Server 4.1 documentation
  • Download Couchbase Server 4.1
  • Couchbase Server documentation
  • Release Notes for 4.1
  • Couchbase ODBC and JDBC Drivers with SQL Connector

Share feedback with us at forums.couchbase.com or Stackoverflow.

Enjoy!

Docker Bridge and Overlay Network with Compose Variable Substitution

Docker Multi-Host networking allows you to create virtual networks and attach containers to them so you can create the network topology that is right for your application. Bridge networks can be created for single host and overlay networks can be created for multiple hosts. Creating application-specific networks provides complete isolation for containers.

Docker Compose file can be targeted at a single host, and --x-networking will create a bridge network exclusive for the application. If the sample application is targeted at multiple hosts, say using Docker Swarm cluster, then an overlay network is created. Single host networking and multi host networking provide more details on how to set this up.

What if a bridge or an overlay network already exists and you’d like to assign this to your application started using Docker Compose?

Docker Networking

Docker 1.9 introduced variable substitution, and we can use that feature to target an application to a pre-created network.

Create New Docker Bridge Network

  1. Create a new network:
  2. List the networks:
    Docker create three networks for each host automatically:

    NETWORK NAME PURPOSE
    bridge Default network that containers connect to. This is docker0 network in all Docker installations.
    none Container-specific networking stack
    host Adds a container on hosts networking stack. Network configuration is identical to the host.

    In addition, you also see mynet network that was just created.

  3. Inspect the newly created network using docker network inspect mynet:

    No containers are assigned to it yet.

Docker Compose and Networking

  1. This new network can be used for any new container using docker run --net=<NETWORK> command. This blog will show how to target this network to a Compose file:

    Note how net is specified here to use a custom network. This Compose file is at: github.com/arun-gupta/docker-images/blob/master/wildfly-couchbase-javaee7-network/docker-compose.yml.

  2. Start the application, using our newly created network, as:

  3. Inspect the network again:

    And now the two containers are assigned to this network as well.

  4. Check the container id using docker ps:

  5. Check the network for one container:

  6. More details about the network:

  7. More details about the container can be found using docker inspect, relevant portion is shown here:

Create New Docker Overlay Network

Creating a new Docker overlay network requires to setup a key/value service and a Docker Swarm cluster. Multi-host and multi-container blog provide more details on that.

More details at Docker Networks.

Show Layers of Docker Image

Every speaking opportunity is a learning lesson, and hence a new opportunity to share. This blog will address one of the questions that has been bothering me for the past few days. It is about how to show different layers of Docker image, and their sizes, after it is downloaded.

Docker Logo

I was invited to talk about Docker for Java developers at Peru JUG this morning. The recording is now available:

The real content starts from 5:27.

Lets address the question now.

Each Docker image consists of a series of layers. Here is a quote from docs.docker.com:

Each image consists of a series of layers. Docker makes use of union file systems to combine these layers into a single image. Union file systems allow files and directories of separate file systems, known as branches, to be transparently overlaid, forming a single coherent file system.

How does a Docker image work?

The exact size of the image can be easily seen using docker images:

The command to show different layers, and their size, is docker history:

This command shows different layers, command used for each layer, and the exact size of each image.

The corresponding Dockerfile for this image is at github.com/couchbase/docker/blob/master/enterprise/couchbase-server/4.0.0/Dockerfile.

Now I can compare the Dockerfile with the exact size and easily find which image layers are the biggest. For example, biggest contributors in this Couchbase image are shown below:

  • Ubuntu from Dockerfile 12.04 is 135.9 MB
  • apt-get from Dockerfile causes another 23.57 MB
  • Couchbase server is 212 MB

--no-trunc can be specified as an additional CLI option to history to show the complete command executed to build the layer.

As always, the latest slides are available at github.com/javaee-samples/docker-java/tree/master/slides.

“Network timed out” Docker error

One of the most dreaded errors seen during Docker workshops around the world is:

Restarting Docker Machine is typically known to fix the issue.

Lets say your machine name is couchbase-javaee. Machine can be restarted as:

Setting up Docker environment may work:

But certain times it gives the error:

So you need to regenerate the certificates. A successful execution will give output as:

And sometimes even this command goes berserk and gives the output:

Either way, setting Docker environment:

sets up the environment correctly.

Docker Machine, Swarm and Compose for multi-container and multi-host applications with Couchbase and WildFly

This blog will explain how to create multi-container application deployed on multiple hosts using Docker. This will be achieved using Docker Machine, Swarm and Compose.

Yes, all three tools together makes this blog that much more interesting!

Docker Swarm Machine Compose

The diagram explains the key components:

  • Docker Machine is used to provision multiple Docker hosts
  • Docker Swarm will be used to create a multi-host cluster
  • Each node in Docker Swarm cluster is registered/discovered using Consul
  • Multi-container application will be deployed using Docker Compose
  • WildFly and Couchbase are provisioned on different hosts
  • Docker multi-host networking is used for WildFly and Couchbase to communicate

In addition, Maven is used to configure Couchbase and deploy application to WildFly.

Latest instructions at Docker for Java Developers.

No story, just pure code, lets do it!

Create Discovery Service using Docker Machine

  1. Create a Machine that will host discovery service:
  2. Connect to this Machine:
  3. Run Consul service using the following Compose file:
    This Compose file is available at https://github.com/arun-gupta/docker-images/blob/master/consul/docker-compose.yml.
    Started container can be verified as:

Create Docker Swarm Cluster using Docker Machine

Swarm is fully integrated with Machine, and so is the easiest way to get started.

  1. Create a Swarm Master and point to the Consul discovery service:
    Few options to look here:

    1. --swarm configures the Machine with Swarm
    2. --swarm-master configures the created Machine to be Swarm master
    3. --swarm-discovery defines address of the discovery service
    4. --cluster-advertise advertise the machine on the network
    5. --cluster-store designate a distributed k/v storage backend for the cluster
    6. --virtualbox-disk-size sets the disk size for the created Machine to 5GB. This is required so that WildFly and Couchbase image can be downloaded on any of the nodes.
  2. Find some information about this machine:
    Note that the disk size is 5GB.
  3. Connect to the master by using the command:
  4. Find some information about the cluster:
  5. Create a new Machine to join this cluster:
    Notice no --swarm-master is specified in this command. This ensure that the created Machines are worker nodes.
  6. Create a second Swarm node to join this cluster:
  7. List all the created Machines:
    The machines that are part of the cluster have cluster’s name in the SWARM column, blank otherwise. For example,consul-machine is a standalone machine where as all other machines are part of the swarm-master cluster. The Swarm master is also identified by (master) in the SWARM column.
  8. Connect to the Swarm cluster and find some information about it:

    Note, --swarm is specified to connect to the Swarm cluster. Otherwise the command will connect to swarm-masterMachine only.

    This shows the output as:

    There are 3 nodes – one Swarm master and 2 Swarm worker nodes. There is a total of 4 containers running in this cluster – one Swarm agent on master and each node, and there is an additional swarm-agent-master running on the master. This can be verified by connecting to the master and listing all the containers.

  9. List nodes in the cluster with the following command:

Start Application Environment using Docker Compose

Make sure you are connected to the cluster by giving the command eval "$(docker-machine env --swarm swarm-master)".

  1. List all the networks created by Docker so far:
    Docker create three networks for each host automatically:

    Network Name Purpose
    bridge Default network that containers connect to. This is docker0 network in all Docker installations.
    none Container-specific networking stack
    host Adds a container on hosts networking stack. Network configuration is identical to the host.

    This explains a total of nine networks, three for each node, as shown in this Swarm cluster.

  2. Use Compose file to start WildFly and Couchbase:

    In this Compose file:

    1. Couchbase service has a custom container name defined by container_name. This name is used when creating a new environment variable COUCHBASE_URI during WildFly startup.
    2. arungupta/wildfly-admin image is used as it binds WildFly’s management to all network interfaces, and in addition also exposes port 9990. This enables WildFly Maven Plugin to be used to deploy the application.Source for this file is at https://github.com/arun-gupta/docker-images/blob/master/wildfly-couchbase-javaee7/docker-compose.yml.

    This application environment can be started as:

    --x-networking creates an overlay network for the Swarm cluster. This can be verified by listing networks again:

    Three new networks are created:

    1. Containers connected to the multi-host network are automatically connected to the docker_gwbridge network. This network allows the containers to have external connectivity outside of their cluster, and is created on each worker node.
    2. A new overlay network wildflycouchbasejavaee7 is created. Connect to different Swarm nodes and check that the overlay network exists on them.

      Lets begin with master:

      Next, with swarm-node-01:

      Finally, with swarm-node-02:

      As seen, wildflycouchbasejavaee7 overlay network exists on all Machines. This confirms that the overlay network created for Swarm cluster was added to each host in the cluster. docker_gwbridge only exists on Machines that have application containers running.

      Read more about Docker Networks.

  3. Verify that WildFly and Couchbase are running:

Configure Application and Database

  1. Clone https://github.com/arun-gupta/couchbase-javaee.git. This workspace contains a simple Java EE application that is deployed on WildFly and provides a REST API over travel-sample bucket in Couchbase.
  2. Couchbase server can be configured using REST API. The application contains a Maven profile that allows to configure Couchbase server with travel-sample bucket. This can be invoked as:
  3. Deploy the application to WildFly by specifying three parameters:
    1. Host IP address where WildFly is running
    2. Username of a user in WildFly’s administrative realm
    3. Password of the user specified in WildFly’s administrative realm

Access Application

Now that WildFly and Couchbase server have started, lets access the application. You need to specify IP address of the Machine where WildFly is running:

Complete set of REST API for this application is documented at github.com/arun-gupta/couchbase-javaee.

Latest instructions at Docker for Java Developers.

Enjoy!

JavaOne4Kids 2015 Wrapup – Devoxx4Kids and Oracle Academy Together!

JavaOne4Kids is focused on promoting technology to next generation of developers; kids who want to learn more about programming, robotics and engineering.

Oracle Academy collaborated with Devoxx4Kids to bring kids content that includes several topics like Minecraft Modding, Java, Python, Scratch, Raspberry Pi, Arduino, NAO robot, LEGO Mindstorms, Greenfoot, Alice, and others at JavaOne 2015.

The attendance grew 3x from last year and it was certainly very heartening to see that!

If you live in/around San Francisco Bay Area, and want a more continued experience through out the year, then its highly recommend to join meetup.com/Devoxx4Kids-BayArea/!

Here are some statistics from the event:

javaone4kids-2015-boys-girls

A survey was sent to the attendees and some of them responded back. 95% of responses rated were happy with the event:

javaone4kids-2015-rate

90%+ would recommend JavaOne4Kids to a friend:

javaone4kids-2015-recommend

Instructors seem to have done a good job with 97% presenting in good, very good, and excellent way:

javaone4kids-2015-clear-way

Minecraft Modding continues to be the top rated workshop:

javaone4kids-2015-course

Here are some pictures from the event:

 
 Oracle Oracle
 Oracle Oracle
 Oracle Oracle
 
 

Check out the complete album:

JavaOne4Kids 2015 Album

Picture is worth a thousand words, and a video is worth a million words. Check out kids in action from the event, and then subsequently in JavaOne Community Keynote:

It takes a village to run an event like this. This was certainly not possible without the impeccable support from Oracle team, instructors, and volunteers who helped us through out the event!

Do we expect these kids to come back to again next year? Yes, absolutely!

At least, 88% of them want to come back :)

javaone4kids-2015-another-event

Don’t forget to join the local meetup.com/devoxx4kids-bayarea for local events in Bay Area.

Docker Swarm Cluster using Consul

Docker Swarm is native clustering for Docker. It allows you create and access to a pool of Docker hosts using the full suite of Docker tools. Because Docker Swarm serves the standard Docker API, any tool that already communicates with a Docker daemon can use Swarm to transparently scale to multiple hosts.

Docker Swarm Logo Consul Logo

Docker Swarm has a Manager, a pre-defined Docker Host, and is a single point for all administration. Swarm manager orchestrates and schedules containers on the entire cluster, and can be configured in High Availability. The containers are deployed on Nodes that are additional Docker Hosts.

Swarm talks to a hosted Discovery Service that maintains a list of IPs in your cluster. For development, its easy to use the default discovery service hosted on Docker Hub. Complete instructions for that are available in Install and Create Docker Swarm. This blog will show how to setup Docker Swarm Cluster using Consul.

Lets get started!

Create Consul Discovery Service

  1. Create a Machine that will host discovery service:
  2. Connect to this Machine:
  3. Run Consul service using the following Compose file:
    This file is also available at github.com/arun-gupta/docker-images/tree/master/consul.

    The service is started as:

    Started container can be verified as:

Create Docker Swarm Cluster using Consul

Swarm is fully integrated with Machine, and so is the easiest way to get started.

  1. Create a Swarm Master using the Consul discovery service:
    Three options to look here:

    • --swarm-discovery defines address of the discovery service
    • --cluster-advertise advertise the machine on the network
    • --cluster-store designate a distributed k/v storage backend for the cluster

    In addition, --swarm configures the Machine with Swarm, --swarm-master configures the created Machine to be Swarm master.

  2. Connect to this newly created master and find some information about it:

    This will show the output as:

  3. Create a new Machine to be part of this Swarm cluster:

    Machine talks to the Discovery Service using --swarm-discovery.

  4. Create a second node in this cluster:

  5. List all the created Machines:

    The machines that are part of the cluster have cluster’s name in the SWARM column, blank otherwise. For example, “default” and “consul-machine” are standalone machines where as all other machines are part of the “swarm-master” cluster. The Swarm master is also identified by (master) in the SWARM column.

  6. Connect to the Swarm cluster and find some information about it:

    The main difference here is --swarm when finding information about Swarm cluster as opposed to a single Machine.

    This shows the output as:

    There are 3 nodes – one Swarm master and 2 Swarm _worker_ nodes. There is a total of 4 containers running in this cluster – one Swarm agent on master and each node, and there is an additional swarm-agent-master running on the master. This can be verified by connecting to the master and listing all the containers.

  7. List nodes in the cluster with the following command:

Subsequent blog will explain how to deploy applications to this Docker Swarm Cluster.

Enjoy!

 

Docker Networking with Couchbase and WildFly

Docker Multi-Host networking allows you to create virtual networks and attach containers to them so you can create the network topology that is right for your application. This blog will show how to use it with Docker Compose.

CRUD Java Application with Couchbase, Java EE, and WildFly explained how to use a Java EE application to provide a CRUD/REST interface on a data bucket in Couchbase. It required to manually download and run WildFly. The blog also used Couchbase server using Docker and required manual configuration to load travel-sample bucket.

Configure Couchbase Docker Container using REST API explained how to use Couchbase REST API to configure Couchbase Server.

Docker Multi-Host Networking

This blog will remove the explicit download of WildFly and manual configuration of Couchbase server:

  • Use Docker Compose to start WildFly and Couchbase (no download required)
  • Use a Maven profile to configure Couchbase server (no manual configuration required)
  • Uses Docker multi-host networking so that WildFly and Couchbase server can talk to each other

Lets get started!

Start Couchbase and WildFly using Docker Multi-Host Networking and Compose

  1. Start WildFly and Couchbase server using docker-compose.yml file from github.com/arun-gupta/docker-images/blob/master/wildfly-couchbase-javaee7/docker-compose.yml:
    arungupta/wildfly-admin image is used as it binds WildFly’s management to all network interfaces, and in addition also exposes port 9990. This enables WildFly Maven Plugin to be used to deploy the application.

    container_name is specified for Couchbase service and referred in WildFly service using COUCHBASE_URI. This is then used to connect to Couchbase from the Java EE application.

    The application environment is started as:

    --x-networking is an experimental switch added to Docker Compose 1.9 that allows to create a bridge or an overlay network. By default, it creates a bridge network that works on a single host. The network created can be seen as:

    Issue 2221 provide more explanation about the default networks created. wildflycouchbasejavaee7 is the new bridge network created for our application.  Issue #2345 provide some details about incorrect driver name in the output message.

Configure Couchbase Server

  1. Clone couchbase-javaee repo:

  2. Configure Couchbase server:

    exec-maven-plugin is used to invoke REST API and configure Couchbase server and is configured in a Maven profile. Make sure to setup docker.host property in pom.xml.

  3. Deploy the application to WildFly:

    Make sure to specify the correct host on CLI. In this case, this is the IP address obtained using docker-machine ip default.

Invoke the Application

  1. Invoke the REST endpoint using cURL:

    Complete set of REST endpoints are documented at CRUD Java Application with Couchbase, Java EE and WildFly. They are listed here for convenience:

    1. GET a single airline:
    2. Create a new airline using POST:

    3. Update an existing airline using PUT:
    4. Delete an existing airline using DELETE:

Enjoy!

Configure Couchbase Docker Container using REST API

Couchbase Docker image is published at hub.docker.com/_/couchbase. The easiest way to start this image is:

8091 is the network port used by Couchbase Web Console for REST traffic. Complete set of ports are documented at Couchbase Network Configuration. This image can be configured using Single Host Single Container configuration as explained at hub.docker.com/_/couchbase.

This blog will show you can create a single node Couchbase cluster using Docker, configure it with Data, Index, and Query service, load a sample bucket, and query it.

Couchbase Docker Container

Start Couchbase Docker Container

Start Couchbase Docker Container using the following docker-compose.yml:

This Docker Compose file can be downloaded from github.com/arun-gupta/docker-images/tree/master/couchbase-server.

The container can be started as:

Status of the running container can be seen as:

Logs can be seen as:

Configure Couchbase Docker Container

  1. Get IP address of the Docker Host:

    Use this IP address in all the subsequent commands.

  2. Configure memory for Data and Index services:

  3. Configure Data, Query, and Index services:

  4. Setup credentials for the cluster:

Install Couchbase Travel Sample Bucket

Query Couchbase Docker Container using CBQ

  1. List container id of Couchbase server:

    This output shows the complete information about the container. Alternatively, just the container id can be obtained as:

  2. Run Couchbase Query tool:

  3. Run a query:

    Did you realize, this was a SQL query for JSON document? How cool.  Learn more about in this interactive N1QL tutorial.

Cluster overview can be seen at 192.168.99.100:8091:

Couchbase Docker Container Cluster Overview

Data buckets can seen as:

Couchbase Docker Container Databucket

Ask your questions at forums.couchbase.com, learn more about Couchbase REST API or read more in Couchbase 4 Docs.

A subsequent blog will show how all of these steps can be fully automated.

Enjoy!

Attach Shell to Docker container

Docker Logo

Often there is a need to attach a shell to an already running Docker container. This is very useful, especially for debugging. This blog will explain how to attach a shell to an already running Docker container.

So let’s say you run a Couchbase Docker container in detached mode:

Running the container gives you the complete container id, 02061ddf0a3d1b2806a1ee6e354f4064d9d2ff4d84d8c96c0273c8883917a92f in this case.

This can be verified as:

Or complete container id can be verified as:

Attach Shell to Docker Container

Bash shell can be attached to an already running container using docker exec -it {CID} bash. The {CID} can be the complete container id, truncated container id, or even the first few digits of the container ids amongst all the currently running containers.

So the following three commands are equivalent in our case:

Attach Using Complete Container Id

Attach Using Truncated Container Id

Attach Using Unique Numbers from Container Id

In this case, just specifying 0 would work because this is the only running container. If multiple containers are running that have 0 as the starting number then the first few digits that make the container id unique are required.

Enjoy!

CRUD Java Application with Couchbase, Java EE and WildFly

Couchbase is an open-source, NoSQL, document database. It allows to access, index, and query JSON documents while taking advantage of integrated distributed caching for high performance data access.

Developers can write applications to Couchbase using different languages (Java, Go, .NET, Node, PHP, Python, C) multiple SDKs. This blog will show how you can easily create a CRUD application using Java SDK for Couchbase.

REST with Couchbase

The application will use curl to issue REST commands to a JAX-RS endpoint deployed on WildFly. These commands will then perform CRUD operations on travel-sample bucket in Couchbase. N1QL (SQL query language for JSON) will be used to communicate with Couchbase to retrieve results. Both the “builder pattern” and raw N1QL commands will be used.

Couchbase CRUD using WildFly and Curl

TL;DR

Complete source code and instructions for the sample are available at github.com/arun-gupta/couchbase-javaee.

Lets get started!

Run Couchbase Server

Couchbase server can be easily downloaded from Couchbase Server Downloads page. In a containerized world, its a lot easier to fire up a Couchbase server using Docker.

If Docker is configured on your machine then the easiest way is to use Docker Compose for Couchbase:

Starting up the application server shows:

And then the logs can be seen as:

The database needs to be configured and is explained at Configure Couchbase Server. Make sure to install travel-sample bucket.

Deploy the Java EE Application on WildFly

  • Download WildFly 9.0.2 , unzip, and start WildFly application server as ./wildfly-9.0.0.Final/bin/standalone.sh.
  • Git clone the repo: git clone https://github.com/arun-gupta/couchbase-javaee.git
  • Change directory cd couchbase-javaee
  • Deploy the application to WildFly: mvn install -Pwildfly.

The application uses Java SDK for Couchbase by importing the following Maven coordinates:

Invoke the REST Endpoints Using cURL

GET Airline resources (limit to 10)

Lets query the database to list 10 Airline resources.

Request

Response

The N1QL query for this is written as:
And can also be alternatively written as:
You may optionally update the code to include ORDER BY clause as shown in N1QL Tutorial.

GET one Airline resource

Use id attribute to query a single Airline resource

Request

Response

POST a new Airline resource

Learn how to run N1QL queries from the CLI using CBQ tool and verify the existing sample data:

This query retrieve documents where airline’s name is Airlinair. The count is shown in metrics.resultCount.

Create a new document using POST.

Request

Response

Query again using CBQ and now the results are shown as:
Note that two JSON documents are returned instead of one as before the POST command was issued.

PUT an existing Airline resource

Update an existing resource using HTTP POST.

The data model for travel-sample bucket requires to include “id” attribute in the payload and in the URI as well.

Request

Name of the airline is updated from “Airlinair” to “Airlin Air”, all other attributes stay the same.

Response

The updated record is shown in the response.

Querying for Airlinair gives:

So the previously added record is now updated and thus does not appear in query results. Querying for Airlin Airgives:

This shows the newly updated document.

DELETE an existing Airline resource

Query for a unique id:

Notice that one document is returned.

Lets delete this document.

Request

Response

The deleted document is shown in the response.

Query again for the deleted id:

And no results are returned!

As mentioned earlier, the complete code base is at github.com/arun-gupta/couchbase-javaee.

Enjoy!

Docker, Kubernetes, and Microservices Replay from Devoxx 2015

Devoxx 2015 BE Arun

Java gives us Write Once Run Anywhere (WORA) because of the common abstraction provided by Java Virtual Machine. The binary byte code produced by Java is understood by the JVM running on multiple operating systems. This allows Java code to run on any operating system. But deploying such applications typically requires to tune the JVM, setup application server, install appropriate database drivers, other similar configuration. Docker nicely complements WORA by defining a way to package Java applications, and include all the configuration in a an easy to describe format. This can be called as Package Once Deploy Anywhere, or PODA.

Docker PODA

Do you want to learn more about how Docker helps with PODA?
How do you create multi-container applications using Docker Compose?
How do you deploy this multi-container application on multiple hosts running in a Docker Swarm cluster?
What new features Docker introduced in 1.9 release – particularly multi-host networking and persistent storage?
How do you deploy Docker containers using Kubernetes?
How does Kubernetes help with scaling applications?
How does Docker Swarm compare with Kubernetes?

I had the privilege of delivering a 3-hour university on Docker and Kubernetes followed by a 3-hour hands-on lab. The replay of the university session is now available:

Slides for the university are available at: github.com/javaee-samples/docker-java/tree/master/slides

The contents from the hands-on lab are available at bit.ly/dockerlab.

In addition, I also gave a 3-hour university on Refactor your Java EE Applications using Microservices and Containers. This session explained:

  • Basic characteristics of Microservices
  • Showed a simple shopping cart monolithic application, and how it was refactored to multiple microservices (github.com/arun-gupta/microservices)
  • Talked about transactions, event sourcing and CQRS
  • How KumuluzEE was used to create standalone JARs
  • Explained how such microservices can be deployed using Docker

Slides from this session are available at: github.com/arun-gupta/microservices/tree/master/slides

Replay is available at:

The response on twitter was quite positive:

And in case you are interested, watch a quick interview with Voxxed team:

Enjoy!