workshop msf4j - getting started with microservices and java

89
msf4j – Microservices Framework for Java Workshop Edgar Silva edgar @wso2.com

Upload: edgar-silva

Post on 16-Apr-2017

909 views

Category:

Technology


3 download

TRANSCRIPT

Page 1: Workshop MSF4J - Getting Started with Microservices and Java

msf4j – Microservices Framework for JavaWorkshopEdgar Silva edgar @wso2.com

Page 2: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 3: Workshop MSF4J - Getting Started with Microservices and Java

Part 1 of 3

3

Page 4: Workshop MSF4J - Getting Started with Microservices and Java

4

Basics Pre-Reqs

Page 5: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 6: Workshop MSF4J - Getting Started with Microservices and Java

Installer

Page 7: Workshop MSF4J - Getting Started with Microservices and Java

Mount the root pom.xml project into your preferred IDE

7

Page 8: Workshop MSF4J - Getting Started with Microservices and Java

In Case of WSO2 Developer Studio

8

Page 9: Workshop MSF4J - Getting Started with Microservices and Java

Let’s work in the samples directoryo So, cd

<msf4j_HOME>/samples

o Make you sure you could import the project hello_world

9

Page 10: Workshop MSF4J - Getting Started with Microservices and Java

Command Line time ...o Go to the dir:

o <msf4j_HOME>/samples/hello_worldo Type:

mvn package

10

Page 11: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 12: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 13: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 14: Workshop MSF4J - Getting Started with Microservices and Java

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(); }

Page 15: Workshop MSF4J - Getting Started with Microservices and Java

Testing

15

edgar$ curl -v http://localhost:8080/hello/valentina

Page 16: Workshop MSF4J - Getting Started with Microservices and Java

cURL for Windows Userso Take a look on this:o http://www.confusedbycode.com/curl/

16

Page 17: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 18: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 19: Workshop MSF4J - Getting Started with Microservices and Java

Example #2:stockquote-service

19

Page 20: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 21: Workshop MSF4J - Getting Started with Microservices and Java

Exercise #1:o In the console type: (Windows Userso curl -v http://localhost:8080/stockquote/GOOG

21

Page 22: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 23: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 24: Workshop MSF4J - Getting Started with Microservices and Java

Going beyond the simple java -jar

24

Page 25: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 26: Workshop MSF4J - Getting Started with Microservices and Java

“Legacy Web .NET”

26

WebServe

rASP.NET ADO.NET

Windows OS

.NET CLR RuntimeFront-StoreHTML

Page 27: Workshop MSF4J - Getting Started with Microservices and Java

“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

Page 28: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 29: Workshop MSF4J - Getting Started with Microservices and Java

MicroServices :: Composition

29

Product Service

CustomerService

AddressService

AddressService

ProductService

Customer Service

AddressService Customer Service

SinglePage APPHTML

FrontStoreService

Delivery Service

Page 30: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 32: Workshop MSF4J - Getting Started with Microservices and Java

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.

Page 33: Workshop MSF4J - Getting Started with Microservices and Java

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 )

Page 34: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 35: Workshop MSF4J - Getting Started with Microservices and Java

Part 2 of 3

35

Page 36: Workshop MSF4J - Getting Started with Microservices and Java

o Part 2:We ship in the distroan example showing how to run a whole deploy from severaldifferent container machines managedby Kubernetes.

36

Page 37: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 39: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 40: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 41: Workshop MSF4J - Getting Started with Microservices and Java

Understanding the Example (Demo)

41

Our traditional Pet Store Sample has the following Microservices …

fileserver frontend-admin frontend-user

Pet (store) transactionsecurity

Page 42: Workshop MSF4J - Getting Started with Microservices and Java

What is happening….o If you are using VirtualBox as the

Hypervisor (recommended), you will see the following 3 VMs started

42

Page 43: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 44: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 45: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 46: Workshop MSF4J - Getting Started with Microservices and Java

Accessing Kubernetes UIhttp://172.17.8.101:8080/ui Here is the Kubernetes Admin that you can open in your browser

46

Page 47: Workshop MSF4J - Getting Started with Microservices and Java

Accessing Kubernetes UIRelationship between the VMs and Kubernetes (Nodes)

47

Here you can see the IpsAttached to the each Node

Page 48: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 49: Workshop MSF4J - Getting Started with Microservices and Java

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)

Page 50: Workshop MSF4J - Getting Started with Microservices and Java

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.

Page 51: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 52: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 53: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 54: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 55: Workshop MSF4J - Getting Started with Microservices and Java

Understanding our Application DemoWhat do you have up and running now (main components)

55

Getting Actual pods: $ kubectl get pods

Page 56: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 57: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 58: Workshop MSF4J - Getting Started with Microservices and Java

Understanding our Application DemoWhat do you have up and running now (main components)

58

Getting Actual Services: $ kubectl describe service store-fe

Page 59: Workshop MSF4J - Getting Started with Microservices and Java

Basic Application Overview

59

Persistence Repository / Services

"Containerized” Microservices based in pure java –jar approach

Client Apps (PHP)petstore-admin petstore

Page 60: Workshop MSF4J - Getting Started with Microservices and Java

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/

Page 61: Workshop MSF4J - Getting Started with Microservices and Java

61

admin/admin

Page 62: Workshop MSF4J - Getting Started with Microservices and Java
Page 63: Workshop MSF4J - Getting Started with Microservices and Java
Page 64: Workshop MSF4J - Getting Started with Microservices and Java

fileserver

Page 65: Workshop MSF4J - Getting Started with Microservices and Java

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/

Page 66: Workshop MSF4J - Getting Started with Microservices and Java
Page 67: Workshop MSF4J - Getting Started with Microservices and Java

Here the Microservices TransactionIs invoked.

Page 68: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 69: Workshop MSF4J - Getting Started with Microservices and Java

What nexto Proposed Lab:

o Execute the previous samples from Part1 in Kubernetes + Docker

69

Page 70: Workshop MSF4J - Getting Started with Microservices and Java

Part 3 of 3 - Analytics

70

Page 71: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 72: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 73: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 74: Workshop MSF4J - Getting Started with Microservices and Java

74

WSO2 DAS is ready and configured!

Page 75: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 76: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 77: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 78: Workshop MSF4J - Getting Started with Microservices and Java

78

Page 79: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 80: Workshop MSF4J - Getting Started with Microservices and 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

Page 81: Workshop MSF4J - Getting Started with Microservices and Java

Now, time to browse the data in DAS:https://localhost:9443/monitoring/

81

Page 82: Workshop MSF4J - Getting Started with Microservices and Java
Page 84: Workshop MSF4J - Getting Started with Microservices and Java

Conclusion

84

Page 85: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 86: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 87: Workshop MSF4J - Getting Started with Microservices and Java

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

Page 88: Workshop MSF4J - Getting Started with Microservices and Java

Thanks!

88

@jedgarsilva

http://www.wso2.com

Edgar [email protected]