MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP /...

74
MICROSERVICES WITH JBOSS EAP / OPENSHIFT Babak Mozaffari, Consulting Software Engineer Diogenes Rettori, Principal Product Manager June 2016

Transcript of MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP /...

Page 1: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

MICROSERVICES WITH JBOSS EAP / OPENSHIFT

Babak Mozaffari, Consulting Software EngineerDiogenes Rettori, Principal Product ManagerJune 2016

Page 2: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Microservice Architecture

Page 3: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Define Microservice Architecture

● Software architectural style● Applications as a suite of small services● Each an independent process, in its own logical machine● Built around capabilities, single responsibility principle● Can independently replace / upgrade / scale / deploy services● Standard lightweight communication● Potentially heterogeneous environments are supported

Page 4: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Advantages of Microservices

● Faster and simpler deployment and rollback with smaller services● Ability to horizontally scale out individual services● Selecting the right tool, language and technology per service● Better ability to plan for fault isolation and avoiding chain failure● Continuous Delivery and Integration comes included● DevOps culture, higher service self-containment, less infrastructure maintenance● More autonomous teams, faster/better development.● Traditional divide and conquer benefits● A/B testing & canary deployments

Page 5: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Disadvantages of Microservices

● Less tooling / IDE support given the distributed nature● Tracing and monitoring becomes more complicated● QA, particularly integration testing can be difficult● Debugging is always more difficult for distributed systems● Higher complexity – higher fixed cost and overhead● Heterogenous environments are difficult and costly to maintain● Network reliability is always a challenge

Page 6: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Why Now?

● Global user base for software as a result of technological progress● Businesses demanding ever-greater agility● Cloud computing is cheaper and more widely available● DevOps, agile movement has made continuous integration and delivery common● Simply current iteration of applying the best practices of the past

Page 7: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

What's Too Modular?

● The number of developers, between 5 and 10 per team?● Comfortable on the cutting edge of technology?● DevOps, not every organization is prepared for the required cultural change● How skilled are you at troubleshooting?● Can you afford higher up-front costs?● Can your network support the architecture?

Page 8: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Monolithic Applications

● Some Microservices advocates use the term monolithic disparagingly● We reserve judgement, it is the result of legitimate trade-offs● Preferable for certain situations and not for others● May be just as modular as microservices, but typically bundled as one unit● Example, a single EAR or WAR file deployed on a single application server● Modules take advantage of the same infrastructure● Maximize efficiency by minimize network traffic and latency● Sometimes even possible to pass arguments by reference

Page 9: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Monolithic Application

Page 10: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Monolithic Application, Clustered

Page 11: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Tactical Microservices

● Selectively embrace cost/benefits of microservices● Avoid proactively decomposing the application into microservices● Take advantage of common infrastructure/environment uniformity● Identify and extract microservices based on goals

Page 12: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Tactical Microservices

Page 13: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Tactical Microservices, HA

Page 14: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Strategic Microservices

● Fully embrace microservices and its costs/benefits● Decompose entire applications into microservices● Implement entire systems as separately deployed microservices

Page 15: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Strategic Microservices

Page 16: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Strategic Microservices, HA

Page 17: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Business-Driven Microservices

● Microservices, many real benefits, but with real costs● System complexity can grow exponentially with distributed components● Modularity of the services can determine the complexity● API Gateway Pattern, often with a bit of orchestration● Individual services rarely depend on one another

Page 18: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Business-Driven Microservices

Page 19: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Cross-Cutting Concerns

● Containerization● Service Discovery● Load Balancer● Cache● Throttling, Circuit Breaker, Composable Asynchronous Execution● Security● Monitoring and Management● Resilience Testing

Page 20: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Anatomy of a Microservice

● Microservice design and development not covered by architectural style description● Allowing choice for the developers of each microservice is a stated goal● Individual microservices largely resemble other enterprise software components● Each microservice will have its own significant dependencies and technical

requirements:– Persistence, database connection pooling, connection management– External dependencies, integration with legacy systems– Authentication and authorization, declarative security– Transactional behavior within the service

● Common enterprise requirements, leading to application servers / frameworks

Page 21: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

JBoss EAP / OpenShift Enterprise

Page 22: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Red Hat JBoss Enterprise Application Platform 7

● Java EE 7– WebSockets– JSON– HTML5– Batch processing

● Lightweight, fast to startup, optimized for cloud and containers● Full Java EE across all environments – on-premise, virtual, cloud ● Rock solid reliability, stability, scalability● Security and compliance, including Common Criteria Certification

