40
TRANSITIONING TO JAVA MICROSERVICES ON DOCKER LANA KALASHNYK W CONSULTING GROUP

Java Microservices HJUG

Embed Size (px)

Citation preview

Page 1: Java Microservices HJUG

TRANSITIONING TO JAVA MICROSERVICES ON DOCKERLANA KALASHNYK W CONSULTING GROUP

Page 2: Java Microservices HJUG

scopeLet’s talk about …

• Microservices :

• Microservices … what makes them different ?

• Microservice Architectures

• What is Docker?

• Containers are in .

• How to use Docker for microservices.

• A Step Further :

• 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 3: Java Microservices HJUG

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.co Twitter: lana_vk

Page 4: Java Microservices HJUG

THE RISE OF THE MICROSERVICES

When  did  Microservices  became  a  thing?    Not  a  new  Idea,  but  a  rela:vely  new  trend  in  

So=ware  Architectures  for  smaller  companies.  Men:oned  in  2011  Yegge  Memo  to  Google  about  

the  way    Amazon  largely  runs  their  company  as  microservices  communica:ng  exclusively  over  APIs.  

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

Page 5: Java Microservices HJUG

COMPANY CULTURE MATTERS

“Any  organiza:on  that  designs  a  system  (defined  broadly)  will  produce  a  design  whose  structure  is  a  copy  of  the  organiza:on's  communica:on  

structure.”  –  Melvin  Conway,  1967  

Page 6: Java Microservices HJUG

WHAT IS A MICROSERVICE ?

Microservices  is  a  so=ware  architecture  style  in  which  complex  applica:ons  are  composed  of  small,  independent  processes.  

These  processes  communicate  with  each  other  using  language-­‐agnos:c  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,  transac:on.  

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

Page 7: Java Microservices HJUG

BENEFITS OF USING MICROSERVICES

A  Well  wriJen  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  communica:on.    Doesn’t  have  to  be  all  in  one  technology  Infrastructure  Automa:on  :  making  releases  boring  .  Fault  isola:on  :  Helps  with  fixing  the  issue,  rather  than  deploying  a  patch  .  

Page 8: Java Microservices HJUG

PITFALLS OF USING MICROSERVICES

Crea:ng  so-­‐called  Nanoservices  .  Services  that  are  too  granular,  their  maintenance  and  implementa:on  are  counterproduc:ve.    

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

Change  Management,  Transac:ons  and  Data  Integrity  can  be  tricky

Page 9: Java Microservices HJUG

MICROSERVICE ARCHITECTURE PATTERNS

Aggregate  :  An  aggregator  invokes  mul:ple  microservices  to  achieve  desired  func:onality.

Page 10: Java Microservices HJUG

MICROSERVICE ARCHITECTURE PATTERNS

Chained:  A  single  microservice  produces  a  single  consolidated  response.  Service  A  invokes  Service  B…  etc

Page 11: Java Microservices HJUG

MICROSERVICE ARCHITECTURE PATTERNS

Shared  data:  Microservices  access  the  same  database.

Page 12: Java Microservices HJUG

APPROACH TO ENGINEERING : THE TWELVE FACTORS I. Codebase

One codebase tracked in revision control, many deploys II. Dependencies

Explicitly declare and isolate dependencies III. Config

Store config in the environment IV. Backing Services

Treat backing services as attached resources V. Build, release, run

Strictly separate build and run stages VI. Processes

Execute the app as one or more stateless processes VII. Port binding

Export services via port binding VIII. Concurrency

Scale out via the process model IX. Disposability

Maximize robustness with fast startup and graceful shutdown X. Dev/prod parity

Keep development, staging, and production as similar as possible XI. Logs

Treat logs as event streams XII. Admin processes

Run admin/management tasks as one-off processes

Page 13: Java Microservices HJUG
Page 14: Java Microservices HJUG

WHAT ARE CONTAINERS ?

Containers  are  a  solu:on  to  the  problem  of  how  to  get  so=ware  to  run  reliably  when  moved  from  one  compu:ng  environment  to  another.    

Put  simply,  a  container  consists  of  an  en:re  run:me  environment:  an  applica:on,  plus  all  its  dependencies,  libraries  and  other  binaries,  and  configura:on  files  needed  to  run  it,  bundled  into  one  package.  

“By  containerizing  the  applica:on    plalorm  and  its  dependencies,    differences  in  OS  distribu:ons    and  underlying  infrastructure    are  abstracted  away.”  –Paul  Rubens  CIO  

Page 15: Java Microservices HJUG

WHY DO WE NEED THEM?

Immutable  Servers  –  BIG  WIN  Immutable  containers  mean  that  they  will  never  be  changed.  A  new  version  of  an  applica:on  is  

deployed  in  a  new  container  

No  More  Magical  Servers.    You  deploy  in  the  environment  you  build  the  product  for…  all  the  :me.    

Isola:on.  Applica:ons  are  isolated  by  the  container  boundaries.    Smarter  use  of  resources  when  compared  to  separate  VMs.  Con:nuous  Integra:on  and  Delivery  .

Page 16: Java Microservices HJUG

WHAT IS DOCKER?

Docker  is  an  open  plalorm  for  building,  shipping  and  running  distributed  applica:ons.  It  gives  programmers,  development  teams  and  opera:ons  engineers  the  common  toolbox  they  need  to  take  advantage  of  the  distributed  and  networked  nature  of  modern  applica:ons.  

