TransitioningToMicroServonDocker_MS

41
1 Transitioning to Java MicroServices on Docker Lana Kalashnyk W Consulting Group

Transcript of TransitioningToMicroServonDocker_MS

Page 1: TransitioningToMicroServonDocker_MS

1

Transitioning to Java MicroServices on Docker

Lana KalashnykW Consulting Group

Page 2: TransitioningToMicroServonDocker_MS

Welcome to Houston TechFest

• Please turn off all electronic devices or set them to vibrate.• If you must take a phone call, please do so in the lobby so as not

to disturb others.• Thanks to our Diamond Sponsors:

Thank you for being a part of the 9th Annual Houston TechFest!

Page 3: TransitioningToMicroServonDocker_MS

3

Transitioning to Java Microservices• Let’s talk about …• Microservices :

– Microservices … what makes them different ?– Microservice Architectures

• What is Docker? – Containers are in . – How to use Docker for microservices.

• Business Application: Outdated Web Services. • Demo of a Java Microservice running in a Docker container,

providing status updates on a Bitcoin Node block height.

Page 4: TransitioningToMicroServonDocker_MS

4

About MeLana Kalashnyk

•BAAS Computer Science minor Business Administration •AS in Computer Science emphasis on Networking

•Cisco CCNA I-IV, Red Hat JAX-RS, NetSuite, CODE WPF, Oracle Java training

•Houston Java User Group, Houston .Net User Group, Texas DevOps User Group, PuppetConf Test Pilot

•Fan of back end engineering, cloud technologies, innovation and running.

www.wcgp.coTwitter: lana_vk

Page 5: TransitioningToMicroServonDocker_MS

5

• When did Microservices became a thing?

• Not a new Idea, but a relatively new trend in Software Architectures for smaller companies.

• Mentioned in 2011 Yegge Memo to Google about the way Amazon largely runs their company as microservices communicating exclusively over APIs.

https://plus.google.com/+RipRowan/posts/eVeouesvaVX

The Rise of The Microservices

Page 6: TransitioningToMicroServonDocker_MS

6

Company culture matters

“Any organization that designs a system (defined broadly) will produce a design whose structure is a copy of the

organization's communication structure.” – Melvin Conway, 1967

Page 7: TransitioningToMicroServonDocker_MS

7

What is a Microservice ?Microservices is a software architecture style in which complex applications are composed of small, independent processes.

– These processes communicate with each other using language-agnostic APIs.

– These services are small, highly decoupled and focus on doing a small task.

– Facilitate a modular approach to system-building.– The service is autonomous; it is full-stack and has control of all

the components – UI, middleware, persistence, transaction.

•“A perfect JavaEE microservice is single ECB component within a WAR deployed on a single server”

Page 8: TransitioningToMicroServonDocker_MS

8

Benefits of using MicroservicesA Well written Microservice operates on a single

resource. •Allows to focus on building a product rather than a project. (each service is developed independently, not a refactor of a monolith) •Independently deployable.•Smart endpoints. •RESTFUL protocols or Message queues can be used for non-blocking communication. •Doesn’t have to be all in one technology•Infrastructure Automation : making releases boring .•Fault isolation : Helps with fixing the issue, rather than deploying a patch .

Page 9: TransitioningToMicroServonDocker_MS

9

Pitfalls of using Microservices• Creating so-called Nanoservices . Services that are too

granular, their maintenance and implementation are counterproductive.

• Lack of Tooling. Transitioning to a microservice architecture implies investment into management and monitoring tools. Failure to do so will surely result in a poor system.

• Change Management, Transactions and Data Integrity can be tricky

Page 10: TransitioningToMicroServonDocker_MS

10

Microservice Architecture Patterns• Aggregate : An aggregator invokes multiple

microservices to achieve desired functionality.

Page 11: TransitioningToMicroServonDocker_MS

11

Microservice Architecture Patterns• Chained: A single microservice produces a single

consolidated response. Service A invokes Service B… etc

Page 12: TransitioningToMicroServonDocker_MS

12

Microservice Architecture Patterns• Shared data: Microservices access the same

database.

Page 13: TransitioningToMicroServonDocker_MS

13

What are containers ?• Containers are a solution to the problem of how to get software to

