Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Tutorial: Using REST and OSGi Standards for Micro Services"

(Created page with "It's currently popular to create REST-based networked services. This is understandable, as the ubiquity http/https, the simplicity of the REST approach, and the availability...")
 
Line 1: Line 1:
 +
<!-- This CSS wraps code blocks in pretty boxes -->
 +
<css>
 +
.mw-code {
 +
  background-color: #fafafa;
 +
  padding: 20px;
 +
  border-color: #ddd;
 +
  border-width: 3px;
 +
  border-style: solid;
 +
  border-radius: 5px;
 +
  margin-left: 10px;
 +
  margin-right: 10px;
 +
  overflow-x: auto;
 +
}
 +
 +
.mw-headline {
 +
  font-family: Helvetica Neue,Helvetica,Arial,sans-serif;
 +
  color: rgb(51, 51, 51);
 +
}
 +
 +
p {
 +
  font-family: Helvetica Neue,Helvetica,Arial,sans-serif;
 +
  text-align: justify;
 +
}
 +
 +
h1 {
 +
  border-bottom-color: rgb(238, 238, 238);
 +
  font-weight: bold;
 +
  padding-bottom: 17px;
 +
  font-size: 30px;
 +
  line-height: 36px;
 +
}
 +
 +
h2 {
 +
  border-bottom-color: rgb(238, 238, 238);
 +
  padding-bottom: 12px;
 +
  font-weight: bold;
 +
  font-size: 24px;
 +
  line-height: 36px;
 +
}
 +
 +
h3 {
 +
  border-bottom-width: 1px;
 +
  border-bottom-style: solid;
 +
  border-bottom-color: rgb(238, 238, 238);
 +
  padding-bottom: 8px;
 +
  font-size: 18px;
 +
  line-height: 27px;
 +
}
 +
 +
h4 {
 +
  font-size: 14px;
 +
}
 +
 +
h5 {
 +
  font-size: 12px;
 +
}
 +
 +
h6 {
 +
  font-size: 11px;
 +
  color: #EBEBEB;
 +
  text-transform: uppercase;
 +
}
 +
 +
a {
 +
  color: rgb(0, 105, 214);
 +
  font-family: Helvetica Neue,Helvetica,Arial,sans-serif;
 +
}
 +
 +
a:visited {
 +
  color: rgb(0, 105, 214);
 +
}
 +
</css>
 +
 +
==Introduction==
 +
 
It's currently popular to create REST-based networked services.  This is understandable, as the ubiquity http/https, the simplicity of the REST approach, and the availability of open, cross-language object serialization formats like JSON and XML, along with the availability of quality distribution frameworks all make it easier than ever to define, implement, and deploy a service and it's associated API.
 
It's currently popular to create REST-based networked services.  This is understandable, as the ubiquity http/https, the simplicity of the REST approach, and the availability of open, cross-language object serialization formats like JSON and XML, along with the availability of quality distribution frameworks all make it easier than ever to define, implement, and deploy a service and it's associated API.
  
Line 6: Line 81:
 
#Service-Level Concerns:  How to discover the service?  How to version the service?  How to keep the service and clients/consumers separate from the underlying mechanisms/implementations?, How to secure the service?  How to describe and document the service so that others may easily understand and use it?  How to combine services, etc.
 
#Service-Level Concerns:  How to discover the service?  How to version the service?  How to keep the service and clients/consumers separate from the underlying mechanisms/implementations?, How to secure the service?  How to describe and document the service so that others may easily understand and use it?  How to combine services, etc.
  
==Transport/Distribution Concerns==
+
==Transport and Distribution Concerns==
  
 
From the service implementer's perspective, the selection of a framework or implementation makes de-facto design choices about transport or distribution concerns.  For example, some REST frameworks use XML, some JSON, some support both.  However, once such a framework is chosen the service implemented and deployed, it can become very difficult, time-consuming, or even technically impossible to change to use other frameworks or distribution systems.   
 
