Advertisement
Advertisement

More Related Content

Advertisement
Advertisement

Microservices and modularity with java

  1. Microservices and modularity with Java Elek Márton @anzix 2015 December DPC Consulting
  2. Agenda ● Microservice definition ● Core implementation patterns
  3. People need modularity not microservices
  4. Microservices is one way to achieve modularity. Other ways: OSGi, SPI, java9 jigsaw
  5. What are microservices? The fire making rule: ● Use more sticks than you think they would be enough ● Use a little bit smaller sticks than you think would be enough
  6. Informal definition Microservices when you have smaller, separated services as usual ● Many times they are not micro (group of services) ● Many times REST is used (but not always) ● Separated means different process/JVM instances
  7. More formal definition ● “an approach to developing a single application as a suite of small services, ● each running in its own process ● and communicating with lightweight mechanisms, often an HTTP resource API. These services are built around business capabilities and independently deployable by fully automated deployment machinery.” Lewis, Fowler
  8. Microservices and REST REST is just one solution to communicate ● between services ● or call any of the service
  9. Microservices vs SOA Even if the “Microservices != SOA” a very common statement there are a lot of shared concept. ● Usually we have new tools (using existing solutions differently) not totally new concept
  10. Implementation patterns
  11. Monolith application
  12. Spring boot example @EnableAutoConfiguration @RestController @ComponentScan public class TimeStarter { @Autowired TimeService timerService; @RequestMapping("/now") public Date now() { return timerService.now(); } public static void main(String[] args) { SpringApplication.run(TimeStarter.class, args); } }
  13. Modular monolith
  14. API gateway
  15. API gateway Goals: ● Hide available microservices behind a service facade pattern – Routing, Authorization – Deployment handling, Canary testing – Logging, SLA Implementations: ● Spring cloud: Netflix Zuul ● Netflix Zuul based implementation ● Twitter Finagle based implementation ● Amazon API gateway ● Simple Nginx reverse proxy configuration
  16. Spring Cloud + Zuul example @EnableAutoConfiguration @ComponentScan @EnableEurekaClient @EnableZuulProxy public class ApiGatewayStarter { public static void main(String[] args) { SpringApplication.run(ApiGatewayStarter.class, args); } }
  17. Service registry
  18. Service registry Goal: ● Store the location and state of the available services – Health check – DNS interface Implementations: ● Spring cloud: Eureka ● Netflix eureka based implementation ● Consul.io ● etcd, zookeeper ● Simple: DNS or hosts file
  19. Eureka
  20. Eureka server @SpringBootApplication @EnableEurekaServer public class EurekaStarter { public static void main(String[] args) { new SpringApplicationBuilder(EurekaStarter.class).web(true).run(args); } }
  21. Eureka client @EnableAutoConfiguration @RestController @ComponentScan @EnableEurekaClient public class TimeStarter { @Autowired TimeService tttService; @RequestMapping("/now") public Date now() {return tttService.now();} public static void main(String[] args) { SpringApplication.run(TimeStarter.class, args); } }
  22. SIMPLE Discovery client @Service public class Client { @Autowired private DiscoveryClient discoveryClient; public void list() { for (ServiceInstance instance : discoveryClient.getInstances("BOOTSTRAP")) { System.out.println(instance.getHost()); System.out.println(instance.getPort()); System.out.println(instance.getUri()); } } }
  23. Config service
  24. Monolith configuration ● Store configuration outside of the project – Versioning? Who did what ● Store configuration in separated versioned project – Which binary version need which config version? ● Store configuration with the source code – Sensitive configuration?
  25. Config Service Goals: ● One common place for all of the configuration – Versioning – Auditing – Multiple environment support Solutions: ● Spring Cloud config service ● Zookeeper ● Most of the service registry have key->value store ● Any persistence datastore
  26. Config server @SpringBootApplication @EnableConfigServer @EnableEurekaClient public class ConfigServer { public static void main(String[] args) { new SpringApplicationBuilder(ConfigServer.class).run(args); } }
  27. Config repository elek@sc /tmp/config-repo [master] > ls -lah drwxr-xr-x 3 elek elek 100 Dec 14 14:32 . drwxrwxrwt 57 root root 1.8K Dec 14 14:37 .. -rw-r--r-- 1 elek elek 55 Dec 14 14:16 application.properties drwxr-xr-x 8 elek elek 260 Dec 14 14:40 .git -rw-r--r-- 1 elek elek 12 Dec 14 14:32 timer.properties
  28. Config client @Service public class TimeService { @Value("${version:Unknown}") String version; public Date now() { return new Date(); } public String version() { return version; } }
  29. Monitoring
  30. Monitoring Goal: ● Combined business log ● Health check, exceptions, what is running? ● Metrics Solutions (for one of the goals): ● Netflix Spectator, Servo, Atlas ● Spring cloud: Hystrix (circuit breaker) ● ELK: ElasticSearch + Logstash + Kibana ● Cloud based solutions (logentries...) ● Metrics: Dropwizard based implementations
  31. Kibana (from ELK)
  32. Hystrix
  33. Hystrix @Service public class TimeService { @HystrixCommand(fallbackMethod = "safeNow") public Date now() { return new Date(); } public Date safeNow() { return new Date(0); } }
  34. Inter-module calls
  35. Inter-module calls
  36. Inter-module calls Goal: ● RPC calls between modules ● Protocol ● Usage of the Service Registry Solutions: ● Spring cloud/Netflix: ribbon ● Simple REST calls ● Message Oriented solutions: – Rabbit MQ, ZMQ, ...
  37. Distributed tracing
  38. Distributed tracing (simlified)
  39. Distributed tracing Goal: ● Global information about latency and errors – Service enter/exit point measurement – Transaction identification Implementations: ● Twitter’s Zipkin ● Apache (Cloudera) HTrace ● Spring cloud Sleuth
  40. Zipkin source: https://bitbucket.org/aktivecortex/aktivecortex/wiki/tracing.md
  41. Summary
  42. Advantages of monolith Modular monolith Microservices
  43. Advantages of microservices ● Modular, loosely coupled ● Easily scalable to multiple machines ● Separated, modular development lifecycle ● Partial deploy without restart [like OSGi] ● Supports heterogeneous architecture (tech stack migration)
  44. Summary ● Focus on modularity ● Be familiar with the microservice related tools: they could be useful even with monolith applications…
  45. Start with monolith? Stefan Tilkov: http://martinfowler.com/articles/dont- start-monolith.html Martin Fowler: http://martinfowler.com/bliki/MonolithFirst. html
  46. Q&A Márton Elek DPC consulting http://dpc.hu twitter: @anzix http://blog.dpc.hu, http://blog.anzix.net
Advertisement