Docker  consists  of:  The  Docker  Engine  -­‐  container  virtualiza:on  technology  combined  with  a  work  flow  for  building  and  

containerizing  applica:ons.  Docker  Hub  -­‐  SaaS  service  for  sharing  and  managing  your  applica:on  stacks.  

Page 17: Java Microservices HJUG

DOCKER ARCHITECTURE

Page 18: Java Microservices HJUG

DEPLOYING MICROSERVICES ON DOCKER

Microservice  and  any  necessary  so=ware  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  Mul:-­‐container  applica:ons  can  be  defined  using  Docker  compose.    

Containers  can  be  deployed  as  a  Cluster  using  Docker  SWARM.

Page 19: Java Microservices HJUG

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  dras:cally  cut  costs  Low  risk  project  since  doesn’t  provide  cri:cal  data  writes  Other  applica:ons  in  the  eco  system  were  already  Java  based

Page 20: Java Microservices HJUG

USAGE EXAMPLE: PREVIOUS ARCHITECTURE

Page 21: Java Microservices HJUG

USAGE EXAMPLE: NEW ARCHITECTURE

Page 22: Java Microservices HJUG

WORK IN PROGRESS

Commipng  to  a  microservice  architecture  beyond  a  small  project  is  a  much  bigger  undertaking.  

Define  architecture  paJerns  for  cross  technology  interac:ons  Define  scaling  mechanisms    Define  state  monitoring    Or  deploy  on  AWS  or  Google  to  leverage  their  pre-­‐built  tools

Page 23: Java Microservices HJUG

Example

The  following  solu:on  imitates  the  previous  architecture.  It  consists  of  :  

Virtual  Machine  :  Complex  Web  Applica:on  (  Bitcoin  Node)  Docker  Container  :  Java  Microservice  polling  the  Bitcoin  Node  Simple  ReactJS  Web  Page  displaying  the  results.

Page 24: Java Microservices HJUG

PRE REQUISITES

Install  a  Bitcoin  Node    I  used  AWS  EC2  t2.medium  instance  w/  100  GiB  volume.  Configure  the  Security  group  to  allow  traffic  to  our  node  from  other  bitcoin  nodes  and  our  applica:on.    

   

Page 25: Java Microservices HJUG

CONFIGURE THE BITCOIN NODE

Install  the  bitcoin  node  so=ware  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  ge:nfo

Page 26: Java Microservices HJUG

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 27: Java Microservices HJUG

WRITING A JAVA MICROSERVICE

Page 28: Java Microservices HJUG

WRITING A JAVA MICROSERVICE

Page 29: Java Microservices HJUG

WILDFY SWARM

Add  Wildfly  Swarm  Dependency  &  Plugin  to  package  the  service  into  a  container  in  your  pom  file  

<dependency>              <groupId>org.wildfly.swarm</groupId>              <ar:factId>wildfly-­‐swarm-­‐jaxrs-­‐weld</ar:factId>              <version>1.0.0.Alpha5-­‐SNAPSHOT</version>  </dependency>  <dependency>                <groupId>org.projectlombok</groupId>                <ar:factId>lombok</ar:factId>                <version>1.16.2</version>  </dependency>  …  

<!-­‐-­‐  ***  Swarm  Plugin  ***  -­‐-­‐>  <!-­‐-­‐  Specify  the  main  class  in  your    app  -­‐-­‐>  <!-­‐-­‐  Executu:on  phase  and  goal  -­‐-­‐>  <plugin>            <groupId>org.wildfly.swarm</groupId>            <ar:factId>wildfly-­‐swarm-­‐plugin</ar:factId>          <configura:on>        <mainClass>com.mius.javajaxrsmicroservice.JaxRSServriceSwarmMain</mainClass>          </configura:on>          <execu:ons>                  <execu:on>                          <phase>package</phase>                          <goals>                              <goal>package</goal>                          </goals>                    </execu:on>            </execu:ons>    </plugin>    

Page 30: Java Microservices HJUG

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 31: Java Microservices HJUG

INSTALL DOCKER

hJps://docs.docker.com/compose/install/  Depending  on  your  OS  you  can  either  run  Docker  na:vely  or  in  a  VirtualBox  

Docker  Toolbox  installs  Docker  Machine  and  other  tools  like  Kitema:c.  

Page 32: Java Microservices HJUG

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 33: Java Microservices HJUG

DOCKER COMPOSE

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 34: Java Microservices HJUG

CREATE YOUR DOCKER IMAGE

Run  the  following  commands  :  docker-­‐compose  build  docker  run  –p  8080:8080  javamsdocker_wildflyswarm  Or  docker-­‐compose  up

Page 35: Java Microservices HJUG

THE SERVICE IS LIVE !

Page 36: Java Microservices HJUG

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 37: Java Microservices HJUG

FINAL SOLUTION

Page 38: Java Microservices HJUG

https://en.wikipedia.org/wiki/Microservices

http://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

https://www.sequoiacap.com/article/build-us-microservices/

http://12factor.net

Page 39: Java Microservices HJUG

GitHub  Repo  for  the  Bitcoin  Monitor  Demo  

hJps://github.com/lana-­‐vk/JavaMicroServiceDocker

Page 40: Java Microservices HJUG

Lana  Kalashnyk  W  Consul:ng  Group  

wcgp.co  TwiJer  :  @lana_vk