From the service implementer's perspective, the selection of a framework or implementation makes de-facto design choices about transport or distribution concerns.  For example, some REST frameworks use XML, some JSON, some support both.  However, once such a framework is chosen the service implemented and deployed, it can become very difficult, time-consuming, or even technically impossible to change to use other frameworks or distribution systems.   
  
For REST-based services, however, standards are beginning to emerge that allow REST services to be defined and implemented without making a specific choice of distribution system or framework.  One example in Java is [https://en.wikipedia.org/wiki/Java_API_for_RESTful_Web_Services JAX Remote Services] or JAX-RS.  JAX-RS defines/standardizes Java annotations for classes and methods that are to be exposed for remote access.  JAX-RS implementations then use these annotations to 'export' the service (make it available for remote access) and handle the mapping between http/https requests, and the appropriate method invocation.
+
For REST-based services, however, standards are beginning to emerge that allow REST services to be defined and implemented without making a specific choice of distribution system or framework.  One example in Java is [https://en.wikipedia.org/wiki/Java_API_for_RESTful_Web_Services JAX Remote Services] or JAX-RS.  JAX-RS defines/standardizes Java annotations for classes and methods that are to be exposed for remote access.  JAX-RS implementations then use these annotations to 'export' the service (make it available for remote access) and handle the mapping between http/https requests, and the appropriate method invocation.  For example, here's a small 'hello-world' service that uses JAX-RS annotations:
 +
 
 +
<source lang="java">
 +
import javax.ws.rs.GET;
 +
import javax.ws.rs.Produces;
 +
import javax.ws.rs.Path;
 +
 
 +
// The Java class will be hosted at the URI path "/helloworld"
 +
@Path("/helloworld")
 +
public class HelloWorldResource {
 +
   
 +
    // The Java method will process HTTP GET requests
 +
    @GET
 +
    // The Java method will produce content identified by the MIME Media
 +
    // type "text/plain"
 +
    @Produces("text/plain")
 +
    public String getClichedMessage() {
 +
        // Return some cliched textual content
 +
        return "Hello World";
 +
    }
 +
}
 +
</source>
 +
 
 +
One very useful aspect of the JAX-RS annotations is that multiple implementations may exist, and it is relatively easy to switch from one implementation to another.
 +
 
 +
==Service-Level Concerns==
 +
 
 +
Although frequently of secondary concern to service implementers, service-level concerns are typically of primary importance to service '''consumers'''.  For example, before I can use any REST-based service, I have to know about (discover) the service and needed meta-data:  the service location (i.e. it's URL), what methods exist and arguments are required, and what can be expected in response.  Additionally, what version of the service is being accessed, what credentials/security are required, what are the run-time qualities of the remote service?
 +
 
 +
===OSGi Remote Services===
 +
 
 +
In recent releases the OSGi Alliance has defined a specification called [http://www.osgi.org/Specifications/HomePage Remote Services] (chapter 100 in the enterprise spec).  Along with the Remote Service Admin specification (chapter 122 in enterprise spec), Remote Services defines ways to express answers to service-level concerns in an implementation-independent way, similar to what JAX-RS does for transport-level concerns.  For example, the Remote Service spec allows meta-data for service type, service version, service endpoint/URL identification, security, and quality of service properties to be communicated between service implementer and service consumer without referring to any Remote Services implementation.  Further, Remote Services/RSA defines a way to dynamically discover and un-discover remote services, allowing for easy consumer support for networked and frequently unreliable services.
 +
 
 +
Several implementations of OSGi Remote Services exist, including open source [https://wiki.eclipse.org/ECF#OSGi_Remote_Services ECF], [http://cxf.apache.org/ CXF], [http://www.amdatu.org/ Amdatu] and commercial implementations.
  
For example, here's a small service that uses JAX-RS annotations:
+
OSGi RSA has the concept of a distribution system responsible for exporting a local service and making it available for remote access.  According to the specification, many distributions systems may be used even for a single service.  One of the unique characteristics of ECF's open implementation is that it has an open API for creating/adding new distribution systems.

Revision as of 15:11, 3 September 2015


Introduction

It's currently popular to create REST-based networked services. This is understandable, as the ubiquity http/https, the simplicity of the REST approach, and the availability of open, cross-language object serialization formats like JSON and XML, along with the availability of quality distribution frameworks all make it easier than ever to define, implement, and deploy a service and it's associated API.

One thing that is relatively new, however, is that there are now open standards that deal with two levels of concerns

  1. Transport/Distribution-Level Concerns: What protocol and serialization format are to be used? How to support cross-language type systems? How to be as bandwidth efficient and performant as possible? How to handle network failure? etc.
  2. Service-Level Concerns: How to discover the service? How to version the service? How to keep the service and clients/consumers separate from the underlying mechanisms/implementations?, How to secure the service? How to describe and document the service so that others may easily understand and use it? How to combine services, etc.

Transport and Distribution Concerns

From the service implementer's perspective, the selection of a framework or implementation makes de-facto design choices about transport or distribution concerns. For example, some REST frameworks use XML, some JSON, some support both. However, once such a framework is chosen the service implemented and deployed, it can become very difficult, time-consuming, or even technically impossible to change to use other frameworks or distribution systems.

For REST-based services, however, standards are beginning to emerge that allow REST services to be defined and implemented without making a specific choice of distribution system or framework. One example in Java is JAX Remote Services or JAX-RS. JAX-RS defines/standardizes Java annotations for classes and methods that are to be exposed for remote access. JAX-RS implementations then use these annotations to 'export' the service (make it available for remote access) and handle the mapping between http/https requests, and the appropriate method invocation. For example, here's a small 'hello-world' service that uses JAX-RS annotations:

import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;
 
// The Java class will be hosted at the URI path "/helloworld"
@Path("/helloworld")
public class HelloWorldResource {
 
    // The Java method will process HTTP GET requests
    @GET
    // The Java method will produce content identified by the MIME Media
    // type "text/plain"
    @Produces("text/plain")
    public String getClichedMessage() {
        // Return some cliched textual content
        return "Hello World";
    }
}

One very useful aspect of the JAX-RS annotations is that multiple implementations may exist, and it is relatively easy to switch from one implementation to another.

Service-Level Concerns

Although frequently of secondary concern to service implementers, service-level concerns are typically of primary importance to service consumers. For example, before I can use any REST-based service, I have to know about (discover) the service and needed meta-data: the service location (i.e. it's URL), what methods exist and arguments are required, and what can be expected in response. Additionally, what version of the service is being accessed, what credentials/security are required, what are the run-time qualities of the remote service?

OSGi Remote Services

In recent releases the OSGi Alliance has defined a specification called Remote Services (chapter 100 in the enterprise spec). Along with the Remote Service Admin specification (chapter 122 in enterprise spec), Remote Services defines ways to express answers to service-level concerns in an implementation-independent way, similar to what JAX-RS does for transport-level concerns. For example, the Remote Service spec allows meta-data for service type, service version, service endpoint/URL identification, security, and quality of service properties to be communicated between service implementer and service consumer without referring to any Remote Services implementation. Further, Remote Services/RSA defines a way to dynamically discover and un-discover remote services, allowing for easy consumer support for networked and frequently unreliable services.

Several implementations of OSGi Remote Services exist, including open source ECF, CXF, Amdatu and commercial implementations.

OSGi RSA has the concept of a distribution system responsible for exporting a local service and making it available for remote access. According to the specification, many distributions systems may be used even for a single service. One of the unique characteristics of ECF's open implementation is that it has an open API for creating/adding new distribution systems.

Copyright © Eclipse Foundation, Inc. All Rights Reserved.