run reliably when moved from one computing environment to another.

• Put simply, a container consists of an entire runtime environment: an application, plus all its dependencies, libraries and other binaries, and configuration files needed to run it, bundled into one package.

“By containerizing the application platform and its dependencies, differences in OS distributions and underlying infrastructure are abstracted away.” –Paul Rubens CIO

Page 14: TransitioningToMicroServonDocker_MS

14

Why do we need them?• Immutable Servers – BIG WIN

– Immutable containers mean that they will never be changed. A new version of an application is deployed in a new container

• No More Magical Servers.– You deploy in the environment you build the product for… all the time.

• Isolation. Applications are isolated by the container boundaries.

• Smarter use of resources when compared to separate VMs.

• Continuous Integration and Delivery .

Page 15: TransitioningToMicroServonDocker_MS

15

What is Docker?

Docker is an open platform for building, shipping and running distributed applications. It gives programmers, development teams and operations engineers the common toolbox they need to take advantage of the distributed and networked nature of modern applications.

Docker consists of:•The Docker Engine - container virtualization technology combined with a work flow for building and containerizing applications.•Docker Hub - SaaS service for sharing and managing your application stacks.

CoreOS rkt is the only other major player in the field.

Page 16: TransitioningToMicroServonDocker_MS

16

Docker Architecture

Page 17: TransitioningToMicroServonDocker_MS

17

Deploying Microservices on Docker• Microservice and any necessary software is

“baked” into a Docker Image.• The Image is created based on a Docker file. All

necessary set up is specified here. • Docker Images are stored in Docker Registry• Multi-container applications can be defined using

Docker compose. • Containers can be deployed as a Cluster using

Docker SWARM.

Page 18: TransitioningToMicroServonDocker_MS

18

Transition from .Net Web Services on IIS • In a recent project we replaced outdated .Net

Web Services running on an IIS Service with Java Microservice deployed on Docker in a Wildfly AS .– Good candidate due to few dependencies. – Already separated by product in separate web services– Needed to drastically cut costs– Low risk project since doesn’t provide critical data

writes– Other applications in the eco system were already Java

based

Page 19: TransitioningToMicroServonDocker_MS

19

Usage Example: Previous Architecture

Page 20: TransitioningToMicroServonDocker_MS

20

Usage Example: New Architecture

Page 21: TransitioningToMicroServonDocker_MS

21

Work in Progress• Committing to a microservice architecture beyond

a small project is a much bigger undertaking.• Define architecture patterns for cross technology

interactions• Define scaling mechanisms • Define state monitoring • Or deploy on AWS or Google to leverage their pre-

built tools

Page 22: TransitioningToMicroServonDocker_MS

22

And now a working Example !• The following solution imitates the previous

architecture. It consists of :– Virtual Machine : Complex Web Application ( Bitcoin

Node)– Docker Container : Java Microservice polling the

Bitcoin Node– Simple ReactJS Web Page displaying the results.

Page 23: TransitioningToMicroServonDocker_MS

23

Pre requisites• Install a Bitcoin Node

– I used AWS EC2 t2.medium instance w/ 48 GiB volume.– Configure the Security group to allow traffic to our

node from other bitcoin nodes and our application.

Page 24: TransitioningToMicroServonDocker_MS

24

Configure the Bitcoin Node• Install the bitcoin node software on your provisioned server. • Configure the node to accept JSON-RPC requests from our IP only.

– sudo vi .bitcoin/bitcoin.conf

• Start the bitcoin daemon. Now you can manage it using bitcoin-cli commands

– sudo bitcoind –daemon– sudo bitcoin-cli getinfo

Page 25: TransitioningToMicroServonDocker_MS

25

Writing a Java Microservice• Install Maven, Git, NetBeans (or an IDE of your

choice)• Only work on a single resource

– Here a Bitcoin Node • Make the end points RESTFUL (PUT, POST, GET)• Use JSON to package objects. • Report Errors• Log

Page 26: TransitioningToMicroServonDocker_MS

26

Writing a Java Microservice

Page 27: TransitioningToMicroServonDocker_MS

27

Writing a Java Microservice

Page 28: TransitioningToMicroServonDocker_MS

28

