87
Microservices Server – MSS Workshop Edgar Silva edgar @wso2.com

Microservices Server - MSS Workshop

Embed Size (px)

Citation preview

Page 1: Microservices Server - MSS Workshop

Microservices Server – MSSWorkshopEdgar Silva edgar @wso2.com

Page 2: Microservices Server - MSS Workshop

Before you getting started …o This workshop is intend to cover how to use the

WSO2 Microservices Server – MSS, 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: Microservices Server - MSS Workshop

Part 1 of 3

3

Page 4: Microservices Server - MSS Workshop

4

Basics Pre-Reqs

Page 5: Microservices Server - MSS Workshop

You will need the following installed:o Java 8o Maveno See the MSS’s releases page:

o https://github.com/wso2/product-mss/releaseso Let’s work direct from the source:

o Git pullo https://github.com/wso2/product-mss

o Or simply download from this url: (easier) https://github.com/wso2/product-mss/archive/master.zip

5

Page 6: Microservices Server - MSS Workshop

Mount the root pom.xml project into your preferred IDE

6

Page 7: Microservices Server - MSS Workshop

Let’s work in the samples directoryo So, cd

<MSS_HOME>/samples

o Make you sure you could import the project hello_world

7

Page 8: Microservices Server - MSS Workshop

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

o <MSS_HOME>/samples/hello_worldo Type:

mvn package

8

Page 9: Microservices Server - MSS Workshop

Command Line time ...o After Maven process

o ( be pacient while 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.

9

Page 10: Microservices Server - MSS Workshop

Executing the HelloService MicroService Server

10

• WSO2 Microservices Server booting in my case 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 11: Microservices Server - MSS Workshop

Understading our first sample:HelloService class

11

package org.wso2.carbon.mss.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 12: Microservices Server - MSS Workshop

Back to the Basicso The simplest Java Class startupo See the main method:

12

public class Application { public static void main(String[] args) { new MicroservicesRunner() .deploy(new HelloService()) .start(); }

Page 13: Microservices Server - MSS Workshop

Testing

13

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

Page 14: Microservices Server - MSS Workshop

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

14

Page 15: Microservices Server - MSS Workshop

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

15

Page 16: Microservices Server - MSS Workshop

Going Further....

16

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 17: Microservices Server - MSS Workshop

Example #2:stockquote-service

17

Page 18: Microservices Server - MSS Workshop

Executing the Service1. Enter in the

<mss_home>/samples/stockquote-service

2. mvn package3. java -jar target/stockquote-service-1.0.0-

SNAPSHOT.jar

18

Page 19: Microservices Server - MSS Workshop

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

19

Page 20: Microservices Server - MSS Workshop

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

20

Page 21: Microservices Server - MSS Workshop

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 and deploy them into your Mic’service Server (powered by WSO2)

21

Page 22: Microservices Server - MSS Workshop

Going beyond the simple java -jar

22

Page 23: Microservices Server - MSS Workshop

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

23

Page 24: Microservices Server - MSS Workshop

“Legacy Web .NET”

24

WebServe

rASP.NET ADO.NET

Windows OS

.NET CLR RuntimeFront-StoreHTML

Page 25: Microservices Server - MSS Workshop

“Legacy Web Java”

25

WebServe

r

Web Framework

(JSF, Struts,

etc)

Persitence(JPA,

Hibernate,SpringTemplates)

Any OS

JVM

Other(JMS, JTA

etc)

App Server

Front-StoreHTML

Page 26: Microservices Server - MSS Workshop

New Approach : MicroServices

26

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 27: Microservices Server - MSS Workshop

MicroServices :: Composition

27

Product Service

CustomerService

AddressService

AddressService

ProductService

Customer Service

AddressService Customer Service

SinglePage APPHTML

FrontStoreService

Delivery Service

Page 28: Microservices Server - MSS Workshop

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

28

Page 30: Microservices Server - MSS Workshop

Recap on Microservices Definition

30

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 31: Microservices Server - MSS Workshop

Some on what you gain on Microserviceso Benefits of using microservices:

o Deployabilityo Availabilityo Scalabilityo Modifiabilityo Management

31

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 32: Microservices Server - MSS Workshop

Some on what you gain on Microserviceso I would add:

o Deployabilityo Availability (Auto-Scaling via Containers) o Analyticso Scalabilityo Modifiabilityo Management

32

Paulo Merson https://insights.sei.cmu.edu/saturn/2015/11/microservices-beyond-the-hype-what-you-gain-and-what-you-lose.html

Page 33: Microservices Server - MSS Workshop

Part 2 of 3

33

Page 34: Microservices Server - MSS Workshop

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

34

Page 35: Microservices Server - MSS Workshop

Running Everything you need in one placeo In your command line, go to your: <mss_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

35

Page 36: Microservices Server - MSS Workshop

More detailed Informationo https://github.com/wso2/product-mss/tree/mast

er/samples/petstore/deployment/kubernetes-vagrant-coreos-cluster

o Disclaimer: This tutorial is focused on WSO2

Micro Services Server – MSS, and some introduction to the basics on Kubernetes is strongly recommended before you move forward on this tutorial.

36

Page 37: Microservices Server - MSS Workshop

37

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 38: Microservices Server - MSS Workshop

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

38

Page 39: Microservices Server - MSS Workshop

Understanding the Example (Demo)

39

Our traditional Pet Store Sample has the following Microservices …

fileserver frontend-admin frontend-user

Pet (store) transactionsecurity

Page 40: Microservices Server - MSS Workshop

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

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

40

Page 41: Microservices Server - MSS Workshop

When the whole run.sh get finished…o Please, execute the command:

o kubectl get podso The result must be like this:

41

All the pods, must beLike this, keep repeatingThis process until all getready

Page 42: Microservices Server - MSS Workshop

When the whole run.sh get finished…o Please, execute the command:

o kubectl get podso The result must be like this:

42

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 43: Microservices Server - MSS Workshop

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)

43

Page 44: Microservices Server - MSS Workshop

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

44

Page 45: Microservices Server - MSS Workshop

Accessing Kubernetes UIRelationship between the VMs and Kubernetes (Nodes)

45

Here you can see the IpsAttached to the each Node

Page 46: Microservices Server - MSS Workshop

Accessing Kubernetes UI Resources

46

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 47: Microservices Server - MSS Workshop

Accessing Kubernetes UI Resources

47

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 48: Microservices Server - MSS Workshop

Accessing Kubernetes UI Resources

48

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 49: Microservices Server - MSS Workshop

Accessing Kubernetes UI Resources

49

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 50: Microservices Server - MSS Workshop

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

50

Your Machine OS

Hypervisor

Kubernetes Cluster-Master

K8S Node 01 K8S Node 02

Pods Pods

Replication Controller

Service

Page 51: Microservices Server - MSS Workshop

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

51

Your Machine OS

HypervisorKubernetes Cluster-Master

K8S Node 01 K8S Node 02Pods Pods

Replication Controller

Service

Browser

Page 52: Microservices Server - MSS Workshop

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

52

Your Machine OS

HypervisorKubernetes Cluster-Master

K8S Node 01 K8S Node 02Pods Pods

Replication Controller

Service

Browser

NGINX

Page 53: Microservices Server - MSS Workshop

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

53

Getting Actual pods: $ kubectl get pods

Page 54: Microservices Server - MSS Workshop

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

54

Getting details about some pod, for instance$ kubectl describe pods store-fe-r43hm

Page 55: Microservices Server - MSS Workshop

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

55

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 56: Microservices Server - MSS Workshop

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

56

Getting Actual Services: $ kubectl describe service store-fe

Page 57: Microservices Server - MSS Workshop

Basic Application Overview

57

Persistence Repository / Services

"Containerized” Microservices based in pure java –jar approach

Client Apps (PHP)petstore-admin petstore

Page 58: Microservices Server - MSS Workshop

Basic Application Overview :Invoking Admin PHP App

58

You will need to browse the service: admin-fe

http:// Node Server IP: Node Port

Ex: http://172.17.8.102:30984/

Page 59: Microservices Server - MSS Workshop

59

admin/admin

Page 60: Microservices Server - MSS Workshop
Page 61: Microservices Server - MSS Workshop
Page 62: Microservices Server - MSS Workshop

fileserver

Page 63: Microservices Server - MSS Workshop

Basic Application Overview :Invoking Store PHP App

63

You will need to browse the service: store-fe

http:// Node Server IP: Node Port

Ex: http://172.17.8.102:31466/

Page 64: Microservices Server - MSS Workshop
Page 65: Microservices Server - MSS Workshop

Here the Microservices TransactionIs invoked.

Page 66: Microservices Server - MSS Workshop

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 67: Microservices Server - MSS Workshop

What nexto Proposed Lab:

o Execute the previous samples from Part1 in Kubernetes + Docker

67

Page 68: Microservices Server - MSS Workshop

Part 3 of 3 - Analytics

68

Page 69: Microservices Server - MSS Workshop

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

69

Page 70: Microservices Server - MSS Workshop

Running MSS Metrics Sampleo Step 1: Configure WSO2 DAS

1. Go to <MSS_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!

70

Page 71: Microservices Server - MSS Workshop

Running MSS 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)

71

Page 72: Microservices Server - MSS Workshop

72

WSO2 DAS is ready and configured!

Page 73: Microservices Server - MSS Workshop

Running Metrics Sampleo Step 3: (based on

https://github.com/wso2/product-mss/tree/master/samples/metrics)

1. Go to <MSS_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” 73

Page 74: Microservices Server - MSS Workshop

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

74

Page 75: Microservices Server - MSS Workshop

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:

75

Page 76: Microservices Server - MSS Workshop

76

Page 77: Microservices Server - MSS Workshop

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.mss.example.service.StudentService.java

Page 78: Microservices Server - MSS Workshop

Also we have the data-agent-conf.xml

78

WSO2 MSS 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 MSS communicate with DAS

Page 79: Microservices Server - MSS Workshop

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

79

Page 80: Microservices Server - MSS Workshop
Page 82: Microservices Server - MSS Workshop

Conclusion

82

Page 83: Microservices Server - MSS Workshop

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

83

Page 84: Microservices Server - MSS Workshop

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

84

Page 85: Microservices Server - MSS Workshop

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/

85

Page 86: Microservices Server - MSS Workshop

Thanks!

86

@jedgarsilva

http://www.wso2.com

Edgar [email protected]