Tuesday, February 7, 2017

Microservices and Reactive Architecture

Micro services
The Microservice architectural style is an approach to developing a single application as a suite of small services, each running in its own process and communicating with lightweight mechanisms. 

Critical characteristics of a Micro Services are
  1. Componentization via services
  2. Organized around Business Capabilities
  3. Smart End points
  4. Infrastructure Automation
  5. Design for failure


Key considerations for efficient Micro Services development and deployment.
  1. Infrastructure Automation and Rapid Provisioning
  2. Basic Monitoring
  3. Rapid Application Deployment
  4. Devops 


Advantages for Microservices are 
  1. Ability to deploy different services independently
  2. Reliability, due to the ability for a service to run even if another service is down. 
  3. Ability to build Services on different platforms, e.g. as a way to experiment with a new language.


Reactive Architecture
The need for error-tolerance and fast reaction in dynamic environments have resulted in reactive architectures. They have task-specific modules that initiate direct reactions in response to specific situations that occur in the environment. 

Key characteristics of Reactive Architecture are: 
  1. Responsive
  2. Resilient
  3. Elastic/Scalable
  4. Message Driven


Systems built as Reactive Systems are more flexible, loosely-coupled and scalable. They are significantly more tolerant of failure

Benefits of both Styles:
While Micro services enables looking at application in terms of small individually deployable services, Reactive Architecture helps building applications which can scale as need (at required later by scaling required services) and ensure fail over. Together they bring ease of maintenance, reliability, scalability and fail over to the application. 

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.