Wednesday, February 1, 2017

Microservice implementation using OSGI, Apache Karaf and CXF

OSGi (Open Service Gateway Initiative) is a Java framework for developing and deploying modular software programs and libraries. It’s a specification that defines how to develop components that can be deployed and invoked as Services (as in SOA)

OSGi only defines specifications for component development and basic execution environment. OSGi specifications are implemented by projects like Apache Felix.

Apache Karaf adds utilities to Apache Felix. These include ability to Hot deploy, Logging and Monitoring etc.
It also includes a Container that contains these Utilities and along with Apache Felix. It also provides ability to Remote login to the container. Services developed using OSGi specs can be deployed into this container.

Karaf container sounds similar to Docker. In case of Docker developer need to build an image specifying OS, app server (like tomcat) and then application can be deployed in that Image. Where as Karaf Container has all necessary environment to run OSGi services. Developer need to bring the container up and deploy the services.

OSGi services when complimented by the features provides by Karaf can be a good choice to implement Microservices architecture. I don't feel OSGi service (deployed on plain Apache Helix) can meet all required criteria to be Microservice architecture. Because features like Scaling, Monitoring etc need to be custom implemented.

In fact OSGi services need to be exposed as REST services (using Apache CXF) so that they can be invoked from any application/platform/technology.

Below are the quick steps to set up and get basic service with REST interface up on Apache Karaf.

Lets first define a simple service

Interface:

public interface MyService {

public String printMessage();
}

Service Implementation

public class MyServiceImpl implements MyService {

@Override
public String printMessage() {
// TODO Auto-generated method stub
return "My Service";

}

}



Next define a REST interface for this service using Jersey framework

@Path("/myService")
public class MyRestService {

@Produces({ MediaType.APPLICATION_JSON })
@GET
public String getMessage() {
MyService service = new MyServiceImpl();
return service.printMessage();
}
}



Create a simple Java project to contain above code components. Compile, build and package as a Jar file. Now lets setup Karaf and CXF to deploy this bundle.

Below are the quick steps

  1. Install Karaf container. 
    • Download Karaf container(binary distribution for quick start) from http://karaf.apache.org/download.html
    • Unzip the contents to
  2. Start the container
    • /bin/karaf
    • Karaf shell should be active now (karaf@root()>)
  3. In Karaf shell execute below commands to install Apache CXF and HTTP features to enable http interface and rest services
    • feature:repo-add cxf 3.1.5
    • feature:install http cxf-jaxws http-whiteboard
    • feature:install  cxf-jaxrs
  4. Copy the Jar bundle to /deploy
  5. Command "list" should list the service just deployed if deployment is successful.
  6. Use command "bundle:diag" to check errors with last operation


Use URL "http://localhost:8181/cxf/sample/myService" to test the Service

This post discussed basic service implementation with Karaf. Shall look at other aspects of Microservice implementation in a later post.

No comments: