Microservices CXF Karaf PDF
Microservices CXF Karaf PDF
Benefits:
• Services themselves are simple, focusing on doing
one thing well
• Systems are loosely coupled
• Services and can be (relatively) independently
developed and deployed by different teams
• Services can be scaled differently
• Service team can use the most appropriate
technologies and programming languages
Microservices: Pros and Cons
Downsides:
• Mistakes in services boundaries definition are costly
• Remote calls are expensive and unreliable
• Testing, debugging and monitoring in distributed system
became more difficult
• Change syntax or semantic of remote contracts
introduces additional risks
• Infrastructure becomes more complex
• Eventual consistency
OSGi => Modular Applications
jars modules
OSGi: software modules
But:
• It is hard to achieve loosely coupling between the
modules (only partial solutions: Class.forName;
ServiceLoader; log-appenders)
• You cannot encapsulate functionality in the module
• Missing runtime control which version of the
dependencies functionality will be used
• Self-describing module contract is missing
• Keep the name and version of JAR file
• Add explicit package dependencies
OSGi bundle
(requirements)
• Add explicit package exports (capabilities)
• Provide API as external contract (OSGi services)
OSGi Services
Exports: Imports:
org.apache.activemq.pool 5.14.0, org.apache.activemq.pool [5.14,6),
org.apache.activemq.command 5.14.0 org.apache.activemq.command [5.14,6)
Decoupling
JMS Communication
MyBusinessDomain Bundle Implementation
• Blueprint
Classic Microservices vs OSGi
Aspect Microservices OSGi
Application structure Suite of small services Suite of bundles /
modules
Boundaries Around business Modularization around
capabilities business and technical
aspects
Communication Lightweight remote Flexible: local or remote
Contract Remote API Local java interfaces or
remote API
Decentralized Data Desired Depends on
Management requirements for single
process, desired for
multiple processes
Infrastructure Desired Desired
Automation
Apache Karaf
mvn:my.company/order-service-features/1.0.0/xml
Maven Repo /
Nexus
Karaf
org.apache.karaf.features.cfg
featuresRepositories=…, mvn:my.company/order-service-features/
1.0.0/xml
featuresBoot= …, order-service
console
feature:addurl mvn:my.company/order-service-features/1.0.0/xml
feature:install order-service
Migration to OSGi in eCommerce Project
Web Browser
SAP Oracle DB
Online Shop Design
Core Domain
DAOs
SAP
Messaging
DB DB DB
Online Shop Design
Core Domain
DAOs
SAP
Messaging
DB DB DB
Step 1: Packages Refactoring
com.mycompany com.mycompany
.domain.user .domain.user
.routes .account
.processors
.password
.converters
.mappers .deliveryaddress
.exceptions .billingaddress
• Classes for business function are grouped together -> high cohesion
• Less dependencies between packages -> low coupling
• Private and public packages are easily recognizable (model, api,
impl)
Christian Schneider ApacheCon Europe 2014 “Reflection of Design of Business Applications”
Step 2: Connectors API
Core Domain
API: OSGI Svc API: OSGI Svc API: OSGI Svc API: OSGI Services API: OSGI Services
DB Connector DB Connector DB Connector SAP Connector Messaging Connector
SAP
ActiveMQ
DB DB DB
Step 3: Parallel Web And OSGi Deployment
Web Application
Module Code
WAR
OSGi
Features
resources
spring
OSGI-INF/blueprint
<packaging>bundle</packaging>
…
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</
artifactId>
</plugin>
Step 4: Refactor Complex Domain Logic (Camel Routes)
from(„vm:PriceAndAvailability“) PriceAndAvailResult getPriceAndAvail (Cart cart,
1
.bean(availabilityOptionsMapper) AvailabilityOptions options);
.multicast(hdrAggregationStrategy
)
ATPOptions atpOptions = mapAvailabilityOptions(options);
.parallelProcessing().timeout(100) …
.to(„direct:getPrice“) final Future<AvailabilityReturner> availabilityFuture =
executorService.submit(availabilityTask);
.to („direct:getAvailability“) 2 final Future<PriceReturner> priceFuture =
.end executorService.submit(priceTask);
.validate(availabilityValidator) …
validateAvailability(availability);
.bean(priceAvailResponseMapper) PriceAndAvailResult result = new
PriceAndAvailabilityResult(availability,
price);
API: OSGI Svc API: OSGI Services API: OSGI Svc API: OSGI Svc API: OSGI Services
DB Connector SAP Connector DB Connector DB Connector Messaging Connector
SAP
DB DB DB ActibeMQ
Step 5: Separate containers for some services
RE RE
ST ST
REST REST
Newsletter
Address Service
Service Messaging
async
SAP
Messaging
DB DB
REST Communication in OSGi
• DOSGi and Aries Remote Service Admin (RSA)
Christian Schneider “Lean Microservices on OSGi“, ApacheCon EU 2016
<packaging>bundle</packaging>
…
<plugin>
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
</plugin>
Decoupling
SAP JCO Communication
MyBusinessDomain Bundle Implementation
LB LB
Developer Tests • QA
Production
• Performance
tests
• Load tests
Swagger in JAXRS API
Swagger in JAXRS API: Java First
Swagger in JAXRS API: WADL First