Page 23: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Integration ServicesBy JBoss Fuse

JBoss Application Services

Real time Decision ServiceBy JBoss BRMS

In Memory Data GridBy JBoss Data Grid

Messaging ServicesBy JBoss A-MQ

Java EE Application ServerBy JBoss EAP

TomcatBy JBoss Web Server

Page 24: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

JBoss EAP for OpenShift

Page 25: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

JBoss Data Grid for OpenShift

Page 26: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Fuse Integration Services

Page 27: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

JBoss A-MQ for OpenShift

Page 28: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

JBoss BRMS for OpenShift

Page 29: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

JBoss BPM Suite for OpenShift

Page 30: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Integration ServicesBy JBoss Fuse

JBoss Application Services

Real time Decision ServiceBy JBoss BRMS

In Memory Data GridBy JBoss Data Grid

Messaging ServicesBy JBoss A-MQ

Java EE Application ServerBy JBoss EAP

TomcatBy JBoss Web Server

Single Sign On by RH SSO

Intelligent Process ServerBy JBoss BPM Suite

Page 31: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Red Hat SSO for OpenShift

Page 32: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Red Hat SSO for OpenShift

Page 33: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Red Hat SSO for OpenShift

Page 34: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Red Hat SSO for OpenShift

Page 35: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Red Hat SSO for OpenShift

Page 36: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Red Hat SSO for OpenShift

Page 37: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Red Hat SSO for OpenShift

Page 38: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Integration ServicesBy JBoss Fuse

JBoss Application Services

Real time Decision ServiceBy JBoss BRMS

In Memory Data GridBy JBoss Data Grid

Messaging ServicesBy JBoss A-MQ

Java EE Application ServerBy JBoss EAP

TomcatBy JBoss Web Server

Single Sign On by RH SSO

Intelligent Process ServerBy JBoss BPM Suite

API Management

Page 39: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Access Control Rate Limiting Developer Portal

Billing and Payments API DocumentationAnalytics

https://www.3scale.net/redhat-integration

Page 40: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Design and Development

Page 41: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Reference Application: Technology

● JBoss EAP 7– Java SE 8, Java EE 7– Java Persistence API (JPA)– JAX-RS 2.0– Concurrency API

● Maven● OpenShift Enterprise 3.2

– Docker / Kubernetes– Containerization, Service Discovery, Load Balancer, etc

Page 42: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Reference Application: Use Case

● Online Shopping example application● Customer interface is a simple Web App / API Gateway, called presentation● Core functionality implemented as 3 microservices:

– Product microservice: product catalog and availability– Sales microservice: customers and orders– Billing microservice: payment proxy service

● Product and sales each rely on their own database service

Page 43: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Data Model using JPA

Page 44: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Basic JPA Entity

● Annotate Java class as JPA Entity:

@Entity

public class Product

● Define fields and JavaBean accessors:

private String name;

private String description;

...

private Boolean featured;

private Integer availability;

private BigDecimal price;

private String image;

Page 45: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

JPA Features

● Designate a primary key and allow the database to generate its value:

@Id

@GeneratedValue(strategy = GenerationType.IDENTITY)

private Long sku;

● Declare a named query to allow easy efficient access to data:

@NamedQuery(name = "Product.findFeatured",query = "SELECT p FROM Product p WHERE p.featured = true")

● Also annotate the class as a JAXB bean, to get XML/JSON marshaling:

@XmlRootElement

Page 46: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

JPA Relationships

● Bidirectional many to many relationship, owning side:

@ManyToMany(fetch = FetchType.EAGER)

@JoinTable(name = "PRODUCT_KEYWORD",

joinColumns = @JoinColumn(name = "SKU", referencedColumnName = "SKU"),

inverseJoinColumns = @JoinColumn(name = "KEYWORD",

referencedColumnName = "KEYWORD"))

private List<Keyword> keywords;

● Non-owning side:

@ManyToMany(fetch = FetchType.EAGER, mappedBy = "keywords")

private List<Product> products;

Page 47: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

JPA Configuration

● Configure persistence.xml:

<persistence-unit name="primary">

<jta-data-source>java:jboss/datasources/ProductDS</jta-data-source>

<properties>

<!-- Properties for Hibernate -->

<property name="hibernate.hbm2ddl.auto" value="update" />

<property name="hibernate.show_sql" value="true" />