Wildfy Swarm• Add Wildfly Swarm Dependency & Plugin

to package the service into a container in your pom file

<dependency> <groupId>org.wildfly.swarm</groupId>

<artifactId>wildfly-swarm-jaxrs-weld</artifactId>

<version>1.0.0.Alpha5-SNAPSHOT</version></dependency><dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.2</version></dependency>…

<!-- *** Swarm Plugin *** --><!-- Specify the main class in your app --><!-- Executution phase and goal --><plugin> <groupId>org.wildfly.swarm</groupId> <artifactId>wildfly-swarm-plugin</artifactId> <configuration> <mainClass>com.mius.javajaxrsmicroservice.JaxRSServriceSwarmMain</mainClass> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>package</goal> </goals> </execution> </executions> </plugin>

Page 29: TransitioningToMicroServonDocker_MS

29

Could stop here..• Build the Service and package it into a Swarm

Container – mvn clean package

• Jar file is fully executable… • Execute using

– “java –jar JavaMSDocker-swarm.jar”

• but then we wouldn’t get to talk about Docker.

Page 30: TransitioningToMicroServonDocker_MS

30

Install Docker• https://docs.docker.com/compose/install/• Depending on your OS you can either run Docker

natively or in a VirtualBox• Docker Toolbox installs Docker Machine and other

tools like Kitematic.

Page 31: TransitioningToMicroServonDocker_MS

31

Create a Docker File• Create a file name “Dockerfile”

– Specify the base image • FROM java:openjdk-8-jdk

– Copy over your files into the container• ADD target/JavaMSDocker-swarm.jar /opt/JavaMSDocker-

swarm.jar

– Open port 8080 for our Service• EXPOSE 8080

– Configure the container to run as an executable• ENTRYPOINT ["java", "-jar", "/opt/JavaMSDocker-swarm.jar"]

Page 32: TransitioningToMicroServonDocker_MS

32

Docker Compose• For Development use. Optional.• Define all services to be ran together in a .yml file• Create a file name “docker-compose.yml”

wildflyswarm: // define service name build: . // path to the Dockerfile ports: - "8080:8080" // ports to expose HOST:CONTAINER

Page 33: TransitioningToMicroServonDocker_MS

33

Create your Docker Image• Run the following commands :

docker-compose builddocker run –p 8080:8080 javamsdocker_wildflyswarmOrdocker-compose up

Page 34: TransitioningToMicroServonDocker_MS

34

The service is live !

Page 35: TransitioningToMicroServonDocker_MS

35

React Page to display the data• For simplicity uses simple jquery to GET the

bitcoin node status from the java microservice• Uses virtual DOM• JSX transforms in browser

Page 36: TransitioningToMicroServonDocker_MS

36

Final Page

Page 37: TransitioningToMicroServonDocker_MS

37

Referenceshttps://en.wikipedia.org/wiki/Microserviceshttp://blog.arungupta.me/microservices-monoliths-noops/https://www.voxxed.com/blog/2015/04/coupling-versus-autonomy-in-microservices/

http://www.javacodegeeks.com/2015/04/microservice-design-patterns.html

http://www.cio.com/article/2924995/enterprise-software/what-are-containers-and-why-do-you-need-them.html

https://docs.docker.com/introduction/understanding-docker/

http://wildfly.org/swarm/

http://tutorialzine.com/2014/07/5-practical-examples-for-learning-facebooks-react-framework/

http://blog.arungupta.me/deploy-javaee-docker-swarm-cluster/

https://bitcoin.org/en/full-node#ubuntu-1410

Page 38: TransitioningToMicroServonDocker_MS

38

Pull thisGitHub Repo for the Bitcoin Monitor Demo

https://github.com/lana-vk/JavaMicroServiceDocker

Page 39: TransitioningToMicroServonDocker_MS

39

Thank You !

Lana KalashnykW Consulting Group

wcgp.coTwitter : @lana_vk

Page 40: TransitioningToMicroServonDocker_MS

Please Leave Feedback During Q&AIf you leave session feedback and provide contact information in the survey, you will be qualified for a prize

Scan the QR Code to the right or go to http://bit.ly/1K1Hvi5

Page 41: TransitioningToMicroServonDocker_MS

Thanks to all our Sponsors!