workshop msf4j - getting started with microservices and java
TRANSCRIPT
msf4j – Microservices Framework for JavaWorkshopEdgar Silva edgar @wso2.com
Before you getting started …o This workshop is intend to cover how to use the
WSO2 Microservices Server for Java – msf4j, we will cover some basic overview about some concepts, but we strongly recommend you look for more detailed basics about Microservices.
o Recommended reading:o http://nginx.com/blog/introduction-to-microservices/ o https://www.nginx.com/blog/building-microservices-using
-an-api-gateway/
2
Part 1 of 3
3
4
Basics Pre-Reqs
You will need the following installed:o Java 8o Maveno See the MSF4J releases page:
o https://github.com/wso2/msf4j o Let’s work direct from the source:o Git pull
o https://github.com/wso2/msf4j o Or simply download from this url: (easier)
https://github.com/wso2/msf4j/archive/master.zip
5
Installer
Mount the root pom.xml project into your preferred IDE
7
In Case of WSO2 Developer Studio
8
Let’s work in the samples directoryo So, cd
<msf4j_HOME>/samples
o Make you sure you could import the project hello_world
9
Command Line time ...o Go to the dir:
o <msf4j_HOME>/samples/hello_worldo Type:
mvn package
10
Command Line time ...o After Maven process
o ( be pacient with downloading ) o What is happening:
o The pom.xml inside the sample, inherits the dependencies from the root’s pom.xml, that’s why you don’t need to worry with this process
o In a few (seconds) you will have a hello_service....jar into your target folder.
11
Executing the HelloService
12
• Msf4j is booting in my case in about less than 300ms• In the previous maven process, every dependency from other jars
were included into your helloworld-1.0.0-SNAPSHOT.jar, including the reference to the Main Java Class.
• Everything you need is ready
Understading our first sample:HelloService class
13
package org.wso2.carbon.msf4j.example; import javax.ws.rs.GET;import javax.ws.rs.Path;import javax.ws.rs.PathParam; /** * Hello service resource class. */@Path("/hello")public class HelloService { @GET @Path("/{name}") public String hello(@PathParam("name") String name) { return "Hello " + name; } }}
JAX-RS
Simple class (REST Endpoint
REST Java Method
Back to the Basicso The simplest Java Class startupo See the main method:
14
public class Application { public static void main(String[] args) { new MicroservicesRunner() .deploy(new HelloService()) .start(); }
Testing
15
edgar$ curl -v http://localhost:8080/hello/valentina
cURL for Windows Userso Take a look on this:o http://www.confusedbycode.com/curl/
16
Update: o Thanks @jpviragine for that great tip:
https://github.com/jkbrzt/httpie o Really great tool, works like cURL, but much better and more user
friendly o Syntax: http <options> service or URLo But if you prefer to be “roots”, ok, continue if cURL
17
Going Further....
18
public class Application { public static void main(String[] args) { new MicroservicesRunner(7888, 8888) .deploy(new HelloService()) .start(); }
Services exposed through different ports(*)
(*) Default port is 8080 when no ports are specified
Example #2:stockquote-service
19
Executing the Service1. Enter in the
<msf4j_home>/samples/stockquote-service
2. mvn package3. java -jar target/stockquote-service-1.0.0-
SNAPSHOT.jar
20
Exercise #1:o In the console type: (Windows Userso curl -v http://localhost:8080/stockquote/GOOG
21
Exercise #2:o Now we will send a POST message to our Service:o Please type (or copy and paste) the following command:
o curl -v -X POST -H "Content-Type:application/json" -d '{"symbol":"BVMF","name": "Bovespa","last":149.62,"low":150.78,"high":149.18,"createdByHost":"localhost"}' http://localhost:8080/stockquote
o This command will save a new Symbol into our Stock Quote Service
22
What had you learned so faro You had used:
o Java 8 + maven for building our samples and exercises
o Executed Microservices with basic jar –jar approach
o You saw how easy you can build REST Services using Microservices approach
23
Going beyond the simple java -jar
24
Understanding the basics about Microservices
o Several approaches for a “decoupled SOA”o In this tutorial we will use the “Container-
based approach”o Microservices are about:
o Lighter o Business Need Orientedo Composable
25
“Legacy Web .NET”
26
WebServe
rASP.NET ADO.NET
Windows OS
.NET CLR RuntimeFront-StoreHTML
“Legacy Web Java”
27
WebServe
r
Web Framework
(JSF, Struts,
etc)
Persitence(JPA,
Hibernate,SpringTemplates)
Any OS
JVM
Other(JMS, JTA
etc)
App Server
Front-StoreHTML
New Approach : MicroServices
28
Product Service
get /productspost /productsget /products/{id}get /products/offset/10/1
CustomerService
get /customerspost /customersget /customers/{id}get /customers/export
AddressService
get /addresspost /address/{zip}get /address/geo/{x}/{y}
3Examples
MicroServices :: Composition
29
Product Service
CustomerService
AddressService
AddressService
ProductService
Customer Service
AddressService Customer Service
SinglePage APPHTML
FrontStoreService
Delivery Service
One of the Big Wins on Microserviceso Quick deployments
o You are deploying a loosely-coupled, modular component only o Not a huge EAR with dozens of Jars as you used to do
in a monolithic enterprise App
30
Monolithic vs Microservices
31
Monolithic
Microservices
Reference: http://martinfowler.com/articles/microservices.html#ComponentizationViaServices
Recap on Microservices Definition
32
Paulo Merson https://insights.sei.cmu.edu/saturn/2015/11/defining-microservices.html Great definition, written by another Brazilian
From an architecture perspective, the microservice style belongs primarily to the deployment view.
It dictates that the deployment unit should contain only one service or just a few cohesive services.
The deployment constraint is the distinguishing factor. As a result, microservices are easier to deploy, become more scalable, and can be developed more independently by different teams using different technologies.
“
Some on what you gain on Microserviceso Benefits of using microservices:
o Deployabilityo Availabilityo Scalabilityo Modifiabilityo Management
33
Paulo Merson https://insights.sei.cmu.edu/saturn/2015/11/microservices-beyond-the-hype-what-you-gain-and-what-you-lose.html ( Great post )
Some on what you gain on Microserviceso I would add:
o Deployabilityo Availability (Auto-Scaling via Containers) o Analyticso Scalabilityo Modifiabilityo Management
34
Paulo Merson https://insights.sei.cmu.edu/saturn/2015/11/microservices-beyond-the-hype-what-you-gain-and-what-you-lose.html
Part 2 of 3
35
o Part 2:We ship in the distroan example showing how to run a whole deploy from severaldifferent container machines managedby Kubernetes.
36
Running Everything you need in one placeo In your command line, go to your: <msf4j_home>/samples/petstore
o Enter in deployment folder and execute: run.sh
o That’s all, time to get some juice, it will download everything you need: o Vagranto CoreOSo Kuberneteso Docker
37
More detailed Informationo https://github.com/wso2/product-msf4j/
tree/master/samples/petstore/deployment/kubernetes-vagrant-coreos-cluster
o Disclaimer: This tutorial is focused on WSO2 Micro
Services Server – msf4j, and some introduction to the basics on Kubernetes is strongly recommended before you move forward on this tutorial.
38
39
This step will get a few minutes, according to your internet and machine,So get relaxed while you can enjoy some “Matrix-like” in your console
http://thenextweb.com/wp-content/blogs.dir/1/files/2013/11/relax-at-work.jpg
Troubleshooting Double check if your JAVA_HOME is Java 8 Please , if you are using MacOS, make sure you that you have
wget installed. Recommend you use brew install wget
If you get errors communicating with Kubernetes nodes, please add this variable before execute run.sh:
export KUBERNETES_MASTER=http://172.17.8.101:8080 That will be the default Kubernetes UI Console and API Address
40
Understanding the Example (Demo)
41
Our traditional Pet Store Sample has the following Microservices …
fileserver frontend-admin frontend-user
Pet (store) transactionsecurity
What is happening….o If you are using VirtualBox as the
Hypervisor (recommended), you will see the following 3 VMs started
42
When the whole run.sh get finished…o Please, execute the command:
o kubectl get podso The result must be like this:
43
All the pods, must beLike this, keep repeatingThis process until all getready
When the whole run.sh get finished…o Please, execute the command:
o kubectl get podso The result must be like this:
44
Troubleshooting: If your pet-xxx appears the READY info as 0/1It might be not initializedSyncronized with Redis.
To solve that, execute:./clean.sh and later on./petstore.sh
What will we see now?o Kubernetes UI
o Nodeso Services o Podso General Info
o Pet Store Admin (PHP App)o Pet Store Site (PHP App)
45
Accessing Kubernetes UIhttp://172.17.8.101:8080/ui Here is the Kubernetes Admin that you can open in your browser
46
Accessing Kubernetes UIRelationship between the VMs and Kubernetes (Nodes)
47
Here you can see the IpsAttached to the each Node
Accessing Kubernetes UI Resources
48
Viewing the Pods
In Kubernetes, rather than individual application containers, pods are the smallest deployable units that can be created, scheduled, and managed.
http://kubernetes.io/v1.0/docs/user-guide/pods.html
Accessing Kubernetes UI Resources
49
Viewing the Pods
In Kubernetes, rather than individual application containers, pods are the smallest deployable units that can be created, scheduled, and managed.
http://kubernetes.io/v1.0/docs/user-guide/pods.html
The context of the pod can be defined as the conjunction of several Linux namespaces:
• PID namespace (applications within the pod can see each other's processes)• Network namespace (applications within the pod have access to the same IP and port space)• IPC namespace (applications within the pod can use SystemV IPC or POSIX message queues to
communicate)• UTS namespace (applications within the pod share a hostname)
Accessing Kubernetes UI Resources
50
Viewing the Services
Services are an interface to a group of containers so that consumers do not have to worry about anything beyond a single access location. By deploying a service, you easily gain discover-ability and can simplify your container designs.
Accessing Kubernetes UI Resources
51
Viewing Replication Controllers
In Kubernetes, the base unit of deployment is a pod (intro to pods), which is a group of containers that work together and therefore are logically grouped. The replication controller stores a pod template in order to create new pods if needed.
https://coreos.com/kubernetes/docs/latest/replication-controller.html
Understanding our Application DemoWhat do you have up and running now (main components)
52
Your Machine OS
Hypervisor
Kubernetes Cluster-Master
K8S Node 01 K8S Node 02
Pods Pods
Replication Controller
Service
Understanding our Application DemoWhat do you have up and running now (main components)
53
Your Machine OS
HypervisorKubernetes Cluster-Master
K8S Node 01 K8S Node 02Pods Pods
Replication Controller
Service
Browser
Understanding our Application DemoWhat do you have up and running now (main components)
54
Your Machine OS
HypervisorKubernetes Cluster-Master
K8S Node 01 K8S Node 02Pods Pods
Replication Controller
Service
Browser
NGINX
Understanding our Application DemoWhat do you have up and running now (main components)
55
Getting Actual pods: $ kubectl get pods
Understanding our Application DemoWhat do you have up and running now (main components)
56
Getting details about some pod, for instance$ kubectl describe pods store-fe-r43hm
Understanding our Application DemoWhat do you have up and running now (main components)
57
Getting details about some pod, for instance$ kubectl describe pods store-fe-r43hm
Please, note here which is the Pod internal IP: 10.244.36.23
And in which Node this podIs actually running
Important: notice that the pod’s id will change if you restart your environment
In my actual case, my pod is store-fe-<id>, id= r43m
Understanding our Application DemoWhat do you have up and running now (main components)
58
Getting Actual Services: $ kubectl describe service store-fe
Basic Application Overview
59
Persistence Repository / Services
"Containerized” Microservices based in pure java –jar approach
Client Apps (PHP)petstore-admin petstore
Basic Application Overview :Invoking Admin PHP App
60
You will need to browse the service: admin-fe
http:// Node Server IP: Node Port
Ex: http://172.17.8.102:30984/
61
admin/admin
fileserver
Basic Application Overview :Invoking Store PHP App
65
You will need to browse the service: store-fe
http:// Node Server IP: Node Port
Ex: http://172.17.8.102:31466/
Here the Microservices TransactionIs invoked.
Recapo Executing the petstore sampleo Understanding the basics from Kubernetes
and its concepts, such as pods, services and Replication Controller.
o Executing the Services and Apps
What nexto Proposed Lab:
o Execute the previous samples from Part1 in Kubernetes + Docker
69
Part 3 of 3 - Analytics
70
For this Part of this Workshop the WSO2 Data Analytics Server is requiredo Please go to :
http://wso2.com/products/data-analytics-server/ o Download the producto Install the product:
1. Unzip2. That’s all3. Let’s call your installation destination folder as DAS_HOME
from now ono MySQL for this sample is also required
71
Running msf4j Metrics Sampleo Step 1: Configure WSO2 DAS
1. Go to <msf4j_HOME>/analytics/das-setup and execute setup.sh :1. /setup.sh -d <DAS_HOME> -u admin -p 2. Done, everything will be done by the script!
72
Running msf4j Metrics Sampleo Step 2: Execute DAS Server
1. Enter in DAS_HOME2. Make sure that Java 8 is in the path3. Type sh bin/wso2server.sh 4. Wait until to see a message in the console like
this:5. Open this browser URL, it will let you see the
WSO2 Data Analytics Server Console (default user admin and password admin)
73
74
WSO2 DAS is ready and configured!
Running Metrics Sampleo Step 3: (based on
https://github.com/wso2/product-msf4j/tree/master/samples/metrics)
1. Go to <msf4j_HOME>/samples/metrics2. Execute mvn clean install3. Please, export the following system
variables:1. export METRICS_REPORTING_DAS_DATAAGENTCONFIGPATH="data-agent-
conf.xml”2. export HTTP_MONITORING_DAS_DATAAGENTCONFIGPATH="data-agent-
conf.xml”
75
Running Metrics Sample (cont.)4. Execute: $ java -jar target/metrics-*.jar5. Invoke the following URLs via command line:
o curl -v http://localhost:8080/test/rand/500o curl -v http://localhost:8080/test/total/10o curl -v http://localhost:8080/test/echo/testo curl -v http://localhost:8080/student/910760234Vo curl -v --data
"{'nic':'860766123V','firstName':'Jack','lastName':'Black','age':29}" -H "Content-Type: application/json" http://localhost:8080/student
o curl -v http://localhost:8080/student/860766123Vo curl -v http://localhost:8080/student
76
Running Metrics Sample (cont.)o What is happening:
o Now, after the invocation from cURLs, some information were sent from WSO2 Microservices Server to WSO2 Data Analytics Server.
o The Metrics are also present in the command line where you are running the jar:
77
78
Why this magic happens?Take a look in the extra annotations into our services
@GET @Path("/{nic}") @Produces("application/json") @Timed @HTTPMonitoring public Student getStudent(@PathParam("nic") String nic) { return students.get(nic); } @POST @Consumes("application/json") @Metered @HTTPMonitoring public void addStudent(Student student) { students.put(student.getNic(), student); }
org.wso2.carbon.msf4j.example.service.StudentService.java
Also we have the data-agent-conf.xml
80
WSO2 msf4j WSO2 DAS
<Agent> <Name>Thrift</Name> <DataEndpointClass> org.wso2.carbon.databridge.agent.endpoint.thrift.ThriftDataEndpoint </DataEndpointClass> <TrustSore>client-truststore.jks</TrustSore> <TrustSorePassword>wso2carbon</TrustSorePassword> ….
Thrift
This file is in charge to define how will msf4j communicate with DAS
Now, time to browse the data in DAS:https://localhost:9443/monitoring/
81
Referenceso http://kubernetes.io/v1.1/ o https://www.digitalocean.com/community/tutorials/an-introd
uction-to-kubernetes
o https://insights.sei.cmu.edu/saturn/2015/11/defining-microservices.html
o http://www.slideshare.net/afkham_azeez/wso2con-2015usintroductiontomssv2?related=1
o http://www.slideshare.net/afkham_azeez/wso2conus-2015-introduction-to-wso2-microservices-server-mss?related=2
Conclusion
84
If you want to check the Product Websiteo http://wso2.com/products/microservices-server/ o Lightweight and fast runtime
o 6MB pack sizeo Starts within 400mso Based on the new WSO2 Carbon 5.0 kernelo ~25MB memory consumption for the WSO2 MSS framework
o Simple development, deployment, and monitoringo WSO2 Developer Studio-based tooling for generating microservices projects starting from a
Swagger API definitiono Built-in metrics and analytics APIs via WSO2 Data Analytics Servero Tracing of requests using a unique message ID
o High scalability and reliabilityo Transport based on Netty 4.0o JWT-based securityo Custom interceptorso Streaming input and streaming output supporto Comprehensive samples demonstrating how to develop microservices applications
85
Congratulations!o Tutorial done!o Next steps:
o Keep watching how WSO2 MSS will evolveo Don’t miss our upcoming Webinars covering
this and even more
86
More infoo Please, if you need to understand more, or
want to talk to one of our specialists to help you and your company’s projects, please contact us here:
o http://wso2.com/contact/
87