<property name="hibernate.dialectvalue="org.hibernate.dialect.MySQLDialect" />

</properties>

</persistence-unit>

Page 48: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

OpenShift Database Service

Page 49: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

OpenShift Enterprise Database Images

● This sample application uses database services built on the supported MySQL image● Single command to build a database instance and configure it:

oc new-app -e MYSQL_USER=product,MYSQL_PASSWORD=password,\

MYSQL_DATABASE=product,\

MYSQL_ROOT_PASSWORD=passwd \

mysql --name=product-db

Page 50: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

REST with JAX-RS 2.0

Page 51: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Enabling JAX-RS Support

● Enable JAX-RS services in a web application by configuring its descriptor:

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee

http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

<servlet-mapping>

<servlet-name>javax.ws.rs.core.Application</servlet-name>

<url-pattern>/*</url-pattern>

</servlet-mapping>

</web-app>

Page 52: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Creating JAX-RS Services

● Annotate Java class to create a JAX-RS service listening on a certain path:

@Path("/")

public class ProductService● Create an annotated Java method for each service operation● Each operation has its own path● Service operation address is comprised of the multiple paths leading to it:

– Application Context / JAX-RS Servlet / JAX-RS Service Path / Operation Path● Specify HTTP method for each operation with annotations● Annotate operation with request and response media type

Page 53: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Sample JAX-RS Operation

● To add a product by posting it as either JSON or XML:

@Path("/products")

@POST

@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})

@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})

public Product addProduct(Product product)

{

● The response is also returned as either JSON or XML● With JAX-RS servlet url pattern of /* and service path of /, this operation is accessible

at http://host:port/application/products

Page 54: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

JAX-RS interaction with JPA

● JPA persistence may require a transactional context● Easier to rely on the container, then to leverage user transactions● Any class annotated as a REST service can also be a stateless session bean:

@Path("/")

@Stateless

@LocalBean

public class ProductService

● An entity manager can then be injected:

@PersistenceContext

private EntityManager em;

Page 55: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Error Handling

● Standard HTTP codes along with descriptive information to communicate errors● For example, return HTTP error code 422 when request is invalid:

throw new WebApplicationException( 422 );

● To return error description details, use a JAXB class:

@XmlRootElement

public class Error

{

private int code;

private String message;

private String details;

Page 56: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Error Handling

● Communicate errors by throwing WebApplicationException● Provide details by passing a JAX-RS Response to the exception constructor● Build a Response by providing a JAXB object to ResponseBuilder:

ResponseBuilder responseBuilder = Response.status( code );

responseBuilder = responseBuilder.entity( error );

throw new WebApplicationException( responseBuilder.build() );

Page 57: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Resource API Design

● No strict standards govern RESTful service API design● Conventions and common practice promote consistent behavior● Use standard HTTP methods for CRUD capabilities for a resource● Use the plural form of the resource name as the first path of URL● Specific HTTP methods or URL patterns are used for each of the CRUD operation

Page 58: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Create Resource

● Use HTTP POST to add a new resource instance● Receive the resource as the request content● Return the persisted resource, including generated content

@Path("/products")

@POST

@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})

@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})

public Product addProduct(Product product) {

em.persist( product );

return product;

Page 59: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Read Resource / Search

● Use GET against resource URL with potential query parameters:

@GET

@Path("/products")

@Produces({"application/json", "application/xml"})

public Collection<Product> getProducts(@Context UriInfo uriInfo) {

MultivaluedMap<String, String> queryParams = uriInfo.getQueryParameters();

if( queryParams.containsKey( "featured" ) )

{

return em.createNamedQuery( "Product.findFeatured"

Product.class ).getResultList();

}

Page 60: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Read Resource / Lookup

● Issue a GET request to resource address and append unique resource identifier● Example: http://host:port/application/products/1001

@GET

@Path("/products/{sku}")

@Produces({"application/json", "application/xml"})

public Product getProduct(@PathParam("sku") Long sku) {

Product product = em.find( Product.class, sku );

if( product == null )

throw new Error( HttpURLConnection.HTTP_NOT_FOUND,

"Product not found" ).asException();

return product;

Page 61: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Update Resource / Full Update

● When updating, the request does not always include every resource attribute● Distinguish between a full and partial update in a RESTful API● One common approach is to use the distinct HTTP methods of PUT and PATCH● For a full update:

@PUT

@Path("/products/{sku}")

@Consumes({"application/json", "application/xml"})

@Produces({"application/json", "application/xml"})

public Product updateProduct(@PathParam("sku") Long sku, Product product)

Page 62: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Update Resource / Partial Update

● No native support for PATCH, so first declare an annotation for this purpose

@Target({ElementType.METHOD})

@Retention(RetentionPolicy.RUNTIME)

@HttpMethod("PATCH")

public @interface PATCH

{

}

● Once declared, simply use PATCH instead of PUT as the annotation● Provide an implementation that ignores any fields missing in JSON or XML request

Page 63: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Delete Resource

● Use the DELETE method to remove a resource, providing a direct link to it:

@DELETE

@Path("/products/{sku}")

@Consumes({"application/json", "application/xml"})

@Produces({"application/json", "application/xml"})

public void deleteProduct(@PathParam("sku") Long sku)

{

Product product = getProduct( sku );

em.remove( product );

}

Page 64: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Sub-Resources

● Orders only exist in the context of customers, so they are modeled as sub-resources● In REST services, address sub-resources within the context of their parent resource.● The CRUD operations are otherwise the same. For example, to delete an order:

@DELETE

@Path("/customers/{customerId}/orders/{orderId}")

public void deleteOrder( @PathParam("customerId") Long customerId,

@PathParam("orderId") Long orderId)

{

...

Page 65: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Java EE Concurrency API

● Java EE 7 introduces ManagedExecutorService for server thread pools● In EAP 7, do a JNDI lookup to get a reference to this service :

ManagedExecutorService executorService =

InitialContext.doLookup( "java:comp/DefaultManagedExecutorService" );

● JBoss EAP 7 can configure to throttle and otherwise optimize managed executor service thread pools.

Page 66: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

JAX-RS Asynchronous Processing

● JAX-RS 2.0 introduces async capability for both server and client-side● Annotate operation as Suspended and inject an AsyncResponse:

public void process( final Transaction transaction,

final @Suspended AsyncResponse asyncResponse) {

Runnable runnable = () -> {

Result result = processSync( transaction );

asyncResponse.resume( result );

};

getExecutorService().execute( runnable );

}

Page 67: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

JAX-RS Client

● JAX-RS 2.0 introduces a client API● For example, to look up a product with a GET call to the resource API:

Client client = ClientBuilder.newClient();

WebTarget target = client.target("http://product-service").path("/product");

target = target.path("products").path(sku);

Response response = webTarget.request(MediaType.APPLICATION_JSON).get();

If( response.getStatus() < HttpStatus.SC_BAD_REQUEST ) {

Product product = response.readEntity(Product.class);

}

Page 68: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

OpenShift Enterprise Deployment

Page 69: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Red Hat xPaaS Images

● Red Hat xPaaS images are configured as image streams in OSE 3:

# oc get imagestreams

NAME DOCKER REPO

...

jboss-eap64-openshift registry.access.redhat.com/jboss-eap-6/eap64-openshift

jboss-eap70-openshift registry.access.redhat.com/jboss-eap-7/eap70-openshift

...

Page 70: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Build & Deploy

● OSE S2I allows source code to be pulled from a git repository, built and deployed● Provide a maven pom.xml file at the root● An optional EAP server configuration file can be provided in the project● Use context-dir if the application code is not at the root of the repository:

oc new-app

jboss-eap70-openshift~https://github.com/RHsyseng/MSA-EAP7-OSE.git

--context-dir=Billing

--name=billing-service

Page 71: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Configure EAP 7 Image

● The name of an OpenShift application is resolved to its address as a hostname● Environment variables may be used to configure a server image● Configure the product service to access the product database image:

oc new-app -e MYSQL_USER=product,MYSQL_PASSWORD=password

jboss-eap70-openshift~https://github.com/RHsyseng/MSA-EAP7-OSE.git

--context-dir=Product --name=product-service

Page 72: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

OpenShift Router / Load Balancer

● Hardware load balancers such as the F5 BIG-IP® router can be configured● Alternatively, the provided software HAProxy Router may be used● Expose applications accessed externally to have the router forward requests:

oc expose service presentation –hostname=msa.example.com

● Once a service is exposed, requests pointing to router with the given hostname are routed and load balanced between the replicas of the intended service

Page 73: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service

Application Homepage

Page 74: MICROSERVICES WITH JBOSS EAP / OPENSHIFT - Red · PDF fileMICROSERVICES WITH JBOSS EAP / OPENSHIFT ... –Authentication and authorization, ... Any class annotated as a REST service